/
similar-image-test.c
122 lines (109 loc) · 3.79 KB
/
similar-image-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* This test file tests some of the small functions within
similar-image.c for sane behaviour. Its output is in TAP (Test
Anything Protocol) format. It can be compiled using
make -f mymakefile similar-image-test
and then run with
prove ./similar-image-test
It is also run by the author test xt/similar-image-test.t.
*/
#include <stdio.h>
#include "similar-image.h"
#define OK(test, counter, message, ...) { \
counter++; \
if (test) { \
printf ("ok %d - ", counter); \
} \
else { \
printf ("not ok %d - ", counter); \
} \
printf (message, ## __VA_ARGS__); \
printf (".\n"); \
}
static void
test_xo_yo_to_direction (int * test)
{
int direction;
int xo, yo;
simage_status_t status;
direction = xo_yo_to_direction (-1, -1);
OK (direction == 0, (*test), "%d should be 0", direction);
status = direction_to_xo_yo (direction, & xo, & yo);
OK (status == simage_ok, (*test), "status is OK");
OK (xo == -1, (*test), "got correct value %d for xo", xo);
OK (yo == -1, (*test), "got correct value %d for yo", yo);
direction = xo_yo_to_direction (-1, 0);
OK (direction == 3, (*test), "%d should be 3", direction);
status = direction_to_xo_yo (direction, & xo, & yo);
OK (status == simage_ok, (*test), "status is OK");
OK (xo == -1, (*test), "got correct value %d for xo", xo);
OK (yo == 0, (*test), "got correct value %d for yo", yo);
direction = xo_yo_to_direction (1, 1);
OK (direction == 7, (*test), "%d should be 7", direction);
status = direction_to_xo_yo (direction, & xo, & yo);
OK (status == simage_ok, (*test), "status is OK");
OK (xo == 1, (*test), "got correct value %d for xo", xo);
OK (yo == 1, (*test), "got correct value %d for yo", yo);
status = direction_to_xo_yo (100, & xo, & yo);
OK (status != simage_ok, (*test), "bad status for impossible direction");
}
static void
test_x_y_to_entry (int * test)
{
int entry;
int expect;
entry = x_y_to_entry (0, 0);
expect = 0;
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (1, 0);
expect = 1;
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (0, 1);
expect = 9;
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (8, 8);
expect = 80;
OK (entry == expect, (*test), "%d should be %d", entry, expect);
expect = -1;
entry = x_y_to_entry (-1, 0);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (1, 10);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (10, 1);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (10, 10);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (1, -1);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
entry = x_y_to_entry (-1, -1);
OK (entry == expect, (*test), "%d should be %d", entry, expect);
}
static void
test_inside (int * test)
{
int cell;
int direction;
cell = 0;
direction = 4;
OK (inside (cell, direction), (*test), "%d %d is inside", cell, direction);
cell = 0;
direction = 0;
OK (! inside (cell, direction), (*test), "%d %d is not inside", cell, direction);
cell = 80;
direction = 0;
OK (inside (cell, direction), (*test), "%d %d is inside", cell, direction);
cell = 80;
direction = 7;
OK (! inside (cell, direction), (*test), "%d %d is not inside", cell, direction);
}
int main ()
{
// Test directioner.
int test;
test = 0;
test_xo_yo_to_direction (& test);
test_x_y_to_entry (& test);
test_inside (& test);
// Print the test plan.
printf ("1..%d\n", test);
return 0;
}