Reputation: 1
My main method is:
using System;
namespace Rational
{
public class Rational
{
public int Denom { get; private set; }
public int Numer { get; private set; }
public Rational(int numerator = 0, int denominator = 1)
{
Denom = denominator;
Numer = numerator;
}
public void IncreaseBy(Rational num)
{
Numer = Numer * num.Denom + num.Denom * Denom;
Denom = Denom * num.Denom;
}
public void DecreaseBy(Rational num)
{
Numer = Numer * num.Denom - num.Denom * Denom;
Denom = Denom * num.Denom;
}
override public String ToString() { return (Numer + "/" + Denom); }
}
My Program that calls on my method is:
using System;
namespace Rationals
{
public class Rationals
{
class Program
{
static void Main(string[] args)
{
Rational num1 = new Rational(10, 8);
Rational num2 = new Rational(8, 10);
Console.WriteLine("1st number is: " + num1);
Console.WriteLine("2nd number is: " + num2);
num1.IncreaseBy(num2);
Console.WriteLine("1st number + 2nd number = " + num1);
num1.DecreaseBy(num2);
Console.WriteLine("1st number - 2nd number = " + num2);
}
}
}
}
I have to Write a Unit test to test my IncreaseBy() as well as my DecreaseBy() and finally ToString(). can someone please help me!!!?
This is what i have so far in the UnitTest please crap on it as much as you want. I AM UNABLE TO MAKE THE TEST PASS IT KEEPS COMING UP WITH A DIFFERENT ANSWER ALSO THE ToString() brings up an error.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rationals_123456789;
namespace ratonalstest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void DecreaseByTest()
{
int num = 1, den = 6, num1 = 2, den1 = 9;
Rational fraction1 = new Rational(num, den);
Rational fraction2 = new Rational(num1, den1);
Rational expectedFraction = new Rational(-1, 18);
//when
fraction1.DecreaseBy(fraction2);
//then
Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);
}
[TestMethod]
public void IncreaseByTest()
{
int num = 1, den = 4, num1 = 1, den1 = 4;
Rational fraction1 = new Rational(num, den);
Rational fraction2 = new Rational(num1, den1);
Rational expectedFraction = new Rational(7, 18);
//when
fraction1.IncreaseBy(fraction2);
//then
Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);
//when
fraction1.DecreaseBy(fraction2);
//then
Assert.AreEqual(fraction1.Numer, expectedFraction.Numer);
Assert.AreEqual(fraction1.Denom, expectedFraction.Denom);
}
[TestMethod]
public void ToString()
{
int num = 1, den = 6, num1 = 2, den2 = 9;
Rational fraction1 = new Rational(num, den);
Rational fraction2 = new Rational(num1, den2);
string expectedString = "1/6";
string outputString = fraction1.ToString();
//then
Assert.AreEqual(expectedString, outputString);
}
}
}
Upvotes: 0
Views: 127
Reputation: 391276
There are two issues with your code.
First problem is that these two expressions:
Numer = Numer * num.Denom + num.Denom * Denom;
Numer = Numer * num.Denom - num.Denom * Denom;
should use num.Numer
instead of num.Denom
so these are correct:
Numer = Numer * num.Denom + num.Numer * Denom;
Numer = Numer * num.Denom - num.Numer * Denom;
Now, this will still not make your tests pass, but let's try your code now:
Rational fraction1 = new Rational(1, 6);
Rational fraction2 = new Rational(2, 9);
fraction1.DecreaseBy(fraction2);
Console.WriteLine(fraction1.ToString());
this outputs:
-3/54
Your test expects -1/18
, but these are in fact the same number. Your tests have to handle this discrepancy, either by introducing fraction normalization or by comparing in a different manner.
Lastly I have some tips:
struct
Upvotes: 1