olve maudal, cisco systems norway - pvv.orgoma/tddinc_yahtzee_27oct2011.pdf · olve maudal, cisco...

299
Test-Driven Development in C a Yahtzee game kata Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the last decade Test-Driven Development has become an established practice for developing software in the industry. In this lightning talk I will demonstrate Test-Driven Development in C.

Upload: others

Post on 18-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Test-Driven Development in Ca Yahtzee game kata

Olve Maudal, Cisco Systems Norway

A 15 minute lightning talkdagen@IFI, October 27, 2011

During the last decade Test-Driven Development has become an established practice for developing software in the industry. In this lightning talk I will demonstrate Test-Driven Development in C.

Page 2: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

The requirement

Write a C library that can score the lower section of a game of yahtzee.

Page 3: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Examples

(see also http://en.wikipedia.org/wiki/Yahtzee)

Page 4: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

yahtzee.a

yahtzee_tests.c

yahtzee.c

yahtzee.h

Page 5: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

CC=gccCFLAGS=-std=c99 -O -Wall -Wextra -pedanticLD=gcc

all: yahtzee.a

yahtzee.a: yahtzee.o ! ar -rcs $@ $^

check: yahtzee_tests! ./yahtzee_tests

yahtzee.o: yahtzee.c yahtzee.h

yahtzee_tests.o: yahtzee_tests.c yahtzee.h

yahtzee_tests: yahtzee_tests.o yahtzee.a! $(LD) -o $@ $^

clean:! rm -f *.o *.a yahtzee_tests

Makefile

Page 6: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

yahtzee_test.c

Page 7: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 8: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make check

Page 9: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.c

Page 10: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.c

Page 11: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.o

Page 12: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a

Page 13: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_tests

Page 14: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsAssertion failed: (6*9 == 42), function main, file yahtzee_tests.c, line 6.

Page 15: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsAssertion failed: (6*9 == 42), function main, file yahtzee_tests.c, line 6.

Our first unit test failed. This is good! Exactly what we want. The rythm of TDD is : fail, fix, pass... fail, fix, pass

Page 16: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsAssertion failed: (6*9 == 42), function main, file yahtzee_tests.c, line 6.

Our first unit test failed. This is good! Exactly what we want. The rythm of TDD is : fail, fix, pass... fail, fix, passFail - Fix - Pass

Page 17: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*9 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee.o yahtzee.car -rcs yahtzee.a yahtzee.ogcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsAssertion failed: (6*9 == 42), function main, file yahtzee_tests.c, line 6.

Our first unit test failed. This is good! Exactly what we want. The rythm of TDD is : fail, fix, pass... fail, fix, passFail - Fix - Pass

Page 18: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 19: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

Fail - Fix - Pass

Page 20: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsYahtzee tests OK

Fail - Fix - Pass

Page 21: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsYahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 22: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsYahtzee tests OK

All tests are OK!

Fail - Fix - Pass

Fail - Fix - Pass

Page 23: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include <assert.h>#include <stdio.h>

int main(void){ assert(6*7 == 42);

puts("Yahtzee tests OK");}

yahtzee_test.c

Let’s write a proper unit test

$ make checkgcc -std=c99 -O -Wall -Wextra -pedantic -c -o yahtzee_tests.o yahtzee_tests.cgcc -o yahtzee_tests yahtzee_tests.o yahtzee.a./yahtzee_testsYahtzee tests OK

All tests are OK!

Fail - Fix - Pass

Fail - Fix - Pass

Page 24: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 25: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 26: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 27: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 28: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 29: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 30: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

Page 31: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return ??}

yahtzee.c

Page 32: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return ??}

yahtzee.cwhat should we return?

Page 33: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return ??}

yahtzee.c

Remember fail-fix-pass? Return something that you know will fail.

what should we return?

Page 34: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Page 35: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2)

Page 36: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2)

Fail - Fix - Pass

Page 37: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2)

Fail - Fix - Pass

Page 38: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 39: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Fail - Fix - Pass

Page 40: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Page 41: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 42: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Yahtzee tests OK

Congratulation. We have completed our first proper fail-fix-pass cycle.

Returning 7 is a minimal change to make it pass. This is OK because what we are concerned about now is just to make sure that the “wiring” of the test is OK. Is the test really being called and is it testing the right function?

Fail - Fix - Pass

Fail - Fix - Pass

Page 43: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Yahtzee tests OK

Congratulation. We have completed our first proper fail-fix-pass cycle.

Returning 7 is a minimal change to make it pass. This is OK because what we are concerned about now is just to make sure that the “wiring” of the test is OK. Is the test really being called and is it testing the right function?

Let’s add another unit test.

Fail - Fix - Pass

Fail - Fix - Pass

Page 44: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Yahtzee tests OK

Congratulation. We have completed our first proper fail-fix-pass cycle.

Returning 7 is a minimal change to make it pass. This is OK because what we are concerned about now is just to make sure that the “wiring” of the test is OK. Is the test really being called and is it testing the right function?

Let’s add another unit test.

Fail - Fix - Pass

Fail - Fix - Pass

Page 45: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 46: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 47: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4)

Page 48: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4)

Fail - Fix - Pass

Page 49: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4)

Fail - Fix - Pass

Should we “cheat” again and check for the last dice, if 4 then return 10 otherwise 7?

Page 50: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4)

Fail - Fix - Pass

Should we “cheat” again and check for the last dice, if 4 then return 10 otherwise 7?

No! Another principle of TDD is that while you are supposed to do simple and “naive” increments you are

not allowed to do “obviously stupid” stuff.

Page 51: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4)

Fail - Fix - Pass

Should we “cheat” again and check for the last dice, if 4 then return 10 otherwise 7?

No! Another principle of TDD is that while you are supposed to do simple and “naive” increments you are

not allowed to do “obviously stupid” stuff.

A simple and naive thing to do here is to just sum the dice and return the value. That would satisfy all the tests and we

know the functionality will eventually be needed.

Page 52: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 53: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 54: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return 7;}

yahtzee.c

Page 55: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 56: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 57: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 58: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 59: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Fail - Fix - Pass

Page 60: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Page 61: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 62: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

But wait a minute. When indexing an array in C you should really use size_t. Let’s fix it now as all tests are OK

Page 63: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 64: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 65: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 66: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 67: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (int die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 68: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 69: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 70: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Page 71: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Nice. Let’s start a new fail-fix-pass cycle

Page 72: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

Nice. Let’s start a new fail-fix-pass cycle

Page 73: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 74: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 75: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

puts("Yahtzee tests OK");}

yahtzee_test.c

Page 76: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

puts("Yahtzee tests OK");}

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0)

Page 77: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

puts("Yahtzee tests OK");}

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0)

Fail - Fix - Pass

Page 78: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

puts("Yahtzee tests OK");}

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0)

Fail - Fix - Pass

So how do we fix this?

Page 79: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

#include "yahtzee.h"#include <assert.h>#include <stdio.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

puts("Yahtzee tests OK");}

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0)

Fail - Fix - Pass

So how do we fix this?

We need to check that we really have three of a kind.

Page 80: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 81: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 82: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 83: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 84: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice);}

yahtzee.c

Page 85: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice);}

yahtzee.c

Page 86: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice);

}

yahtzee.c

Page 87: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice);

}

yahtzee.c

Page 88: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 89: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 90: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 91: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 92: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 93: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 94: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 95: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 96: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 97: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 98: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 99: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 100: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 101: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>#include <stdbool.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 102: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>#include <stdbool.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 103: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>#include <stdbool.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Page 104: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stddef.h>#include <stdbool.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Page 105: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

yahtzee_test.c

Page 106: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0);

yahtzee_test.c

Page 107: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5);

yahtzee_test.c

Page 108: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5);

yahtzee_test.c

Yahtzee tests OK

Page 109: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5);

yahtzee_test.c

Yahtzee tests OK

Page 110: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2);

yahtzee_test.c

Page 111: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2);

yahtzee_test.c

Yahtzee tests OK

Page 112: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2);

yahtzee_test.c

Yahtzee tests OK

Looking good! Looking good!

Page 113: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2);

yahtzee_test.c

Yahtzee tests OK

Looking good! Looking good!

Page 114: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Looking good! Looking good!

Page 115: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Looking good! Looking good!

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Page 116: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Page 117: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

Page 118: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

Page 119: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

Page 120: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

Page 121: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

But of course...

Page 122: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

But of course...

Page 123: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Assertion failed: (score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7)

Oh no... what happened?

But of course...

it should be at least three...

Page 124: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 125: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 126: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 127: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 128: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 129: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 130: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 131: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Page 132: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

phew!

Page 133: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Page 134: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.cNow we have a

decent implementation of

three of a kind

Page 135: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 136: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 137: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 138: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 139: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Page 140: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

yahtzee_test.c

Page 141: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

Page 142: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

Page 143: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);yahtzee.h

Page 144: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

Page 145: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return ??}

yahtzee.c

Page 146: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return ??}

yahtzee.c what should we return?

Page 147: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return ??}

yahtzee.c

Remember fail-fix-pass? First we want a failing test

what should we return?

Page 148: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Page 149: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Page 150: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Fail - Fix - Pass

Page 151: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Fail - Fix - Pass

Page 152: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Page 153: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Fail - Fix - Pass

Page 154: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Page 155: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 156: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Page 157: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Page 158: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Page 159: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ return 9;}

yahtzee.c

Page 160: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

Page 161: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

if (have_at_least_fou

Page 162: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

if (have_at_least_fou

hmm...

Page 163: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

if (have_at_least_fou

hmm...

Perhaps we need a have_at_least_n_of_a_kind()?

Page 164: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.chmm...

Perhaps we need a have_at_least_n_of_a_kind()?

Page 165: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.chmm...

Perhaps we need a have_at_least_n_of_a_kind()?

Let’s go back to “green”

Page 166: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

Page 167: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Page 168: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Page 169: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); //assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Page 170: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); //assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Yahtzee tests OK

Page 171: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); //assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Yahtzee tests OK

And now that we are at green we can do some refactoring

Page 172: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 173: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_three_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_three_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 174: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_n_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 175: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_n_of_a_kind(const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= 3) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 176: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= n) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(3, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 177: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= n) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(3, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Page 178: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

...static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;} static bool have_at_least_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= n) return true; return false;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(3, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

And now we have prepared the ground for implementing score_four_of_a_kind()

Page 179: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); //assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Yahtzee tests OK

Page 180: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); //assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Yahtzee tests OK

Page 181: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Page 182: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Page 183: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Fail - Fix - Pass

Page 184: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){

}

yahtzee.c

return 9;

Assertion failed: (score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9)

Fail - Fix - Pass

Page 185: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 186: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Fail - Fix - Pass

Page 187: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Page 188: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 189: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 190: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0); assert(score_four_of_a_kind((int[5]){1,1,1,1,1}) == 5);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Page 191: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

... assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,3,4}) == 3+3+4); assert(score_three_of_a_kind((int[5]){1,2,3,4,5}) == 0); assert(score_three_of_a_kind((int[5]){5,3,5,5,2}) == 15+5); assert(score_three_of_a_kind((int[5]){1,1,6,6,6}) == 18+2); assert(score_three_of_a_kind((int[5]){6,1,6,6,6}) == 18+7);

assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0); assert(score_four_of_a_kind((int[5]){1,1,1,1,1}) == 5);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

yahtzee.c

Yahtzee tests OK

Page 192: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 193: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 194: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 195: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 196: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

yahtzee_test.c

Page 197: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

yahtzee_test.c

Page 198: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

Page 199: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

Page 200: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);

yahtzee.h

Page 201: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Page 202: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Page 203: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Assertion failed: (score_full_house((int[5]){3,3,3,5,5}) == 25)

Page 204: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Assertion failed: (score_full_house((int[5]){3,3,3,5,5}) == 25)

Fail - Fix - Pass

Page 205: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Assertion failed: (score_full_house((int[5]){3,3,3,5,5}) == 25)

Fail - Fix - Pass

Page 206: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Page 207: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Fail - Fix - Pass

Page 208: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Yahtzee tests OK

Fail - Fix - Pass

Page 209: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 210: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 211: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Page 212: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Assertion failed: (score_full_house((int[5]){3,3,1,5,5}) == 0)

Page 213: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Assertion failed: (score_full_house((int[5]){3,3,1,5,5}) == 0)

Fail - Fix - Pass

Page 214: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Fix?

Assertion failed: (score_full_house((int[5]){3,3,1,5,5}) == 0)

Fail - Fix - Pass

Page 215: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Fix?

Assertion failed: (score_full_house((int[5]){3,3,1,5,5}) == 0)

Fail - Fix - Pass

We know how to do this!

Page 216: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

int score_full_house(const int dice[5]){ return 25;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Fix?

Assertion failed: (score_full_house((int[5]){3,3,1,5,5}) == 0)

Fail - Fix - Pass

We know how to do this!

Page 217: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

static bool have_exactly_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == n) return true; return false;}

int score_full_house(const int dice[5]){ if (have_exactly_n_of_a_kind(3, dice) && have_exactly_n_of_a_kind(2, dice)) return 25; return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Page 218: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

static bool have_exactly_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == n) return true; return false;}

int score_full_house(const int dice[5]){ if (have_exactly_n_of_a_kind(3, dice) && have_exactly_n_of_a_kind(2, dice)) return 25; return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Fail - Fix - Pass

Page 219: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

static bool have_exactly_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == n) return true; return false;}

int score_full_house(const int dice[5]){ if (have_exactly_n_of_a_kind(3, dice) && have_exactly_n_of_a_kind(2, dice)) return 25; return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Yahtzee tests OK

Fail - Fix - Pass

Page 220: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0);

yahtzee_test.c

static bool have_exactly_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == n) return true; return false;}

int score_full_house(const int dice[5]){ if (have_exactly_n_of_a_kind(3, dice) && have_exactly_n_of_a_kind(2, dice)) return 25; return 0;}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);

yahtzee.h

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 221: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 222: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 223: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 224: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 225: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 226: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

Page 227: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ return 0;}

yahtzee.c

Page 228: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

Page 229: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

It is OK to plan ahead when implenting a fix... so...

Page 230: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

It is OK to plan ahead when implenting a fix... so...

Page 231: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

It is OK to plan ahead when implenting a fix... so...

Page 232: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

It is OK to plan ahead when implenting a fix... so...

we know that we need five in a row soon. So therefor

we do n in a row now

Page 233: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

It is OK to plan ahead when implenting a fix... so...

we know that we need five in a row soon. So therefor

we do n in a row now

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

Page 234: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){

}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Page 235: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){

}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

?

Page 236: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){

}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

?But how to implement this?

Page 237: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){

}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

?But how to implement this?

Use your head and think for a while... TDD is not about not thinking!

Page 238: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){

}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

?But how to implement this?

Use your head and think for a while... TDD is not about not thinking!

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

Page 239: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Page 240: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Fail - Fix - Pass

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Page 241: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Fail - Fix - Pass

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 242: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Fail - Fix - Pass

Assertion failed: (score_small_straight((int[5]){1,2,3,4,1}) == 30)

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Fail - Fix - Pass

Yahtzee tests OK

Fail - Fix - Pass

Page 243: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30); assert(score_small_straight((int[5]){1,1,1,1,1}) == 0);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Page 244: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30); assert(score_small_straight((int[5]){1,1,1,1,1}) == 0);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Yahtzee tests OK

Page 245: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30); assert(score_small_straight((int[5]){1,1,1,1,1}) == 0); assert(score_small_straight((int[5]){6,5,4,3,2}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Page 246: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_small_straight((int[5]){1,2,3,4,1}) == 30); assert(score_small_straight((int[5]){1,1,1,1,1}) == 0); assert(score_small_straight((int[5]){6,5,4,3,2}) == 30);

yahtzee_test.c

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

yahtzee.c

Yahtzee tests OK

Page 247: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 248: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 249: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 250: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 30);

yahtzee_test.c

Page 251: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ return 0;}

yahtzee.c

Page 252: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

Page 253: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

Fail - Fix - Pass

Page 254: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ return 0;}

yahtzee.c

Page 255: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ return 0;}

yahtzee.c

Page 256: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Page 257: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 258: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 259: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

hmm...

Fail - Fix - Pass

Page 260: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

hmm...

Fail - Fix - Pass

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

Page 261: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

hmm...

Fail - Fix - Pass

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

Page 262: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

assert(score_large_straight((int[5]){1,2,3,4,5}) == 30);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

hmm...

Fail - Fix - Pass

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

Page 263: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

What?Inconceivable

Assertion failed: (score_large_straight((int[5]){1,2,3,4,5}) == 30)

assert(score_large_straight((int[5]){1,2,3,4,5}) == 30);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

hmm...

There is a bug in the test.

Fail - Fix - Pass

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

Page 264: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 30);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Page 265: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 30);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Page 266: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Page 267: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 268: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Yahtzee tests OK

Fail - Fix - Pass assert(score_large_straight((int[5]){1,2,3,4,5}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 269: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

Yahtzee tests OK

Fail - Fix - Pass assert(score_large_straight((int[5]){1,2,3,4,5}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Fail - Fix - Pass

Page 270: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 40); assert(score_large_straight((int[5]){6,6,6,6,6}) == 0); assert(score_large_straight((int[5]){6,5,4,2,3}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Page 271: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_large_straight((int[5]){1,2,3,4,5}) == 40); assert(score_large_straight((int[5]){6,6,6,6,6}) == 0); assert(score_large_straight((int[5]){6,5,4,2,3}) == 40);

yahtzee_test.c

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

yahtzee.c

Yahtzee tests OK

Page 272: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 273: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 274: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 275: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 276: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

Page 277: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return 0;}

yahtzee.c

Page 278: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_yahtzee((int[5]){1,1,1,1,1}) == 50)

Page 279: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_yahtzee((int[5]){1,1,1,1,1}) == 50)

Fail - Fix - Pass

Page 280: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return 0;}

yahtzee.c

Assertion failed: (score_yahtzee((int[5]){1,1,1,1,1}) == 50)

Fail - Fix - Pass

Page 281: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return have_exactly_n_of_a_kind(5,dice) ? 50 : 0;}

yahtzee.c

Page 282: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return have_exactly_n_of_a_kind(5,dice) ? 50 : 0;}

yahtzee.c

Fail - Fix - Pass

Page 283: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return have_exactly_n_of_a_kind(5,dice) ? 50 : 0;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Page 284: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0);

yahtzee_test.c

int score_yahtzee(const int dice[5]){ return have_exactly_n_of_a_kind(5,dice) ? 50 : 0;}

yahtzee.c

Yahtzee tests OK

Fail - Fix - Pass

Fail - Fix - Pass

Page 285: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 286: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 287: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 288: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 289: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 290: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);

yahtzee.h

Page 291: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);

yahtzee.h

Page 292: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);int score_chance(const int dice[5]);

yahtzee.h

Page 293: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_chance((int[5]){1,1,4,1,1}) == 8); assert(score_chance((int[5]){2,3,4,5,6}) == 20); assert(score_chance((int[5]){6,6,6,6,6}) == 30);

yahtzee_test.c

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);int score_chance(const int dice[5]);

yahtzee.h

Page 294: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

assert(score_chance((int[5]){1,1,4,1,1}) == 8); assert(score_chance((int[5]){2,3,4,5,6}) == 20); assert(score_chance((int[5]){6,6,6,6,6}) == 30);

yahtzee_test.c

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Yahtzee tests OK

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);int score_chance(const int dice[5]);

yahtzee.h

Page 295: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 296: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the
Page 297: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

int score_three_of_a_kind(const int dice[5]);int score_four_of_a_kind(const int dice[5]);int score_full_house(const int dice[5]);int score_small_straight(const int dice[5]);int score_large_straight(const int dice[5]);int score_yahtzee(const int dice[5]);int score_chance(const int dice[5]);

#include "yahtzee.h"#include <stdio.h>#include <assert.h>

int main(void){ assert(score_three_of_a_kind((int[5]){1,1,1,2,2}) == 3+2+2); assert(score_three_of_a_kind((int[5]){1,1,1,6,4}) == 3+10); assert(score_three_of_a_kind((int[5]){1,1,6,1,4}) == 3+10); assert(score_three_of_a_kind((int[5]){1,6,1,2,4}) == 0); assert(score_three_of_a_kind((int[5]){6,6,6,6,6}) == 30); assert(score_four_of_a_kind((int[5]){1,1,1,1,5}) == 9); assert(score_four_of_a_kind((int[5]){1,1,3,1,5}) == 0); assert(score_four_of_a_kind((int[5]){1,1,1,1,1}) == 5);

assert(score_full_house((int[5]){3,3,3,5,5}) == 25); assert(score_full_house((int[5]){3,3,5,5,5}) == 25); assert(score_full_house((int[5]){3,3,1,5,5}) == 0); assert(score_small_straight((int[5]){1,2,3,4,6}) == 30); assert(score_small_straight((int[5]){2,3,4,5,6}) == 30); assert(score_small_straight((int[5]){2,3,1,5,6}) == 0); assert(score_small_straight((int[5]){6,5,4,3,3}) == 30);

assert(score_large_straight((int[5]){1,2,3,4,5}) == 40); assert(score_large_straight((int[5]){6,6,6,6,6}) == 0); assert(score_large_straight((int[5]){6,5,4,2,3}) == 40);

assert(score_yahtzee((int[5]){1,1,1,1,1}) == 50); assert(score_yahtzee((int[5]){6,6,6,6,6}) == 50); assert(score_yahtzee((int[5]){1,1,4,1,1}) == 0); assert(score_chance((int[5]){1,1,4,1,1}) == 8); assert(score_chance((int[5]){2,3,4,5,6}) == 20); assert(score_chance((int[5]){6,6,6,6,6}) == 30); puts("Yahtzee tests OK");}

yahtzee.h yahtzee_tests.h

CC=gccCFLAGS=-std=c99 -O -Wall -Wextra -pedanticLD=gcc

all: yahtzee.a

yahtzee.a: yahtzee.o ! ar -rcs $@ $^

check: yahtzee_tests! ./yahtzee_tests

yahtzee.o: yahtzee.c yahtzee.h

yahtzee_tests.o: yahtzee_tests.c yahtzee.h

yahtzee_tests: yahtzee_tests.o yahtzee.a! $(LD) -o $@ $^

clean:! rm -f *.o *.a yahtzee_tests

Makefile

Page 298: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

#include "yahtzee.h"#include <stdbool.h>#include <stddef.h>

static int sum_of_dice(const int dice[5]){ int sum = 0; for (size_t die = 0; die < 5; die++) sum += dice[die]; return sum;}

static int count_face(int face, const int dice[5]){ int count = 0; for (size_t die = 0; die < 5; die++) if (dice[die] == face) count++; return count;}

static bool have_at_least_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) >= n) return true; return false;}

static bool have_exactly_n_of_a_kind(int n, const int dice[5]){ for (int face = 1; face <= 6; face++) if (count_face(face,dice) == n) return true; return false;}

int have_at_least_n_in_a_row(int n, const int dice[5]){ int max_seq_len = 0; int seq_len = 0; for (int face = 1; face <= 6; face++) { if (count_face(face,dice) == 0) seq_len = 0; else seq_len++; if (seq_len > max_seq_len) max_seq_len = seq_len; } return max_seq_len >= n;}

int score_three_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(3, dice)) return sum_of_dice(dice); return 0;}

int score_four_of_a_kind(const int dice[5]){ if (have_at_least_n_of_a_kind(4, dice)) return sum_of_dice(dice); return 0;}

int score_full_house(const int dice[5]){ if (have_exactly_n_of_a_kind(3, dice) && have_exactly_n_of_a_kind(2, dice)) return 25; return 0;}

int score_small_straight(const int dice[5]){ if (have_at_least_n_in_a_row(4,dice)) return 30; return 0;}

int score_large_straight(const int dice[5]){ if (have_at_least_n_in_a_row(5,dice)) return 40; return 0;}

int score_yahtzee(const int dice[5]){ if (have_exactly_n_of_a_kind(5, dice)) return 50; return 0;}

int score_chance(const int dice[5]){ return sum_of_dice(dice);}

yahtzee.c

Page 299: Olve Maudal, Cisco Systems Norway - pvv.orgoma/TDDinC_Yahtzee_27oct2011.pdf · Olve Maudal, Cisco Systems Norway A 15 minute lightning talk dagen@IFI, October 27, 2011 During the

!