The following summarizes some of what we discussed in lecture on Fri 1/24/2020.
Going forward, notes will usually be posted as scanned handwritten pages.
HW02 using Test-Driven Development (TDD)
- Set up files
- Open hw02.h from bash (vim hw02.h).
- Create hw02.c in a vertical split (:vs hw02.c) and add
headers at the top (#include <stdio.h> ⋯ #include
<stdlib.h> ⋯ #include "hw02.h").
Save (:w).
- Create hw02test.c in a horizontal split below hw02.c (:split
hw02test.c) and add the same headers at the top. Save (:w).
- Create an empty hw02test.txt in a horizontal split below hw02.h
(:split hw02test.txt). Save (:w).
- Save everything.
- TDD (simplified) general pattern for HW02:
- ① Create test code in hw02test.c.
- ② Add expected output to hw02test.txt.
- ③ Implement just enough of the target code in hw02.c to pass all
test(s).
- ④ Compile and test.
- ⑤ Debug, as needed.
- ⑥ Repeat.
- Step 0: Empty test
- Setup left you with everything you need, including…
- ① Create an empty main(…) in hw02test.c.
- ② Leave hw02test.txt empty for now.
- ③ Create an empty print_integer(…) in hw02.c. You can copy (yy)
and paste (p) from hw02.h and then change the semicolon
to curly braces (/; s{…}).
- ④ Compile and test. ⋯ gcc -o hw02test hw02.c hw02test.c
⋯ diff <(./hw02test) hw02test.txt
- ⑤ No output indicates success.
- Step 1: One particular single-digit number in base 10.
- ① Add test code for a single digit in base 10 to main(…). ⋯ print_integer(1,
10, "") ⋯ fputc('\n', stdout)
- ② Add expected test output to hw02test.txt to hw02test.txt. ⋯
1
- ③ Add implementation code. It can be trivial at this point. ⋯
fputc('1', stdout)
- ④ Compile and test. ⋯ gcc -o hw02test hw02.c hw02test.c
⋯ diff <(./hw02test) hw02test.txt
- ⑤ No output indicates success.
- Step 2: Any non-negative single-digit number in base 10.
- Step 3: + any base
- Step 4: + multiple digits
- Step 5: + negative numbers
- Step 6: + single-character prefix
- Step 7: + multi-character prefix
- Test edge cases
- Test corner cases
Resources: The Wikipedia page on test-driven
development (TDD) has a great overview in the introduction. Our
simplified TDD is explained in more detail in 16au HW03,
Overview of the files
- Test code. hw02test.c will call your print_integer(…)
in various ways to test if it works correctly. This is only useful
for testing.
- Implementation code. hw02.c will contain the code people
care about. You could imagine other people using this file in their
programs, like a library.
- Expected output. hw02test.txt contains the output that
should result from running hw02test.c, if hw02.c is implemented
correctly.
- Header file. hw02.h contains declarations of each function
(in this case just one). This declares to the compiler what the
function returns (void) and the types of each parameter (int, int,
char*).
Pitfalls.
- Don't use an array. You must find a way to print digits left to
right. Reason: Your array declaration would entail assumptions
about the size of an int, which is not allowed.
- Use char constants (e.g., '0') not int constants (e.g., 48). In
general, int constants should only be used to indicate the “number
of {something}”.
- Watch out for edge cases.
- GCC error: “implicit declaration of function…” → Probably missing
a header file (i.e., #include "hw02.h") or a forward declaration
(for a helper function).
- GCC error: “passing argument 1 of ‘fputc’ makes integer from…”
and “expected ‘int’ but argument is of type ‘char *’” → Got a string
literal (e.g., "0") where a char constant (e.g., '0') was expected.
Solution: Check your use of single and double quotes.
- Single quotes ('▒') are for char constants; double quotes ("▒")
are for string literals.
- #include <▒▒▒.h> for standard headers (that come with GCC).
#include "▒▒▒.h" for your own header files (in your code directory).
- Diff error: “No newline at end of file” → Text file has a hidden
newline at the end of the last line. Solution: Add fputc('\n',
stdout) at the end of your main(…).
Commands
- diff <(./hw02test) hw02test.txt compares the output of hw02test
with the contents of hw02test.txt. It is a little less typing than
what is in the assignment page.
- gcc -o hw02test hw02.c hw02test.c && diff <(./hw02test)
hw02test.txt ⋯ compiles and tests in one command.
- Type gc{up arrow key} from bash to autocomplete the entire line
from your bash command history.
© Copyright 2020 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.