我正在为这个类进行单元测试,这是一个三角形类,它取边并确定三角形是不等边、等腰还是等边。
public struct Point
{
public int x, y;
public Point(int a, int b)
{
x = a;
y = b;
}
}
public class Triangle
{
double[] sides;
public Triangle(double a, double b, double c)
{
if ((a <= 0) || (b <= 0) || (c <= 0))
{
throw new ArgumentException(" ");
}
sides = new double[] { a, b, c };
}
public Triangle(double[] s)
{
sides = new double[s.Length];
for (int i = 0; i < s.Length; i++)
sides[i] = s[i];
}
public Triangle(Point a, Point b, Point c)
{
sides = new double[3];
sides[0] = Math.Sqrt(Math.Pow((double)(b.x - a.x), 2.0) + Math.Pow((double)(b.y - a.y), 2.0));
sides[1] = Math.Sqrt(Math.Pow((double)(b.x - c.x), 2.0) + Math.Pow((double)(b.x - a.x), 2.0));
sides[2] = Math.Sqrt(Math.Pow((double)(c.x - a.x), 2.0) + Math.Pow((double)(c.x - a.x), 2.0));
}
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));
}
private int uniqueSides()
{
return sides.Distinct<double>().Count();
}
public bool isScalene()
{
if (uniqueSides() == 1)
return true;
return false;
}
public bool isEquilateral()
{
if (uniqueSides() == 3)
return true;
return false;
}
public bool isIsosceles()
{
if (uniqueSides() == 2)
return true;
return false;
}
}
}
isScaleneTest 失败了,我不知道为什么?
[TestMethod()]
public void isScaleneTest()
{
Triangle target = new Triangle(1.2, 2.1, 7.1);
Assert.IsTrue(target.isScalene());
}
我也很难弄清楚如何测试其他方法。isEquilateral 和 isIsosceles 已通过测试并通过,但我无法通过任何其他测试。有任何想法吗?测试构造函数有什么意义吗?感谢帮助。