Top Banner
TDD in C Illustrated by the Recently-Used-List kata Olve Maudal
269

TDD in C - Recently Used List Kata

Jan 16, 2017

Download

Software

Olve Maudal
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: TDD in C - Recently Used List Kata

TDD in C

Illustrated by the Recently-Used-List kata

Olve Maudal

Page 2: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt <empty> <empty> <empty> <empty>

Source: cyber-dojo.org

Page 3: TDD in C - Recently Used List Kata

README.txt <empty> <empty> <empty> <empty>

Page 4: TDD in C - Recently Used List Kata

README.txt <empty> <empty> <empty> <empty>

Page 5: TDD in C - Recently Used List Kata

README.txt rul_tests.c <empty> <empty> <empty>

Page 6: TDD in C - Recently Used List Kata

README.txt rul_tests.c <empty> <empty> <empty>

Page 7: TDD in C - Recently Used List Kata

README.txt rul_tests.c <empty> <empty> <empty>

Page 8: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 9: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 10: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 11: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

cc rul_tests.c && ./a.outAll tests passed

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 12: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 13: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 14: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){

printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 15: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 16: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 17: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 18: TDD in C - Recently Used List Kata

#include <stdio.h>

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 19: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 20: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 21: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 22: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

cc rul_tests.c && ./a.outa.out: rul_tests.c:6: main: Assertion `3 == 4' failed.

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 23: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

cc rul_tests.c && ./a.outa.out: rul_tests.c:6: main: Assertion `3 == 4' failed.

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 24: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 4); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 25: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 26: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 27: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 28: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

cc rul_tests.c && ./a.outAll tests passed

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 29: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 30: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt <empty> <empty> <empty> <empty>

Page 31: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt <empty> <empty> <empty> <empty>

Page 32: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt <empty> <empty> <empty> <empty>

Page 33: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 34: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 35: TDD in C - Recently Used List Kata

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

int main(void){ assert(3 == 3); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 36: TDD in C - Recently Used List Kata

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

int main(void){

printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 37: TDD in C - Recently Used List Kata

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

int main(void){

printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 38: TDD in C - Recently Used List Kata

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

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 39: TDD in C - Recently Used List Kata

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

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 40: TDD in C - Recently Used List Kata

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

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 41: TDD in C - Recently Used List Kata

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

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 42: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 43: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 44: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 45: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

cc rul_tests.c && ./a.outa.out: rul_tests.c:6: test_list_is_initially_empty: Assertion `3 == 4' failed.

TEST

Page 46: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 47: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 48: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 49: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 50: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

cc rul_tests.c && ./a.outAll tests passed

TEST

Page 51: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 52: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 53: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(3 == 3);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 54: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert( );}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 55: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert( );}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 56: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 57: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 58: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 59: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 60: TDD in C - Recently Used List Kata

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 61: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 62: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

Page 63: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

TEST

Page 64: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

cc rul_tests.c && ./a.outrul_tests.c:1:17: fatal error: rul.h: No such file or directory

TEST

Page 65: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c <empty> <empty> <empty>

cc rul_tests.c && ./a.outrul_tests.c:1:17: fatal error: rul.h: No such file or directory

TEST

Page 66: TDD in C - Recently Used List Kata

README.txt rul_tests.c <empty> <empty> <empty>

Page 67: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

Page 68: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

Page 69: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

Page 70: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endifTEST

Page 71: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c && ./a.outrul_tests.c:(.text+0xa): undefined reference to `rul_size'

TEST

Page 72: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c && ./a.outrul_tests.c:(.text+0xa): undefined reference to `rul_size'

TEST

Page 73: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c && ./a.outrul_tests.c:(.text+0xa): undefined reference to `rul_size'

Page 74: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c rul.c && ./a.out

Page 75: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c rul.c && ./a.out

TEST

Page 76: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c rul.c && ./a.outcc: error: rul.c: No such file or directory

TEST

Page 77: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

cc rul_tests.c rul.c && ./a.outcc: error: rul.c: No such file or directory

TEST

Page 78: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h <empty> <empty>

Page 79: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

Page 80: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return ;}

Page 81: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return ;}

?

Page 82: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 42;}

Page 83: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 42;} TEST

Page 84: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 42;}

cc rul_tests.c rul.c && ./a.outtest_list_is_initially_empty: Assertion `rul_size() == 0' failed.

TEST

Page 85: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 42;}

cc rul_tests.c rul.c && ./a.outtest_list_is_initially_empty: Assertion `rul_size() == 0' failed.

TEST

Page 86: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 0;}

Page 87: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 0;} TEST

Page 88: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 0;}

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 89: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 0;}

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 90: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 91: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

TEST

Page 92: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 93: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 94: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 95: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 96: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size() == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 97: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 98: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 99: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 100: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 101: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 102: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

size_t rul_size(void);

#endif

Page 103: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

size_t rul_size(const struct rul * rul);

#endif

Page 104: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

size_t rul_size(const struct rul * rul);

#endif

Page 105: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(void){ return 0;}

Page 106: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;}

Page 107: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;} TEST

Page 108: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;}

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 109: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;}

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 110: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 111: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 112: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul * rul = NULL; assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 113: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 114: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 115: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 116: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

size_t rul_size(const struct rul * rul);

#endif

Page 117: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);

#endif

Page 118: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);

#endif

Page 119: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;}

Page 120: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

size_t rul_size(const struct rul * rul){ return 0;}

Page 121: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return NULL;}

size_t rul_size(const struct rul * rul){ return 0;}

Page 122: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return NULL;}

size_t rul_size(const struct rul * rul){ return 0;}

Page 123: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return NULL;}

size_t rul_size(const struct rul * rul){ return 0;} TEST

Page 124: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return NULL;}

size_t rul_size(const struct rul * rul){ return 0;}

cc rul_tests.c rul.c && ./a.outtest_list_is_initially_empty: Assertion `rul == &myrul' failed.

TEST

Page 125: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return NULL;}

size_t rul_size(const struct rul * rul){ return 0;}

cc rul_tests.c rul.c && ./a.outtest_list_is_initially_empty: Assertion `rul == &myrul' failed.

TEST

Page 126: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

Page 127: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

Page 128: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;} TEST

Page 129: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 130: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 131: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

TEST

Page 132: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 133: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 134: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 135: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 136: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 137: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 138: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 139: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 140: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

TEST

Page 141: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outAll tests passed

TEST

Page 142: TDD in C - Recently Used List Kata

#include "rul.h"

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

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 143: TDD in C - Recently Used List Kata

...

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 144: TDD in C - Recently Used List Kata

...

int main(void){ test_list_is_initially_empty(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 145: TDD in C - Recently Used List Kata

...

int main(void){ test_list_is_initially_empty();

printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 146: TDD in C - Recently Used List Kata

...

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 147: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 148: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outtest_size_of_list_increases_as_we_add_unique_items: Assertion `3 == 4' failed.

Page 149: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 150: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ assert(3 == 4);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 151: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); }

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 152: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); }

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outrul_tests.c:(.text+0x92): undefined reference to `rul_add'

Page 153: TDD in C - Recently Used List Kata

...static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); }

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

cc rul_tests.c rul.c && ./a.outrul_tests.c:(.text+0x92): undefined reference to `rul_add'

Page 154: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);

#endif

Page 155: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 156: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 157: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

Page 158: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

void rul_add(struct rul * rul, const char * num){}

Page 159: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

void rul_add(struct rul * rul, const char * num){}

cc rul_tests.c rul.c && ./a.outa.out: rul_tests.c:19: test_size_of_list_increases_as_we_add_unique_items: Assertion `rul_size(rul) == 1' failed.

Page 160: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return 0;}

void rul_add(struct rul * rul, const char * num){}

Page 161: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){}

Page 162: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){}

Page 163: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

Page 164: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

Page 165: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

Page 166: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 167: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul {

};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 168: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 169: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 170: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 171: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 172: TDD in C - Recently Used List Kata

static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 173: TDD in C - Recently Used List Kata

static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); rul_add(rul, "1001"); assert(rul_size(rul) == 2); rul_add(rul, "1002"); rul_add(rul, "1003"); rul_add(rul, "1004"); assert(rul_size(rul) == 5);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 174: TDD in C - Recently Used List Kata

static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); rul_add(rul, "1001"); assert(rul_size(rul) == 2); rul_add(rul, "1002"); rul_add(rul, "1003"); rul_add(rul, "1004"); assert(rul_size(rul) == 5);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 175: TDD in C - Recently Used List Kata

static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); rul_add(rul, "1001"); assert(rul_size(rul) == 2); rul_add(rul, "1002"); rul_add(rul, "1003"); rul_add(rul, "1004"); assert(rul_size(rul) == 5);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 176: TDD in C - Recently Used List Kata

static void test_size_of_list_increases_as_we_add_unique_items(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); rul_add(rul, "1001"); assert(rul_size(rul) == 2); rul_add(rul, "1002"); rul_add(rul, "1003"); rul_add(rul, "1004"); assert(rul_size(rul) == 5);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 177: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 178: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 179: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 180: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 181: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 182: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 183: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 184: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 185: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 186: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);

#endif

Page 187: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

Page 188: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

Page 189: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

Page 190: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "";}

Page 191: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "";}

Page 192: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "";}

cc rul_tests.c rul.c && ./a.outrul_tests.c:34: test_indexing_into_the_list: Assertion `strcmp(rul_get(rul, 0), "1000") == 0' failed.

Page 193: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "";}

?

cc rul_tests.c rul.c && ./a.outrul_tests.c:34: test_indexing_into_the_list: Assertion `strcmp(rul_get(rul, 0), "1000") == 0' failed.

Page 194: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

cc rul_tests.c rul.c && ./a.outrul_tests.c:34: test_indexing_into_the_list: Assertion `strcmp(rul_get(rul, 0), "1000") == 0' failed.

Page 195: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

Page 196: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 197: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 198: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#include "rul.h"

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 199: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 200: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 201: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 202: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outrul_tests.c:36: test_indexing_into_the_list: Assertion `strcmp(rul_get(rul, 0), "1001") == 0' failed.

Page 203: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 204: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 205: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 206: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 207: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 208: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

struct rul { size_t n_numbers;};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

Page 209: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

#define MAX_NUMLEN 15#define MAX_NUMBERS 10

struct rul { size_t n_numbers; char numbers[MAX_NUMBERS][MAX_NUMLEN+1];};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

Page 210: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

#define MAX_NUMLEN 15#define MAX_NUMBERS 10

struct rul { size_t n_numbers; char numbers[MAX_NUMBERS][MAX_NUMLEN+1];};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 211: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

#define MAX_NUMLEN 15#define MAX_NUMBERS 10

struct rul { size_t n_numbers; char numbers[MAX_NUMBERS][MAX_NUMLEN+1];};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 212: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

...

Page 213: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

...

Page 214: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

cc rul_tests.c rul.c && ./a.outAll tests passed

...

Page 215: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

...

Page 216: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return "1000";}

...

Page 217: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return rul->numbers[0];}

...

Page 218: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return rul->numbers[0];}

cc rul_tests.c rul.c && ./a.outAll tests passed

...

Page 219: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 220: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 221: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); // rul_add(rul, "1001"); // assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 222: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 223: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 224: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 225: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0); assert(strcmp(rul_get(rul, 1), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outrul_tests.c: test_indexing_into_the_list: Assertion `strcmp(rul_get(rul, 1), "1000") == 0' failed.

Page 226: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return rul->numbers[0];}

...

Page 227: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; strcpy(rul->numbers[0], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ return rul->numbers[0];}

...

Page 228: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) return; strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

Page 229: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) return; strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 230: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) return; strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 231: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0); assert(strcmp(rul_get(rul, 1), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 232: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0); assert(strcmp(rul_get(rul, 1), "1000") == 0); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(strcmp(rul_get(rul, 0), "1003") == 0); assert(strcmp(rul_get(rul, 1), "1002") == 0); assert(strcmp(rul_get(rul, 2), "1001") == 0); assert(strcmp(rul_get(rul, 3), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 233: TDD in C - Recently Used List Kata

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0); assert(strcmp(rul_get(rul, 1), "1000") == 0); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(strcmp(rul_get(rul, 0), "1003") == 0); assert(strcmp(rul_get(rul, 1), "1002") == 0); assert(strcmp(rul_get(rul, 2), "1001") == 0); assert(strcmp(rul_get(rul, 3), "1000") == 0);}

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 234: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 235: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 236: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 237: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 238: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 239: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 240: TDD in C - Recently Used List Kata

static void test_adding_unique_items_to_full_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); char num[MAX_NUMLEN+1]; for (int i = 0; i < MAX_NUMBERS; i++) { sprintf(num, "10%02d", i); rul_add(rul, num); } assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1000") == 0); rul_add(rul, "1234"); assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, 0), "1234") == 0); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1001") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 241: TDD in C - Recently Used List Kata

static void test_adding_unique_items_to_full_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); char num[MAX_NUMLEN+1]; for (int i = 0; i < MAX_NUMBERS; i++) { sprintf(num, "10%02d", i); rul_add(rul, num); } assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1000") == 0); rul_add(rul, "1234"); assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, 0), "1234") == 0); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1001") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outtest_adding_unique_items_to_full_list: Assertion `strcmp(rul_get(rul, 0), "1234") == 0' failed.

Page 242: TDD in C - Recently Used List Kata

static void test_adding_unique_items_to_full_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); char num[MAX_NUMLEN+1]; for (int i = 0; i < MAX_NUMBERS; i++) { sprintf(num, "10%02d", i); rul_add(rul, num); } assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1000") == 0); rul_add(rul, "1234"); assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, 0), "1234") == 0); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1001") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outtest_adding_unique_items_to_full_list: Assertion `strcmp(rul_get(rul, 0), "1234") == 0' failed.

Page 243: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) return; strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

Page 244: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) return; strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

Page 245: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

Page 246: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static void remove_oldest_number(struct rul * rul){ for (size_t i = 0; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

Page 247: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static void remove_oldest_number(struct rul * rul){ for (size_t i = 0; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 248: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static void remove_oldest_number(struct rul * rul){ for (size_t i = 0; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

Page 249: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 250: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 251: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 252: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 253: TDD in C - Recently Used List Kata

int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); test_adding_existing_numbers_to_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 254: TDD in C - Recently Used List Kata

static void test_adding_existing_numbers_to_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); rul_add(rul, "1001"); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(rul_size(rul) == 4); rul_add(rul, "1002"); assert(rul_size(rul) == 4); assert(strcmp(rul_get(rul, 0), "1002") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); test_adding_existing_numbers_to_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

Page 255: TDD in C - Recently Used List Kata

static void test_adding_existing_numbers_to_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); rul_add(rul, "1001"); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(rul_size(rul) == 4); rul_add(rul, "1002"); assert(rul_size(rul) == 4); assert(strcmp(rul_get(rul, 0), "1002") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); test_adding_existing_numbers_to_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outrul_tests.c:73: test_adding_existing_numbers_to_list: Assertion `rul_size(rul) == 4' failed.

Page 256: TDD in C - Recently Used List Kata

static void test_adding_existing_numbers_to_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); rul_add(rul, "1001"); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(rul_size(rul) == 4); rul_add(rul, "1002"); assert(rul_size(rul) == 4); assert(strcmp(rul_get(rul, 0), "1002") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_items(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); test_adding_existing_numbers_to_list(); printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>...

cc rul_tests.c rul.c && ./a.outrul_tests.c:73: test_adding_existing_numbers_to_list: Assertion `rul_size(rul) == 4' failed.

Page 257: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

...

Page 258: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

...

Page 259: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

...

Page 260: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static size_t find_number(struct rul * rul, const char * num){ for (size_t i = 0; i < rul->n_numbers; i++) if (strcmp(rul->numbers[i], num) == 0) return i; return rul->n_numbers;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

...

Page 261: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static size_t find_number(struct rul * rul, const char * num){ for (size_t i = 0; i < rul->n_numbers; i++) if (strcmp(rul->numbers[i], num) == 0) return i; return rul->n_numbers;}

static void remove_number_at_index(struct rul * rul, size_t index){ for (size_t i = index; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}...

Page 262: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static size_t find_number(struct rul * rul, const char * num){ for (size_t i = 0; i < rul->n_numbers; i++) if (strcmp(rul->numbers[i], num) == 0) return i; return rul->n_numbers;}

static void remove_number_at_index(struct rul * rul, size_t index){ for (size_t i = index; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

...

Page 263: TDD in C - Recently Used List Kata

README.txt rul_tests.c rul.h rul.c <empty>

static size_t find_number(struct rul * rul, const char * num){ for (size_t i = 0; i < rul->n_numbers; i++) if (strcmp(rul->numbers[i], num) == 0) return i; return rul->n_numbers;}

static void remove_number_at_index(struct rul * rul, size_t index){ for (size_t i = index; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

...

cc rul_tests.c rul.c && ./a.outAll tests passed

...

Page 264: TDD in C - Recently Used List Kata

Develop a recently-used-list module for holding a limited set ofunique phone numbers in a Last-In-First-Out order.

Initially, you may assume that numbers added to the list are no longerthan 15 digits. You may also assume that the capacity of the list is10 items. (Imagine that this code is to be used to show a list of themost recently used phone numbers in a very simple cordless phone.)

o) A recently-used-list is initially empty. As you add unique numbers, the list grows, until capacity is reached.

o) Numbers in the list can be looked up by index. The zeroth element is the most recently used item. o) Numbers in the list are unique. If you add a number that already exists, the number is moved to the zeroth element in the list.

o) When adding a unique number to a full list, the oldest number is dropped to make room for the new entry.

README.txt rul_tests.c rul.h rul.c <empty>

Page 265: TDD in C - Recently Used List Kata

#ifndef UTILS_RUL_H_INCLUDED#define UTILS_RUL_H_INCLUDED

#include <stddef.h>

#define MAX_NUMLEN 15#define MAX_NUMBERS 10

struct rul { size_t n_numbers; char numbers[MAX_NUMBERS][MAX_NUMLEN+1];};

struct rul * rul_init(struct rul * rul);size_t rul_size(const struct rul * rul);void rul_add(struct rul * rul, const char * num);const char * rul_get(const struct rul * rul, size_t index);

#endif

README.txt rul_tests.c rul.h rul.c <empty>

Page 266: TDD in C - Recently Used List Kata

#include "rul.h"#include <string.h>#include <assert.h>

struct rul * rul_init(struct rul * rul){ rul->n_numbers = 0; return rul;}

size_t rul_size(const struct rul * rul){ return rul->n_numbers;}

static size_t find_number(struct rul * rul, const char * num){ for (size_t i = 0; i < rul->n_numbers; i++) if (strcmp(rul->numbers[i], num) == 0) return i; return rul->n_numbers;}

static void remove_number_at_index(struct rul * rul, size_t index){ for (size_t i = index; i < rul->n_numbers - 1; i++) strcpy(rul->numbers[i], rul->numbers[i+1]); rul->n_numbers--;}

static void remove_oldest_number(struct rul * rul){ remove_number_at_index(rul, 0);}

void rul_add(struct rul * rul, const char * num){ if (strlen(num) > MAX_NUMLEN) return; size_t i = find_number(rul, num); if (i != rul->n_numbers) remove_number_at_index(rul, i); if (rul->n_numbers == MAX_NUMBERS) remove_oldest_number(rul); assert(rul->n_numbers < MAX_NUMBERS); strcpy(rul->numbers[rul->n_numbers], num); rul->n_numbers++;}

const char * rul_get(const struct rul * rul, size_t index){ if (index + 1 > rul->n_numbers) return ""; return rul->numbers[rul->n_numbers - (index + 1)];}

README.txt rul_tests.c rul.h rul.c <empty>

Page 267: TDD in C - Recently Used List Kata

#include "rul.h"#include <assert.h>#include <stdio.h>#include <string.h>

static void test_list_is_initially_empty(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); assert(rul == &myrul); assert(rul_size(rul) == 0);}

static void test_size_of_list_increases_as_we_add_unique_numbers(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(rul_size(rul) == 1); rul_add(rul, "1001"); assert(rul_size(rul) == 2); rul_add(rul, "1002"); rul_add(rul, "1003"); rul_add(rul, "1004"); assert(rul_size(rul) == 5);}

static void test_indexing_into_the_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); assert(strcmp(rul_get(rul, 0), "1000") == 0); rul_add(rul, "1001"); assert(strcmp(rul_get(rul, 0), "1001") == 0); assert(strcmp(rul_get(rul, 1), "1000") == 0); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(strcmp(rul_get(rul, 0), "1003") == 0); assert(strcmp(rul_get(rul, 1), "1002") == 0); assert(strcmp(rul_get(rul, 2), "1001") == 0); assert(strcmp(rul_get(rul, 3), "1000") == 0);}

static void test_adding_unique_items_to_full_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); char num[MAX_NUMLEN+1]; for (int i = 0; i < MAX_NUMBERS; i++) { sprintf(num, "10%02d", i); rul_add(rul, num); } assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1000") == 0); rul_add(rul, "1234"); assert(rul_size(rul) == MAX_NUMBERS); assert(strcmp(rul_get(rul, 0), "1234") == 0); assert(strcmp(rul_get(rul, MAX_NUMBERS-1), "1001") == 0);}

static void test_adding_existing_numbers_to_list(void){ struct rul myrul; struct rul * rul = rul_init(&myrul); rul_add(rul, "1000"); rul_add(rul, "1001"); rul_add(rul, "1002"); rul_add(rul, "1003"); assert(rul_size(rul) == 4); rul_add(rul, "1002"); assert(rul_size(rul) == 4); assert(strcmp(rul_get(rul, 0), "1002") == 0);} int main(void){ test_list_is_initially_empty(); test_size_of_list_increases_as_we_add_unique_numbers(); test_indexing_into_the_list(); test_adding_unique_items_to_full_list(); test_adding_existing_numbers_to_list();

printf("All tests passed\n");}

README.txt rul_tests.c rul.h rul.c <empty>

Page 268: TDD in C - Recently Used List Kata

!

Page 269: TDD in C - Recently Used List Kata

https://www.amazon.com/Driven-Development-Embedded-Pragmatic-Programmers/dp/193435662X http://cyber-dojo.org/

by Jon Jagger

To learn more about Test-Driven Development in C:

by James W. Grenning

more presentations like this can be found on http://www.olvemaudal.com/talks