CSC161 2010F Imperative Problem Solving

Assignment 6: A Testing Library

Assigned: Friday, 15 October 2010
Due: 11:00 p.m., Wednesday, 27 October 2010

This assignment is also available in PDF.

Summary: In this assignment, you will build a library of macros that you can use to test the procedures that you write.

Purposes: To give you further experience with testing. To give you experience writing macros. To give you practice with various aspects of C programming.

Expected Time: Two to three hours.

Collaboration: I encourage you to work in groups of two or three students. However, you may work on your own or in groups of up to size four. You may discuss the assignment with anyone you wish, provided you clearly document such discussions.

Submitting: Email me a tarball of your important files (your .c files, your .h files, your Makefile, and anything else you deem appropriate).

Warning: So that this assignment is a learning experience for everyone, I may spend class time publicly critiquing your work.

Testing

As you no doubt have noted by this time, our strategy for testing is fairly standardized.

Since that code is so similar, it makes sense to set up a library to help us handle it. However, there's one difficulty: In order to print an appropriate error message, it's useful to know the text of the call. (It may also be useful to know the result, but that's an issue that we can also deal with in the debugger.)

We came up with the following macro for that approach.

#define TEST_EQUAL(EXP,RESULT) if ((EXP) != RESULT) { ++errors; printf ("Did not get expected result for %s.\n", #EXP); }

For example, we might write

  TEST_EQUAL (square (5), 25)

Now it's time to incorporate the TEST_EQUAL macro into something we can use throughout our programming.

Assignment

Part 1: A Header File

a. Create a header file, test.h, that declares errors and the macro above. (Or a corrected version of that macro.) You will also need a macro for the end of main. Call that macro TESTS_FINISHED.

b. Verify to your satisfaction that the header file works appropriately.

Note that you should make sure to test some of the more complex ways we now build C programs. For example, what happens if we have three .c files, one of which defines a function testA(), that does some tests, the second of which defines testB(), which does some more tests, and the third of which defines main(), which calls testA() and testB(), and all three of the the files include test.h?

Hint: You may have to read about modifiers for variables to learn how to handle situations like this.

Part 2: A Better Header File

Update the definition of TEST_EQUAL so that it prints the line number and file of the error. (You'll need to look at the manual for the GNU C Preprocessor to figure out how to access that information.)

Part 3: Checking for Primes

Consider a function, is_prime (int n), that determines whether or note n is prime.

a. Write a header file, primes.h, that declares is_prime.

b. Using test.h to help, write a program that experimentally verifies whether or not is_prime works correctly.

c. Write a code file, primes.c, that defines is_prime.

If you've written good enough tests, you should be confident that your implementation of is_prime is correct.

Part 4: Fibonacci Numbers

As you may recall, the Fibonacci sequence is defined as follows:

a. Write a header file, fibonacci.h, that declares a function, int fib (int n), that computes the nth Fibonacci number.

b. Using test.h to help, write a program that experimentally verifies whether or not fib works correctly.

c. Write a code file, fibonacci.c, that defines fib.

If you've written good enough tests, you should be confident that your implementation of fib is correct.

Part 5: Is it a Fibonacci number?

a. To fibonacci.h add a declaration for a function, is_fib (int n), that determines whether or not n is an element of the standard Fibonacci sequence.

b. Using test.h to help, write a program that experimentally verifies whether or not is_fib works correctly.

c. Add a definition of is_fib to fibonacci.c.

If you've written good enough tests, you should be confident that your implementation of is_fib is correct.

 

History

Saturday, 23 October 2010 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Sat Oct 23 21:00:31 2010.
The source to the document was last modified on Sat Oct 23 21:00:30 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Assignments/assignment.06.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu