Testing Triangles : Bugs

Yesterday, I was looking at testing triangle exercise posted here. As it happens I was just curious to look at the code straight away, fortunately I could access the code as it was javascript. So looking at the code, leaving apart the graphics code the most important thing was the condition "this.s1 >= this.s2 + this.s3" which gives invalid triangle, other conditions are straight away. The cases which are not handled here are:
- When all sides are given as zeros, in which case it is a point which I feel is also a triangle, but the above condition fails.
- There is no check that sides can only be positive intergers/floating point numbers, so it can take non-negative intergers and strings as well, in which case the results makes no sense. In case of strings it may end up with NaN at many places with weird behavior.
Interesting bug is that for condition "this.s1 >= this.s2 + this.s3" to work, whatever the order in which user enters the values, we need to sort it such a way that s1>=s2>=S3 (or atleast s1 should be the maximum of all the three sides) for this, I had to check whether the sorting function works properly or not. The interesting thing is that sort fuction used in this code was string sorting and not numeric sorting.If input is 8,7,9, sort function will reorder them as 7,8,9, but in case input values are 9,12,7 the sort function is reordering them as 12,7,9. So from this we can make out that sorting is happening based on string matching and since 1 < 7, it has sorted 12 as less than 7 :). The cases wherin the graphic does not appear properly are because of this sorting itself.
Well one thing, I would definitely missed out using just code inspection is the case where user enters 2.1,2.2,4.3, which should have been an invalid triangle, but 2.1+2.2 is being calculated as 4.300000000000001 which is greater than 4.3 and hence it gives as scalene triangle.


  1. Excellent! Great bugs!

  2. Thanks Elisabeth, Looking for more such challenges :)