January 25, 2009

WinTask - The Triangle Test

One of the first books on testing that I ever read was "The Art of Software Testing" by Glenford J. Myers

The first paragraph of the book asks the reader to take a short self-test to help demonstrate the difficulty in developing adequate test cases for even a very simple program.  The program is widely known as The Triangle Problem (from Gerald Weinberg) or The Triangle Test, and is reproduced here as a WinTask script.

If you compile this script to a .rob file, and give the new or prospective tester a copy of WinTask and the .rob file, she can try testing the program for herself, using test cases she has devised.

You may even choose to intentionally create a buggy version of this script, to see if the new tester can find all of the bugs.

This is a basic implementation of The Triangle Test.




'
' Triangle Test - Demonstrates testing a simple application
'
' Author: Joe Strazzere
'
' Adapted from Gerald Weinberg and Glenford Myers
'
Dim Answers$(1000)

'
' The function which actually calculates the triangle type
' It is simple to introduce errors into this code if desired
'
Function TriangleType$(side1$,side2$,side3$)
s1=Val(side1$)
s2=Val(side2$)
s3=Val(side3$)
If (Str$(s1) <> Ltrim$(Rtrim$(side1$))) or (Str$(s2) <> Ltrim$(Rtrim$(side2$))) or (Str$(s2) <> Ltrim$(Rtrim$(side2$))) Then
 TriangleType$="INVALID"
Else
 If (s1 <= 0) or (s2 <= 0) or (s3 <= 0) Then
  TriangleType$="INVALID"
 Else
  If (s1 > s2) and (s1 > s3) and (s1 >= s2 + s3)Then
   TriangleType$="INVALID"
  Else
   If (s2 > s1) and (s2 > s3) and (s2 >= s1 + s3)Then
    TriangleType$="INVALID"
   Else
    If (s3 > s1) and (s3 > s2) and (s3 >= s1 + s2)Then
     TriangleType$="INVALID"
    Else
     If (s1 = s2) and (s2 = s3)Then
      TriangleType$="EQUILATERAL"
     Else
      If (s1 = s2) or (s2 = s3) or (s1 = s3) Then
       TriangleType$="ISOSCELES"
      Else
       TriangleType$="SCALENE"
      EndIf
     EndIf
    EndIf
   EndIf
  EndIf
 EndIf
EndIf
EndFunction

BEGINDIALOG Dialog 347, 362, 550, 317
CAPTION "Triangle Test"
 ICON "Question", 22, 27, 32, 32
 TEXT "Enter the three sides.", 125, 30
 TEXT "Then click OK to analyze, or Cancel to quit.", 125, 50
 EDITTEXT Edit1$, 137, 88, 70, 20
 EDITTEXT Edit2$, 245, 89, 70, 20
 EDITTEXT Edit3$, 346, 88, 70, 20
 TEXT Type1$, 137, 196
 TEXT Type2$, 243, 196
 DEFPUSHBUTTON "OK", btnOK, 355, 238, 75, 23
 PUSHBUTTON "Cancel", btnCancel, 444, 238, 75, 23
ENDDIALOG

'
' Instructions
'
Instr$=""
Instr$=Instr$+"The program reads three integer values."+CRLF
Instr$=Instr$+CRLF
Instr$=Instr$+"The three values are interpreted as representing the sides of a triangle."+CRLF
Instr$=Instr$+CRLF
Instr$=Instr$+"The program displays a message that states whether the triangle is "+CRLF
Instr$=Instr$+"SCALENE, EQUILATERAL, ISOSCELES or INVALID"
MsgBox(Instr$,64,"Triangle Test")

'
' Initial Display
'
Type$=""
In$=""
CallDialog Dialog,"Triangle Test"

'
' Subsequent Display
'
Type1$="Type of Triangle: "
i=0
Correct = 0
Incorrect = 0
While btnOK = 1
 i=i+1
 If i > 1000 Then
  End
 EndIf
 In$="Input: ["+Edit1$+"] , ["+Edit2$+"] , ["+Edit3$+"]"
 Type2$=TriangleType$(Edit1$,Edit2$,Edit3$)
 CallDialog Dialog,"Triangle Test"
Wend