Imagine a scenario where you are asked to write the following function as a part of a larger project:
Task: 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
Input: 3 numeric values
Output: 1 string stating the type of triangle
Function: triangleType(side1, side2, side3)
1. If side1 = side2 =side3
Then Return “Equilateral Triangle”
2. Else if side1=side2 or side1=side3 or side2=side3
Then Return “Isosceles Triangle”
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. String_toLowerCase(triangle_type(6,7,8))=”scalar triangle” = Correct
2. String_toLowerCase(triangle_type(6,6,6))=”equilateral triangle” = Correct
3. String_toLowerCase(triangle_type(6,7,6))=”isosceles triangle” = Correct
Well, things look good up till here. But the ones that fail are:
4. String_toLowerCase(triangle_type(0,0,0))=”not a triangle” = Incorrect
5. String_toLowerCase(triangel_type(-6,-7,-8))=”not a triangle” = Incorrect
6. String_toLowerCase(triangle_type(5,3,8))=”not a triangle” = Incorrect
Let’s look at them one by one.
In the 4th statement we see that 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 seen 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
True True False
It fails the test of triangularity. Hence lengths (3,5,8) do not form a triangle.
Hmm, 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:
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+side2>side3) and (side3+side1>side2) and (side2+side3>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.
The complete source code in python is here:
# 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.
This article has been submitted for review at GeeksforGeeks as well.