-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_cache_encoder_marker_test.c
111 lines (94 loc) · 2.04 KB
/
simple_cache_encoder_marker_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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "simple_cache_encoder_marker.h"
typedef bool (*test_case)(void);
bool test_basic_encoded()
{
Marker s = {
.count = 10,
.encoding_size = 7,
.is_encoded = true,
};
uint16_t v = encode_marker(s);
Marker d;
if (decode_marker(&d, v) != 0)
{
fprintf(stderr, "cannot decode\n");
return false;
}
if (!(marker_eq(s, d)))
{
fprintf(stderr, "exp != got\n");
log_marker(stderr, s);
log_marker(stderr, d);
return false;
}
return true;
}
bool test_basic_not_encoded()
{
Marker s = {
.count = 10,
.is_encoded = false,
};
uint16_t v = encode_marker(s);
Marker d;
if (decode_marker(&d, v) != 0)
{
fprintf(stderr, "cannot decode\n");
return false;
}
if (!(marker_eq(s, d)))
{
fprintf(stderr, "exp != got\n");
log_marker(stderr, s);
log_marker(stderr, d);
return false;
}
return true;
}
bool test_basic_not_encoded_max()
{
Marker s = {
.count = 127,
.is_encoded = false,
};
uint16_t v = encode_marker(s);
Marker d;
if (decode_marker(&d, v) != 0)
{
fprintf(stderr, "cannot decode\n");
return false;
}
if (!(marker_eq(s, d)))
{
fprintf(stderr, "exp != got\n");
log_marker(stderr, s);
log_marker(stderr, d);
return false;
}
return true;
}
int main()
{
test_case tests[] = {
test_basic_encoded,
test_basic_not_encoded,
test_basic_not_encoded_max,
};
int num_tests = sizeof(tests) / sizeof(tests[0]);
printf("tests: num_tests=%d\n", num_tests);
int num_ok = 0;
for (int i = 0; i < num_tests; i++)
{
bool ok = tests[i]();
printf("tc=%d: %s\n", i, ok ? "ok" : "fail");
if (ok)
{
num_ok++;
}
}
printf("tests: %s\n", num_tests == num_ok ? "ok" : "fail");
return !(num_ok == num_tests);
}