Advanced C Programming

Autumn 2016 :: ECE 264 :: Purdue University

This is for Fall 2016 (5 years ago)
Due 9/2

Test-driven development


This assignment has the following goals:

  1. Learn how to tackle a programming assignment incrementally.
  2. Learn the basics of test-driven development
  3. Prepare to do HW04.


In this class—and virtually all future programming courses—the only viable way to build up a project is to build it incrementally, while testing and re-testing your implementation at every step. For many students, it can take a long time to learn this valuable lesson.

This is a short assignment, but an important one. If you give this some thought, and then apply it to HW04 and all future assignments, you will find that programming—and ECE 264 as a whole—is a lot more fun.

How to spend less time debugging (and more time enjoying the sunshine)

Let's illustrate t by example. Consider two approaches to doing HW04:

Student X and Student Y spend the same amount of time reading, coding, and making tests. The difference is that if Student Y has a bug, it must have been introduced no more than about 10 minutes ago, since the code is never broken for more than about 10 minutes at a time. When Student X has a bug, it could be practically anywhere, so each bug takes much longer to diagnose and fix.

Although this is just an illustration, it is based on our observations of students in past semesters, our own experiences, and a well-established software engineering process. Our version of TDD is simplified, but the core process is the same.

Test-driven development

To begin, first create an empty test code file (e.g., a main(…) with just one line: return EXIT_SUCCESS;) and an empty output file (e.g., empty text file). Make sure your testing process is working by simply running the test code file and verifying that the output matches the test output file. (Commands were given in HW02.) Then, proceed as follows:
  1. Create test. Choose the smallest/easiest piece of functionality that you can add to your implementation file (mintf.c). Add a test to your test code file (test_mintf.c) and the expected output in your test output file (test_mintf.txt).
  2. Add code. Write just enough code so that the new test passes. Your impelementation won't be complete yet, but everything you've written so far should work.
  3. Test. Check that your code now passes all of the tests written so far. If anything doesn't work, fix it before you go on. At the end of each cycle, 100% of your code should be working, even though some parts for other features may not be written yet.


Read the instructions for HW04 before you proceed.

Create a file called plan.c with only a main(…) function.

In the main(…) function, write out a series of at least 10 stages that you will use to build your HW04 incrementally using TDD. Each stage must be in the following form:

// Test ##

These stages will trace the steps from beginning your HW04 to finishing it. Each stage adds a small amount of required functionality that was not present in previous stages.

Later, when you implement your code for HW04, your code should never be broken for more than 10-15 minutes at a time, because you will be writing it in very small increments. After each stage, you will get your code completely working—for that subset of the functionality.

Assume that you are starting with a working print_integer(…). Thus, you do not need to have steps for testing internal aspects of print_integer(…). You are planning the development of your mintf(…) function for HW04.

More detailed criteria are given below under Requirements.

Note: Your plan.c will not be compiled or run by us. Instead, it is just serving as a sort of document for you to communicate your planned stages to us. Yes, this is weird.

Definitions: For purposes of these instructions, “implementation code” means your mintf.c; “test code” means your test_mintf.c; “test output” means your test_mintf.txt; and “test” means a small section of your plan.c (most likely in the main(…) and the corresponding snippet in your test_mintf.txt, which together can be used to verify that some aspect of your program works according to the specification.


  1. One file is required: plan.c
  2. Your plan.c must contain a main(…) containing ≥10 stages meeting the following criteria:
    1. Stage 00 is an empty test to check your testing framework. It should always pass.
    2. Tests for all stages from 0 to n pass with the implementation for stage n. In other words, the changes you make in stage i will not require breaking any of the tests associated with previous stages.
    3. Implementation code in stage i would fail the tests for stages i<1.
    4. Implementation code in stage i+1 will be different from code in stage i.
    5. Implementation code in stage n+1 will add required functionality that was not in stage n.
    6. Implementation code in stage n+2 has more sloc* than implementation code for stage n.
      * sloc = “source lines of code” (excluding comments and blank lines)
    7. Altogether, the tests will comprehensively test your final implementation of mintf(…) for HW04. In other words, any implementation of mintf(…) that passes all of your tests must be working perfectly.
  3. Each stage must be in the following format:
    // Test ##
    // Expect: "EXPECTED_OUTPUT"
  4. Special for this assignment:
    1. You may copy anything in the screenshot, including stages 00 and 01. The rest of your test cases must be your own.
    2. Your code does not need to compile or run for this assignment (HW03).

While these requirements may initially sound complex, they are not. If you are following the spirit of the TDD method, all of this will almost certainly happen naturally.


This assignment will be human-graded. Scores will be based on how well your steps follow the requirements.


Submit using the usual command. (See the previous assignments and printed on your reference sheet.)

How much work is this?

This assignment is expected to take 1-2 hours for most students, but you may find that it takes more or less. You will need to read the assignment description for HW04 and think creatively about how you will tackle it.