-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.c
46 lines (37 loc) · 1.14 KB
/
container.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
#include "nacc.h"
Vector *new_vector() {
Vector *vec = malloc(sizeof(Vector));
vec->data = malloc(sizeof(void *) * 16);
vec->capacity = 16;
vec->len = 0;
return vec;
}
void vec_push(Vector *vec, void *elem) {
if (vec->capacity == vec->len) {
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
void expect(int line, int expected, int actual) {
if (expected == actual) return;
fprintf(stderr, "%d: %d expected, but got %d\n", line, expected, actual);
exit(1);
}
void test_vector() {
Vector *vec = new_vector();
expect(__LINE__, 0, vec->len);
// intだとサイズが違って警告が出るので代わりにlongを使う
// warning: cast to pointer from integer of different size
// [-Wint-to-pointer-cast]
// for (int i = 0; i < 100; i++) vec_push(vec, (void *)i);
for (long i = 0; i < 100; i++) vec_push(vec, (void *)i);
expect(__LINE__, 100, vec->len);
expect(__LINE__, 0, (long)vec->data[0]);
expect(__LINE__, 50, (long)vec->data[50]);
expect(__LINE__, 99, (long)vec->data[99]);
}
void runtest() {
test_vector();
printf("OK\n");
}