April 4, 2006

The Triangle Test

"The Art of Software Testing" by Glenford Myers was one of the first books on testing I read.
It includes the famous "Triangle Test":
This program accepts as input three integers which it interprets as the lengths of sides of a triangle. It reports whether the triangle is equilateral, isosceles, or scalene (neither equilateral nor isosceles). Write a set of test data to test this program.
    1. Do you have a test case which represents a valid scalene triangle?
    2. Do you have a test case which represents a valid equilateral triangle?
    3. Do you have a test case which represents a valid isosceles triangle?
    4. Do you have at least three test cases which represent valid isosceles triangles such that you have tried all three permutations of two equal sides?
    5. Do you have a test case in which one side is zero?
    6. Do you have a test case in which one side is negative?
    7. Do you have a test case with three positive integers such that the sum of two is equal to the third?
    8. Do you have at least three test cases in category 7 such that you have tried all three permutations where the length of one side is equal to the sum of the lengths of the other two sides?
    9. Do you have a test case with the sum of two of the numbers less than the third?
    10. Do you have at least three cases in category 9 such that you have tried all three permutations?
    11. Do you have a test case with all sides zero?
    12. Do you have at least one test case specifying non-integer values?
    13. Do you have at least one test case specifying the wrong number of values (two or four)?
    14. Most importantly, for each test case, did you specify the expected output?
(To see the text of this test, read here or here.)

When I have an audience that may not appreciate the amount of work necessary to create test cases, I sometimes give a presentation and ask that they take this test.



Elisabeth Hendrickson's Test Obsessed site includes a nice Javascript implementation of the program under test here:
http://www.testobsessed.com/exercises/triangle.html



Note: An interesting PowerPoint deck from The University of Limerick's Department of Computer Science and Information Systems Website is here: http://www.csis.ul.ie/Modules/CS5703/BlackBox-Triangle.ppt.  It uses the Triangle Test as the basis for demonstrating some testing approaches.