-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
218 lines (194 loc) · 6.29 KB
/
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <unity.h>
#include "dyn-array.h"
typedef struct elem_t {
int val_;
} elem_t;
void setUp(void) {
}
void tearDown(void) {
}
void test_uninitialized_array_has_zero_size(void) {
elem_t* elems = NULL;
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
}
void test_uninitialized_array_is_empty(void) {
elem_t* elems = NULL;
const bool empty = array_empty(elems);
TEST_ASSERT_TRUE(empty);
}
void test_zero_reserved_array_has_zero_size(void) {
elem_t* elems = NULL;
array_reserve(elems, 0);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
array_free(elems);
}
void test_zero_reserved_array_is_empty(void) {
elem_t* elems = NULL;
array_reserve(elems, 0);
const bool empty = array_empty(elems);
TEST_ASSERT_TRUE(empty);
array_free(elems);
}
void test_array_reserve_one_element_from_uninitialized(void) {
elem_t* elems = NULL;
array_reserve(elems, 1);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(1, capacity);
array_free(elems);
}
void test_array_reserve_n_element_from_uninitialized(void) {
elem_t* elems = NULL;
array_reserve(elems, 5);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(5, capacity);
array_free(elems);
}
void test_array_push_grows_by_one_initially(void) {
elem_t* elems = NULL;
array_push(elems, (elem_t){.val_ = 1});
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(1, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(1, capacity);
array_free(elems);
}
void test_array_push_grows_exponentially(void) {
const int growth_rate[] = {1, 2, 4, 4, 8, 8, 8, 8, 16, 16};
elem_t* elems = NULL;
for (int i = 0; i < 10; ++i) {
array_push(elems, (elem_t){.val_ = 1});
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(i + 1, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(growth_rate[i], capacity);
}
array_free(elems);
}
void test_array_reserved_and_not_resized(void) {
elem_t* elems = NULL;
array_reserve(elems, 10);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(10, capacity);
array_free(elems);
}
void test_array_reserved_and_not_resized_after_initial_reserve(void) {
elem_t* elems = NULL;
array_reserve(elems, 10);
array_reserve(elems, 20);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(20, capacity);
array_free(elems);
}
void test_array_does_not_grow_after_push_with_reservation(void) {
elem_t* elems = NULL;
array_reserve(elems, 20);
for (int i = 0; i < 20; ++i) {
array_push(elems, (elem_t){.val_ = i});
}
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(20, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(20, capacity);
array_free(elems);
}
void test_array_growth_doubles_after_exceeding_reservation(void) {
elem_t* elems = NULL;
array_reserve(elems, 20);
for (int i = 0; i < 21; ++i) {
array_push(elems, (elem_t){.val_ = i});
}
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(21, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(40, capacity);
array_free(elems);
}
void test_array_can_be_resized_from_uninitialized(void) {
elem_t* elems = NULL;
array_resize(elems, 10);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(10, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(10, capacity);
array_free(elems);
}
void test_array_can_be_resized_to_increase_after_initial_resize(void) {
elem_t* elems = NULL;
array_resize(elems, 10);
array_resize(elems, 20);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(20, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(20, capacity);
array_free(elems);
}
void test_array_can_be_resized_to_decrease_after_initial_resize(void) {
elem_t* elems = NULL;
array_resize(elems, 50);
array_resize(elems, 30);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(30, size);
const int capacity = array_capacity(elems);
TEST_ASSERT_EQUAL_INT32(50, capacity);
array_free(elems);
}
void test_array_single_element_pop(void) {
elem_t* elems = NULL;
array_push(elems, (elem_t){.val_ = 5});
array_pop(elems);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
}
void test_array_size_can_be_decreased_with_pop(void) {
elem_t* elems = NULL;
array_resize(elems, 50);
array_pop(elems);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(49, size);
}
void test_array_pop_on_empty_has_no_effect(void) {
elem_t* elems = NULL;
array_reserve(elems, 0);
array_pop(elems);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
}
void test_array_pop_on_uninitialized_has_no_effect(void) {
elem_t* elems = NULL;
array_pop(elems);
const int size = array_size(elems);
TEST_ASSERT_EQUAL_INT32(0, size);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_uninitialized_array_has_zero_size);
RUN_TEST(test_uninitialized_array_is_empty);
RUN_TEST(test_zero_reserved_array_has_zero_size);
RUN_TEST(test_zero_reserved_array_is_empty);
RUN_TEST(test_array_reserve_one_element_from_uninitialized);
RUN_TEST(test_array_reserve_n_element_from_uninitialized);
RUN_TEST(test_array_push_grows_by_one_initially);
RUN_TEST(test_array_push_grows_exponentially);
RUN_TEST(test_array_reserved_and_not_resized);
RUN_TEST(test_array_reserved_and_not_resized_after_initial_reserve);
RUN_TEST(test_array_does_not_grow_after_push_with_reservation);
RUN_TEST(test_array_growth_doubles_after_exceeding_reservation);
RUN_TEST(test_array_can_be_resized_from_uninitialized);
RUN_TEST(test_array_can_be_resized_to_increase_after_initial_resize);
RUN_TEST(test_array_can_be_resized_to_decrease_after_initial_resize);
RUN_TEST(test_array_single_element_pop);
RUN_TEST(test_array_size_can_be_decreased_with_pop);
RUN_TEST(test_array_pop_on_empty_has_no_effect);
RUN_TEST(test_array_pop_on_uninitialized_has_no_effect);
return UNITY_END();
}