Don’t Forget The Edge Cases !

Imagine a scenario where you want to write the following function as a part of some larger project :

Write a function to return the type of a triangle based on the value of the length of 3 sides of a triangle. Let’s make it a bit more easy, by assuming that test for input data type is already in place, so you receive only numeric values to work with.

The situation looks easy you go ahead and write the function which looks something like this –

Algorithm :

Input : 3 numeric values
Output : 1 string stating type of triangle
Function : triangleType(side1, side2, side3)
Start :
1. If side1 == side2 == side3
   Then Return “Equilateral Triangle”
2. Else if side1 == side2 or side1 == side3 or side2 == side3
   Then Return “Isosceles Triangle”
3. Else
   Return “Scalar Triangle”

After you complete the function, you are given a couple of assertions to perform. And to your surprise, you find that only 50% of the cases passed.

Let's look at the test statements. The ones that pass are :

1. Assert if (String_toLowerCase(triangle_type(6,7,8))==”scalar triangle”) = Correct
2. Assert if (String_toLowerCase(triangle_type(6,6,6))==”equilateral triangle”) = Correct
3. assert(String_toLowerCase(triangle_type(6,7,6))==”isosceles triangle”) = Correct

Well, things look good up till here. But the ones that fail are :

4. Assert if (String_toLowerCase(triangle_type(0,0,0))==”not a triangle”) = Incorrect
5. Assert if (String_toLowerCase(triangel_type(-6,-7,-8))==”not a triangle”) = Incorrect
6. Assert if (String_toLowerCase(triangle_type(5,3,8))==”not a triangle”) = Incorrect

  • In the 4th statement input values are (0,0,0). Now we know that (0,0,0) form a point and not a triangle. In fact if any input value is zero, the triangle is not possible. But in our case, it will return equilateral triangle!
  • Also, the 5th statement reminds us that the length can never be a negative value. You see a scale that is -30 cm long. Thus, if we have even one -ve value of length, the triangle is not possible. But in our case depending on the value it may return any of the 3 results. Here it returns a scalar.
  • Now, what about the 6th statement. All values are >= 0, and it surely is a scalar triangle. Or is it? Remember the rule that in a triangle sum of 2 sides is always greater than the 3rd.

Here we see for :

8 + 3 > 5
8 + 5 > 3
5 + 3 > 8

Output :


It fails the test of triangularity. Hence lengths (3,5,8) do not form a triangle.

So what we need is a kind of triangle validation in place, which tells us if what we have is even a triangle or not. As part of the solution you write another function that looks like :

Algorithm :

Input : 3 sides of the triangle
Output : Boolean value: True if 3 sides form a triangle, false otherwise
Function : triangleValidator(side1, side2, side3)
1. If (side1 <= 0 or side2 <= 0 or side3 <= 0) and
      (side2 + side3 > side1) and
      (side3 + side1 > side1)
   then return True
3. Return False

Our previous function now includes 2 extra lines at the beginning and wola! all are tests are passing now.

This is just a simple an example scenario to remind us that when we writing production level code, we have to be careful with even simple things. By keeping the simple edge cases in mind, and checking with uniform string cases, we increased our test coverage and made our program return more mathematically correct results.

Below is the implementation of above approach :

# Check if given sides form a triangle or not
def triangleValidator(side1, side2 , side3):
    if side1 <= 0 or side2 <= 0 or side3 <= 0:
        return False
    elif (side1 + side2 > side3) and\
         (side2 + side3 > side1) and\
         (side3 + side1 > side2):
        return True
    return False

# Return the type of trinagle
def triangleType(side1, side2, side3):
    # If not a triangle, return "Not a triangle"
    if triangleValidator(side1, side2, side3) == False:
        return "Not A Triangle"
    # Else perform type checking
    if side1 == side2 == side3:
        return "Equilateral Triangle"
    elif (side1 == side2) or\
         (side2 == side3) or\
         (side3 == side1):
        return "Isoceles Triangle"
    return "Scalar Triangle"

assert (triangleType(6,7,8).lower() == "scalar triangle")
assert (triangleType(6,6,6).lower() == "equilateral triangle")
assert (triangleType(6,7,6).lower() == "isoceles triangle")
assert (triangleType(0,0,0).lower() == "not a triangle")
assert (triangleType(-6,-7,-8).lower() == "not a triangle")
assert (triangleType(5,3,8).lower() == "not a triangle")

Upon successful completion, no output is returned, indicating that all tests have passed.

Output :

No output

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :
Please write to us at to report any issue with the above content.

Recommended Posts:

0 Average Difficulty : 0/5.0
No votes yet.

User Actions