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
123
124
125
|
#include <al/test.h>
#include <al/array.h>
#include <al/lib.h>
#include "lib.h"
#include "common.h"
static bool lib_test_add_wrap(void)
{
AL_TEST_START("lib_add_wrap");
// u32
u32 counter32 = UINT32_MAX - 5;
counter32 = al_u32_add_wrap(counter32, 15, UINT32_MAX);
AL_TEST_EQ(counter32, 10, u32);
counter32 = UINT32_MAX;
counter32 = al_u32_add_wrap(counter32, 1, UINT32_MAX);
AL_TEST_EQ(counter32, 1, u32);
counter32 = UINT32_MAX - 25;
counter32 = al_u32_add_wrap(counter32, 20, UINT32_MAX);
AL_TEST_EQ(counter32, UINT32_MAX - 5, u32);
counter32 = al_u32_add_wrap(counter32, 6, UINT32_MAX);
AL_TEST_EQ(counter32, 1, u32);
// u16
u16 counter16 = UINT16_MAX - 5;
counter16 = al_u16_add_wrap(counter16, 15, UINT16_MAX);
AL_TEST_EQ(counter16, 10, u16);
counter16 = UINT16_MAX;
counter16 = al_u16_add_wrap(counter16, 1, UINT16_MAX);
AL_TEST_EQ(counter16, 1, u16);
counter16 = UINT16_MAX - 25;
counter16 = al_u16_add_wrap(counter16, 20, UINT16_MAX);
AL_TEST_EQ(counter16, UINT16_MAX - 5, u16);
counter16 = al_u16_add_wrap(counter16, 6, UINT16_MAX);
AL_TEST_EQ(counter16, 1, u16);
// inc_wrap
counter16 = UINT16_MAX - 4;
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 3, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 2, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX - 1, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), UINT16_MAX, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 0, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 1, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 2, u16);
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 3, u16);
// From 3 to UINT16_MAX.
for (u16 i = 0; i < UINT16_MAX - 3; i++) {
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), i + 4, u16);
}
// Wrap to 0.
AL_TEST_EQ((counter16 = al_u16_inc_wrap(counter16)), 0, u16);
AL_TEST_END();
}
static bool lib_test_offset_vs_at_and_ref(void)
{
AL_TEST_START("lib_offset_vs_at_and_ref");
// Test that &al_array_at and al_array_offset are equivalent and that we can
// have NULL entires in an array of pointers.
array(struct test_t *) objects;
al_array_init(objects);
for (u32 i = 0; i < 10; i++) {
if (i == 4) {
al_array_push(objects, (struct test_t *)NULL);
} else {
al_array_push(objects, al_alloc_object(struct test_t));
}
}
AL_TEST_EQ(&al_array_at(objects, 2), al_array_offset(objects, 2), ptr);
AL_TEST_EQ(&al_array_at(objects, 3), al_array_offset(objects, 3), ptr);
AL_TEST_NEQ(*&al_array_at(objects, 3), NULL, ptr);
AL_TEST_EQ(&al_array_at(objects, 4), al_array_offset(objects, 4), ptr);
AL_TEST_EQ(*&al_array_at(objects, 4), *al_array_offset(objects, 4), ptr);
AL_TEST_NEQ(*&al_array_at(objects, 4), *al_array_offset(objects, 2), ptr);
AL_TEST_EQ(*&al_array_at(objects, 4), NULL, ptr);
AL_TEST_EQ(&al_array_at(objects, 5), al_array_offset(objects, 5), ptr);
for (u32 i = 0; i < 10; i++) {
if (i != 4) {
al_free(al_array_at(objects, i));
}
}
AL_TEST_END();
}
static bool lib_test_min_max(void)
{
AL_TEST_START("lib_min_max");
AL_TEST_EQ(MAX((u32)4, (u32)3), 4, u32);
AL_TEST_EQ(MIN((u32)4, (u32)3), 3, u32);
// Compiler warnings.
//AL_TEST_EQ(MAX(4, (u32)3), 4, u32);
//AL_TEST_EQ(MIN(4, (u32)3), 3, u32);
AL_TEST_END();
}
bool lib_tests_run(void)
{
AL_TEST_START_GROUP("lib");
AL_TEST_RUN(lib_test_add_wrap);
AL_TEST_RUN(lib_test_offset_vs_at_and_ref);
AL_TEST_RUN(lib_test_min_max);
AL_TEST_END_GROUP();
}
|