Exercise 1.11 - Test Word count program

Question

How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?

Program

#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
    int c, nl, nw, nc, state;
    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF) {
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT) {
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}
Run this

Explanation

Testing the word count program involves, giving three kinds of inputs.

  1. Valid Inputs.
  2. Boundary Condition Inputs.
  3. Invalid Inputs.

For Valid Inputs, it could be any stream of space separate text. It has valid space, newline and tab characters. For Boundary conditions, a file entirely consisting of n, or a file entirely consisting of t character or a empty file.

For invalid Inputs, an unclosed file which does not have EOF, which is tricky to provide can be given to this program. A unicode character file can be given and see if getchar() handles it properly. We tested it and it works.

Comments by Disqus