public void ShouldThrowExceptionIfDenominatorIsZeroWhenAdding() { Fraction fraction1 = new Fraction(3, 6); Fraction fraction2 = new Fraction(2, 0); Assert.Throws<FractionException>(() => _service.Add(fraction1, fraction2)); }
public static FractionModel Map(Fraction domain) { return new FractionModel { Numerator = domain.Numerator, Denominator = domain.Denominator }; }
public Fraction Pow(Fraction fraction, int power) { if (_operationsServiceHelper.IsFractionValid(fraction)) { Fraction pow = _operations.Pow(fraction, power); return _operationsServiceHelper.Normalize(pow); } throw new FractionException("Fraction is NaN as Denominator is 0."); }
public Fraction Mul(Fraction fraction1, Fraction fraction2) { if (_operationsServiceHelper.IsFractionValid(fraction1) && _operationsServiceHelper.IsFractionValid(fraction2)) { Fraction prod = _operations.Mul(fraction1, fraction2); return _operationsServiceHelper.Normalize(prod); } throw new FractionException("Fraction is NaN as Denominator is 0."); }
public void SetUp() { _model = new FractionModel { Numerator = 3, Denominator = 7 }; _domain = new Fraction(5, 17); }
public Fraction Sub(Fraction fraction1, Fraction fraction2) { if (_operationsServiceHelper.IsFractionValid(fraction1) && _operationsServiceHelper.IsFractionValid(fraction2)) { Fraction diff = _operations.Sub(fraction1, fraction2); return _operationsServiceHelper.Normalize(diff); } throw new FractionException("Fraction is NaN as Denominator is 0."); }
public void ShouldSubtract2PositiveFractions() { _fraction1 = new Fraction(3, 4); _fraction2 = new Fraction(2, 3); var diff = _operations.Sub(_fraction1, _fraction2); Assert.IsNotNull(diff); Assert.AreEqual(1, diff.Numerator); Assert.AreEqual(12, diff.Denominator); }
public Fraction Div(Fraction fraction1, Fraction fraction2) { if (_operationsServiceHelper.IsFractionValid(fraction1) && _operationsServiceHelper.IsFractionValid(fraction2)) { Fraction div = _operations.Div(fraction1, fraction2); if (_operationsServiceHelper.IsFractionValid(div)) return _operationsServiceHelper.Normalize(div); throw new FractionException("Result is NaN as Denominator is 0."); } throw new FractionException("Fraction is NaN as Denominator is 0."); }
public void ShouldAdd2PositiveFractions() { _fraction1 = new Fraction(3, 4); _fraction2 = new Fraction(2, 3); var sum = _operations.Add(_fraction1, _fraction2); Assert.IsNotNull(sum); Assert.AreEqual(17, sum.Numerator); Assert.AreEqual(12, sum.Denominator); }
public Fraction Normalize(Fraction fraction) { if (fraction.Numerator == 0 || fraction.Denominator == 0) { return new Fraction(0,1); } var gcd = GCD(Math.Abs(fraction.Numerator), Math.Abs(fraction.Denominator)); var numerator = Math.Abs(fraction.Numerator) / gcd; var denominator = Math.Abs(fraction.Denominator) / gcd; if (fraction.IsNegative) return new Fraction(-numerator, denominator); return new Fraction(numerator, denominator); }
public void ShouldAdd1NegativeAnd1PostiveFractions() { _fraction1 = new Fraction(3, -7); _fraction2 = new Fraction(2, 3); var sum = _operations.Add(_fraction1, _fraction2); Assert.IsNotNull(sum); Assert.AreEqual(5, sum.Numerator); Assert.AreEqual(21, sum.Denominator); Assert.AreEqual(false, sum.IsNegative); }
public void ShouldAdd2NegativeFractions() { _fraction1 = new Fraction(-3, 4); _fraction2 = new Fraction(2, -3); var sum = _operations.Add(_fraction1, _fraction2); Assert.IsNotNull(sum); Assert.AreEqual(-17, sum.Numerator); Assert.AreEqual(12, sum.Denominator); Assert.AreEqual(true, sum.IsNegative); }
public void ShouldSubtract2Fractions() { Fraction fraction1 = new Fraction(3, 4); Fraction fraction2 = new Fraction(2, 3); _helper.Expect(helper => helper.IsFractionValid(Arg<Fraction>.Is.Anything)).Return(true); _operations.Expect(operation => operation.Sub(fraction1, fraction2)).Return(new Fraction(1, 12)); _helper.Expect(helper => helper.Normalize(Arg<Fraction>.Is.Anything)).Return(new Fraction(1, 12)); var diff = _service.Sub(fraction1, fraction2); _operations.VerifyAllExpectations(); _helper.VerifyAllExpectations(); Assert.IsNotNull(diff); Assert.AreEqual(1, diff.Numerator); Assert.AreEqual(12, diff.Denominator); Assert.AreEqual(false, diff.IsNegative); }
public void ShouldAdd2Fractions() { Fraction fraction1 = new Fraction(3, 4); Fraction fraction2 = new Fraction(2, 3); _helper.Expect(helper => helper.IsFractionValid(Arg<Fraction>.Is.Anything)).Return(true); _operations.Expect(operation => operation.Add(fraction1, fraction2)).Return(new Fraction(17, 12)); _helper.Expect(helper => helper.Normalize(Arg<Fraction>.Is.Anything)).Return(new Fraction(17, 12)); var sum = _service.Add(fraction1, fraction2); _operations.VerifyAllExpectations(); _helper.VerifyAllExpectations(); Assert.IsNotNull(sum); Assert.AreEqual(17, sum.Numerator); Assert.AreEqual(12, sum.Denominator); Assert.AreEqual(false, sum.IsNegative); }
public void ShouldDivide1NegativeAnd1PostiveFractions() { _fraction1 = new Fraction(3, -7); _fraction2 = new Fraction(2, 3); var div = _operations.Div(_fraction1, _fraction2); Assert.IsNotNull(div); Assert.AreEqual(-9, div.Numerator); Assert.AreEqual(14, div.Denominator); Assert.AreEqual(true, div.IsNegative); }
public void ShouldDivide2NegativeFractions() { _fraction1 = new Fraction(-3, 4); _fraction2 = new Fraction(2, -3); var div = _operations.Div(_fraction1, _fraction2); Assert.IsNotNull(div); Assert.AreEqual(9, div.Numerator); Assert.AreEqual(8, div.Denominator); Assert.AreEqual(false, div.IsNegative); }
public void ShouldMultiply1NegativeAnd1PostiveFractions() { _fraction1 = new Fraction(3, -7); _fraction2 = new Fraction(2, 3); var prod = _operations.Mul(_fraction1, _fraction2); Assert.IsNotNull(prod); Assert.AreEqual(-6, prod.Numerator); Assert.AreEqual(21, prod.Denominator); Assert.AreEqual(true, prod.IsNegative); }
public void ShouldMultiply2NegativeFractions() { _fraction1 = new Fraction(-3, 4); _fraction2 = new Fraction(2, -3); var prod = _operations.Mul(_fraction1, _fraction2); Assert.IsNotNull(prod); Assert.AreEqual(6, prod.Numerator); Assert.AreEqual(12, prod.Denominator); Assert.AreEqual(false, prod.IsNegative); }
public void ShouldDivide2FractionsAndNormalize() { Fraction fraction1 = new Fraction(3, 6); Fraction fraction2 = new Fraction(2, 8); _helper.Expect(helper => helper.IsFractionValid(Arg<Fraction>.Is.Anything)).Return(true); _operations.Expect(operation => operation.Div(fraction1, fraction2)).Return(new Fraction(24, 12)); _helper.Expect(helper => helper.Normalize(Arg<Fraction>.Is.Anything)).Return(new Fraction(2, 1)); var div = _service.Div(fraction1, fraction2); _operations.VerifyAllExpectations(); _helper.VerifyAllExpectations(); Assert.IsNotNull(div); Assert.AreEqual(2, div.Numerator); Assert.AreEqual(1, div.Denominator); Assert.AreEqual(false, div.IsNegative); }
public bool IsFractionValid(Fraction fraction) { return fraction.Denominator != 0; }
public void ShouldPowerFractionAndNormalize() { Fraction fraction1 = new Fraction(2, 4); _helper.Expect(helper => helper.IsFractionValid(Arg<Fraction>.Is.Anything)).Return(true); _operations.Expect(operation => operation.Pow(fraction1, 3)).Return(new Fraction(8, 64)); _helper.Expect(helper => helper.Normalize(Arg<Fraction>.Is.Anything)).Return(new Fraction(1, 8)); var pow = _service.Pow(fraction1, 3); _operations.VerifyAllExpectations(); _helper.VerifyAllExpectations(); Assert.IsNotNull(pow); Assert.AreEqual(1, pow.Numerator); Assert.AreEqual(8, pow.Denominator); Assert.AreEqual(false, pow.IsNegative); }
public void ShouldThrowExceptionIfDenominatorIsZeroWhenDividingByZero() { Fraction fraction1 = new Fraction(3, 6); Fraction fraction2 = new Fraction(0, 1); var fraction = new Fraction(3, 0); _helper.Expect(helper => helper.IsFractionValid(fraction1)).Return(true); _helper.Expect(helper => helper.IsFractionValid(fraction2)).Return(true); _helper.Expect(helper => helper.IsFractionValid(fraction)).Return(false); _operations.Expect(operation => operation.Div(Arg<Fraction>.Is.Anything, Arg<Fraction>.Is.Anything)) .Return(fraction); Assert.Throws<FractionException>(() => _service.Div(fraction1, fraction2)); _operations.VerifyAllExpectations(); }
public void ShouldNegativePowerPositiveFraction() { _fraction1 = new Fraction(3, 4); var pow = _operations.Pow(_fraction1, -2); Assert.IsNotNull(pow); Assert.AreEqual(16, pow.Numerator); Assert.AreEqual(9, pow.Denominator); }
public static bool IsNegative(Fraction fraction) { if (fraction.Numerator < 0 && fraction.Denominator > 0 || fraction.Numerator > 0 && fraction.Denominator < 0) return true; return false; }
public void ShouldSubtract1NegativeAnd1PostiveFractions() { _fraction1 = new Fraction(3, -7); _fraction2 = new Fraction(2, 3); var sum = _operations.Sub(_fraction1, _fraction2); Assert.IsNotNull(sum); Assert.AreEqual(-23, sum.Numerator); Assert.AreEqual(21, sum.Denominator); Assert.AreEqual(true, sum.IsNegative); }
public void ShouldThrowExceptionIfDenominatorIsZeroWhenExponentiating() { Fraction fraction = new Fraction(2, 0); Assert.Throws<FractionException>(() => _service.Pow(fraction, 3)); }
public void ShouldMultiply2FractionsAndNormalize() { Fraction fraction1 = new Fraction(3, 6); Fraction fraction2 = new Fraction(2, 8); _helper.Expect(helper => helper.IsFractionValid(Arg<Fraction>.Is.Anything)).Return(true); _operations.Expect(operation => operation.Mul(fraction1, fraction2)).Return(new Fraction(6, 48)); _helper.Expect(helper => helper.Normalize(Arg<Fraction>.Is.Anything)).Return(new Fraction(1, 8)); var prod = _service.Mul(fraction1, fraction2); _operations.VerifyAllExpectations(); _helper.VerifyAllExpectations(); Assert.IsNotNull(prod); Assert.AreEqual(1, prod.Numerator); Assert.AreEqual(8, prod.Denominator); Assert.AreEqual(false, prod.IsNegative); }