82 Intermediate C Programming
• The value to search is an element of the array, somewhere in the middle of the array.
• The value is not an element of the array but between some elements in the array.
• The value is the same as the first element.
• The value is the same as the last element.
• The value is smaller than all elements.
• The value is larger than all elements.
• The array has only one element and the value is the same as this only element.
• The array is empty and the value is irrelevant.
Why is it necessary to test these different cases? Depending on the search algorithm and
how the algorithm is implemented, one test case may fail to detect any problem. Creating
good test inputs is not trivial. A different approach is called formal verification by proving a
program is correct regardless of inputs. This is an advanced topic and will not be discussed
here.
It is important to understand the limitation of test coverage. Low coverage means that
the test inputs need improvement. However, high coverage is not necessarily better. A good
test input is one that can detect problems in your programs. A simple program like the
following can get 100% coverage:
#in clude < stdio .h >1
#in clude < stdlib .h >2
int main ( i n t argc , char * argv [])3
{4
return EXIT _SUCCES S ;5
}6
This program does not do anything. Pursuing high coverage should not be a goal in
itself. The goal should be detecting and fixing problems.
It is necessary to further explain the limitations of testing. Some students believe that
their programs are correct if the programs pass all test cases given by their professors. This is
wrong for a very simple reason: It is difficult, almost impossible, to test all possible scenarios.
Every if condition in a program creates two possible scenarios. Studies show that an if
condition appears approximately every 10 to 15 lines of code (excluding comments). If your
program has 15,000 lines, there are approximately 1,000 if conditions and 2
1000
possible
scenarios. How large is this number? The fastest computer in the world can perform about
50 × 10
15
(2
55
) operations per second. Testing 2
1000
scenarios would simply be impossible.
5.6 Limit Core Size
In some cases, invalid memory accesses will cause a “core dumped” message. The core
file is an old way to debug programs. Even though some people still use “core” to debug
their programs, this book does not teach how to do that. A core file can occupy a lot of
space on your disk. Use the following command to find whether a core file exists.
$ cd
$ find . -name ”core”
The first command returns to your home directory. The second command finds any core
file. Core files can be deleted by using the following command:
$ rm ‘find . -name ”core”‘