Code Coverage: What Ran

Measure which parts of the design's code your tests ran: lines, branches, toggles and states. See why every hole matters, and why a perfect score still says nothing about what was never written.

You Will Be Able To

  • Say what line, branch, toggle and state coverage each count.
  • Read a coverage report and find the parts of the design no test reached.
  • Explain why 100 percent code coverage is necessary and never sufficient.
  • Name a bug that full code coverage cannot see: one in code that is missing.
Lesson
B3.5
Time
20 min
Runs on
Your browser
Check
5 questions

The coverage report is green from top to bottom. Every line of the counter ran, every branch went both ways, every bit rose and fell. Then a test presses up and down together, and the counter climbs when it should hold still.

Source Lines Coloured by Whether They RanStep through. A coverage tool counts how often each piece of the design's code ran during the tests. Four kinds of count look at the same code from four sides.
  • Design
  • Result
  • Control / FSM
  • Data
Source Lines Coloured by Whether They RanStep through. A coverage tool counts how often each piece of the design's code ran during the tests. Four kinds of count look at the same code from four sides. design Design source sat_counter, fivelines of logic result Line coverage did each statementrun? result Branch coverage did each if and else goboth ways? result Toggle coverage did each bit rise andfall? result State coverage was each state of astate machinevisited? control What none ofthem see code that is missing countscountscountsfor state machinesstill blind to
  1. design Design sourcesat_counter, five lines of logic
    • counts → Line coverage
    • counts → Branch coverage
    • counts → Toggle coverage
  2. result Line coveragedid each statement run?
  3. result Branch coveragedid each if and else go both ways?
    • for state machines → State coverage
  4. result Toggle coveragedid each bit rise and fall?
  5. result State coveragewas each state of a state machine visited?
    • still blind to → What none of them see
  6. control What none of them seecode that is missing
  1. Count the lines

    Every statement gets a counter. A line that never ran is a part of the design no test touched.

  2. Count the branches

    Every if and else, and every choice of a case statement, gets its own counter.

  3. Count the toggles

    Every bit of every signal: has it gone from 0 to 1, and from 1 to 0?

  4. Count the states

    For a state machine, every state and every move between states.

  5. Know the limit

    All four count code that exists. A line the designer forgot has no counter at all.

Read the diagram as text
  • Design source: sat_counter, five lines of logic
  • Line coverage: did each statement run?
  • Branch coverage: did each if and else go both ways?
  • Toggle coverage: did each bit rise and fall?
  • State coverage: was each state of a state machine visited?
  • What none of them see: code that is missing
  • Design source → Line coverage: counts
  • Design source → Branch coverage: counts
  • Design source → Toggle coverage: counts
  • Branch coverage → State coverage: for state machines
  • State coverage → What none of them see: still blind to

An idea drawing. The counts in the lab come from a real coverage run, below.

Before You Start

Read Corner Cases and How to Think of Them first. A counter that stops at its ends is a refresher in Modulo-N, Up/Down and Loadable Counters.

Five questions this page answers
Question
1What do line, branch, toggle and state coverage count?
2Who decides what code coverage measures: you or the tool?
3What does a hole in code coverage tell you?
4Why is 100 percent code coverage never enough?
5Which kind of bug can no code count see?

The Idea: A Map of What Ran

Every test you write runs some of the design's code and skips the rest. You cannot see which, by reading the tests. So simulators can count, for every line and branch of the design, how often it ran. The report is a map of the design coloured by use, and the pale parts are places no test has been.

This is code coverage. The tool builds it from the design's source, with no work from you. That is its great strength: it finds the forgotten corner automatically. If the reset branch never ran, or one bit of a register never rose, the report says so.

It is also its limit. Code coverage measures the design against itself, not against the specification. It can tell you a line ran. It cannot tell you the line is right, and it cannot count a line that should exist and does not.

New Words

New words on this page
WordMeaning
Code coverageCounts, made by a tool from the design's source, of which parts of the code ran during the tests.
Line coverageWhether each statement ran at least once.
Branch coverageWhether each way out of every if and else was taken, and each choice of every case statement.
Toggle coverageWhether each bit of each signal went from 0 to 1 and from 1 to 0.
State machineLogic that is always in one of a few named states and moves between them on clock edges, like a traffic light.
State coverageWhether each state of a state machine, and each move between states, was visited.
HoleA part of the design that no test reached: a count of zero.

The Smallest Example: Count the Branches by Hand

The design is sat_counter: it counts up or down and stops at 0 and at 15. The specification adds one rule: up and down together leave the count where it is. The test below counts up 17 times, down 17 times, then idles once. Which branches run?

The counter's branches under the test
BranchRuns whenRuns in this test?
rstreset is highyes, at the start
up and not at 15counting upyes, 15 times
down and not at 0counting downyes, 15 times
none (hold)at the top, at the bottom, or idleyes

Every branch runs. Every bit of the count goes from 0 to 1 and back. Now look for the specification's rule about up and down together. Which line of the design handles it?

The Rules

  1. Rule 1: Code coverage comes from the code.

    The tool counts the design's own statements, branches and bits. You do not write it, and it knows nothing of the specification.

  2. Rule 2: Every hole needs an answer.

    A part of the design no test reached is untested. Write a test for it, or show it cannot happen.

  3. Rule 3: 100 percent is necessary.

    Code that never ran was never checked, so a project needs every hole answered.

  4. Rule 4: 100 percent is never sufficient.

    A line that ran may still be wrong, and a line that is missing has no counter at all.

  5. Rule 5: Read coverage with checks.

    Coverage says a line ran. Only a check says the result was right.

Sources for These Rules

Code coverage measures design structure and is derived by the tool. It does not cover the intent of the design: Mehta, section 7.1.

Line, branch and toggle points as reported by Verilator 5.052 (--coverage-line, --coverage-toggle, verilator_coverage --annotate).

The Lab: Every Line Ran, and the Bug Is Still There

First, a real coverage tool. The same counter and test ran on Verilator with line, branch and toggle coverage switched on. Here is the design's source with the count beside each line.

Line and branch counts for sat_counter (Verilator 5.052, recorded)
Source lineCountWhat ran
always_ff @(posedge clk)37every clock edge
if (rst) cnt <= 0;1reset
else if (up && cnt != 15) cnt <= cnt + 1;15counting up
else if (down && cnt != 0) cnt <= cnt - 1;15 taken, 6 not takencounting down, or holding

Each count is the tool's own. The lab's hand counters below see one more hold edge than this report. What matters in both is that no count is zero.

Every count is above zero. The toggle report has 16 points: clk, rst, up, down and the four bits of cnt, each rising and falling. Every one was hit. Rules 1 and 3 at work: the tool built this map from the code, and the map is complete.

The same test without the final idle cycle left one hole: down:1->0, with a count of 0. The test ended with down still high, so down never fell. That hole was real and cheap to close. Rule 2 at work.

Icarus, which runs the lab in this page, has no coverage tool. So the lab counts its branches by hand, with one counter per branch, the way a coverage tool would. It also checks every cycle against the specification:

Printed by the lab (Icarus Verilog version 12.0).
branch rst                    ran 1 times
branch up and not at 15       ran 15 times
branch down and not at 0      ran 15 times
branch none of these (hold)   ran 6 times
bits of cnt seen at 1: 1111, at 0: 1111
checks: 0 errors

Full coverage, zero errors. Now add three cycles of up and down together, and the checks report checks: 3 errors. Rule 4 at work: the design has no line for that rule, so up wins. No count anywhere in the report could have warned you.

Why a Measure with a Blind Spot Is Still Worth Having

Why use a measure that misses whole classes of bug? Because it costs almost nothing and finds what people forget. A team never decides to skip the error branch. It just never writes the test, and code coverage catches that every time.

The missing half comes from the specification, not from the code. Functional coverage, the next lesson, is written by you from the plan. It counts the situations that should happen, whether or not a line exists for them. A project needs both.

Traps

The first trap is treating the number as the goal. A team chases the last few percent with tests that run lines but check nothing. Coverage rises, and no bug is found. A test that runs a line without checking its result adds a count and no confidence.

The second trap is waving holes away. Some code truly cannot run, such as a branch for a value the inputs can never take. Mark it and say why, in writing, so the next reader can agree. A hole excused without a reason is often a missing test.

The third trap is the one in this lab: full coverage on a design that forgot a rule. No green report can see code that was never written. Only a check from the specification, driven by the right stimulus, finds it.

Where This Goes Next

Code coverage is the automatic half of measuring. The Coverage track teaches the half you write yourself.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Counting the situations the plan asks forFunctional Coverage: What Was Tested
Code coverage beside functional coverageWhat Coverage Measures
Closing holes in a coverage reportClosing Holes
Code that can never run, found by a formal toolUnreachability and Coverage Closure
Counters that stop at their ends, in logicModulo-N, Up/Down and Loadable Counters

Try It

Run it: the counter goes up past 15 and down past 0. A counter sits on each branch, and a check runs every cycle.

Add three cycles of up and down together before the quiet cycle: repeat (3) cycle_with(1, 1);. Predict the last line.

Which branch counter changes when you add those cycles, and does any counter stay at zero?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: branch rst ran 1 times branch up and not at 15 ran 15 times branch down and not at 0 ran 15 times branch none of these (hold) ran 6 times bits of cnt seen at 1: 1111, at 0: 1111 checks: 0 errors

Check Yourself

Predict the outputThe test now adds three cycles of up and down together before the quiet cycle. What is the last line it prints?
// sat_counter: counts up or down, and stops at 0 and at 15.
// up and down together leave the count where it is.
module sat_counter (input logic clk, rst, up, down, output logic [3:0] cnt);
  always_ff @(posedge clk)
    if (rst)                       cnt <= 0;
    else if (up && cnt != 15)    cnt <= cnt + 1;
    else if (down && cnt != 0)     cnt <= cnt - 1;
endmodule

module tb;
  logic clk = 0, rst = 1, up = 0, down = 0;
  logic [3:0] cnt, want = 0, seen_one = 0, seen_zero = 0;
  int hits [4];                     // one counter per branch of the design
  string names [4];
  int errors = 0;
  sat_counter dut (.clk(clk), .rst(rst), .up(up), .down(down), .cnt(cnt));
  always #5 clk = ~clk;
  // Count which branch each edge takes, the way a coverage tool counts lines for you.
  always @(posedge clk)
    if (rst) hits[0]++;
    else if (up && cnt != 15) hits[1]++;
    else if (down && cnt != 0) hits[2]++;
    else hits[3]++;
  // Toggle coverage: has each bit of cnt been seen at 1 and at 0?
  always @(negedge clk) begin seen_one |= cnt; seen_zero |= ~cnt; end
  // The check: the specification, worked out in the bench.
  task automatic cycle_with(input logic u, input logic d);
    @(negedge clk) begin up = u; down = d; end
    if (!(u && d)) begin
      if (u && want != 15) want++;
      else if (d && want != 0) want--;
    end
    @(posedge clk) #1 if (cnt !== want) begin errors++; want = cnt; end   // count a wrong step once
  endtask
  initial begin
    names[0] = "rst";
    names[1] = "up and not at 15";
    names[2] = "down and not at 0";
    names[3] = "none of these (hold)";
    @(posedge clk); @(negedge clk) rst = 0;
    repeat (17) cycle_with(1, 0);   // up to 15, and two more at the top
    repeat (17) cycle_with(0, 1);   // down to 0, and one more at the bottom
    repeat (3) cycle_with(1, 1);    // up and down together
    cycle_with(0, 0);               // one quiet cycle at the end
    for (int i = 0; i < 4; i++) $display("branch %-22s ran %0d times", names[i], hits[i]);
    $display("bits of cnt seen at 1: %b, at 0: %b", seen_one, seen_zero);
    $display("checks: %0d errors", errors);
    $finish;
  end
endmodule
ConceptWho decides what code coverage counts?
ConceptToggle coverage reports down:1->0 with a count of 0. What does that mean?
ConceptWhy is 100 percent code coverage never sufficient?
ConceptWhich bug can full code coverage never point at?

Common Mistakes

Wrong Take 100 percent code coverage as proof the design works.

Coverage counts what ran, not whether it was right, and cannot count missing code.

Fix Read coverage beside checks, and add functional coverage from the specification.

Wrong Add tests that run lines without checking anything.

The number rises and no bug can be found.

Fix Every test that closes a hole must also check the result.

Wrong Wave a hole away as unreachable without a reason.

Most such holes are missing tests.

Fix Write a test, or record in writing why the code cannot run.

Remember

  • Code coverage counts which lines, branches, bits and states of the design ran.
  • The tool builds it from the code with no work from you; every hole needs an answer.
  • 100 percent is necessary and never sufficient: a line can run and be wrong.
  • Missing code has no counter; only checks from the specification find it.

Practice

Report a problem with this page