I’m working on a laboratory practical with unit-testing, and below is a piece of code from the application that i’m testing. Most of the unit-testings are finished, but regarding the constructor below I just can’t figure out how to test it. For example, what exactly is the constructor doing with the array elements? What would be a good way of testing the construtor?
Is there maybe a kind soul out there who could give me a kick in the right direction?
public struct Point {
public int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
}
…
public Triangle(Point[] s) {
sides = new double[s.Length];
sides[0] = Math.Sqrt(Math.Pow((double)(s[1].x - s[0].x), 2.0) + Math.Pow((double)(s[1].y - s[0].y), 2.0));
sides[1] = Math.Sqrt(Math.Pow((double)(s[1].x - s[2].x), 2.0) + Math.Pow((double)(s[1].x - s[2].x), 2.0));
sides[2] = Math.Sqrt(Math.Pow((double)(s[2].x - s[0].x), 2.0) + Math.Pow((double)(s[2].x - s[0].x), 2.0));
}
…
[TestMethod()]
public void TriangleConstructorTest1()
{
Point[] s = null; // TODO: Initialize to an appropriate value
Triangle target = new Triangle(s);
Assert.Inconclusive("TODO: Implement code to verify target");
}
You are talking about the Traingle-constructor, aren’t you?
It looks like it is using the Pythagorean theorem to calculate the length of the triangle sides.
So you should test whether this calculation is done correctly.
In addition, you could check if the constructor detects invalid arguments, e.g.: