Testing Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Testing.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Systematically running a program with known inputs to verify that its outputs are correct. Testing involves designing test cases that cover normal inputs, boundary values, and error conditions, then comparing actual results against expected results.

Try to break it before users do. Test normal cases, edge cases, and error cases.

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Testing can prove that bugs exist, but cannot prove that no bugs remain—exhaustive testing is impossible.

Common stuck point: Test edge cases: empty input, zero, maximum values, unexpected types.

Sense of Study hint: When writing test cases, cover three categories: typical inputs (normal use), boundary values (edges like 0, empty strings, maximum values), and error cases (invalid input that should be rejected gracefully). For each test, define the input, the expected output, and compare it to the actual output.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

easy
A function isEven(n) should return TRUE if n is even and FALSE otherwise. Suggest three test cases, including a normal, boundary, and erroneous value.

Answer

Normal: isEven(4) → TRUE. Boundary: isEven(0) → TRUE. Erroneous: isEven('hello') → error/FALSE.

First step

1
Step 1: Normal test: isEven(4) should return TRUE. This tests typical valid input.

Full solution

  1. 2
    Step 2: Boundary test: isEven(0) should return TRUE. Zero is an edge case that is technically even.
  2. 3
    Step 3: Erroneous test: isEven('hello') should handle the invalid input gracefully (return an error or FALSE). This tests that the function does not crash on bad input.
Good testing uses normal data (typical valid input), boundary data (edge cases at limits), and erroneous data (invalid input). This ensures the program works correctly in all situations.

Example 2

medium
A program grades students: 90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, below 60 = F. Design a complete set of test data including boundary values.

Example 3

medium
A function daysInMonth(m)\text{daysInMonth}(m) takes m{1,,12}m \in \{1,\dots,12\}. List one boundary, one normal, and one error input.

Example 4

medium
A function checks if a year is a leap year. Suggest a normal, two boundary, and one error test.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
A password validation function requires: at least 8 characters, at least one uppercase letter, and at least one digit. Design test cases that check each requirement independently.

Example 2

hard
Explain the difference between white-box and black-box testing. Which approach would you use to test a sorting function, and why?

Example 3

easy
A test gives input 3 and expects output 9 (square). The function returns 9. Does the test pass?

Example 4

easy
Can testing prove a program has zero bugs?

Example 5

easy
Testing only normal inputs and skipping unusual ones is called testing only the ____ path.

Example 6

easy
For a function valid on ages 0 to 120, name one boundary value to test.

Example 7

easy
A test for division should include which special input to catch errors?

Example 8

easy
Should tests depend on the order they run in?

Example 9

easy
A test expects 'true' but the function returns 'false'. Pass or fail?

Example 10

easy
Trying inputs that should make a program crash is testing the ____ cases.

Example 11

medium
A function adds 1 to its input. Test cases: input 0->1, input -1->0, input 5->6. How many test cases, and do they pass if the function is correct?

Example 12

medium
A sort function is tested on [][], [1][1], and [2,1][2,1]. Which of these is the empty-input edge case?

Example 13

medium
Code: `if x = 5` (single equals) was meant to compare. What classic bug is this, and what was intended?

Example 14

medium
A test suite covers inputs 1, 2, 3 but a bug only appears at input 1000. What does this illustrate about testing?

Example 15

medium
A function should return the max of a list. Choose a good edge-case input that often breaks naive implementations.

Example 16

medium
Two tests must run; test B reads a file that test A creates. Why is this a bad test design?

Example 17

medium
A binary-search function returns -1 for 'not found'. Which two inputs best test the not-found path?

Example 18

medium
A function `isEven(n)` is tested with 2->true, 3->false, 0->true. The code returns true for 0. Pass or fail on the 0 case?

Example 19

medium
A login function is tested with: valid user, wrong password, empty username. Which case is the error/edge case?

Example 20

challenge
You have a function over the integer range 0..1000. You cannot test all 1001 values. Justify testing 0, 1, 500, 999, 1000 specifically.

Example 21

challenge
A program passes all 50 tests but fails in production. Give the most likely testing gap and one fix.

Example 22

challenge
Why does the formula 'test passes     oe=oa\iff o_e = o_a' require the expected value oeo_e to be computed independently of the code under test?

Example 23

easy
A function abs(n)\text{abs}(n) returns n|n|. The test abs(7)7\text{abs}(-7) \to 7 runs and the function returns 77. Pass or fail?

Example 24

easy
For a function valid on inputs 1n501 \le n \le 50, name an erroneous (out-of-range) input to test.

Example 25

easy
A test expects 4242 but the function returns 2424. Pass or fail?

Example 26

easy
Which input type catches bugs at the limits of allowed values: normal, boundary, or error?

Example 27

medium
A function returns the average of a non-empty list. Suggest a normal, boundary, and error test input.

Example 28

medium
A test sets a global counter to 00, then asserts it equals 11 after calling inc()\text{inc}(). Another test assumes the counter starts at 11 already. What design flaw is this?

Example 29

medium
A regression test is added after a bug is fixed. What is its purpose?

Example 30

medium
A function sqrt(x)\text{sqrt}(x) is defined for x0x \ge 0. Give a normal, boundary, and error input.

Example 31

medium
Code: `if temperature > 100: alert()`. The team only tests 5050, 8080, 9999. What input is missing to test the threshold?

Example 32

medium
A test calls login(alice,pw)\text{login}('alice', 'pw'). The function returns success but writes wrong data to the database. Why might this test still pass?

Example 33

medium
A function factorial(n)\text{factorial}(n) accepts n0n \ge 0. Which input is the boundary value with 00 multiplications?

Example 34

medium
Two test cases share a long setup. A developer extracts the setup into a helper function. Which test-quality property does this preserve?

Example 35

medium
A test sets oe=f(3)o_e = f(3) where ff is the function under test, then asserts f(3)=oef(3) = o_e. Why is this test worthless?

Example 36

medium
What is the smallest set of inputs that should be tested for a function with valid range 1n1001 \le n \le 100?

Example 37

hard
A function rounds floats to 2 decimal places. The test asserts round(0.1+0.2)=0.30\text{round}(0.1 + 0.2) = 0.30. It fails. Why?

Example 38

hard
A function search(L,k)\text{search}(L, k) returns the index of kk in list LL or 1-1. Design tests covering: empty list, first element, last element, missing element, duplicates.

Example 39

hard
A team uses code coverage to measure tests. The coverage is 100%100\%, but a critical bug still ships. How is this possible?

Example 40

hard
A function f(x,y)f(x, y) has 3 categories for xx and 4 for yy. How many cases does exhaustive equivalence-class testing require, and why is this cheaper than testing every input pair?

Example 41

challenge
A program processes a list and is tested only on lists of length 10\le 10. A bug appears only when length 216\ge 2^{16}. Why did testing miss it, and name a strategy that would have caught it.

Example 42

challenge
Given the formula test passes    oe=oa\text{test passes} \iff o_e = o_a, explain why a non-deterministic function (returns random output) makes this definition problematic, and state one fix.

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithmdebugging