GCC Code Coverage Report


Directory: ./
File: submodules/ECS/tests/library/vector/vector_tests.c
Date: 2023-09-29 04:53:15
Exec Total Coverage
Lines: 0 167 0.0%
Branches: 0 472 0.0%

Line Branch Exec Source
1 /*
2 * Filename: /workspaces/our_rpg/tests/library/vector/vector_tests.c
3 * Path: /workspaces/our_rpg/tests/library/vector
4 * Created Date: Thursday, December 29th 2022, 10:41:19 pm
5 * Author: osvegn
6 *
7 * Copyright (c) 2023 our_rpg
8 */
9
10 #include <criterion/criterion.h>
11 #include "vector.h"
12
13 Test(a, a)
14 {
15 cr_assert_eq(1, 1);
16 }
17
18 Test(construct_vector, construct_vector)
19 {
20 vector_t vector;
21 int rvalue = vector_constructor(&vector, sizeof(int), 0);
22
23 cr_assert_eq(rvalue, 0);
24 vector.destructor(&vector);
25 cr_assert_eq(0, 0);
26 }
27
28 Test(vector_emplace_back, vector_emplace_back)
29 {
30 vector_t vector;
31 int rvalue = vector_constructor(&vector, sizeof(int), 0);
32 int data = 10;
33
34 cr_assert_eq(rvalue, 0);
35 vector.emplace_back(&vector, &data);
36 vector.destructor(&vector);
37 cr_assert_eq(0, 0);
38 }
39
40 Test(vector_emplace, vector_emplace)
41 {
42 vector_t vector;
43 int rvalue = vector_constructor(&vector, sizeof(int), 0);
44 int data = 10;
45
46 cr_assert_eq(rvalue, 0);
47 vector.emplace(&vector, &data, 0);
48 vector.destructor(&vector);
49 cr_assert_eq(0, 0);
50 }
51
52 Test(vector_clear, vector_clear)
53 {
54 vector_t vector;
55 int rvalue = vector_constructor(&vector, sizeof(int), 0);
56 int data = 10;
57
58 cr_assert_eq(rvalue, 0);
59 vector.emplace_back(&vector, &data);
60 vector.emplace_back(&vector, &data);
61 vector.emplace_back(&vector, &data);
62 vector.emplace_back(&vector, &data);
63 rvalue = vector.size(&vector);
64 cr_assert_eq(rvalue, 4);
65 vector.clear(&vector);
66 rvalue = vector.size(&vector);
67 cr_assert_eq(rvalue, 0);
68 vector.destructor(&vector);
69 }
70
71 Test(vector_at, vector_at_valid)
72 {
73 vector_t vector;
74 int rvalue = vector_constructor(&vector, sizeof(int), 0);
75 int data = 10;
76
77 cr_assert_eq(rvalue, 0);
78 vector.emplace_back(&vector, &data);
79 rvalue = *(int *)vector.at(&vector, 0);
80 cr_assert_eq(rvalue, 10);
81 vector.destructor(&vector);
82 }
83
84 Test(vector_at, vector_at_too_high_index)
85 {
86 vector_t vector;
87 int rvalue = vector_constructor(&vector, sizeof(int), 0);
88 int data = 10;
89 void *ptr;
90
91 cr_assert_eq(rvalue, 0);
92 vector.emplace_back(&vector, &data);
93 ptr = vector.at(&vector, 10);
94 cr_assert_eq(ptr, 0);
95 vector.destructor(&vector);
96 }
97
98 Test(vector_at, vector_at_invalid_pointer)
99 {
100 vector_t vector;
101 int rvalue = vector_constructor(&vector, sizeof(int), 0);
102 void *ptr;
103
104 cr_assert_eq(rvalue, 0);
105 free(vector.pointer);
106 vector.pointer = NULL;
107 vector._size = 1;
108 ptr = vector.at(&vector, 0);
109 cr_assert_eq(ptr, 0);
110 }
111
112 Test(vector_front, vector_front)
113 {
114 vector_t vector;
115 int rvalue = vector_constructor(&vector, sizeof(int), 0);
116 int data = 10;
117
118 cr_assert_eq(rvalue, 0);
119 vector.emplace_back(&vector, &data);
120 data = 5;
121 vector.emplace_back(&vector, &data);
122 rvalue = *(int *)vector.front(&vector);
123 cr_assert_eq(rvalue, 10);
124 vector.destructor(&vector);
125 }
126
127 Test(vector_back, vector_back)
128 {
129 vector_t vector;
130 int rvalue = vector_constructor(&vector, sizeof(int), 0);
131 int data = 10;
132
133 cr_assert_eq(rvalue, 0);
134 vector.emplace_back(&vector, &data);
135 data = 5;
136 vector.emplace_back(&vector, &data);
137 rvalue = *(int *)vector.back(&vector);
138 cr_assert_eq(rvalue, 5);
139 vector.destructor(&vector);
140 }
141
142 Test(vector_back, vector_back_invalid_pointer)
143 {
144 vector_t vector;
145 int rvalue = vector_constructor(&vector, sizeof(int), 0);
146 void *ptr;
147
148 cr_assert_eq(rvalue, 0);
149 free(vector.pointer);
150 vector.pointer = NULL;
151 ptr = vector.back(&vector);
152 cr_assert_eq(ptr, 0);
153 }
154
155 Test(vector_capacity, vector_capacity)
156 {
157 vector_t vector;
158 int rvalue = vector_constructor(&vector, sizeof(int), 10);
159 unsigned int data = 0;
160
161 cr_assert_eq(rvalue, 0);
162 data = vector.capacity(&vector);
163 cr_assert_eq(data, 10);
164 vector.destructor(&vector);
165 }
166
167 Test(vector_empty, vector_empty_true)
168 {
169 vector_t vector;
170 int rvalue = vector_constructor(&vector, sizeof(int), 0);
171
172 cr_assert_eq(rvalue, 0);
173 cr_assert_eq(vector.empty(&vector), true);
174 }
175
176 Test(vector_empty, vector_empty_false)
177 {
178 vector_t vector;
179 int rvalue = vector_constructor(&vector, sizeof(int), 0);
180 int data = 10;
181
182 cr_assert_eq(rvalue, 0);
183 vector.emplace_back(&vector, &data);
184 cr_assert_eq(vector.empty(&vector), false);
185 }
186
187 Test(vector_swap, vector_swap_valid)
188 {
189 vector_t vector;
190 int rvalue = vector_constructor(&vector, sizeof(int), 10);
191 int data = 10;
192
193 cr_assert_eq(rvalue, 0);
194 for (int i = 0; i < 10; i++) {
195 vector.emplace_back(&vector, &data);
196 data++;
197 }
198 vector.swap(&vector, 0, 1);
199 rvalue = *(int *)vector.at(&vector, 0);
200 cr_assert_eq(rvalue, 11);
201 rvalue = *(int *)vector.at(&vector, 1);
202 cr_assert_eq(rvalue, 10);
203 vector.destructor(&vector);
204 }
205
206 Test(vector_swap, vector_swap_element1_invalid_size)
207 {
208 vector_t vector;
209 int rvalue = vector_constructor(&vector, sizeof(int), 10);
210 int data = 10;
211
212 cr_assert_eq(rvalue, 0);
213 for (int i = 0; i < 10; i++) {
214 vector.emplace_back(&vector, &data);
215 data++;
216 }
217 vector.swap(&vector, 100, 1);
218 rvalue = *(int *)vector.at(&vector, 0);
219 cr_assert_eq(rvalue, 10);
220 rvalue = *(int *)vector.at(&vector, 1);
221 cr_assert_eq(rvalue, 11);
222 vector.destructor(&vector);
223 }
224
225 Test(vector_swap, vector_swap_element2_invalid_size)
226 {
227 vector_t vector;
228 int rvalue = vector_constructor(&vector, sizeof(int), 10);
229 int data = 10;
230
231 cr_assert_eq(rvalue, 0);
232 for (int i = 0; i < 10; i++) {
233 vector.emplace_back(&vector, &data);
234 data++;
235 }
236 vector.swap(&vector, 1, 100);
237 rvalue = *(int *)vector.at(&vector, 0);
238 cr_assert_eq(rvalue, 10);
239 rvalue = *(int *)vector.at(&vector, 1);
240 cr_assert_eq(rvalue, 11);
241 vector.destructor(&vector);
242 }
243
244 Test(vector_shrink_to_fit, vector_shrink_to_fit_failure)
245 {
246 vector_t vector;
247 int rvalue = vector_constructor(&vector, sizeof(int), 10);
248
249 cr_assert_eq(rvalue, 0);
250 cr_assert_eq(vector.capacity(&vector), 10);
251 cr_assert_eq(vector.shrink_to_fit(&vector), -1);
252 cr_assert_eq(vector.capacity(&vector), 0);
253 vector.destructor(&vector);
254 }
255
256 Test(vector_shrink_to_fit, vector_shrink_to_fit_success)
257 {
258 vector_t vector;
259 int rvalue = vector_constructor(&vector, sizeof(int), 10);
260 int data = 10;
261
262 cr_assert_eq(rvalue, 0);
263 vector.emplace_back(&vector, &data);
264 cr_assert_eq(vector.capacity(&vector), 10);
265 cr_assert_eq(vector.shrink_to_fit(&vector), 0);
266 cr_assert_eq(vector.capacity(&vector), 1);
267 vector.destructor(&vector);
268 }
269
270 Test(vecotr_erase, vector_erase_valid)
271 {
272 vector_t vector;
273 int rvalue = vector_constructor(&vector, sizeof(int), 10);
274 int data = 10;
275
276 cr_assert_eq(rvalue, 0);
277 for (int i = 0; i < 10; i++) {
278 vector.emplace_back(&vector, &data);
279 data++;
280 }
281 cr_assert_eq(vector.erase(&vector, 1), 0);
282 cr_assert_eq(*(int *)vector.at(&vector, 1), 12);
283 vector.destructor(&vector);
284 }
285