예제 #1
0
        /// <summary>
        /// Notifies the user that the session ended, stops pinging, empties out old sheet, and closes the server.
        /// </summary>
        private void EndSession(string reason)
        {
            fullStateReceived = false;
            window.ShowErrorMessageBox(reason + ": The session has ended.");
            EmptyAllCells(new HashSet <string>(sheet.GetNamesOfAllNonemptyCells()));
            ResetSheet(new HashSet <string>(sheet.GetNamesOfAllNonemptyCells()));


            foreach (string cell in clientCells.Values)
            {
                UnfocusCell(cell);
            }
            clientCells.Clear();


            window.StopPinging();

            if (theServer != null && theServer.Connected)
            {
                theServer.Shutdown(SocketShutdown.Both);
                theServer.Close();
            }

            theServer = null;
        }
 public void GetNamesOfAllNonemptyCellsTest3()
 {
     Spreadsheet test_spreadsheet = new Spreadsheet();
     test_spreadsheet.SetCellContents("A1", "x+1");
     Assert.AreEqual("A1", new List<string>(test_spreadsheet.GetNamesOfAllNonemptyCells())[0]);
     Assert.AreEqual(1, new List<string>(test_spreadsheet.GetNamesOfAllNonemptyCells()).Count);
 }
예제 #3
0
 public void RunRandomizedTest(int seed, int size)
 {
     Spreadsheet s = new Spreadsheet();
     Random rand = new Random(seed);
     for (int i = 0; i < 10000; i++)
     {
         try
         {
             switch (rand.Next(3))
             {
                 case 0:
                     s.SetContentsOfCell(randomName(rand), "3.14");
                     break;
                 case 1:
                     s.SetContentsOfCell(randomName(rand), "hello");
                     break;
                 case 2:
                     s.SetContentsOfCell(randomName(rand), randomFormula(rand));
                     break;
             }
         }
         catch (CircularException)
         {
         }
     }
     ISet<string> set = new HashSet<string>(s.GetNamesOfAllNonemptyCells());
     Assert.AreEqual(size, set.Count);
 }
예제 #4
0
 public void TestEmptyGetNamesOfCells()
 {
     Spreadsheet sheet = new Spreadsheet();
     var nameEnum = sheet.GetNamesOfAllNonemptyCells().GetEnumerator();
     nameEnum.MoveNext();
     Assert.IsTrue(nameEnum.Current == null);
 }
예제 #5
0
        public void TestMethod16()
        {
            var spreadsheet = new SS.Spreadsheet();

            var random = new Random();

            var listOfNonEmpty = new List <string>();

            foreach (var cell in GenerateCells(26, 26))
            {
                var shouldBeEmpty = random.Next() % 2 == 0;

                spreadsheet.SetCellContents(cell, shouldBeEmpty ? string.Empty : "Not empty bro");

                if (!shouldBeEmpty)
                {
                    listOfNonEmpty.Add(cell);
                }
            }

            var notEmpties = spreadsheet.GetNamesOfAllNonemptyCells().ToList();

            foreach (var element in notEmpties)
            {
                Assert.IsTrue(listOfNonEmpty.Contains(element));
            }
        }
예제 #6
0
        /// <summary>
        /// Constructor for a Spreadsheet Form created from a file
        /// </summary>
        /// <param name="filename">The name of the file given</param>
        public Form1(string filename)
        {
            InitializeComponent();
            ss = new Spreadsheet(filename, Validator, UppercaseString, version);
            updateTextBox(spreadsheetPanel1);

            //Make sure all the existing cells show up
            foreach (string name in ss.GetNamesOfAllNonemptyCells())
            {
                updateCellValue(name);
            }
        }
예제 #7
0
        /// <summary>
        /// Handle 'Open' from File Menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "sprd files (*.sprd)|*.sprd|All files (*.*)|*.*";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // get hashset of old spreadsheet so that we can add to it and erase old and set new cells
                HashSet<string> oldSpread = new HashSet<string> (model.GetNamesOfAllNonemptyCells());

                model = new Spreadsheet(openFileDialog1.FileName, s => Regex.IsMatch(s, @"[A-Z][1-9][0-9]?"), s => s.ToUpper(), "PS6");
                foreach (string cell in model.GetNamesOfAllNonemptyCells())
                {
                    oldSpread.Add(cell);
                }
                refresh(oldSpread);
            }          
        }
예제 #8
0
        /// <summary>
        /// Empties the spreadsheet pane and sets its contents to the new spreadsheet model at fileLocation
        /// </summary>
        private void OpenSpreadsheetFromFile(string fileLocation)
        {
            // empty the current spreadsheetpane
            EmptyAllCells(new HashSet <string>(sheet.GetNamesOfAllNonemptyCells()));

            // window text is now the name of the new file
            window.WindowText = Path.GetFileName(fileLocation);

            // open the spreadsheet
            sheet = new SS.Spreadsheet(fileLocation, CellValidator, CellNormalizer, "ps6");

            // set the contents of the spreadsheet pane
            HashSet <string> nonEmpty = new HashSet <string>(sheet.GetNamesOfAllNonemptyCells());

            SetSpreadsheetPanelValues(nonEmpty);

            // update the current window selection
            window.SetCellSelectionToDefault();
            UpdateCurrentCellBoxes();
        }
예제 #9
0
        //A constructor that takes a file path and attempts to construct a spreadsheet from it. Things may still go wrong.
        public SSGUI(string file)
        {
            InitializeComponent();

            //The validity and normalization functions
            Func<string, bool> isValid = delegate(string s)
            { return Regex.IsMatch(s, @"[a-zA-Z][1-9][0-9]?"); };
            Func<string, string> normalize = delegate(string s)
            { return s.ToUpper(); };

            //Link with actual spread sheet
            _sheet = new Spreadsheet(file, isValid, normalize, "ps6");

            //Set the filepath to the file the spreadsheet opened
            filepath = file;

            //Loads the values into panel
            LoadPanelValues(new HashSet<string>(_sheet.GetNamesOfAllNonemptyCells()));

            //Display the first selection correctly.
            DisplaySelection(SSPanel);
        }
        // Deals with the Open menu
        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if the spreadsheet is changed display a popup
            if (thisSheet.Changed == true)
            {
                //display popup asking to discard or save changes
                String message = "Do you want to save your changes?";
                String caption = "Warning Unsaved Changes";
                var    result  = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    //Save changes
                    SaveCurrent();
                }
            }
            // Displays a OpenFileDialog so the user can open the spreadsheet
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter       = "Spreadsheets|*.sprd|All Files|*.*";
            ofd.DefaultExt   = "sprd";
            ofd.AddExtension = true;
            DialogResult res = ofd.ShowDialog();

            if (res.Equals(DialogResult.OK) && ofd.FileName != "")
            {
                spreadsheetPanel.Clear();
                thisSheet = new Spreadsheet(ofd.FileName, isValidDelegate, s => s.ToUpper(), "ps6");
            }
            if (res.Equals(DialogResult.Cancel))
            {
                ofd.Dispose();
            }

            IEnumerable <string> cellsToUpdate = thisSheet.GetNamesOfAllNonemptyCells();

            UpdateCellValues(cellsToUpdate);
        }
예제 #11
0
        public void TestGetNamesOfNonEmptyCells()
        {
            Spreadsheet sheet = new Spreadsheet();
            sheet.SetCellContents("A1", 10.5);
            sheet.SetCellContents("B1", "horse");
            sheet.SetCellContents("E1", 1.5);
            sheet.SetCellContents("A", "dad");

            var nameEnum = sheet.GetNamesOfAllNonemptyCells().GetEnumerator();

            List<object> names = new List<object>();
            for (int i = 0; i < 4; i++)
            {
                nameEnum.MoveNext();
                names.Add(nameEnum.Current);
            }
            
            Assert.IsTrue(names.Count == 4);

            Assert.IsTrue(names.Contains("A1"));
            Assert.IsTrue(names.Contains("B1"));
            Assert.IsTrue(names.Contains("E1"));
            Assert.IsTrue(names.Contains("A"));
        }
예제 #12
0
 public void TestPossibleCircular()
 {
     AbstractSpreadsheet sheet = new Spreadsheet();
     sheet.SetCellContents("A1", 10.0);
     sheet.SetCellContents("B1", new Formula("A1 * A1"));
     sheet.SetCellContents("C1", new Formula("B1 + A1"));
     sheet.SetCellContents("D1", new Formula("B1 - C1"));
     List<string> cell_names = new List<string>(sheet.GetNamesOfAllNonemptyCells());
     Assert.AreEqual(4, cell_names.Count);
 }
예제 #13
0
        public void Test18()
        {
            Spreadsheet s = new Spreadsheet();

            Assert.IsFalse(s.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
        }
예제 #14
0
 public void Test1900()
 {
     Spreadsheet s = new Spreadsheet();
     s.SetContentsOfCell("B1", "");
     Assert.IsFalse(s.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
 }
예제 #15
0
 public void Test2300()
 {
     Spreadsheet s = new Spreadsheet();
     s.SetContentsOfCell("A1", "17.2");
     s.SetContentsOfCell("C1", "hello");
     s.SetContentsOfCell("B1", "=3.5");
     Assert.IsTrue(new HashSet<string>(s.GetNamesOfAllNonemptyCells()).SetEquals(new HashSet<string>() { "A1", "B1", "C1" }));
 }
예제 #16
0
 public void TestGetNamesOfAllNonemptyCells2()
 {
     Spreadsheet sheet = new Spreadsheet();
     sheet.SetContentsOfCell("a1", "0.0");
     sheet.SetContentsOfCell("b2", "hello");
     sheet.SetContentsOfCell("c3", "=5 + 6");
     sheet.SetContentsOfCell("d4", "5.5");
     sheet.SetContentsOfCell("e5", "new cell");
     HashSet<string> correct = new HashSet<string> { "a1", "b2", "c3", "d4", "e5" };
     HashSet<string> output = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();
     Assert.IsTrue(correct.SetEquals(output));
 }
예제 #17
0
 public void TestRead3()
 {
     string path = "read3.xml";
     Spreadsheet sheet = new Spreadsheet();
     sheet.SetContentsOfCell("a1", "=1.5+4");
     sheet.SetContentsOfCell("b2", "=a2-2.6");
     sheet.SetContentsOfCell("c3", "=3.7*e5");
     sheet.SetContentsOfCell("d4", "hil");
     sheet.SetContentsOfCell("e5", "5.9");
     sheet.Save(path);
     HashSet<string> original = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();
     Spreadsheet sheet2 = new Spreadsheet(path, x => true, s => s, "default");
     foreach (string s in original) {
         Assert.IsTrue(sheet.GetCellContents(s).Equals(sheet2.GetCellContents(s)));
         Assert.IsTrue(sheet.GetCellValue(s).Equals(sheet2.GetCellValue(s)));
     }
 }
예제 #18
0
 public void Test23()
 {
     Spreadsheet s = new Spreadsheet();
     s.SetCellContents("A1", 17.2);
     s.SetCellContents("C1", "hello");
     s.SetCellContents("B1", new Formula("3.5"));
     Assert.IsTrue(new HashSet<string>(s.GetNamesOfAllNonemptyCells()).SetEquals(new HashSet<string>() { "A1", "B1", "C1" }));
 }
예제 #19
0
        public void GetNamesOfAllNonemptyCellsTest1()
        {
            Spreadsheet target = new Spreadsheet();
            List<string> expected = new List<string>();
            expected.Add("A1");
            expected.Add("B1");
            expected.Add("C1");
            expected.Add("D1");
            expected.Add("E1");
            expected.Add("F1");
            expected.Add("G1");
            target.SetContentsOfCell("A1", "4");
            target.SetContentsOfCell("B1", "4");
            target.SetContentsOfCell("C1", "4");
            target.SetContentsOfCell("D1", " 4");
            target.SetContentsOfCell("E1", "HI");
            target.SetContentsOfCell("F1", "=1+2");
            target.SetContentsOfCell("G1", "4");
            IEnumerable<string> actual = target.GetNamesOfAllNonemptyCells();
            CollectionAssert.AreEqual(expected, new List<string>(actual));

            // nothing at all
            target = new Spreadsheet();
            expected = new List<string>();
            actual = target.GetNamesOfAllNonemptyCells();
            CollectionAssert.AreEqual(expected, new List<string>(actual));
        }
예제 #20
0
 public void TestGetAllNames1()
 {
     AbstractSpreadsheet sheet = new Spreadsheet();
     sheet.SetCellContents("A1", 10.00);
     sheet.SetCellContents("A2", 10.00);
     sheet.SetCellContents("A3", 10.00);
     sheet.SetCellContents("A4", 10.00);
     sheet.SetCellContents("A5", 10.00);
     sheet.SetCellContents("A6", 10.00);
     sheet.SetCellContents("A7", 10.00);
     List<string> cell_names = new List<string>(sheet.GetNamesOfAllNonemptyCells());
     string[] cell_array = cell_names.ToArray();
     string text = string.Join(",", cell_array);
     Assert.AreEqual(7, cell_names.Count);
     Assert.AreEqual("A1,A2,A3,A4,A5,A6,A7", text);
 }
예제 #21
0
        public void TestAddingEmptyCell()
        {
            Spreadsheet sheet = new Spreadsheet();
            sheet.SetCellContents("A1", new Formula("D1"));
            sheet.SetCellContents("B1", "purple");
            sheet.SetCellContents("C1", 4);

            sheet.SetCellContents("A1", "");
            sheet.SetCellContents("B1", "");
            sheet.SetCellContents("C1", "");

            Assert.IsFalse(sheet.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
        }
예제 #22
0
        public void TestValidName()
        {
            Spreadsheet sheet = new Spreadsheet();
            sheet.SetCellContents("___", new Formula("D1"));
            sheet.SetCellContents("_1", "purple");
            sheet.SetCellContents("a_b2", 4);
            sheet.SetCellContents("a_b_2", 4);
            sheet.SetCellContents("a1_b2", 4);
            sheet.SetCellContents("a", 4);
            sheet.SetCellContents("A", 4);
            sheet.SetCellContents("A3", "");

            int cellCount = 0;
            IEnumerator<string> nonEmpty = sheet.GetNamesOfAllNonemptyCells().GetEnumerator();
            while (nonEmpty.MoveNext())
                cellCount++;

            Assert.AreEqual(cellCount, 7);
        }
예제 #23
0
		public void SetCellContentsFormulaTestMore()
		{
            Spreadsheet test_spreadsheet = new Spreadsheet();
			test_spreadsheet.SetCellContents("B2", new Formula("A1"));
			test_spreadsheet.SetCellContents("A1", new Formula("B2"));
			Assert.IsTrue(new List<string>(test_spreadsheet.GetNamesOfAllNonemptyCells())[0] == "B1");
			Assert.IsTrue(new List<string>(test_spreadsheet.GetNamesOfAllNonemptyCells()).Count == 1);
		}
예제 #24
0
        public void Names1()
        {
            AbstractSpreadsheet ss = new Spreadsheet();

            Assert.IsFalse(ss.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
        }
예제 #25
0
        public void GetNamesOfAllNonemptyCellsTest5()
        {
            Spreadsheet test_spreadsheet = new Spreadsheet();
            HashSet<String> set = new HashSet<string>();
            set.Add("A1");
            set.Add("B2");
            set.Add("C3");
            test_spreadsheet.SetCellContents("A1", new Formula("1+a1"));
            test_spreadsheet.SetCellContents("B2", new Formula("a1*a1"));
            test_spreadsheet.SetCellContents("C3", 5);
            IEnumerable<string> real_set;
            real_set = test_spreadsheet.GetNamesOfAllNonemptyCells();

            foreach (String s in real_set)
                Assert.IsTrue(set.Contains(s));
        }
예제 #26
0
 public void Test19()
 {
     AbstractSpreadsheet s = new Spreadsheet();
     s.SetCellContents("C2", "hello");
     s.SetCellContents("C2", "");
     Assert.IsFalse(s.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
 }
예제 #27
0
 public void TestGetNamesOfAllNonemptyCells1()
 {
     Spreadsheet sheet = new Spreadsheet();
     sheet.SetContentsOfCell("a1", "0.0");
     HashSet<string> correct = new HashSet<string> { "a1" };
     HashSet<string> output = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();
     Assert.IsTrue(correct.SetEquals(output));
 }
예제 #28
0
 public void Test21()
 {
     AbstractSpreadsheet s = new Spreadsheet();
     s.SetCellContents("B1", 52.25);
     AssertSetEqualsIgnoreCase(s.GetNamesOfAllNonemptyCells(), new string[] { "B1" });
 }
예제 #29
0
 public void TestRead2()
 {
     string path = "read2.xml";
     Spreadsheet sheet = new Spreadsheet();
     sheet.SetContentsOfCell("a1", "1.5");
     sheet.SetContentsOfCell("b2", "2.6");
     sheet.SetContentsOfCell("c3", "3.7");
     sheet.SetContentsOfCell("d4", "4.8");
     sheet.SetContentsOfCell("e5", "5.9");
     sheet.Save(path);
     HashSet<string> original = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();
     sheet = new Spreadsheet(path, x => true, s => s, "default");
     HashSet<string> output = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();
     Assert.IsTrue(original.SetEquals(output));
 }
예제 #30
0
 public void Test23()
 {
     AbstractSpreadsheet s = new Spreadsheet();
     s.SetCellContents("A1", 17.2);
     s.SetCellContents("C1", "hello");
     s.SetCellContents("B1", new Formula("3.5"));
     AssertSetEqualsIgnoreCase(s.GetNamesOfAllNonemptyCells(), new string[]{ "A1", "B1", "C1" });
 }
예제 #31
0
 public void Test1800()
 {
     Spreadsheet s = new Spreadsheet();
     Assert.IsFalse(s.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
 }
예제 #32
0
 public void Names1()
 {
     AbstractSpreadsheet ss = new Spreadsheet();
     Assert.IsFalse(ss.GetNamesOfAllNonemptyCells().GetEnumerator().MoveNext());
 }
예제 #33
0
 public void Test2100()
 {
     Spreadsheet s = new Spreadsheet();
     s.SetContentsOfCell("B1", "52.25");
     Assert.IsTrue(new HashSet<string>(s.GetNamesOfAllNonemptyCells()).SetEquals(new HashSet<string>() { "B1" }));
 }
예제 #34
0
 public void Names3()
 {
     AbstractSpreadsheet ss = new Spreadsheet();
     ss.SetContentsOfCell("A1", "hello");
     ss.SetContentsOfCell("B1", "55");
     ss.SetContentsOfCell("C1", "=A1+B1");
     ss.SetContentsOfCell("A1", "");
     HashSet<string> names = new HashSet<string>();
     names.Add("B1");
     names.Add("C1");
     Assert.IsTrue(names.SetEquals(ss.GetNamesOfAllNonemptyCells()));
 }
예제 #35
0
        public void StressTest1()
        {
            Spreadsheet sheet = new Spreadsheet();

            int SIZE = 1000;
            // Add a ton of items
            for (int i = 0; i < SIZE; i++)
                sheet.SetContentsOfCell("a" + i, 1.0 + i + "");

            for (int i = 0; i < SIZE; i++)
                sheet.SetContentsOfCell("b" + i, "hi" + i);

            for (int i = 0; i < SIZE; i++)
                sheet.SetContentsOfCell("c" + i, "=5+" + i);

            // Make correct HashSet
            HashSet<string> correct = new HashSet<string>();
            for (int i = 0; i < SIZE; i++)
                correct.Add("a" + i);
            for (int i = 0; i < SIZE; i++)
                correct.Add("b" + i);
            for (int i = 0; i < SIZE; i++)
                correct.Add("c" + i);

            // Get all of the names of the cells
            HashSet<string> output = (HashSet<string>)sheet.GetNamesOfAllNonemptyCells();

            // Confirm that the names in the spreadsheet are correct
            Assert.IsTrue(correct.SetEquals(output));

            // Check the cell contents of all the cells
            for (int i = 0; i < SIZE; i++)
                Assert.AreEqual(1.0 + i, sheet.GetCellContents("a" + i));
            for (int i = 0; i < SIZE; i++)
                Assert.AreEqual("hi" + i, sheet.GetCellContents("b" + i));
            for (int i = 0; i < SIZE; i++)
                Assert.AreEqual(new Formula("5+" + i), sheet.GetCellContents("c" + i));

            // You did it!
            Assert.IsTrue(true);
        }
예제 #36
0
 public void Stress5()
 {
     int seed = 47;
     int size = 831;
     AbstractSpreadsheet s = new Spreadsheet();
     Random rand = new Random(seed);
     for (int i = 0; i < 1000; i++)
     {
         try
         {
             switch (rand.Next(3))
             {
                 case 0:
                     s.SetContentsOfCell(randomName(rand), "3.14");
                     break;
                 case 1:
                     s.SetContentsOfCell(randomName(rand), "hello");
                     break;
                 case 2:
                     s.SetContentsOfCell(randomName(rand), "=" + randomFormula(rand));
                     break;
             }
         }
         catch (CircularException)
         {
         }
     }
     ISet<string> set = new HashSet<string>(s.GetNamesOfAllNonemptyCells());
     //return size == set.Count;
     Assert.AreEqual(size, set.Count);
 }