Directed Tests
Turn a feature list into a handful of tests with answers worked out by hand. See what directed tests do well, and why the list always runs out before the bugs do.
You Will Be Able To
- Write a directed test case from one line of a feature list, with its expected answer worked out by hand.
- Say what directed tests are good at: first bring-up, named features and exact corners.
- Explain why a list of cases cannot cover the input space of even a small design.
- Find a case the feature list never named by looking where two features meet.
- Lesson
- B3.1
- Time
- 20 min
- Runs on
- Your browser
- Check
- 5 questions
Five tests, five passes. The ALU adds with and without a carry, subtracts, ANDs, and raises its zero flag when it should. You tick every feature on the list and go home happy. The bug is still there, in a case nobody wrote down.
- Design
- Test
- Storage
- Result
- Control / FSM
- Data
- Read the features
Each line of the feature list names something the ALU must do.
- Write a case per feature
A directed case picks the inputs and works out the answer by hand.
- See where the cases land
Five cases are five points in a space of 262,144 legal inputs.
- See where the bug lives
The lab counts 255 inputs that give a wrong answer. None of the five cases touches them.
Read the diagram as text
- Feature list: ADD, SUB, AND, XOR; carry, borrow, zero
- Five directed cases: each with a hand-worked answer
- All legal inputs: 4 operations x 256 x 256 = 262,144
- Five points checked: all five pass
- 255 wrong points: a sum that wraps to exactly zero
- Feature list → Five directed cases: one case per feature
- Five directed cases → All legal inputs: five points
- All legal inputs → Five points checked: checked
- All legal inputs → 255 wrong points: never visited
An idea drawing. The counts, 262,144 inputs and 255 wrong ones, are measured by the lab below on Icarus.
Before You Start
You should know what a testbench is and how a test decides pass or fail. The Simplest Test That Can Fail and Where the Right Answer Comes From teach both. A little binary arithmetic helps, such as how a carry appears: Half Adders, Full Adders and Ripple Carry.
| Question | |
|---|---|
| 1 | What is a directed test, and where do its cases come from? |
| 2 | What are directed tests good at? |
| 3 | Why does a list of directed cases run out before the bugs do? |
| 4 | Where do the cases that nobody writes down usually hide? |
| 5 | How does a directed case know the right answer? |
The Idea: One Case per Feature
A directed test is the most natural test there is. You read a feature, choose inputs that use it, and work out by hand what the design must answer. The bench applies those inputs and compares. Each case points at one thing on purpose, so a failure tells you at once which feature broke.
That is why every project starts with directed tests. On the first day the design barely works, and you want small, readable checks that light up one feature at a time. Directed tests are also the only sure way to hit an exact value. Think of the largest number, or a carry out of the top bit.
The trouble is size. Even our tiny ALU has 262,144 legal inputs. Five cases visit five of them. Nobody can write cases for the rest, and nobody knows which of the rest hide a bug. So the list of directed cases always ends long before the space does.
New Words
| Word | Meaning |
|---|---|
| ALU | Arithmetic logic unit: a block that adds, subtracts or combines two numbers, picked by an operation code. |
| Directed test | A test whose inputs are chosen by a person, for one feature, with the answer worked out in advance. |
| Bring-up | The first days of testing a new design, when you check that its basic features work at all. |
| Feature list | The things a design must do, read from its specification, one per line. |
| Input space | Every combination of legal inputs a design can receive. For the ALU: 4 operations x 256 x 256. |
| Seam | A place where two features meet, such as a carry and a zero result in the same sum. |
| Carry and borrow | The ninth bit of an 8-bit sum is the carry: 1 when the true sum is 256 or more. A borrow is 1 when a subtraction a - b has b larger than a. |
The Smallest Example: Five Cases by Hand
Here is the ALU's feature list as one table, and a directed case for each line. Work each answer out yourself before you read the last columns. The zero flag z is 1 when the 8-bit result y is 0. The flag c is the carry of ADD or the borrow of SUB.
| Feature | Case | y | c | z |
|---|---|---|---|---|
| ADD with no carry | 3 + 4 | 7 | 0 | 0 |
| ADD with a carry | 200 + 100 = 300 | 44 | 1 | 0 |
| SUB with no borrow | 9 - 4 | 5 | 0 | 0 |
| SUB giving zero | 5 - 5 | 0 | 0 | 1 |
| AND giving zero | F0 & 0F (hex) | 0 | 0 | 1 |
Each line of the table is a feature, and each feature has exactly one case. That looks complete. Now look for a place where two lines of the table meet. ADD can carry, and the result can be zero. Can both happen at once?
The Rules
Rule 1: Start from the feature, not from the code.
A case reads one line of the specification. Cases written from the design's code only repeat what the code already does.
Rule 2: Work the answer out before the run.
The expected value comes from the specification, by hand or by a second method. An answer copied from a run checks nothing.
Rule 3: One feature per case.
Then a failure names the feature that broke, with no digging.
Rule 4: Look for the seams.
Bugs gather where two features meet: a carry and a zero, full and empty, a reset during traffic.
Rule 5: A list is a sample, never the space.
Passing every directed case shows those cases work. It says nothing about the inputs nobody chose.
Sources for These Rules
Directed testing, a test per feature from the plan, and why its effort grows with the design: Spear and Tumbush, section 1.4.
Basic testbench steps (generate, apply, capture, check, measure): Spear and Tumbush, section 1.3.
The Lab: Five Passes and a Count of the Space
The lab at the end of this page runs the five cases on alu8. Then it does something no test can afford on a big design. It tries every legal input and compares each answer with a reference model. Here is how one case is written.
task automatic check(input string name, input logic [2:0] o, input logic [7:0] x, w,
input logic [7:0] want_y, input logic want_c, want_z);
op = o; a = x; b = w;
#1 if (y === want_y && c === want_c && z === want_z) $display("... PASS");
else $display("... FAIL");
endtask
check("ADD 200 + 100", 0, 200, 100, 44, 1, 0); // the carry featurecase 1 ADD 3 + 4: y=7 c=0 z=0 PASS
case 2 ADD 200 + 100: y=44 c=1 z=0 PASS
case 3 SUB 9 - 4: y=5 c=0 z=0 PASS
case 4 SUB 5 - 5: y=0 c=0 z=1 PASS
case 5 AND 240 & 15: y=0 c=0 z=1 PASS
5 of 5 directed cases pass
all 262144 legal inputs: 255 give a wrong answerRule 5 at work. All five directed cases pass, yet the sweep finds 255 legal inputs with a wrong answer. Every one of them is an ADD whose true sum is exactly 256. Add the sixth case, 255 + 1, and it fails: case 6 ADD 255 + 1: y=0 c=1 z=0 FAIL, want y=0 c=1 z=1.
Open the design and look at the zero flag. It tests all nine bits of the sum, carry included, instead of the eight bits of y. Rule 4 found the case; no single feature on the list would have.
Why a Directed List Runs Out
Why not test every input, as the lab did? Because the lab's ALU is tiny. Two 32-bit operands give over eighteen billion billion pairs. Real designs also have memory, so the same input can mean different things at different times. Exhaustive testing works only for the smallest blocks.
So directed tests do the jobs they are best at. They bring a design up, check named features and exact corners, and keep old bugs from coming back. For the rest of the space, the next lesson lets the computer choose the inputs.
Traps
The commonest trap is a test that copies its expected values from the design's own output. Someone runs the ALU, sees 44 and carry 1, and pastes those numbers into the test. The test now passes forever, even when 44 is wrong. Work answers out from the specification, or the test only proves the design agrees with itself.
The second trap is the comfortable tick list. Every feature has a case, every case passes, and the list looks finished. Ask where two features meet before you call it done. In this lab, 255 of the 65,536 ADD inputs would have shipped broken.
The third trap is size. A directed list grows one case at a time, while a design doubles in size from one project to the next. Teams that test only by hand fall behind, however hard they work.
Where This Goes Next
Directed tests stay useful all the way to sign-off. These pages take the idea further, and teach the language behind it.
| The idea from this page | Where it is taught |
|---|---|
| Letting the computer choose inputs | Random and Constrained-Random Stimulus |
| Finding the corners and seams on purpose | Corner Cases and How to Think of Them |
| Tasks such as check, and their arguments | Tasks, Functions and Arguments |
| A test plan for a FIFO, feature by feature | Start From the FIFO Rules |
| The test family of a UVM bench | One Environment, a Family of Tests |
Try It
Run it: five directed cases, then every legal input compared with the reference model.
Add a sixth case at the end of the list: check("ADD 255 + 1", 0, 255, 1, 0, 1, 1);. Predict its line before you run it.
Fix the zero flag in alu8 so it looks at y only. How many wrong inputs does the sweep count now?
case 1 ADD 3 + 4: y=7 c=0 z=0 PASS case 2 ADD 200 + 100: y=44 c=1 z=0 PASS case 3 SUB 9 - 4: y=5 c=0 z=0 PASS case 4 SUB 5 - 5: y=0 c=0 z=1 PASS case 5 AND 240 & 15: y=0 c=0 z=1 PASS 5 of 5 directed cases pass all 262144 legal inputs: 255 give a wrong answerCheck Yourself
Common Mistakes
Wrong Copy the expected values from a run of the design.
The test then agrees with whatever the design does, bugs included.
Fix Work each expected value out from the specification before the run.
Wrong Stop when every feature on the list has one passing case.
Bugs gather where features meet, and the list names features one at a time.
Fix Add cases for the seams: a carry with a zero, full with empty, reset during traffic.
Wrong Plan to test a real design by directed cases alone.
The input space is far too large, and the list grows only as fast as people write it.
Fix Use directed cases for bring-up and exact corners, and let random stimulus cover the space.
Remember
- A directed test applies inputs chosen for one feature and compares with an answer worked out from the specification.
- Directed tests are best for bring-up, named features, exact corners and bugs that must not return.
- A list of cases samples the input space; the lab passed five cases while 255 legal inputs were wrong.
- Bugs gather at seams, where two features meet in one case.