About

6.1 The Triangle Problem

Theory: The Triangle Problem

The triangle problem comes from a classic testing textbook, and it is the course’s main exercise for equivalence partitioning, boundary value analysis and positive/negative tests (Lecture 5). The program reads three integers, the sides of a triangle, and says whether the triangle is scalene (all sides different), isosceles (two sides equal) or equilateral (all sides equal). It looks trivial. The textbook’s author gave it to many experienced developers, and almost none of them found all the test objectives.

The Exercise and the Table the Professor Wants

The class first tried the problem on their own; some groups did well and some needed more work. The professor then asked for it again, this time with Lecture 5’s tools:

Some of the first answers were hard to follow, so the professor asked for a table with fixed columns:

No.Test objectiveTest case (a, b, c)Expected outputRemarks
1Valid scalene triangle………
2Valid equilateral triangle………
3.1Valid isosceles triangle (two sides equal)………
3.2… another isosceles case………
4…………

When several test cases share one objective, number them 3.1, 3.2, … under the same objective number.

What a Triangle Is (and Is Not)

KindRuleExample
ValidEquilateralall 3 sides equal(3, 3, 3)
ValidIsoscelesexactly 2 sides equal(3, 3, 4)
ValidScaleneall sides different(3, 4, 5)
Not a triangleFlat linetwo sides add up to exactly the third(1, 2, 3): 1+2=31 + 2 = 3
Not a triangleDoes not closetwo sides add up to less than the third(1, 2, 4): 1+2<41 + 2 < 4

Think of three sticks. If the two short sticks together are exactly as long as the long one, they lie flat along it and enclose no area. If they are shorter, their ends cannot even meet. Only when the two shorter sides together are longer than the longest side do you get a triangle.

Step 1: Start Simple with EP

The valid outputs are a set of three values: scalene, isosceles, equilateral. By the set guideline of 5.2, the starting point is:

Step 2: Refine, and Look for Hidden Boundaries

Four big partitions are too coarse, like a fishing net with big holes: bugs slip through. So we look inside each partition for hidden boundaries, smaller borders that only appear when you think deeper.

For example, an isosceles triangle can have its equal sides in different places: (3, 3, 4), (3, 4, 3), (4, 3, 3). A program might check only a == b and forget a == c and b == c. So the textbook adds an objective: test all three orders (permutations). The same idea applies to the non-triangles: the long side can be in any of the three positions.

Common mistake: testing only (3, 3, 4) for isosceles and only (1, 2, 3) for the flat line. A program that checks just one pair of sides, or assumes the longest side comes last, passes those tests and fails on the other orders. Test the permutations.

The 14 Test Objectives

#Test objectiveExample test caseExpected output
1Valid scalene triangle(3, 4, 5)Scalene
2Valid equilateral triangle(3, 3, 3)Equilateral
3Valid isosceles triangle(3, 3, 4)Isosceles
4Isosceles in all 3 orders (permutations)(3, 3, 4), (3, 4, 3), (4, 3, 3)Isosceles
5One side is zero(0, 4, 5)Invalid
6One side is negative(−1, 4, 5)Invalid
7Two sides add up to exactly the third (a flat line)(1, 2, 3)Not a triangle
8Case 7 in all 3 orders(1, 2, 3), (1, 3, 2), (3, 1, 2)Not a triangle
9Two sides add up to less than the third(1, 2, 4)Not a triangle
10Case 9 in all 3 orders(1, 2, 4), (1, 4, 2), (4, 1, 2)Not a triangle
11All sides zero(0, 0, 0)Invalid
12Non-integer values(2.5, 3, 4)Invalid
13Wrong number of values(3, 4)Error
14Did every test case have an expected output?––

The professor pointed out the pattern:

Picking Values: Simple, and Near the Boundary

The professor’s advice: keep it simple. For integers, start from 0 and 1 and walk upwards. Watch what happens when you look for the simplest valid scalene:

TryResultWhy
(1, 1, 1)Equilateralall equal
(1, 1, 2), (1, 2, 2)Not scalenetwo sides equal ((1, 1, 2) is also flat: 1+1=21 + 1 = 2)
(1, 2, 3)Not a triangle1+2=31 + 2 = 3: a flat line, exactly on the boundary
(1, 2, 4)Not a triangle1+2<41 + 2 < 4: outside the boundary
(2, 3, 4)Scalene2+3=5>42 + 3 = 5 > 4: the first valid scalene, just inside the boundary

This is BVA inside EP: you walk along the edge until you cross from invalid to valid, and the values on either side of the crossing are the best tests you have.

The boundary, stated exactly

Sort the sides so that a≤b≤ca \le b \le c (cc is the longest). The triangle rule is

a+b>ca + b > c

so the three BVA points around it are:

From the slides: the same idea as a partition table. Equilateral (5, 5, 5); isosceles in three orders (5, 5, 3), (5, 3, 5), (3, 5, 5); scalene (3, 4, 5); non-positive sides (0, 5, 5), (5, −1, 5), (5, 5, 0); and the triangle inequality broken in three orders (2, 2, 5), (2, 5, 2), (5, 2, 2). The BVA example: (2, 2, 5) is below the edge (2+2=4<52 + 2 = 4 < 5, not a triangle), (2, 2, 4) is on the edge (flat line), (2, 2, 3) is just inside (a valid isosceles).

How Many Test Cases per Objective?

Finding hidden boundaries takes creativity. It is exploratory work, where generative AI is weaker and a human must guide it. And in real projects, time and budget limit you, so the final choice of how many tests to write is a risk decision (2.2).

Exam-style answer: Design black box test cases for the triangle program using EP, BVA, and positive and negative tests.

No.Test objectiveTest case (a, b, c)Expected outputRemarks
1Valid scalene(3, 4, 5)Scalenepositive, EP
2Valid equilateral(3, 3, 3)Equilateralpositive, EP
3.1–3.3Valid isosceles, all orders(3, 3, 4), (3, 4, 3), (4, 3, 3)Isoscelespositive, permutations
4Side = 0(0, 4, 5)Invalidnegative
5Negative side(−1, 4, 5)Invalidnegative
6.1–6.3a+b=ca + b = c, all orders(1, 2, 3), (1, 3, 2), (3, 1, 2)Not a trianglenegative, BVA on the edge
7.1–7.3a+b<ca + b < c, all orders(1, 2, 4), (1, 4, 2), (4, 1, 2)Not a trianglenegative
8Non-integer(2.5, 3, 4)Invalidnegative, wrong type
9Wrong number of inputs(3, 4)Errornegative

Why this gets marks: the columns the professor asked for, positive tests (1–3) and negative tests (4–9), permutations, a boundary case (6), and an expected output on every row.


Worked Examples

Exercise 1

Why does the triangle solution test isosceles triangles in three different orders?

Show solution ↓
Solution

Because the program might check only one pair of sides (for example a == b) and forget the others (a == c, b == c). Then (3, 3, 4) passes, but (3, 4, 3) or (4, 3, 3) would be misclassified as scalene.

The order of the inputs is a hidden boundary inside the isosceles partition, so each order deserves its own test case: (3, 3, 4), (3, 4, 3), (4, 3, 3) → Isosceles for all three.

Exercise 2

Is (1, 2, 3) a valid triangle? What kind of test is it?

Show solution ↓
Solution

Sorted, the sides are 1≤2≤31 \le 2 \le 3. Check the triangle rule a+b>ca + b > c:

1+2=3≯31 + 2 = 3 \not> 3

No, it is not a triangle: the two short sides lie flat along the long one. It is a negative test that sits exactly on the boundary a+b=ca + b = c, so it is also a BVA test.

Exercise 3

Why must every test case in the triangle table have an expected output?

Show solution ↓
Solution

Without an expected output you have nothing to compare the actual output with, so you cannot decide pass or fail. A test case is an input plus its expected result; an input alone is not a real test case. That is why the textbook’s last objective (14) asks: did every test case have an expected output?

Exercise 4

Classify (2, 2, 5), (2, 2, 4) and (2, 2, 3), and say where each sits relative to the boundary.

Show solution ↓
Solution

In each case the two short sides are 2 and 2, so a+b=4a + b = 4; compare with the longest side cc:

Testa+ba + b vs. ccPositionExpected output
(2, 2, 5)4<54 < 5below the edge (outside)Not a triangle
(2, 2, 4)4=44 = 4on the edge (flat line)Not a triangle
(2, 2, 3)4>34 > 3just insideIsosceles (two sides equal)

Three tests one step apart, straddling the edge: a textbook BVA set.

Exercise 5

Starting from (1, 1, 1) and keeping the values as small as possible, find the simplest valid scalene triangle. Explain why this search is “BVA inside EP”.

Show solution ↓
Solution

Walk upwards and check each candidate:

  • (1, 1, 1): equilateral. (1, 1, 2), (1, 2, 2): two sides equal, not scalene.
  • (1, 2, 3): all different, but 1+2=31 + 2 = 3: flat line, on the boundary.
  • (1, 2, 4): 1+2=3<41 + 2 = 3 < 4: outside the boundary.
  • (2, 3, 4): all different and 2+3=5>42 + 3 = 5 > 4: a valid triangle.

So the simplest valid scalene is (2, 3, 4). ✓ (2+3=5=4+12 + 3 = 5 = 4 + 1, exactly one step inside.)

It is BVA inside EP because we are looking for a sample of the scalene partition (EP), and the search walks along the triangle-inequality edge until it crosses from invalid to valid (BVA). The values on either side of the crossing, (1, 2, 3) and (2, 3, 4), are the most valuable tests.

Exercise 6

Two sides of a triangle are 4 and 7, and the third side cc is to be the longest. Give the value of cc that is exactly on the triangle-inequality boundary, just inside it, and just outside it, with expected outputs.

Show solution ↓
Solution

The rule (sorted, cc longest) is a+b>ca + b > c, and a+b=4+7=11a + b = 4 + 7 = 11.

  • On the boundary: a+b=ca + b = c → c=11c = 11. (4, 7, 11) → Not a triangle (flat line).
  • Just inside: a+b=c+1a + b = c + 1 → c=10c = 10. (4, 7, 10) → Scalene ✓ (11>1011 > 10, all sides different).
  • Just outside: a+b=c−1a + b = c - 1 → c=12c = 12. (4, 7, 12) → Not a triangle (11<1211 < 12).
Exercise 7

Design a compact but defensible black box test set for a program that accepts three positive integers and returns equilateral, isosceles, scalene or invalid. Use the professor’s table format.

Show solution ↓
Solution

Partitions. Valid outputs: equilateral, isosceles, scalene. Invalid values: zero, negative, non-integer, non-numeric. Structural invalidity: two sides sum to the third or less. Hidden boundaries: where the equal sides are placed. Main boundary (sorted a≤b≤ca \le b \le c): a+b>ca + b > c, tested on, just inside and just outside.

No.Test objectiveTest case (a, b, c)Expected outputRemarks
1Valid equilateral(1, 1, 1)Equilateralpositive
2Valid isosceles(5, 5, 8)Isoscelespositive
3Equal-side permutation(5, 8, 5)Isosceleshidden boundary
4Valid scalene(4, 5, 6)Scalenepositive
5Zero side(0, 5, 5)Invalidnegative
6Negative side(−1, 5, 5)Invalidnegative
7Non-integer type(2.5, 5, 5)Invalidnegative, wrong type
8Non-numeric type('a', 5, 5)Invalidnegative, wrong type
9Exactly on the inequality boundary(2, 3, 5)InvalidBVA: 2+3=52 + 3 = 5
10Just inside the boundary(2, 3, 4)ScaleneBVA: 2+3=4+12 + 3 = 4 + 1
11Just outside the boundary(2, 3, 6)InvalidBVA: 2+3=6−12 + 3 = 6 - 1

Checks: (5, 5, 8) is a triangle since 5+5=10>85 + 5 = 10 > 8 ✓; (4, 5, 6) since 4+5=9>64 + 5 = 9 > 6 ✓.

This set covers every valid class, four distinct invalid classes, a hidden isosceles permutation, and the three most valuable points around the triangle-inequality boundary, with an expected output on every row. If the question asks for full positional coverage, add the remaining permutations (3.1–3.3, 6.1–6.3, 7.1–7.3 style).