# Chapter 22. Control-Flow Testing Exercise

It’s time to apply the white-box control-flow testing concepts you’ve learned to a simple program. In Listing 22-1, you’ll find a simple C program that converts hexadecimal character input into an integer. It displays the number of hexadecimal characters converted and their value (in hexadecimal). It ignores nonhexadecimal characters. This program will run on a Windows or Linux system if you have a compiler available to you.

The exercise is in three parts:

1. Understand the number of potential test cases.

2. Create a set of test cases for full statement, branch, condition, and loop coverage.

3. Calculate the McCabe complexity, write the basis paths, and create the basis tests.

I suggest 30 minutes as a time limit. When you’re done, keep reading to see my solution.

Example 22-1. Hex converter program

`1. main() 2. /* Convert hex digits to a number */ 3. { 4. int c; 5. unsigned long int hexnum, nhex; 6. 7. hexnum = nhex = 0; 8. 9. while ((c = getchar()) != EOF) { 10. switch (c) { 11. case '0': case '1': case '2': case '3': case '4': 12. case '5': case '6': case '7': case '8': case '9': 13. /* Convert a decimal digit */ 14. nhex++; 15. hexnum *= 0x10; 16. hexnum += (c - '0'); 17. break; 18. case 'a': case 'b': case 'c': 19. case 'd': case 'e': case 'f': 20. /* Convert a lower case hex digit */ 21. nhex++; 22. hexnum *= 0x10; 23. hexnum += (c - 'a' + 0xa); 24. break; 25. case 'A': case 'B': case 'C': 26. case 'D': case 'E': case 'F': 27. /* Convert an upper ...`

Get Pragmatic Software Testing: Becoming an Effective and Efficient Test Professional now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.