-1

I'm making unit-tests for this class, a triangle class that takes the sides and determines if the triangle is scalene, isosceles or equilateral.

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;
    }
}

}

The isScaleneTest is failing, and I can't figure out why?

[TestMethod()]
    public void isScaleneTest()
    {
        Triangle target = new Triangle(1.2, 2.1, 7.1);
        Assert.IsTrue(target.isScalene());
    }

I also have problem figuring out how to test the other methods. isEquilateral and isIsosceles is tested and passes, but I can't get any other test to pass. Any ideas? Is there any point to testing the constructor? Thanks for help.

ana
  • 475
  • 4
  • 10
  • 21

2 Answers2

1

Your code sides.Distinct<double>().Count(); is returning more than 1.

Have a look at this thread,

Distinct not working with LINQ to Objects

LINQ Distinct is not that smart when it comes to custom objects.

All it does is look at your list and see that it has two different objects (it doesn't care that they have the same values for the member fields).

As shown in the link, you could implement IEquatable

Community
  • 1
  • 1
Dylan Morley
  • 1,656
  • 12
  • 21
  • His use of `Distinct` is with `double` vice a custom object. In his case it uses the default equality comparer for `double`. That may still cause problems, but the test that's not passing right now is due to how he's testing for a scalene triangle. – Mike Cowan Nov 28 '12 at 12:41
  • -1. Distinct works well enough on doubles (and it's not possible to implement IEquatable of `double` anyway), the problem is in the definition for Scalene and Equilateral. – SWeko Nov 28 '12 at 12:42
1

A scalene triangle has three sides that are all unequal. The code for isScalene should be:

public bool isScalene()
{
    if (uniqueSides() == 3)
        return true;
    return false;
}

Also, the code for is isEquilateral is wrong. An equilateral triangle has three equal sides, so the return for uniqueSides for an equilateral triangle should be 1 vice 3.

Mike Cowan
  • 919
  • 5
  • 11