public void IsMixedNumber_ValidResultNegative() { // arrange Fraction fraction1 = new Fraction(-3); Fraction fraction2 = new Fraction(-3, 4); Fraction fraction3 = new Fraction(-3, 2, 5); Boolean expected1 = false; Boolean expected2 = false; Boolean expected3 = true; // action Boolean actual1 = fraction1.IsMixedNumber(); Boolean actual2 = fraction2.IsMixedNumber(); Boolean actual3 = fraction3.IsMixedNumber(); // assert Assert.AreEqual(expected1, actual1, "Invalid result with one argument, negative whole number."); Assert.AreEqual(expected2, actual2, "Invalid result with two arguments, negative proper fraction."); Assert.AreEqual(expected3, actual3, "Invalid result with three arguments, negative mixed number."); }
/// <summary> /// MakeImproper - converts a mixed fraction or whole number to an improper fraction. /// </summary> /// <param name="fraction">Fraction to to convert.</param> /// <returns>Proper or Improper Fraction.</returns> public static Fraction MakeImproper(Fraction fraction) { // Check for mixed number fractions if (fraction.IsMixedNumber()){ // Check whole part for negative sign if(fraction.WholePart < 0) { // Convert a mixed number fraction to an improper fraction return new Fraction( -1 * (fraction.Denominator * Math.Abs(fraction.WholePart) + fraction.Numerator), fraction.Denominator); } else { // Convert a mixed number fraction to an improper fraction return new Fraction((fraction.Denominator * fraction.WholePart) + fraction.Numerator, fraction.Denominator); } } else if (fraction.IsWholeNumber()) { // Convert a whole number to an improper fraction return new Fraction(fraction.WholePart, 1); } else { // If not improper fraction, return original fraction return new Fraction(fraction.Numerator, fraction.Denominator); } }