public void GetCellContentsStringCellExistsTest() { Spreadsheet s = new Spreadsheet(); Assert.AreEqual(true, s.SetContentsOfCell("a1", "Hey there").Contains("a1")); Assert.AreEqual(true, s.SetContentsOfCell("a1", "What's up?").Contains("a1")); Assert.AreEqual("What's up?", s.GetCellContents("a1")); }
public void GetCellContentsDoubleCellExistsTest() { Spreadsheet s = new Spreadsheet(); Assert.AreEqual(true, s.SetContentsOfCell("a1", "2.0").Contains("a1")); Assert.AreEqual(true, s.SetContentsOfCell("a1", "5.3").Contains("a1")); Assert.AreEqual(5.3, s.GetCellContents("a1")); }
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); }
public void SetStringContentTwice() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("A1", "blue"); sheet.SetContentsOfCell("A1", "green"); Assert.AreEqual(sheet.GetCellContents("A1"), "green"); Assert.AreNotEqual(sheet.GetCellContents("A1"), "blue"); }
public void GetNamesOfAllNonemptyCellsTest() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("a1", "20.0"); s.SetContentsOfCell("a2", "Hey there"); IEnumerable<string> names = s.GetNamesOfAllNonemptyCells(); Assert.AreEqual(true, names.Contains("a1")); Assert.AreEqual(true, names.Contains("a2")); }
public void NewTest1() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("A1", "3"); sheet.SetContentsOfCell("B1", "=A1"); Assert.AreEqual(sheet.GetCellValue("B1"), 3.0); sheet.SetContentsOfCell("A1", ""); Assert.IsTrue(sheet.GetCellValue("B1") is FormulaError); }
public void GetCellContentsFormula() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("A1", "blue"); sheet.SetContentsOfCell("A1", "green"); sheet.SetContentsOfCell("B1", "purple"); sheet.SetContentsOfCell("C1", "red"); sheet.SetContentsOfCell("D1", "=C1 + 2 + X1"); Assert.AreEqual(sheet.GetCellContents("D1"), new Formula("C1+ 2+ X1")); }
public void GetCellValueTest() { AbstractSpreadsheet s = new Spreadsheet(); string name; string content; // Try a value of a string from string name = "A1"; content = "Hello"; s.SetContentsOfCell(name, content); string expected1 = content; string actual1 = (string)s.GetCellValue(name); Assert.AreEqual(expected1, actual1); // Try a value of a double from a double name = "B1"; content = "3"; s.SetContentsOfCell(name, content); double expected2 = 3; double actual2 = (double)s.GetCellValue(name); Assert.AreEqual(expected2, actual2); // Try a value of a double from a Formula name = "C1"; content = "=3+1"; s.SetContentsOfCell(name, content); double expected3 = 4; double actual3 = (double)s.GetCellValue(name); Assert.AreEqual(expected3, actual3); // Try all possible FormulaError name = "D1"; content = "=1/0"; s.SetContentsOfCell(name, content); object actual4 = s.GetCellValue(name); Assert.IsInstanceOfType(actual4, typeof(FormulaError)); // Try chaining several formulas together then changing the value of cell. Test before and after. s.SetContentsOfCell("E1", "=F1 + 1"); s.SetContentsOfCell("F1", "=G1 + 1"); s.SetContentsOfCell("G1", "1"); Assert.AreEqual(3, (double)s.GetCellValue("E1")); Assert.AreEqual(2, (double)s.GetCellValue("F1")); s.SetContentsOfCell("G1", "2"); Assert.AreEqual(4, (double)s.GetCellValue("E1")); Assert.AreEqual(3, (double)s.GetCellValue("F1")); // Try switching a cell that has a formula depending on it to string. s.SetContentsOfCell("J1", "This is a string"); s.SetContentsOfCell("K1", "=J1"); Assert.IsInstanceOfType(s.GetCellValue("K1"), typeof(FormulaError)); }
public void GetValueFormula01() { Spreadsheet sheet = new Spreadsheet(s => true, s => s, "default"); sheet.SetContentsOfCell("D1", "=5"); Assert.AreEqual(sheet.GetCellContents("D1"), new Formula("5")); Assert.AreEqual(sheet.GetCellValue("D1"), (double)5); }
public void TestConstructor() { //just some stuff with filewriting Assert.IsTrue(sheet1.IsValid("any old string")); Assert.IsTrue(sheet1.Normalize("dead") == "dead"); Assert.IsTrue(sheet1.Version == "default"); //test 3 arg constructor sheet1 = new Spreadsheet(s => (s.Length >= 2) ? true : false, s => s.Replace(" ", ""), "version1"); Assert.IsTrue(sheet1.IsValid("A1")); Assert.IsFalse(sheet1.IsValid("A")); Assert.IsTrue(sheet1.Normalize("d e a d") == "dead"); Assert.IsTrue(sheet1.Version == "version1"); sheet1.SetContentsOfCell("A 1","loaded!"); string savePath = "save 1.xml"; sheet1.Save(savePath); sheet1 = new Spreadsheet( savePath, s => (s.Length >= 2) ? true : false, s => s.Replace(" ", ""), "version1"); Assert.AreEqual("loaded!",(string)sheet1.GetCellContents("A1")); }
public void GetCellContentsStringTest() { //Gets the contents of a string-type cell Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("a1", "Hey there"); Assert.AreEqual("Hey there", s.GetCellContents("a1")); }
/// <summary> /// gets the current text in the current cell contents box, sets it to the model's cell contents and /// updates the views cell value. If an error occurs, creates a message box with the a message that /// an error occured. /// </summary> private void SetCellContentsFromContentsBox() { // get the location of the currently selected cell window.GetCellSelection(out int row, out int col); // convert row and column to a cell name string cellName = ConvertRowColToCellName(row, col); try { // reset the contents of the cell and recalculate dependent cells ISet <string> cellsToUpdate = sheet.SetContentsOfCell(cellName, window.ContentsBoxText); SetSpreadsheetPanelValues(cellsToUpdate); UpdateCurrentCellBoxes(); } catch (CircularException) { window.ShowErrorMessageBox("Circular dependency detected"); } catch (Exception e) { window.ShowErrorMessageBox(e.Message); } window.SetFocusToContentBox(); }
/// <summary> /// Sets the model's cell contents and updates the panel with the correct values. /// <contents>example is "A1:3"</contents> /// </summary> private void ApplyChange(string contents) { // contents = "A1:=hello" string[] parsed = contents.Split(':'); if (parsed.Length != 2) { return; // discard (message was bad) } // convert row and column to a cell name string cellName = parsed[0]; // reset the contents of the cell and recalculate dependent cells ISet <string> cellsToUpdate = sheet.SetContentsOfCell(cellName, parsed[1]); SetSpreadsheetPanelValues(cellsToUpdate); if (cellName == window.CurrentCellText) { SetCellValueBox(); //SpreadsheetPanel mySheet = window.GetSpreadsheetPanel(); //int row, col; //ConvertCellNameToRowCol(cellName, out row, out col); //mySheet.SetSelection(col, row); } }
public void GetValueDoubleEmptyConstructor() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("D1", "5"); Assert.AreEqual(sheet.GetCellContents("D1"), (double)5); Assert.AreEqual(sheet.GetCellValue("D1"), (double)5); }
public void NewTest11() { Spreadsheet s = new Spreadsheet(x => true, x => x, "\"default\""); s.SetContentsOfCell("a1", "Text"); s.Save("..\\..\\..\\Test.xml"); string sversion = s.GetSavedVersion("..\\..\\..\\Test.xml"); Assert.AreEqual("\"default\"", sversion); }
public void ChangedTest() { string existing_path = @"..\..\..\SpreadSheetTests\validspreadsheet.xml"; // the .. means up a directory Spreadsheet target = new Spreadsheet(existing_path, s => true, s => s.ToUpper(), "dan1"); Assert.AreEqual(false, target.Changed); target.SetContentsOfCell("A1", "33"); Assert.AreEqual(true, target.Changed); }
/// <summary> /// Trigger the events when pressing specific keys happen /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void selectedCellContentsTextBox_KeyDown(object sender, KeyEventArgs e) { try { //spreadsheetPanel.GetSelection(out int col, out int row); if (e.KeyCode == Keys.Enter) { IList <String> cellNamesToUpdate = thisSheet.SetContentsOfCell(selectedCellNameTextBox.Text, "d"); thisSheet.SetContentsOfCell(selectedCellNameTextBox.Text, selectedCellContentsTextBox.Text); SetCellNameAndContents(); UpdateCellValues(cellNamesToUpdate); } } catch (FormulaFormatException) { String caption = "Invalid Formula"; String message = "The formula you have entered is invalid"; var result = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Changed() { // Make sure it starts false AbstractSpreadsheet s = new Spreadsheet(); Assert.IsFalse(s.Changed); // Modify a file, see if changed is true; Use SetContentsOfCell s.SetContentsOfCell("Z7", "1.5"); Assert.IsTrue(s.Changed); // Save a file after modifying, see if it switches back to false s.Save("changed_test.xml"); Assert.IsFalse(s.Changed); }
public void TestGetNamesOfNonEmptyCells() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("A1", "10.5"); sheet.SetContentsOfCell("B1", "horse"); sheet.SetContentsOfCell("E1", "1.5"); sheet.SetContentsOfCell("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")); }
/// <summary> /// takes string contents and column and row number, /// converts col and row to a cell value, adds cell /// to backing sheet and update gui accordingly /// </summary> /// <param name="col"></param> /// <param name="row"></param> /// <param name="contents"></param> private void SetCell(int col, int row, string contents) { string name = GetCellName(col, row); string value = ""; try { //store contents in backing sheet sheet.SetContentsOfCell(name, contents); //get value of cell from backing sheet object valueObj = sheet.GetCellValue(name); //handle formula errors if (valueObj.GetType().Equals(typeof(FormulaError))) { FormulaError fe = (FormulaError)valueObj; value = fe.Reason; } //store value of cell else { value = valueObj.ToString(); } } catch (FormulaFormatException ffe) { //create error message window MessageBox.Show(ffe.Message, "Formula Format Error"); string s = ffe.Message; } isChanged = sheet.Changed; //update gui representation cellValueField.Text = value; spreadsheetPanel1.SetValue(col, row, value); contentsChanged = false; }
/// <summary> /// if the server send back Create or Open a new spreadsheet, the following funciotn will be called /// </summary> public void CreateOrOpenSpreadsheetEvent(String updateCommand) { //here, create a newspreadsheet and pass the value to the spreadsheet fileChooser.Invoke(new Action(() => { //Thread thread = new Thread(() => RunNewSpreadSheet(updateCommand)); Thread thread = new Thread(() => { //you need to create a spreadsheet here. Spreadsheet ss = new Spreadsheet(); String[] temp = CommandParser(updateCommand); ss.Version = temp[1]; for (int i = 2; i < temp.Length && i + 1 < temp.Length; i += 2) { ss.SetContentsOfCell(temp[i], temp[i + 1]); } //realse the spreadsheetmodel update event binding. ReleaseBindingModelEvent(); Application.Run(new SpreadsheetGUI.Form1(ss, spreadsheetmodel)); }); thread.Start(); this.Close(); })); }
public void CreateSaveLoadSpreadsheet() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("D1", "=5"); sheet.SetContentsOfCell("C1", "=4.5"); sheet.SetContentsOfCell("E1", "apples"); sheet.SetContentsOfCell("G1", "-2"); sheet.SetContentsOfCell("H1", "-4"); sheet.SetContentsOfCell("F1", "= G1 + H1"); sheet.Save(@"MyXml2.xml"); Spreadsheet sheet2 = new Spreadsheet(@"MyXml2.xml", s => true, s => s, "default"); Assert.AreEqual(sheet2.GetCellContents("D1"), new Formula("5")); Assert.AreEqual(sheet2.GetCellContents("C1"), new Formula("4.5")); Assert.AreEqual(sheet2.GetCellContents("E1"), "apples"); Assert.AreEqual(sheet2.GetCellContents("G1"), (double)-2); Assert.AreEqual(sheet2.GetCellContents("H1"), (double)-4); Assert.AreEqual(sheet2.GetCellContents("F1"), new Formula("G1 + H1")); Assert.AreEqual(sheet2.GetCellValue("F1"), (double)-6); }
public void TestAddCells1() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("a1", "5.0"); }
public void Test900() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("1AZ", "hello"); }
public void Test800() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell(null, "hello"); }
public void Test700() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("A8", (string)null); }
public void Test600() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("Z7", "1.5"); Assert.AreEqual(1.5, (double)s.GetCellContents("Z7"), 1e-9); }
public void TestAddCells17() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("83", "hello"); }
public void Test4300() { Spreadsheet s = new Spreadsheet(); for (int i = 0; i < 500; i++) { s.SetContentsOfCell("A1" + i, "=A1" + (i + 1)); } HashSet<string> firstCells = new HashSet<string>(); HashSet<string> lastCells = new HashSet<string>(); for (int i = 0; i < 250; i++) { firstCells.Add("A1" + i); lastCells.Add("A1" + (i + 250)); } HashSet<string> set = (HashSet<string>)s.SetContentsOfCell("A1499", "0"); Assert.IsTrue(s.SetContentsOfCell("A1249", "25.0").SetEquals(firstCells)); Assert.IsTrue(s.SetContentsOfCell("A1499", "0").SetEquals(lastCells)); }
/// <summary> /// ///////////////////////////////////////////////////////////////////////////////////// /// I added this method with the intent it be used for when cell change messages are /// received from the server /// ///////////////////////////////////////////////////////////////////////////////////// /// </summary> /// <param name="col"></param> /// <param name="row"></param> /// <param name="contents"></param> private void ChangeCellContents() { this.Invoke((MethodInvoker) delegate { if (cellTextBox.Location.X == 42 && cellTextBox.Location.Y == 79) { if (sheet.GetCellValue("A1").ToString() != null) { cellContentsField.Text = sheet.GetCellContents("A1").ToString(); cellValueField.Text = sheet.GetCellValue("A1").ToString(); cellTextBox.Text = sheet.GetCellValue("A1").ToString(); } else { cellContentsField.Text = ""; cellValueField.Text = ""; cellTextBox.Text = ""; } } string name = state.CellName; string value = ""; try { //store contents in backing sheet if (state.Contents.Length > 0 && state.Contents[0] == '=') { sheet.SetContentsOfCell(name, state.Contents.ToUpper()); } else { sheet.SetContentsOfCell(name, state.Contents); } object valueObj = ""; //get value of cell from backing sheet if (sheet.GetCellContents(name) is String && !sheet.GetCellContents(name).Equals("")) { valueObj = sheet.GetCellValue(name); } //handle formula errors if (valueObj.GetType().Equals(typeof(FormulaError))) { FormulaError fe = (FormulaError)valueObj; value = fe.Reason; } //store value of cell else { value = valueObj.ToString(); } } catch (FormulaFormatException ffe) { value = ffe.Message; } cellValueField.Text = value; spreadsheetPanel1.SetValue(state.Col, state.Row, value); try { foreach (string s in sheet.CheckValues(name)) { int row, col; Int32.TryParse(s.Substring(1, s.Length - 1), out row); col = s[0] - 65; spreadsheetPanel1.SetValue(col, row - 1, sheet.GetCellValue(s).ToString()); cellValueField.Text = sheet.GetCellValue(s) + ""; UpdateGUIFields(s); } } catch (InvalidCastException e) { MessageBox.Show("You can't do that"); } updateCells(name); cellTextBox.Text = ""; contentsChanged = false; }); }
public void TestAddCells13() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("", "=5"); }
public void TestAddCells15() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell(null, "hello"); }
public void Test500() { Spreadsheet s = new Spreadsheet(); s.SetContentsOfCell("1A1A", "1.5"); }
public void TestAddCells18() { Spreadsheet sheet = new Spreadsheet(); sheet.SetContentsOfCell("a1", "0.0"); sheet.SetContentsOfCell("a1", "hello"); sheet.SetContentsOfCell("a1", "=5"); sheet.SetContentsOfCell("a1", "17.23"); sheet.SetContentsOfCell("a1", "hi"); sheet.SetContentsOfCell("a1", "=13+x3"); }
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); }