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 –
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” Stop
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
True True False
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 :
Input : 3 sides of the triangle Output : Boolean value: True if 3 sides form a triangle, false otherwise Function : triangleValidator(side1, side2, side3) Start 1. If (side1 <= 0 or side2 <= 0 or side3 <= 0) and (side2 + side3 > side1) and (side3 + side1 > side1) then return True 3. Return False Stop
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 :
Upon successful completion, no output is returned, indicating that all tests have passed.
- Designing Use Cases for a Project
- Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
- DynamoDB: Understand The Benefits With Real Life Use Cases
- Edge Computing
- What is Edge Computing and Its Importance in the Future?
- Best 5 Strategies to Build Better & Worthwhile Professional Relationships
- Top 5 Places to Practice Ethical Hacking
- 7 React Best Practices Every Web Developer Should Follow
- Top 10 Python Libraries for Data Science in 2020
- Top 8 Tips to Get Your Android App Featured on Google Play Store
- Find a Mother vertex in a Graph using Bit Masking
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Find distance between two nodes in the given Binary tree for Q queries
- Query to count odd and even parity elements in subarray after XOR with K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.