public void TestEqualsFromToString() { string expression = "x + 2"; Formula a = new Formula(expression); Formula b = new Formula(a.ToString()); Assert.AreEqual(true, a.Equals(b)); Assert.AreEqual(true, b.Equals(a)); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens, which are compared as doubles, and variable tokens, /// whose normalized forms are compared as strings. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { //if obj is not a Formula newObj will be null Formula newObj = obj as Formula; //first check if newObject is null (if true short circuit false) if compare string values return(!ReferenceEquals(newObj, null) && this.ToString().Equals(newObj.ToString())); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens, which are compared as doubles, and variable tokens, /// whose normalized forms are compared as strings. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj.GetType() != this.GetType()) { return(false); } Formula temp = (Formula)obj; return(this.ToString().Equals(temp.ToString())); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens and variable tokens. /// Numeric tokens are considered equal if they are equal after being "normalized" /// by C#'s standard conversion from string to double, then back to string. This /// eliminates any inconsistencies due to limited floating point precision. /// Variable tokens are considered equal if their normalized forms are equal, as /// defined by the provided normalizer. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { if (obj is null) { return(false); } if (!(obj is Formula)) { return(false); } string thisString = this.ToString(); Formula passedIn = (Formula)obj; string passedInString = passedIn.ToString(); foreach (string passedInToken in GetTokens(passedIn.ToString())) { if (IsRealNumber(passedInToken)) { double dPassedIn = Double.Parse(passedInToken); string backToStrPassedIn = dPassedIn.ToString(); passedInString = passedInString.Replace(passedInToken, backToStrPassedIn); } } foreach (string thisToken in GetTokens(this.ToString())) { if (IsRealNumber(thisToken)) { double dThis = Double.Parse(thisToken); string backToStrThis = dThis.ToString(); thisString = thisString.Replace(thisToken, backToStrThis); } } return(thisString.Equals(passedInString)); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens and variable tokens. /// Numeric tokens are considered equal if they are equal after being "normalized" /// by C#'s standard conversion from string to double, then back to string. This /// eliminates any inconsistencies due to limited floating point precision. /// Variable tokens are considered equal if their normalized forms are equal, as /// defined by the provided normalizer. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { if (obj == null || !(obj is Formula)) { return(false); } Formula objFormula = (Formula)obj; if (!(objFormula.ToString()).Equals(this.ToString())) { return(false); } return(true); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens and variable tokens. /// Numeric tokens are considered equal if they are equal after being "normalized" /// by C#'s standard conversion from string to double, then back to string. This /// eliminates any inconsistencies due to limited floating point precision. /// Variable tokens are considered equal if their normalized forms are equal, as /// defined by the provided normalizer. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { if (obj == null || !(obj is Formula)) { return(false); } Formula a = (Formula)obj; string[] tokens = GetTokens(a.ToString()).ToArray(); for (int i = 0; i < tokens.Length; i++) { //if token is a number parse it first and change it to string before comparing. if (!tokens[i].Equals(formulaToEvaluate[i])) { double parsedFirstDouble1 = 0.0; bool parsedFirst = double.TryParse(tokens[i], out parsedFirstDouble1); double parsedFirstDouble2 = 0.0; bool parsedSecond = double.TryParse(formulaToEvaluate[i], out parsedFirstDouble2); //checking if parsed first and parsed second is true. if (parsedFirst && parsedSecond) { string first = parsedFirstDouble1.ToString(); string second = parsedFirstDouble2.ToString(); if (first != second) { return(false); } } else { return(false); } } } return(true); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens, which are compared as doubles, and variable tokens, /// whose normalized forms are compared as strings. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { // Preliminary null checks if (obj == null || !(obj is Formula)) { return(false); } // Generate a string from the input formula Formula input = (Formula)obj; String form2 = input.ToString(); // Generate arrays of the tokens of each formula expression string[] formula1 = GetTokens(formulaString).ToArray <string>(); string[] formula2 = GetTokens(form2).ToArray <string>(); // If the two are equal the arrays should be the same size if (formula1.Length != formula2.Length) { return(false); } // Step through each entry in the arrays for (int i = 0; i < formula1.Length; i++) { // If both intries are equal, step to next if (formula1[i] == formula2[i]) { continue; } // Return false if not else { return(false); } } return(true); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens and variable tokens. /// Numeric tokens are considered equal if they are equal after being "normalized" /// by C#'s standard conversion from string to double, then back to string. This /// eliminates any inconsistencies due to limited floating point precision. /// Variable tokens are considered equal if their normalized forms are equal, as /// defined by the provided normalizer. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { //If the object is null, or not a formula, then it returns false if (ReferenceEquals(obj, null) || !(obj is Formula)) { return(false); } //Creates a formula setting the object as a formula Formula formula = obj as Formula; //Gets the string version of the formula object String forStr = formula.ToString(); //Gets the current string, or the original string to be compared with String currStr = ToString(); //Compares if the two strings are equal to each other or not using String's comparison method if (forStr == currStr) { return(true); } return(false); }
public void tostringTest3() { Formula f1 = new Formula("2 + 3"); Assert.IsTrue(f1.ToString() == "2+3"); }
public void TestToString2() { Formula f = new Formula("x + Y"); String temp = "x+Y"; Assert.IsTrue(f.ToString().Equals(temp)); }
public void Test51e() { Formula f1 = new Formula("2"); Formula f2 = new Formula("3"); Assert.IsTrue(f1.ToString().IndexOf("2") >= 0); Assert.IsTrue(f2.ToString().IndexOf("3") >= 0); }
public void ToStringTest() { Formula testFormula = new Formula("4+35"); Assert.IsTrue(testFormula.Equals(new Formula(testFormula.ToString()))); }
public void TestConstructorStandardVariable2() { Formula testFormula = new Formula("4.0 + (x)"); Assert.AreEqual("4.0+(x)", testFormula.ToString()); }
public void PublicTestToString() { Formula f1 = new Formula("5+5-A1/b4"); Assert.AreEqual("5+5-A1/b4", f1.ToString()); }
public void PublicTestToString1() { Formula f1 = new Formula("5+A1"); Assert.AreEqual("5+A1", f1.ToString()); }
public void PublicTestConstructor() { Formula f1 = new Formula("(5+5)"); Assert.AreEqual("(5+5)", f1.ToString()); }
public void PublicTestConstructor1() { Formula f1 = new Formula("6/2+4 - (4+4)"); Assert.AreEqual("6/2+4-(4+4)", f1.ToString()); }
public void SetCellContentsFormulaNull() { Spreadsheet target = new Spreadsheet(); Formula formula = new Formula("a1+3", s => true, s => s); formula = null; target.SetContentsOfCell("a7", formula.ToString()); }
public void SetCellContentsFormulaInvalidName() { Spreadsheet target = new Spreadsheet(); Formula formula = new Formula("i4+78-98.7", s => true, s => s); target.SetContentsOfCell("a0a", formula.ToString()); }
public void SetCellContentsCircularException() { Spreadsheet target = new Spreadsheet(); Formula formula = new Formula("b1+b1", s => true, s => s); target.SetContentsOfCell("b1", "=b1+b1"); formula=new Formula("b1+b1",s=>true,s=>s); target.SetContentsOfCell("a1", formula.ToString()); }
public void TestConstructorFailFunc() { Formula testFormula = new Formula("3 + x", s => s.ToLower(), s => (s == "X") ? true : false); Assert.AreEqual("3+X", testFormula.ToString()); }
public void PublicTestToString2() { Formula f1 = new Formula(" 1 + A1 +xx1 - 67 / 5e+73", s => s.ToUpper(), s => true); Assert.AreEqual("1+A1+XX1-67/5E+73", f1.ToString()); }
public void TestConstructorStandard() { Formula testFormula = new Formula("4.0 + 2.0"); Assert.AreEqual("4.0+2.0", testFormula.ToString()); }
/// <summary> /// If obj is null or obj is not a Formula, returns false. Otherwise, reports /// whether or not this Formula and obj are equal. /// /// Two Formulae are considered equal if they consist of the same tokens in the /// same order. To determine token equality, all tokens are compared as strings /// except for numeric tokens and variable tokens. /// Numeric tokens are considered equal if they are equal after being "normalized" /// by C#'s standard conversion from string to double, then back to string. This /// eliminates any inconsistencies due to limited floating point precision. /// Variable tokens are considered equal if their normalized forms are equal, as /// defined by the provided normalizer. /// /// For example, if N is a method that converts all the letters in a string to upper case: /// /// new Formula("x1+y2", N, s => true).Equals(new Formula("X1 + Y2")) is true /// new Formula("x1+y2").Equals(new Formula("X1+Y2")) is false /// new Formula("x1+y2").Equals(new Formula("y2+x1")) is false /// new Formula("2.0 + x7").Equals(new Formula("2.000 + x7")) is true /// </summary> public override bool Equals(object obj) { Formula f = obj as Formula; return(f != null && ToString() == f.ToString()); }
public void TestConstructorStandardParenthesis2() { Formula testFormula = new Formula("(4.0 + 2.0)"); Assert.AreEqual("(4.0+2.0)", testFormula.ToString()); }
/// <summary> /// Returns the formula with all the variables replaced with their normalized counterpart. /// </summary> /// <param name="f"></param> /// <returns></returns> protected Formula NormalizeFormula(Formula f) { IEnumerable<string> e = f.GetVariables(); foreach (string s in e) { f = new Formula(Regex.Replace(f.ToString(), s, Normalize(s))); } return f; }
/// <summary> /// Takes in a cell name and a formula and returns the dependents /// Calculates the value of the formula then recalculates the value of any cell that depends directly or inderectly on it. /// </summary> /// <param name="name"></param> /// <param name="formula"></param> /// <returns></returns> protected override ISet<string> SetCellContents(string name, Formula formula) { name = Normalize(name); HashSet<string> mySet = new HashSet<string>(); HashSet<string> replaceSet = new HashSet<string>(); if (Object.ReferenceEquals(formula, null)) { throw new ArgumentNullException(); } if (Object.ReferenceEquals(name, null)) { throw new InvalidNameException(); } if (!Regex.IsMatch(name, "^([a-z]|[A-Z])+\\d+$") | !IsValid(name)) { throw new InvalidNameException(); } try { foreach (string s in formula.GetVariables()) { replaceSet.Add(s); } // dependentCells.ReplaceDependees(name, mySet); dependentCells.ReplaceDependees(name, replaceSet); GetCellsToRecalculate(name); } catch (CircularException) { throw new CircularException(); } Formula myEvl = new Formula(formula.ToString(), IsValid, Normalize); myCell = new Cell(formula, myEvl.Evaluate(myLookup)); if (mySpreadsheet.ContainsKey(name)) { mySpreadsheet[name] = myCell; } else { mySpreadsheet.Add(name, myCell); } mySet.Add(name); foreach (string s in GetCellsToRecalculate(name)) { mySet.Add(s); } foreach (string s in mySet) { myCell = new Cell(GetCellContents(s), myEvaluate(s)); mySpreadsheet[s] = myCell; } Changed = true; return mySet; }
public void TestToString() { Formula a = new Formula("A2 + a3"); Assert.AreEqual("A2+a3", a.ToString()); }
public void Test43() { Formula f = new Formula("2*5"); Assert.IsTrue(f.Equals(new Formula(f.ToString()))); }
public void ConstructorTest3() { Formula f1 = new Formula("x2*(y3)"); Assert.AreEqual("x2*(y3)", f1.ToString()); }
public void TestToString() { Formula f = new Formula("x + y", Normalize, s => true); String temp = "X+Y"; Assert.IsTrue(f.ToString().Equals(temp)); }
public void Constructor2Test1() { Formula f1 = new Formula("x+1", s => s.ToUpper(), s => (s == "X") ? true : false); Assert.AreEqual("X+1", f1.ToString()); }
/// <summary> /// An implementation of the abstract method in AbstractSpreadsheet. /// <seealso cref="AbstractSpreadsheet.SetContentsOfCell"/> /// </summary> /// <param name="name"></param> /// <param name="content"></param> /// <returns></returns> public override ISet<string> SetContentsOfCell(string name, string content) { // Private Variable ISet<string> set; // If content is null, throws an ArgumentNullException. // Otherwise, if name is null or invalid, throws an InvalidNameException. name = CorrectInput(name, content); // Otherwise, if content parses as a double, the contents of the named // cell becomes that double. double d; if (Double.TryParse(content, out d)) set = SetCellContents(name, d); // Otherwise, if content begins with the character '=', an attempt is made // to parse the remainder of content into a Formula f using the Formula // constructor. else if (content.Length > 0 && content[0] == '=') { // There are then three possibilities: // (1) If the remainder of content cannot be parsed into a Formula, a // SpreadsheetUtilities.FormulaFormatException is thrown. // Be sure to check the validity of and normalize any variables. // (2) Otherwise, if changing the contents of the named cell to be f // would cause a circular dependency, a CircularException is thrown. Formula f = new Formula(content.Substring(1)); f = NormalizeFormula(f); try { foreach (string v in f.GetVariables()) CorrectInput(v); } catch (InvalidNameException) { throw new FormulaFormatException( String.Format("One or more variables in the formula '{0}' contained in cell {1} aren't valid.", f.ToString(), name)); } // (3) Otherwise, the contents of the named cell becomes f. set = SetCellContents(name, f); } // Otherwise, the contents of the named cell becomes content. else set = SetCellContents(name, content); // Recalculate the values of any cell dependent on the named cell, including the named cell itself. CalculateCellValues(name); //Remove any name associations to cell, reset to how it was before the cell was added. if (content == "") { cells.Remove(name); } // If an exception is not thrown, the method returns a set consisting of // name plus the names of all other cells whose value depends, directly // or indirectly, on the named cell. // For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the // set {A1, B1, C1} is returned. return set; }
public void TestToStringWithNormalizer() { Formula f = new Formula("a2*A4 + A5 / B2", VarToUpper, IsValid); Assert.AreEqual("A2*A4+A5/B2", f.ToString()); }
/// <summary> /// Event handler for "Enter" key being pressed on the cellContents text box. It sends a cell message to the server requesting /// a cell to be changed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cellContents_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Return) { int col; int row; spreadsheetPanel1.GetSelection(out col, out row); //converting the zero-based cell location to the Alphanumeric representation. char colChar = (char)(col + 65); string cell = colChar.ToString() + (row + 1); string contents = cellContents.Text; try { if (cellContents.Text.Length > 0 && cellContents.Text.ElementAt(0) == '=') { //check to see if it can make a new formula. This will check for format errors before sending to server. Formula temporaryFormula = new Formula(cellContents.Text.Substring(1), ss.Normalize, ss.IsValid); contents = "=" + temporaryFormula.ToString(); } //sends cell name and cell contents to server for synchronization. comm.editCell(cell, contents); } catch (FormulaFormatException ex) { System.Windows.Forms.MessageBox.Show(cell + " has an incorrect formula."); } } }
public void ConstructorTest1() { Formula f1 = new Formula("2.0*3.0"); Assert.AreEqual("2.0*3.0", f1.ToString()); }