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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#undef NDEBUG
#define _POSIX_C_SOURCE 200809L
#include <l4array.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test_array_new (void) {
const char tc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const size_t tc_len = sizeof (tc) / sizeof (char);
LbsArray* array = lbs_array_new (sizeof (char));
for (int i = 0; i < tc_len; i++) {
lbs_array_append_data (array, &tc[i]);
}
assert (lbs_array_get_len (array) == tc_len);
assert (lbs_array_get_size (array) == sizeof (char));
assert (lbs_array_get_is_alloc (array) == true);
assert (memcmp (tc, lbs_array_get_data (array), sizeof (tc)) == 0);
lbs_array_unref (array);
printf ("%s => PASS!\n", __func__);
}
void test_array_init (void) {
const char tc[] = "0123456789abcdefghijklmnopqrstuvwxyz";
const size_t tc_len = sizeof (tc) / sizeof (char);
LbsArray array_struct;
LbsArray* array = &array_struct;
lbs_array_init (array, sizeof (char));
for (int i = 0; i < tc_len; i++) {
lbs_array_append_data (array, &tc[i]);
}
assert (lbs_array_get_len (array) == tc_len);
assert (lbs_array_get_size (array) == sizeof (char));
assert (lbs_array_get_is_alloc (array) == false);
assert (memcmp (tc, lbs_array_get_data (array), sizeof (tc)) == 0);
lbs_array_unref (array);
printf ("%s => PASS!\n", __func__);
}
void test_array_copy (void) {
const char tc[] = "b1dc8070-3847-4ec6-8486-65d4e32d16b5";
const size_t tc_len = sizeof (tc) / sizeof (char);
LbsArray* array = lbs_array_new_with_max (sizeof (char), tc_len);
for (int i = 0; i < tc_len; i++) {
lbs_array_append_data (array, &tc[i]);
}
LbsArray* array_copy = lbs_array_copy (NULL, array);
assert (lbs_array_get_len (array) == lbs_array_get_len (array_copy));
assert (lbs_array_get_size (array) == lbs_array_get_size (array_copy));
assert (memcmp (lbs_array_get_data (array),
lbs_array_get_data (array_copy), sizeof (tc)) == 0);
lbs_array_unref (array);
lbs_array_unref (array_copy);
printf ("%s => PASS!\n", __func__);
}
void test_array_cat (void) {
char* tc1 = malloc (15);
char* tc2 = malloc (15);
strcpy (tc1, "!@#$%^&*()TGQ");
strcpy (tc2, ":;<>?/[]{}");
size_t tc1_len = strlen (tc1);
size_t tc2_len = strlen (tc2);
LbsArray *cat, *a1, *a2, a2_struct;
a1 = lbs_array_make_struct (NULL, sizeof (char), tc1_len, 15, tc1);
a2 = &a2_struct;
a2 = lbs_array_make_struct (a2, sizeof (char), tc2_len, 15, tc2);
cat = lbs_array_cat (a1, a2);
assert (a1 == cat);
assert (lbs_array_get_len (cat) == tc1_len + tc2_len);
assert (lbs_array_get_size (cat) == lbs_array_get_size (a2));
assert (memcmp (lbs_array_get_data (cat),
lbs_array_get_data (a1), sizeof (char) * tc1_len) == 0);
assert (memcmp (lbs_array_vp (cat, tc1_len),
lbs_array_get_data (a2), sizeof (char) * tc2_len) == 0);
lbs_array_unref (a1);
free (lbs_array_drop_struct (a2));
printf ("%s => PASS!\n", __func__);
}
void test_array_ref (void) {
LbsArray* array = lbs_array_new (16);
assert (lbs_array_get_ref_count (array) == 1);
assert (lbs_array_ref (array) == array);
assert (lbs_array_ref (array) == array);
assert (lbs_array_ref (array) == array);
assert (lbs_array_get_ref_count (array) == 4);
lbs_array_unref (array);
lbs_array_unref (array);
lbs_array_unref (array);
assert (lbs_array_get_ref_count (array) == 1);
lbs_array_unref (array);
printf ("%s => PASS!\n", __func__);
}
void test_array_ptr (void) {
const char* tc[] = {
"260740d8-6d94-41fc-9235-ee6b11209547",
"8613ff0b-4429-4432-8870-a79477665a89",
"b05adbb1-b628-4b49-8661-5576c477ea6e",
"641992b6-c5a5-4855-9448-11343a60ad67",
"74380d26-376a-4754-be5e-41cb62e52d86",
NULL
};
const size_t tc_len = sizeof (tc) / sizeof (char*);
/* This is a pointer array, so we use generic pointer (void*) */
LbsArray* ptr_array = lbs_array_new (sizeof (void*));
lbs_array_set_free_func (ptr_array, free);
for (int i = 0; i < tc_len; i++) {
if (tc[i] != NULL) {
lbs_array_append_ptr (ptr_array, strdup (tc[i]));
} else {
lbs_array_append_ptr (ptr_array, NULL);
}
}
assert (lbs_array_v (ptr_array, char*, tc_len - 1) == NULL);
assert (lbs_array_get_len (ptr_array) == tc_len);
for (int i = 0; i < tc_len - 1; i++) {
if (tc[i] != NULL) {
assert (strcmp (lbs_array_v (ptr_array, char*, i), tc[i]) == 0);
}
}
lbs_array_unref (ptr_array);
printf ("%s => PASS!\n", __func__);
}
void test_array_op (void) {
LbsArray* array = lbs_array_new (sizeof (int));
for (int i = 0; i < 80; i++) {
#ifdef LBS_COMMON_ISO_C11
lbs_array_append (array, i);
#else
lbs_array_append_data (array, &i);
#endif
}
lbs_array_remove (array);
lbs_array_remove (array);
lbs_array_remove (array);
assert (lbs_array_get_len (array) == 77);
array->len = 20;
lbs_array_minimize (array);
assert (lbs_array_get_len (array) == 20);
array->len = 10;
lbs_array_v (array, int, 15) = 82;
for (int i = 0; i < 10; i++) {
assert (lbs_array_v (array, int, i) == i);
}
lbs_array_unref (array);
printf ("%s => PASS!\n", __func__);
}
int main () {
test_array_new ();
test_array_init ();
test_array_copy ();
test_array_cat ();
test_array_ref ();
test_array_ptr ();
test_array_op ();
return 0;
}
|