Exemplo n.º 1
0
 private int FindSumOfShortestSides(Triangle givenTriangle)
 {
     int sum = 0;
     int longest = FindLengthOfLongestSide(givenTriangle);
     int[] sides = { givenTriangle.Length0, givenTriangle.Length1, givenTriangle.Length2, };
     for (int idx = 0; idx < sides.Length; sum += sides[idx++]) ;
     sum -= longest;
     return sum;
 }
Exemplo n.º 2
0
 private int FindLengthOfLongestSide(Triangle givenTriangle)
 {
     int longest = -1;
     int[] sides = { givenTriangle.Length0, givenTriangle.Length1, givenTriangle.Length2, };
     for (int idx = 0; idx < sides.Length; idx++)
     {
         if (longest < sides[idx]) longest = sides[idx];
     }
     return longest;
 }
 public void TestEquilateralDetection()
 {
     //setup
     int l0, l1, l2;
     l0 = l1 = l2 = 5;
     TriangleType expectedType = TriangleType.EQUILATERAL;
     Triangle givenTriangle = new Triangle(l0, l1, l2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with 3 equal length sides is always equilateral");
 }
Exemplo n.º 4
0
 public TriangleType Classify(Triangle givenTriangle)
 {
     TriangleType type = TriangleType.INVALID;
     if (givenTriangle != null)
     {
         if (!Validate(givenTriangle))
         {
             type = TriangleType.INVALID;
         }
         else if (givenTriangle.Length0 != 0
                 && givenTriangle.Length1 != 0
                 && givenTriangle.Length2 != 0)
         {
             int l0, l1, l2;
             l0 = givenTriangle.Length0;
             l1 = givenTriangle.Length1;
             l2 = givenTriangle.Length2;
             if (l0 == l1 && l1 == l2)
             {
                 type = TriangleType.EQUILATERAL;
             }
             else if (l0 == l1 || l0 == l2 || l1 == l2)
             {
                 type = TriangleType.ISOSCELES;
             }
             else
             {
                 type = TriangleType.SCALENE;
             }
         }
         else
         {
             float a0, a1, a2;
             a0 = givenTriangle.Angle0;
             a1 = givenTriangle.Angle1;
             a2 = givenTriangle.Angle2;
             if (a0 == NINTY || a1 == NINTY || a2 == NINTY)
             {
                 type = TriangleType.RIGHT;
             }
             else if (a0 > NINTY || a1 > NINTY || a2 > NINTY)
             {
                 type = TriangleType.OBTUSE;
             }
             else
             {
                 type = TriangleType.ACUTE;
             }
         }
     }
     return type;
 }
 public void TestIsoscelesDetection()
 {
     //setup
     int l0, l1, l2;
     l0 = l1 = 5;
     l2 = 3;
     TriangleType expectedType = TriangleType.ISOSCELES;
     Triangle givenTriangle = new Triangle(l0, l1, l2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with 2 equal length sides is always isosceles");
 }
 public void TestRightDetection()
 {
     //setup
     float a0, a1, a2;
     a0 = 90.0f;
     a1 = 45.0f;
     a2 = 45.0f;
     TriangleType expectedType = TriangleType.RIGHT;
     Triangle givenTriangle = new Triangle(a0, a1, a2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with 1 90 degrees angle is always right");
 }
 public void TestObtuseDetection()
 {
     //setup
     float a0, a1, a2;
     a0 = 100.0f;
     a1 = 40.0f;
     a2 = 40.0f;
     TriangleType expectedType = TriangleType.OBTUSE;
     Triangle givenTriangle = new Triangle(a0, a1, a2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with 1 angle > 90 degrees is always obtuse");
 }
 public void TestAcuteDetection()
 {
     //setup
     float a0, a1, a2;
     a0 = 80.0f;
     a1 = 50.0f;
     a2 = 50.0f;
     TriangleType expectedType = TriangleType.ACUTE;
     Triangle givenTriangle = new Triangle(a0, a1, a2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with no angle >= 90 degrees is always acute");
 }
Exemplo n.º 9
0
 private bool Validate(Triangle givenTriangle)
 {
     bool valid = true;
     float sumOfAngles = givenTriangle.Angle0 + givenTriangle.Angle1 + givenTriangle.Angle2;
     valid &= sumOfAngles == 0.0f || sumOfAngles == 180.0f;
     valid &= (givenTriangle.Length0 != 0 && givenTriangle.Length1 != 0 && givenTriangle.Length2 != 0) ||
              (givenTriangle.Length0 == 0 && givenTriangle.Length1 == 0 && givenTriangle.Length2 == 0);
     valid &= FindLengthOfLongestSide(givenTriangle) < FindSumOfShortestSides(givenTriangle) ||
              (givenTriangle.Length0 == 0 && givenTriangle.Length1 == 0 && givenTriangle.Length2 == 0);
     return valid;
 }
 public void TestScaleneDetection()
 {
     //setup
     int l0, l1, l2;
     l0 = 2;
     l1 = 4;
     l2 = 3;
     TriangleType expectedType = TriangleType.SCALENE;
     Triangle givenTriangle = new Triangle(l0, l1, l2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle with no equal length sides is always scalene");
 }
 public void TestValidationZeroLengthSides()
 {
     //setup
     int l0, l1, l2;
     l0 = 0;
     l1 = 5;
     l2 = 3;
     TriangleType expectedType = TriangleType.INVALID;
     Triangle givenTriangle = new Triangle(l0, l1, l2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle cannot have a zero length side");
 }
 public void TestValidationTooMuchAngle()
 {
     //setup
     float a0, a1, a2;
     a0 = 91.0f;
     a1 = 45.0f;
     a2 = 45.0f;
     TriangleType expectedType = TriangleType.INVALID;
     Triangle givenTriangle = new Triangle(a0, a1, a2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle cannot have a total of all angles greater than 180 degrees");
 }
 public void TestValidationShortSide()
 {
     //setup
     int l0, l1, l2;
     l0 = 9;
     l1 = 5;
     l2 = 3;
     TriangleType expectedType = TriangleType.INVALID;
     Triangle givenTriangle = new Triangle(l0, l1, l2);
     //execute
     TriangleType actualType = new Classifier().Classify(givenTriangle);
     //assert
     Assert.AreEqual(expectedType, actualType, "A triangle's longest side cannot be longer than the sum of the two shorter sides");
 }