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.

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.

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.

Solution

  1. 1
    Step 1: Normal test: isEven(4) should return TRUE. This tests typical valid input.
  2. 2
    Step 2: Boundary test: isEven(0) should return TRUE. Zero is an edge case that is technically even.
  3. 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.

Answer

Normal: isEven(4) β†’ TRUE. Boundary: isEven(0) β†’ TRUE. Erroneous: isEven('hello') β†’ error/FALSE.
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.

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?

Related Concepts

Background Knowledge

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

algorithmdebugging