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.

A List of Cases Against the Space of All CasesStep through. A feature list becomes a few hand-picked cases. Each case is one point in a space of 262,144 legal inputs, and the bug sits somewhere nobody pointed.
  • Design
  • Test
  • Storage
  • Result
  • Control / FSM
  • Data
A List of Cases Against the Space of All CasesStep through. A feature list becomes a few hand-picked cases. Each case is one point in a space of 262,144 legal inputs, and the bug sits somewhere nobody pointed. design Feature list ADD, SUB, AND, XOR;carry, borrow, zero test Five directedcases each with ahand-worked answer storage All legal inputs 4 operations x 256 x256 = 262,144 result Five pointschecked all five pass control 255 wrong points a sum that wraps toexactly zero one case per featurefive pointscheckednever visited
  1. design Feature listADD, SUB, AND, XOR; carry, borrow, zero
    • one case per feature → Five directed cases
  2. test Five directed caseseach with a hand-worked answer
    • five points → All legal inputs
  3. storage All legal inputs4 operations x 256 x 256 = 262,144
    • checked → Five points checked
    • never visited → 255 wrong points
  4. result Five points checkedall five pass
  5. control 255 wrong pointsa sum that wraps to exactly zero
  1. Read the features

    Each line of the feature list names something the ALU must do.

  2. Write a case per feature

    A directed case picks the inputs and works out the answer by hand.

  3. See where the cases land

    Five cases are five points in a space of 262,144 legal inputs.

  4. 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.

Five questions this page answers
Question
1What is a directed test, and where do its cases come from?
2What are directed tests good at?
3Why does a list of directed cases run out before the bugs do?
4Where do the cases that nobody writes down usually hide?
5How 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

New words on this page
WordMeaning
ALUArithmetic logic unit: a block that adds, subtracts or combines two numbers, picked by an operation code.
Directed testA test whose inputs are chosen by a person, for one feature, with the answer worked out in advance.
Bring-upThe first days of testing a new design, when you check that its basic features work at all.
Feature listThe things a design must do, read from its specification, one per line.
Input spaceEvery combination of legal inputs a design can receive. For the ALU: 4 operations x 256 x 256.
SeamA place where two features meet, such as a carry and a zero result in the same sum.
Carry and borrowThe 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.

Five directed cases for the 8-bit ALU
FeatureCaseycz
ADD with no carry3 + 4700
ADD with a carry200 + 100 = 3004410
SUB with no borrow9 - 4500
SUB giving zero5 - 5001
AND giving zeroF0 & 0F (hex)001

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

  1. 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.

  2. 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.

  3. Rule 3: One feature per case.

    Then a failure names the feature that broke, with no digging.

  4. Rule 4: Look for the seams.

    Bugs gather where two features meet: a carry and a zero, full and empty, a reset during traffic.

  5. 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.

One directed case. string holds text; === compares exactly, so an unknown bit counts as different.
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 feature
Printed by the lab (Icarus Verilog version 12.0).
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 answer

Rule 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.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Letting the computer choose inputsRandom and Constrained-Random Stimulus
Finding the corners and seams on purposeCorner Cases and How to Think of Them
Tasks such as check, and their argumentsTasks, Functions and Arguments
A test plan for a FIFO, feature by featureStart From the FIFO Rules
The test family of a UVM benchOne 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 answer

Check Yourself

Predict the outputThe lab now has a sixth directed case, ADD 255 + 1, with y = 0, c = 1, z = 1 expected. What is the last line it prints?
// alu8: an 8-bit ALU. op: 0 ADD, 1 SUB, 2 AND, 3 XOR; 4 to 7 are reserved.
// c is the carry out of ADD or the borrow of SUB. z is 1 when y is 0.
module alu8 (input logic [2:0] op, input logic [7:0] a, b,
             output logic [7:0] y, output logic c, z);
  logic [8:0] r;                    // nine bits: the result and its carry
  always_comb begin
    case (op)
      3'd0: r = a + b;
      3'd1: r = a - b;
      3'd2: r = {1'b0, a & b};
      3'd3: r = {1'b0, a ^ b};
      default: r = 9'd0;
    endcase
  end
  assign y = r[7:0];
  assign c = r[8];
  assign z = (r == 9'd0);       // the zero flag
endmodule

module tb;
  logic [2:0] op; logic [7:0] a, b, y; logic c, z;
  int pass = 0, n = 0, wrong = 0;
  alu8 dut (.op(op), .a(a), .b(b), .y(y), .c(c), .z(z));
  // One directed case: inputs from the feature list, the answer worked out by hand.
  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; n++;
    #1 if (y === want_y && c === want_c && z === want_z) begin pass++; $display("case %0d %s: y=%0d c=%0d z=%0d  PASS", n, name, y, c, z); end
    else $display("case %0d %s: y=%0d c=%0d z=%0d  FAIL, want y=%0d c=%0d z=%0d", n, name, y, c, z, want_y, want_c, want_z);
  endtask
  // The reference model: the answer the specification gives, worked out a second way.
  function automatic logic [9:0] model(input logic [2:0] op, input logic [7:0] a, b);
    logic [7:0] y; logic c;
    case (op)
      3'd0: begin y = a + b; c = (a + b) > 255; end
      3'd1: begin y = a - b; c = a < b; end
      3'd2: begin y = a & b; c = 0; end
      default: begin y = a ^ b; c = 0; end
    endcase
    return {y, c, y == 0};
  endfunction
  initial begin
    check("ADD 3 + 4", 0, 3, 4, 7, 0, 0);
    check("ADD 200 + 100", 0, 200, 100, 44, 1, 0);
    check("SUB 9 - 4", 1, 9, 4, 5, 0, 0);
    check("SUB 5 - 5", 1, 5, 5, 0, 0, 1);
    check("AND 240 & 15", 2, 240, 15, 0, 0, 1);
    check("ADD 255 + 1", 0, 255, 1, 0, 1, 1);
    $display("%0d of %0d directed cases pass", pass, n);
    // Every legal input, compared with the model: how big is the space the list sampled?
    for (int o = 0; o < 4; o++)
      for (int x = 0; x < 256; x++)
        for (int w = 0; w < 256; w++) begin
          op = o; a = x; b = w;
          #1 if ({y, c, z} !== model(op, a, b)) wrong++;
        end
    $display("all 262144 legal inputs: %0d give a wrong answer", wrong);
  end
endmodule
ConceptWhere does a directed case get its expected value?
ConceptAll five directed cases pass, but the sweep finds 255 wrong inputs. What does that show?
ConceptWhich job suits directed tests best?
ConceptThe sixth case 255 + 1 fails. Why did the feature list not lead anyone to it?

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.

Practice

Report a problem with this page