TDD In C - Programvareverkstedet

Transcription

(frontpage)TDD in C. or The Bowling Game Kata with C and assert()Olve Maudal , oma@pvv.org(a 10 minute lightening-talk @ Smidig 2007 Oslo, 26-27 November)Olve MaudalTDD in CNovember 2007

Bowling Game KataOlve MaudalTDD in CNovember 2007

Bowling Game Kata in CThe following is a demonstration of how you can do test-driven development in plain C,without any bloated test framework. We are going to write some code for scoring a game ofbowling.Since the seminal article "Engineer Notebook: An Extreme Programming Episode" publishedin 2001 by Robert C. Martin and Robert S. Koss: pisode.htmcalculating the score for a bowling game has gained status as an advanced "Hello World" forprogramming languages. For any programming language out there you will find a bowlingscore implementation inspired by the "XP Episode". There is also a lot of derivative work fromthis article, some of them demonstrating how design evolves through Test-DrivenDevelopment.What you will see now is taken more or less directly out of the excellent “Bowling Game Kata”presentation by Robert C. Martin. http://butunclebob.com gGameKata ame%20Kata.pptBasically the only thing I have done is to translate from Java/JUnit into C/assert()Olve MaudalTDD in CNovember 2007

Since Uncle Bob is a nice guy. we include this page, because he asked us to do so:The following slides are not verbatim copies, but they are close enough to deservea proper copyright notice.Some of the material is probably Copyright (C) 2005 by Object Mentor. Permissionto use was given by Uncle Bob.Olve MaudalTDD in CNovember 2007

Scoring BowlingThe game consists of 10 frames as shown above. In each frame the player hastwo opportunities to knock down 10 pins. The score for the frame is the totalnumber of pins knocked down, plus bonuses for strikes and spares.A spare is when the player knocks down all 10 pins in two tries. The bonus forthat frame is the number of pins knocked down by the next roll. So in frame 3above, the score is 10 (the total number knocked down) plus a bonus of 5 (thenumber of pins knocked down on the next roll.)A strike is when the player knocks down all 10 pins on his first try. The bonusfor that frame is the value of the next two balls rolled.In the tenth frame a player who rolls a spare or strike is allowed to roll the extraballs to complete the frame. However no more than three balls can be rolled intenth frame.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

The Requirements.Game roll(pins : int) score() : intWrite a class named “Game” that has two methods: roll(pins : int) is called each time the player rolls aball. The argument is the number of pins knockeddown. score() : int is called only at the very end of thegame. It returns the total score for that game.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

Scoring Bowling & The RequirementsThe game consists of 10 frames as shown above.In each frame the player has two opportunities toknock down 10 pins. The score for the frame isthe total number of pins knocked down, plusbonuses for strikes and spares.A spare is when the player knocks down all 10 pinsin two tries. The bonus for that frame is thenumber of pins knocked down by the next roll. Soin frame 3 above, the score is 10 (the total numberknocked down) plus a bonus of 5 (the number ofpins knocked down on the next roll.)A strike is when the player knocks down all 10 pinson his first try. The bonus for that frame is thevalue of the next two balls rolled.In the tenth frame a player who rolls a spare orstrike is allowed to roll the extra balls to completethe frame. However no more than three balls canbe rolled in tenth frame.Olve MaudalTDD in CGame roll(pins : int) score() : intWrite a class named “Game” that hastwo methods: roll(pins : int) is called each time theplayer rolls a ball. The argument isthe number of pins knocked down. score() : int is called only at thevery end of the game. It returns thetotal score for that game.[source: Uncle Bob]November 2007

A quick design sessionGame roll(pins : int) score() : intClearly we need the Game class.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionGame roll(pins : int) score() : int10FrameA game has 10 frames.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionGame roll(pins : int) score() : int10Frame1.2Roll- pins : intA frame has 1 or two rolls.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionGame roll(pins : int) score() : int10Frame1.2Roll- pins : int1Tenth FrameThe tenth frame has two or three rolls.It is different from all the other frames.[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionGame roll(pins : int) score() : intThe score function mustiterate through all theframes, and calculateall their scores.10Frame1.2Roll- pins : int1Tenth Frame[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionnext frameGame roll(pins : int) score() : int10FrameThe score for aspare or a strikedepends on theframe’s successor1.2Roll- pins : int1Tenth Frame[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionnext frameGame roll(pins : int) score() : int10FrameThe score for aspare or a strikedepends on theframe’s successor1.2Roll- pins : int1Tenth Frame[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

A quick design sessionnext frameGame roll(pins : int) score() : int10otdseFrameThe score for aspare or a strikedepends on theframe’s successor1.2.enOviNr,DOtsNe,NO rate TtnsTenthFrameomdeRolltne- pins: intopmnowleveD1oppussI wa[source: Uncle Bob]Olve MaudalTDD in CNovember 2007

(blank)With TDD you are still “allowed” to do a lot of up-front design if you find ituseful to understand the problem domain, but you should try to let your testsdrive the design process. Doing TDD correctly, you will find that sometimesyou end up with a surprisingly simple and solid design. The key idea is thatyou can evolve the design by telling the system what it should do, rather thathow it should do it.Olve MaudalTDD in CNovember 2007

Getting Started with Test-Driven Development create a new directory named bowling create a 'bowling game test.c' file with a failing test execute and see the test fail// bowling game test.c#include assert.h #include stdbool.h int main() {assert( false && "My first unit test" );}mkdir bowlingcd bowlinged bowling game test.cgcc -std c99 -Wall bowling game test.c && ./a.outbowling game test.c:5: failed assertion false && "My first unit test"'Olve MaudalTDD in CNovember 2007

The First Testtest gutter game()Olve MaudalTDD in CNovember 2007

The First Test// bowling game test.c#include assert.h #include stdbool.h static void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}int main() {test gutter game();}Olve MaudalTDD in CNovember 2007

The First Test// bowling game test.c#include assert.h #include stdbool.h static void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}int main() {test gutter game();}gcc -std c99 -Wall bowling game test.c && ./a.out/usr/bin/ld: Undefined symbols:bowling game initbowling game rollbowling game scoreOlve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h static void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}int main() {test gutter game();}Olve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h static void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}int main() {test gutter game();}gcc -std c99 -Wall bowling game test.c && ./a.out/usr/bin/ld: Undefined symbols:bowling game initbowling game rollbowling game scoreOlve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"void bowling game init() {}void bowling game roll(int pins) {}int main() {test gutter game();}int bowling game score() {return -1;}Olve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"void bowling game init() {}void bowling game roll(int pins) {}int main() {test gutter game();}int bowling game score() {return -1;}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outbowling game test.c:12: failed assertion bowling game score() 0 && "test gutter game()"'Olve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"void bowling game init() {}void bowling game roll(int pins) {}int main() {test gutter game();}int bowling game score() {return -1;}Now we have afailing unit test.gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outbowling game test.c:12: failed assertion bowling game score() 0 && "test gutter game()"'Olve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"void bowling game init() {}void bowling game roll(int pins) {}int main() {test gutter game();}int bowling game score() {return -1;}Olve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();}TDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}int main() {test gutter game();}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outOlve MaudalTDD in CNovember 2007

The First Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"#include assert.h #include stdbool.h // bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();}TDD in CNovember 2007

The Second Testtest all ones()Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();}TDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outbowling game test.c:21: failed assertion bowling game score() 20 && "test gutter game()"'Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outOlve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.c#include assert.h #include stdbool.h Duplicate Codestatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include "bowling game.h"Perhaps we need tostatic int score;do some refactoringvoid bowling game init(){ code?of testscore 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}#include assert.h #include stdbool.h #include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void roll many(int n, int pins) {for (int i 0; i n; i )bowling game roll(pins);}#include assert.h #include stdbool.h #include "bowling game.h"static int score;static void test gutter game() {bowling game init();for (int i 0; i 20; i )bowling game roll(0);assert( bowling game score() 0&& "test gutter game()" );}void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();for (int i 0; i 20; i )bowling game roll(1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void roll many(int n, int pins) {for (int i 0; i n; i )bowling game roll(pins);}#include assert.h #include stdbool.h #include "bowling game.h"static int score;static void test gutter game() {bowling game init();roll many(20,0);assert( bowling game score() 0&& "test gutter game()" );}void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();roll many(20,1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void roll many(int n, int pins) {for (int i 0; i n; i )bowling game roll(pins);}#include assert.h #include stdbool.h #include "bowling game.h"static int score;static void test gutter game() {bowling game init();roll many(20,0);assert( bowling game score() 0&& "test gutter game()" );}void bowling game init() {score 0;Duplicate Code}Eliminatedvoid bowling game roll(int pins){score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();roll many(20,1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Second Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();#include "bowling game.h"// bowling game.cstatic void roll many(int n, int pins) {for (int i 0; i n; i )bowling game roll(pins);}#include assert.h #include stdbool.h #include "bowling game.h"static int score;static void test gutter game() {bowling game init();roll many(20,0);assert( bowling game score() 0&& "test gutter game()" );}void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}static void test all ones() {bowling game init();roll many(20,1);assert( bowling game score() 20&& "test all ones()" );}int main() {test gutter game();test all ones();}Olve MaudalTDD in CNovember 2007

The Third Testtest one spare()Olve MaudalTDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}int main() {test gutter game();test all ones();test one spare();}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outbowling game test.c:34: failed assertion bowling game score() 16 && "test one spare()"'Olve MaudalTDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();test one spare();}tempted to use flag toremember previous roll.TDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalstatic void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}calculates score, butroll()name doesintnotmain()imply{that.test gutter game();test all ones();test one spare();}score() does not calculatescore, but name implies thatit does.TDD in CNovember 2007

Olve MaudalTDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;Designis wrong.void bowling game init(){score 0;Responsibilities}are pins)misplaced.void bowling game roll(int{score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}int main() {test gutter game();test all ones();test one spare();}gcc -std c99 -Wall bowling game test.c bowling game.c && ./a.outbowling game test.c:34: failed assertion bowling game score() 16 && "test one spare()"'Olve MaudalTDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();// test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h// bowling game test.cvoid bowling game init();void bowling game roll(int pins);int bowling game score();.static void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}// bowling game.c#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();// test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h.// bowling game test.c.// bowling game.cstatic void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}#include "bowling game.h"static int score;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();// test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h.// bowling game test.c.// bowling game.cstatic void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}#include "bowling game.h"enum { max rolls 21 };static int rolls[max rolls];static int current roll;void bowling game init() {score 0;}void bowling game roll(int pins) {score pins;}int bowling game score() {return score;}Olve Maudalint main() {test gutter game();test all ones();// test one spare();}TDD in CNovember 2007

The Third Test// bowling game.h.// bowling game test.c.// bowling game.cstatic void test one spare() {bowling game init();bowling game roll(5);bowling game roll(5); // sparebowling game roll(3);roll many(17, 0);assert( bowling game score() 16&& "test one spare()" );}#include "bowling game.h"enum { max rolls 21 };static int rolls[max rolls];static int current roll;void bowling game init() {for (int i 0; i max rolls; i )rolls[i] 0;current roll 0;}i

Olve Maudal TDD in C November 2007 Scoring Bowling & The Requirements The game consists of 10 frames as shown above. In each frame the player has two opportunities to knock down 10 pins.The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares. A spare is when the player knocks down all 10 pins in two tries.