Computer ScienceChapter 2: Computational Thinking & Algorithms
2.1.4 Testing Computational Artifacts
Testing computational artifacts
Explanation level
Introduction
After a computational artifact (such as an algorithm) has been planned and developed, it must be tested before it is converted into actual code. Testing ensures that the algorithm works correctly, produces the expected output, and does not contain logical errors. The process of testing involves tracing the algorithm manually (dry run) and using trace tables to track changes in variables.
Definition
Testing computational artifacts is the process of verifying that an algorithm or solution works correctly by manually simulating its execution, tracking variable values, and checking whether the output matches expectations.
In simple words, testing a computational artifact means running it on paper (not on a computer) to find any logical errors before writing actual code.
Main Idea
The computational artifacts should be tested by considering potential logical errors. For example, you should think what would happen if a user enters invalid or a certain out-of-range input. Testing and accordingly refinement of solution is the iterative process of a computational artifact.
Core Concept
Testing involves:
- Tracing an algorithm (also called desk check or dry run) — manually simulating the execution step by step
- Using trace tables — recording how variables change at each stage
This helps verify that the algorithm is correct and helps find logical errors before implementation.
Detailed Explanation
a) Tracing an Algorithm
Tracing of algorithm is also known as a "desk check or dry-run". The programmers convert the algorithms to code that is ultimately handed over to computer for compilation and execution. But before going to write code for an algorithm, we manually verify that the algorithm works correctly. For this purpose, trace method is used that hand simulates the execution of your algorithm, keeps change track of variable values and flow of logic control.
Tracing of Algorithm involves the following:
a. Understand the Algorithm: Before tracing is started, thoroughly understand the algorithm's logic flow.
b. Choose Test Input: To trace the algorithm it requires input; a complete set of input is therefore chosen at this point.
c. Initialization: Initialize the variables and data structures. This helps in setting up the algorithm in its initial state, where the algorithm has never run before.
d. Trace Each Step: Hand execute the algorithm step-by-step, tracking the flow of control. For each step of algorithm, do the following:
- Track record Inputs: Write the input values and variable values relevant to that step.
- Perform processing: Do computation / perform the operation specified in that step of algorithm.
- Update Variables: Keep an eye on the changes in the variable values and data structures in response to performing operation.
- Go by Control Flow: Normally, the steps of algorithms are followed sequentially but sometimes this sequence is broken by loops, conditional statements, and function calls. Therefore, we need to track how an algorithm decides which step to execute next.
e. Repeat Until Completion: Tracing the algorithm continues step by step until we reach the end of algorithm.
f. Verify Output: At the end, evaluate the final output and ensure that it matches our expectations based on the given set of inputs.
Example — Jeroo Algorithm Tracing
Let's take the example of Jeroo that we solved in the previous topic and apply algorithm tracing.
a. Understand the Algorithm: What we need to do, from where we need to pick flowers and to which location we need to plant flower.
b. Choose test Input: In the example flower pickup location e.g. (3,0) serves as input.
c. Initialization: Initial position of Jeroo and Initial position of flower.
d. Trace each step:
e. Track record input: Jeroo's location, flower's location are the input variables.
f. Processing: Taking moves, taking turns, picking flower, putting flower are the actions that require processing.
g. Update variables: Jeroo's location and flower's location will be updated after each step movement of Jeroo and flower pick/put action respectively.
h. Go by control flow: Follow the algorithm e.g. sequentially.
i. Repeat until Completion: Take moves and turns until the Jeroo reaches its destination and flower is placed at the designated location.
j. Verify output: Verify whether the completion done in above step is same as desired in the problem statement.
Trace Tables
The dry run of an algorithm can be done with a technique known as Trace Tables. These tables show the variables change at each stage in the algorithm. The trace tables generate the output on a given set of input data to test if algorithm is giving expected output. It also helps to check that our logic is correct and find logical errors, if any.
Example — Trace Table:
Consider the following pseudocode:
- number = 3
- PRINT number
- FOR i from 1 to 3:
- number = number + 5
- PRINT number
- PRINT "?"
Trace Table:
| Pseudocode steps | Number | i | Output |
|---|---|---|---|
| 1 | 3 | ||
| 2 | 3 | ||
| 3 | 1 | ||
| 4 | 8 | ||
| 5 | 8 | ||
| 3 | 2 | ||
| 4 | 13 | ||
| 5 | 13 | ||
| 3 | 3 | ||
| 4 | 18 | ||
| 5 | 18 | ||
| 6 | ? |
Another Example:
Consider the same example we discussed in the chapter above.
- Start
- Set c=1
- Read n
- m=n*c
- print m
- c=c+1
- Repeat steps 4 to 6 while (c<=10)
- End
Intermediate Level
Why Testing Is Important
Testing computational artifacts is important because:
- It finds logical errors before writing code
- It saves time and effort in the long run
- It ensures the algorithm produces correct output
- It helps understand the algorithm better
- It verifies that all conditions and loops work properly
How to Choose Test Inputs
Good test inputs should cover:
- Normal cases — typical values the algorithm will receive
- Edge cases — boundary values (e.g., 0, 1, maximum value)
- Invalid cases — out-of-range or unexpected inputs
- Special cases — values that might cause unusual behavior (e.g., negative numbers, empty input)
Example — For an algorithm that finds the largest of three numbers:
- Normal: 10, 20, 30
- Edge: 0, 0, 0
- Edge: -5, -10, -3
- Special: 100, 100, 100 (all equal)
Common Logical Errors Found by Tracing
| Error Type | Example |
|---|---|
| Wrong condition | Using `<` instead of `<=` |
| Off-by-one error | Loop runs one extra or one less time |
| Infinite loop | Loop condition never becomes false |
| Wrong variable update | Updating the wrong variable |
| Wrong order of steps | Swapping two steps changes output |
Formula / Mathematical Form
Not applicable for this topic.
Solved Example
Example — Trace Table for Jeroo Problem
Problem: Jeroo at (0,0) facing East. Flower at (3,0). Pick flower, plant at (3,2), move one hop East and stop.
Algorithm:
- Pick Flower
- Hop 3 times
- Take the flower
- Put Flower
- Turn right
- Hop 2 times
- Plant the flower
- Move East
- Turn left
- Hop 1 time
Trace Table:
| Step | Jeroo Position | Direction | Flower Position | Action |
|---|---|---|---|---|
| Start | (0,0) | East | (3,0) | |
| 1 | (0,0) | East | (3,0) | Pick Flower (ready) |
| 2a | (1,0) | East | (3,0) | Hop |
| 2b | (2,0) | East | (3,0) | Hop |
| 2c | (3,0) | East | (3,0) | Hop |
| 3 | (3,0) | East | (3,0) | Take flower |
| 4 | (3,0) | East | (3,0) | Put Flower (ready) |
| 5 | (3,0) | South | (3,0) | Turn right |
| 6a | (3,1) | South | (3,0) | Hop |
| 6b | (3,2) | South | (3,0) | Hop |
| 7 | (3,2) | South | (3,2) | Plant flower |
| 8 | (3,2) | South | (3,2) | Move East (turn left) |
| 9 | (3,2) | East | (3,2) | Turn left |
| 10 | (4,2) | East | (3,2) | Hop 1 time |
| End | (4,2) | East | (3,2) | Stop |
- Flower planted at (3,2) ✓
- Jeroo at (4,2) facing East ✓
Important Points
- - Testing computational artifacts means verifying the algorithm works correctly before coding.
- - Tracing an algorithm is also called "desk check" or "dry run."
- - Tracing involves: understanding the algorithm, choosing test input, initialization, tracing each step, repeating until completion, and verifying output.
- - Trace tables show how variables change at each stage in the algorithm.
- - Trace tables help check if logic is correct and find logical errors.
- - Testing should consider normal, edge, invalid, and special cases.
- - Testing is an iterative process — test, refine, test again.
Remember
- - Tracing = Desk check = Dry run.
- - Trace Table = Shows variable changes at each step.
- - Steps in tracing: Understand → Choose input → Initialize → Trace each step → Repeat → Verify.
- - Test cases should include: Normal, edge, invalid, special.
- - Trace tables help find: Wrong conditions, off-by-one errors, infinite loops, wrong variable updates.
- - Testing is iterative — repeat until correct.
Common Mistakes
- - Skipping testing: Assuming the algorithm is correct without tracing it.
- - Using only one test case: Not testing with edge cases or invalid inputs.
- - Not tracking all variables: Forgetting to record changes in some variables in the trace table.
- - Ignoring loop iterations: Not tracing each iteration of a loop separately.
- - Not verifying output: Forgetting to check if the final output matches expectations.
- - Giving up after one error: Testing is iterative — fix and test again.
Quick Revision
Testing computational artifacts involves tracing the algorithm (dry run) and using trace tables. The computational artifacts should be tested by considering potential logical errors. For example, you should think what would happen if a user enters invalid or a certain out-of-range input.
Tracing an algorithm involves:
- Understand the algorithm
- Choose test input
- Initialization
- Trace each step (track inputs, perform processing, update variables, go by control flow)
- Repeat until completion
- Verify output
Trace tables show how variables change at each stage. They help check if logic is correct and find logical errors. Testing is an iterative process — test, refine, test again.
60-Second Revision
- Testing = Tracing = Desk check = Dry run.
- Trace Table = Shows variable changes at each step.
- Steps: Understand → Choose input → Initialize → Trace → Repeat → Verify.
- Test cases: Normal, edge, invalid, special.
- Trace tables find: Wrong conditions, off-by-one errors, infinite loops, wrong variable updates.
- Iterative: Test → Refine → Test again.
- Warning: Never skip testing — logical errors must be found before implementation.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.2.1.4 Testing Computational Artifacts
Sticky Notes · Quick Revision
Key Point 1
- Tracing = Desk check = Dry run.
Key Point 2
- Trace Table = Variables change at each step.
Key Point 3
- Steps: Understand → Choose input → Initialize → Trace → Repeat → Verify.
Key Point 4
- Test cases: Normal, edge, invalid, special.
Key Point 5
- Trace tables find: Logical errors, wrong conditions, infinite loops.
Key Point 6
- Iterative process: Test → Refine → Test again.
Key Point 7
- Jeroo Example: Verified flower at (3,2), Jeroo at (4,2) East.
Same supplied content as this Notes page (shared canonical source).