public void When_CompareNumbers_Then_Success() { var mainNumber = new Number(6, 8, 0, 1); var numberToCompare1 = new Number(1, 8, 9, 4); var numberToCompare2 = new Number(6, 8, 0, 1); var numberToCompare3 = new Number(1, 0, 6, 2); var numberComparer = new NumberComparer(); var attemptResult1 = numberComparer.Compare(mainNumber, numberToCompare1); var attemptResult2 = numberComparer.Compare(mainNumber, numberToCompare2); var attemptResult3 = numberComparer.Compare(mainNumber, numberToCompare3); Assert.AreEqual(numberToCompare1, attemptResult1.Number); Assert.AreEqual(1, attemptResult1.Goods); Assert.AreEqual(1, attemptResult1.Regulars); Assert.AreEqual(2, attemptResult1.Bads); Assert.AreEqual(numberToCompare2, attemptResult2.Number); Assert.AreEqual(4, attemptResult2.Goods); Assert.AreEqual(0, attemptResult2.Regulars); Assert.AreEqual(0, attemptResult2.Bads); Assert.AreEqual(numberToCompare3, attemptResult3.Number); Assert.AreEqual(0, attemptResult3.Goods); Assert.AreEqual(3, attemptResult3.Regulars); Assert.AreEqual(1, attemptResult3.Bads); }
public int Compare(IndexValue x, IndexValue y) { if (x.Type == y.Type) { switch (x.Type) { case IndexValueType.Null: return(0); case IndexValueType.DateTime: return(DateTimeComparer.Compare((DateTime)x.Value, (DateTime)y.Value)); case IndexValueType.Integer: return(IntegerComparer.Compare((Int32)x.Value, (Int32)y.Value)); case IndexValueType.Number: return(NumberComparer.Compare((double)x.Value, (double)y.Value)); case IndexValueType.String: return(StringComparer.Compare((string)x.Value, (string)y.Value)); case IndexValueType.Bool: return(BoolComparer.Compare((bool)x.Value, (bool)y.Value)); default: Verify.Argument(true, "Attempt to compare illegal values, {0} and {1}", x.Value, y.Value); throw null; } } return(Comparer <int> .Default.Compare((int)x.Type, (int)y.Type)); }
public void compare_x_less_then_y(string x,string y) { var migrationA = new Migration {Number = x}; var migrationB = new Migration {Number = y}; var comparer = new NumberComparer(); Assert.AreEqual(-1, comparer.Compare(migrationA, migrationB)); }
public void TestSpecificExampleB() { double a = 1 + 1.0028 - 0.9973; double b = 1.0055; Assert.IsFalse(a == b); Assert.AreEqual(0, NumberComparer.Compare(a, b)); }
public void TestSpecificExampleA() { double a = 0.06 - 0.01; double b = 0.05; Assert.IsFalse(a == b); Assert.AreEqual(0, NumberComparer.Compare(a, b)); }
/** * Test whether a value matches a numeric condition. * @param valueEval Value to Check. * @param op Comparator to use. * @param condition Value to check against. * @return whether the condition holds. * @If it's impossible to turn the condition into a number. */ private static bool testNumericCondition( ValueEval valueEval, Operator op, String condition) { // Construct double from ValueEval. if (!(valueEval is NumericValueEval)) { return(false); } double value = ((NumericValueEval)valueEval).NumberValue; // Construct double from condition. double conditionValue = 0.0; try { int intValue = Int32.Parse(condition); conditionValue = intValue; } catch (FormatException e) { // It's not an int. try { conditionValue = Double.Parse(condition); } catch (FormatException e2) { // It's not a double. throw new EvaluationException(ErrorEval.VALUE_INVALID); } } int result = NumberComparer.Compare(value, conditionValue); switch (op) { case Operator.largerThan: return(result > 0); case Operator.largerEqualThan: return(result >= 0); case Operator.smallerThan: return(result < 0); case Operator.smallerEqualThan: return(result <= 0); case Operator.equal: return(result == 0); } return(false); // Can not be reached. }
public void When_AnalyzeRandomNumber_Then_Success() { var numberComparer = new NumberComparer(); var numberBuilder = new NumberBuilder(unitsLength: 3); var numberAnalyzer = new NumberAnalyzer(numberBuilder); var mainNumber = numberBuilder.Build (); var firstTryNumber = numberBuilder.Build (); var attempt = numberComparer.Compare(mainNumber, firstTryNumber); var numberGuessed = false; var attemptsLimit = 20; var attemptsCount = 1; do { var nextNumber = numberAnalyzer.GetNextNumber(lastAttempt: attempt); attempt = numberComparer.Compare(mainNumber, nextNumber); attemptsCount++; if (attempt.Goods == 3) { numberGuessed = true; } } while (!numberGuessed); if (attemptsCount > attemptsLimit) { Assert.Fail("The number has been guessed in {0} attempts. The maximum allowed was {1}", attemptsCount, attemptsLimit); } else { Assert.IsTrue(numberGuessed, "The number has been guessed in {0} attempts", attemptsCount); } }
private static bool Confirm(int i, double a, double b, int expRes) { int actRes = NumberComparer.Compare(a, b); int sgnActRes = actRes < 0 ? -1 : actRes > 0 ? +1 : 0; if (sgnActRes != expRes) { Console.WriteLine("Mismatch example[" + i + "] (" + FormatDoubleAsHex(a) + ", " + FormatDoubleAsHex(b) + ") expected " + expRes + " but got " + sgnActRes); return(false); } return(true); }
public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) { ValueEval veText1; try { veText1 = OperandResolver.GetSingleValue(arg1, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return(e.GetErrorEval()); } String strText1 = OperandResolver.CoerceValueToString(veText1); Double number1 = OperandResolver.ParseDouble(strText1); if (double.IsNaN(number1)) { return(ErrorEval.VALUE_INVALID); } ValueEval veText2; try { veText2 = OperandResolver.GetSingleValue(arg2, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return(e.GetErrorEval()); } String strText2 = OperandResolver.CoerceValueToString(veText2); Double number2 = OperandResolver.ParseDouble(strText2); if (double.IsNaN(number2)) { return(ErrorEval.VALUE_INVALID); } //int result = new BigDecimal(number1).CompareTo(new BigDecimal(number2)); int result = NumberComparer.Compare(number1, number2); return(result == 0 ? ONE : ZERO); }
public int Compare(object x, object y) { int compareResult; ListViewItem listviewX, listviewY; // Cast the objects to be compared to ListViewItem objects listviewX = (ListViewItem)x; listviewY = (ListViewItem)y; // Compare the two items if (ColumnToSort == 0) { compareResult = severityComparer.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); } else if (ColumnToSort == 2 || ColumnToSort == 3) { compareResult = numberComparer.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); } else { compareResult = stringComparer.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); } // Calculate correct return value based on object comparison if (OrderOfSort == SortOrder.Ascending) { return(compareResult); } else if (OrderOfSort == SortOrder.Descending) { return(-compareResult); } else { return(0); } }
private static int CompareBlank(ValueEval v) { if (v == BlankEval.instance) { return(0); } if (v is BoolEval) { BoolEval boolEval = (BoolEval)v; return(boolEval.BooleanValue ? -1 : 0); } if (v is NumberEval) { NumberEval ne = (NumberEval)v; //return ne.NumberValue.CompareTo(0.0); return(NumberComparer.Compare(0.0, ne.NumberValue)); } if (v is StringEval) { StringEval se = (StringEval)v; return(se.StringValue.Length < 1 ? 0 : -1); } throw new ArgumentException("bad value class (" + v.GetType().Name + ")"); }
/// <summary> /// Oyuna tahmin girişi yapar. /// </summary> /// <param name="guess">Tahmin.</param> public void EnterGuess(string guess) { if (!IsGameStarted) { return; } NumberComparer compareresults = NumberComparer.Compare(this.activeNumber, guess); TotalMinus += compareresults.MinusScore; TotalPlus += compareresults.PlusScore; TotalGuessCount++; if (compareresults.Success) { OnEnteredGuess(guess, compareresults); FinishGame(true, GameFinishType.GameFinish_Win); return; } else { if (this.LifeLeft > 0) { if (--this.LifeLeft <= 0) { OnLifeDecreased(); OnEnteredGuess(guess, compareresults); FinishGame(false, GameFinishType.GameFinish_LOseLifeOver); return; } else { OnLifeDecreased(); } } } OnEnteredGuess(guess, compareresults); }
private static int DoCompare(ValueEval va, ValueEval vb) { // special cases when one operand is blank if (va == BlankEval.instance) { return(CompareBlank(vb)); } if (vb == BlankEval.instance) { return(-CompareBlank(va)); } if (va is BoolEval) { if (vb is BoolEval) { BoolEval bA = (BoolEval)va; BoolEval bB = (BoolEval)vb; if (bA.BooleanValue == bB.BooleanValue) { return(0); } return(bA.BooleanValue ? 1 : -1); } return(1); } if (vb is BoolEval) { return(-1); } if (va is StringEval) { if (vb is StringEval) { StringEval sA = (StringEval)va; StringEval sB = (StringEval)vb; return(string.Compare(sA.StringValue, sB.StringValue, StringComparison.OrdinalIgnoreCase)); } return(1); } if (vb is StringEval) { return(-1); } if (va is NumberEval) { if (vb is NumberEval) { NumberEval nA = (NumberEval)va; NumberEval nB = (NumberEval)vb; if (nA.NumberValue == nB.NumberValue) { // Excel considers -0.0 == 0.0 which is different to Double.compare() return(0); } return(NumberComparer.Compare(nA.NumberValue, nB.NumberValue)); } } throw new ArgumentException("Bad operand types (" + va.GetType().Name + "), (" + vb.GetType().Name + ")"); }
/* This function partitions a[] in three parts * a) a[l..i] contains all elements smaller than pivot * b) a[i+1..j-1] contains all occurrences of pivot * c) a[j..r] contains all elements greater than pivot */ public static void Partition(int[] a, int l, int r, ref int i, ref int j) { i = l - 1; j = r; int p = l - 1, q = r; int v = a[r]; while (true) { // From left, find the first element greater than // or equal to v. This loop will definitely terminate // as v is last element while (_comparer.Compare(a[++i], v) < 0) { ; } // From right, find the first element smaller than or // equal to v while (_comparer.Compare(v, a[--j]) < 0) { if (_comparer.Compare(j, l) == 0) { break; } } // If i and j cross, then we are done if (_comparer.Compare(i, j) >= 0) { break; } // Swap, so that smaller goes on left greater goes on right Swap(ref a[i], ref a[j]); // Move all same left occurrence of pivot to beginning of // array and keep count using p if (_comparer.Compare(a[i], v) == 0) { p++; Swap(ref a[p], ref a[i]); } // Move all same right occurrence of pivot to end of array // and keep count using q if (_comparer.Compare(a[j], v) == 0) { q--; Swap(ref a[j], ref a[q]); } } // Move pivot element to its correct index Swap(ref a[i], ref a[r]); // Move all left same occurrences from beginning // to adjacent to arr[i] j = i - 1; for (int k = l; k < p; k++, j--) { Swap(ref a[k], ref a[j]); } // Move all right same occurrences from end // to adjacent to arr[i] i = i + 1; for (int k = r - 1; k > q; k--, i++) { Swap(ref a[i], ref a[k]); } }
public void InvalidFormatX() { var migrationA = new Migration { Number = "ABC" }; var migrationB = new Migration { Number = "111" }; var comparer = new NumberComparer(); Assert.Throws<FormatException>(() => Assert.AreNotEqual(0, comparer.Compare(migrationA, migrationB))); }