Пример #1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="isValid"></param>
 /// <param name="normalize"></param>
 /// <param name="version"></param>
 public Spreadsheet (Func<string, bool> isValid, Func<string, string> normalize, string version) 
     : base(isValid, normalize, version)
 {
     allCells = new Dictionary<string, Cell>();
     graph = new DependencyGraph();
     isChanged = false;
 }
Пример #2
0
 /// <summary>
 /// Constructor which takes in delegate values from the user
 /// </summary>
 /// <param name="isValid">Validity delegate</param>
 /// <param name="normalize">Normalization delegate</param>
 /// <param name="version">Version of spreadsheet</param>
 public Spreadsheet(Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     dependency_graph = new DependencyGraph();
     cell = new Dictionary<String, Cell>();
     Changed = false;            //Test
 }
Пример #3
0
 /// <summary>
 /// Constructor, creates empty spreadsheet, backed by a List of cells
 /// </summary>
 public Spreadsheet() 
     : base(s => true, s => s, "default")
 {
     allCells = new Dictionary<string, Cell>();
     graph = new DependencyGraph();
     isChanged = false;
 }
 /// <summary>
 /// Contructs a new spreadsheet, (4 arguments from user)
 /// </summary>
 public Spreadsheet(string path, Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     cells = new Dictionary<string, Cell>();
     dependencyGraph = new DependencyGraph();
     GetSavedVersion(path);
 }
Пример #5
0
 /// <summary>
 /// Constructor for Spreadsheet with default values added to
 /// delegates
 /// </summary>
 public Spreadsheet()
     : base(s => true, s => s, "default")
 {
     dependency_graph = new DependencyGraph();
     cell = new Dictionary<String, Cell>();
     Changed = false;
 }
 public void AddTest2Test()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("A3", "A2");
     t.AddDependency("A1", "A2");
     Assert.AreEqual(2, t.Size);
 }
Пример #7
0
 /// <summary>
 /// Reads a saved spreadsheet from file and uses it to construct a new spreadsheet. 
 /// The spreadsheet uses the provided validity delegate, normalization delegate and verson. 
 /// </summary>
 /// <param name="file"></param>
 /// <param name="isValid"></param>
 /// <param name="normalize"></param>
 /// <param name="version"></param>
 public Spreadsheet(string file, Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     spreadsheet = new Dictionary<string, Cell>();
     dependencyGraph = new DependencyGraph();
     ReadFile(file);
 }
Пример #8
0
 public void EmptyTest11()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("x", "y");
     Assert.AreEqual(t.Size, 1);
     t.RemoveDependency("x", "y");
     t.RemoveDependency("x", "y");
 }
Пример #9
0
 public void EmptyTest10()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("x", "y");
     Assert.AreEqual(1, t["y"]);
     t.RemoveDependency("x", "y");
     Assert.AreEqual(0, t["y"]);
 }
Пример #10
0
 public Spreadsheet()
     : base(s=>true, s => s, "default")
 {
     this.IsValid = IsValid;
     this.Normalize = Normalize;
     dg = new DependencyGraph();
     cells = new HashSet<Cell>();
     change = false;
 }
Пример #11
0
 public Spreadsheet(Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     this.IsValid = isValid;
     this.Normalize = normalize;
     dg = new DependencyGraph();
     cells = new HashSet<Cell>();
     change = false;
 }
Пример #12
0
 public void EmptyTest12()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("x", "y");
     Assert.AreEqual(t.Size, 1);
     t.RemoveDependency("x", "y");
     t.ReplaceDependents("x", new HashSet<string>());
     t.ReplaceDependees("y", new HashSet<string>());
 }
 public void AddTest1Test()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("A1", "A2");
     t.AddDependency("A1", "A3");
     t.RemoveDependency("A1", "A2");
     Assert.AreEqual(1, t.Size);
     t.AddDependency("A1", "A4");
     Assert.AreEqual(2, t.Size);
     HashSet<string> test = new HashSet<string>();
     test.Add("A3");
     test.Add("A4");
     Assert.AreEqual(true, test.SetEquals(t.GetDependents("A1")));
 }
 public void PrivateDataTest()
 {
     try
     {
         DependencyGraph dg = new DependencyGraph();
         dg.AddDependency("a", "b");
         dg.AddDependency("a", "c");
         ICollection<string> temp = (ICollection<string>)dg.GetDependents("a");
         temp.Add("d");
         Assert.IsTrue(new HashSet<string> { "b", "c", "d" }.SetEquals(temp));
         Assert.IsTrue(new HashSet<string> { "b", "c" }.SetEquals(dg.GetDependents("a")));
     }
     catch (Exception e)
     {
         if (!(e is NotSupportedException || e is InvalidCastException))
             Assert.Fail();
     }
 }
Пример #15
0
        /// <summary>
        /// Opens a saved spreadsheet and puts it back together
        /// </summary>
        /// <param name="myPath"></param>
        /// <param name="myValid"></param>
        /// <param name="myNormalize"></param>
        /// <param name="myVersion"></param>
        public Spreadsheet(string myPath, Func<string, bool> myValid, Func<string, string> myNormalize, string myVersion)
            : base(myValid, myNormalize, myVersion)
        {
            mySpreadsheet = new Dictionary<string, Cell>();
            dependentCells = new DependencyGraph();
            //GetSavedVersion(myPath);
            using (XmlReader myReader = XmlReader.Create(myPath))
            {
                string myContent = null;
                while (myReader.Read())
                {

                    if (myReader.IsStartElement())
                    {
                        switch (myReader.Name)
                        {
                            case "Version Information":
                                break;
                            case "cell":
                                break;

                            case "name":

                                myReader.Read();
                                myContent = myReader.Value;
                                break;
                            case "contents":
                                myReader.Read();
                                HashSet<string> mySet = new HashSet<string>();
                                //Foreach look sets the cell contents and evaluates the value of any direct or indirect dependents
                                foreach (string s in SetContentsOfCell(myContent, myReader.Value))
                                {
                                    mySpreadsheet[s].myValue = myEvaluate(s);
                                }
                                break;

                        }
                    }
                }
            }
            Changed = false;
        }
Пример #16
0
 public Spreadsheet(String filename, Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     this.IsValid = isValid;
     this.Normalize = normalize;
     dg = new DependencyGraph();
     cells = new HashSet<Cell>();
     this.filename = filename;
     change = false;
     if (GetSavedVersion(filename) != version)
     {
         throw new SpreadsheetReadWriteException("Conflicting versions of file.");
     }
     try
     {
         ReadandSetFile(filename);
     }
     catch (Exception)
     {
         throw new SpreadsheetReadWriteException("Could not read file");
     }
 }
Пример #17
0
        /// <summary>
        /// Constructs an abstract spreadsheet by recording its variable validity test,
        /// its normalization method, and its version information.  The variable validity
        /// test is used throughout to determine whether a string that consists of one or
        /// more letters followed by one or more digits is a valid cell name.  The variable
        /// equality test should be used thoughout to determine whether two variables are
        /// equal.
        /// </summary>
        public Spreadsheet(string filePath, Func<string, bool> isValid, Func<string, string> normalize, string version)
            : base(isValid, normalize, version)
        {
            //read save file from filePath
            spreadsheet = new Dictionary<string, Cell> { };
            dependency_graph = new DependencyGraph();
            has_changed = false;
            try
            {
                using (XmlReader read_file = XmlReader.Create(filePath))
                {
                    if (GetSavedVersion(filePath) != version)
                    {
                        throw new SpreadsheetReadWriteException("File versions are not the same.");
                    }
                    try
                    {
                        string cell_name = "";
                        while (read_file.Read())
                        {
                            if (read_file.IsStartElement())
                            {
                                switch (read_file.Name)
                                {
                                    case "spreadsheet":
                                        break;
                                    case "cell":
                                        break;
                                    case "name":
                                        read_file.Read();
                                        cell_name = read_file.Value;
                                        break;
                                    case "contents":
                                        read_file.Read();
                                        try
                                        {
                                            SetContentsOfCell(cell_name, read_file.Value);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new SpreadsheetReadWriteException(ex.Message);
                                        }
                                        break;
                                }
                            }
                        }
                    }
                    catch
                    {

                        throw new SpreadsheetReadWriteException("Error while reading from file");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SpreadsheetReadWriteException(ex.Message);
            }
            has_changed = false;
        }
Пример #18
0
 /// <summary>
 /// Constructs an abstract spreadsheet by recording its variable validity test,
 /// its normalization method, and its version information.  The variable validity
 /// test is used throughout to determine whether a string that consists of one or
 /// more letters followed by one or more digits is a valid cell name.  The variable
 /// equality test should be used thoughout to determine whether two variables are
 /// equal.
 /// </summary>
 public Spreadsheet(Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     spreadsheet = new Dictionary<string, Cell> { };
     dependency_graph = new DependencyGraph();
     has_changed = false;
 }
Пример #19
0
 /// <summary>
 /// Creates a new Spreadsheet.
 /// </summary>
 public Spreadsheet()
     : base(valid_name, name=>name, "default")
 {
     spreadsheet = new Dictionary<string, Cell> { }; //Keeps track of the Cell names and their Correspoding cell objects.
     dependency_graph = new DependencyGraph(); //Keeps track of depencies between Cells.
     has_changed = false;
 }
Пример #20
0
 /// <summary>
 /// The three argument constructor; this allows the user to provide a validity delegate, a normalization delegate, 
 /// and a version (third parameter).
 /// </summary>
 /// <param name="isValid"></param>
 /// <param name="normalize"></param>
 /// <param name="version"></param>
 public Spreadsheet(Func<string, bool> isValid, Func<string, string> normalize, string version)
     : base(isValid, normalize, version)
 {
     //Setup data structures that keep track of which cells depend on which, and the association between cells and cell names.
     dependencies = new DependencyGraph();
     cells = new SortedDictionary<string, Cell>();
     Changed = false;
 }
Пример #21
0
 public void EmptyTest8()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("a", "b");
 }
Пример #22
0
 public void EmptyTest7()
 {
     DependencyGraph t = new DependencyGraph();
     t.RemoveDependency("a", "b");
     Assert.AreEqual(0, t.Size);
 }
Пример #23
0
 public void EmptyTest6()
 {
     DependencyGraph t = new DependencyGraph();
     Assert.AreEqual(0, t["a"]);
 }
Пример #24
0
 public void EmptyTest5()
 {
     DependencyGraph t = new DependencyGraph();
     Assert.IsFalse(t.GetDependents("a").GetEnumerator().MoveNext());
 }
Пример #25
0
 public void EmptyTest3()
 {
     DependencyGraph t = new DependencyGraph();
     Assert.IsFalse(t.HasDependents("a"));
 }
Пример #26
0
        public void StressTest15()
        {
            // Dependency graph
            DependencyGraph t = new DependencyGraph();

            // A bunch of strings to use
            const int SIZE = 100;
            string[] letters = new string[SIZE];
            for (int i = 0; i < SIZE; i++)
            {
                letters[i] = ("" + (char)('a' + i));
            }

            // The correct answers
            HashSet<string>[] dents = new HashSet<string>[SIZE];
            HashSet<string>[] dees = new HashSet<string>[SIZE];
            for (int i = 0; i < SIZE; i++)
            {
                dents[i] = new HashSet<string>();
                dees[i] = new HashSet<string>();
            }

            // Add a bunch of dependencies
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = i + 1; j < SIZE; j++)
                {
                    t.AddDependency(letters[i], letters[j]);
                    dents[i].Add(letters[j]);
                    dees[j].Add(letters[i]);
                }
            }

            // Remove a bunch of dependencies
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = i + 2; j < SIZE; j += 2)
                {
                    t.RemoveDependency(letters[i], letters[j]);
                    dents[i].Remove(letters[j]);
                    dees[j].Remove(letters[i]);
                }
            }

            // Replace a bunch of dependees
            for (int i = 0; i < SIZE; i += 4)
            {
                HashSet<string> newDees = new HashSet<String>();
                for (int j = 0; j < SIZE; j += 7)
                {
                    newDees.Add(letters[j]);
                }
                t.ReplaceDependees(letters[i], newDees);

                foreach (string s in dees[i])
                {
                    dents[s[0] - 'a'].Remove(letters[i]);
                }

                foreach (string s in newDees)
                {
                    dents[s[0] - 'a'].Add(letters[i]);
                }

                dees[i] = newDees;
            }

            // Make sure everything is right
            for (int i = 0; i < SIZE; i++)
            {
                Assert.IsTrue(dents[i].SetEquals(new HashSet<string>(t.GetDependents(letters[i]))));
                Assert.IsTrue(dees[i].SetEquals(new HashSet<string>(t.GetDependees(letters[i]))));
            }
        }
Пример #27
0
 public void EmptyTest1()
 {
     DependencyGraph t = new DependencyGraph();
     Assert.AreEqual(0, t.Size);
 }
Пример #28
0
 public void NonEmptyTest8()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("a", "b");
     t.AddDependency("a", "c");
     t.AddDependency("d", "c");
     t.ReplaceDependees("c", new HashSet<string>() { "x", "y", "z" });
     HashSet<String> cDees = new HashSet<string>(t.GetDependees("c"));
     Assert.IsTrue(cDees.SetEquals(new HashSet<string>() { "x", "y", "z" }));
 }
Пример #29
0
 public void NonEmptyTest6()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("a", "b");
     t.AddDependency("a", "c");
     t.AddDependency("d", "c");
     t.RemoveDependency("a", "b");
     Assert.AreEqual(2, t.Size);
 }
Пример #30
0
 public void NonEmptyTest5()
 {
     DependencyGraph t = new DependencyGraph();
     t.AddDependency("a", "b");
     t.AddDependency("a", "c");
     t.AddDependency("d", "c");
     Assert.AreEqual(0, t["a"]);
     Assert.AreEqual(1, t["b"]);
     Assert.AreEqual(2, t["c"]);
     Assert.AreEqual(0, t["d"]);
     Assert.AreEqual(0, t["e"]);
 }