-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_dynarr.c
121 lines (111 loc) · 2.96 KB
/
test_dynarr.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
#include "test_framework.h"
#include "dynarr.h"
#include <stdlib.h>
#include <stdbool.h>
static bool eq(int* i1, int* i2) {
return *i1 == *i2;
}
DECLARE_DYNARR_DS(int, /*static*/, malloc, free, eq, 1)
static int test_dynarr_as_stack(){
int_dynarr* s = int_dynarr_new();
int_dynarr_push(s, 3);
int_dynarr_push(s, 6);
int_dynarr_push(s, 10);
int_dynarr_push(s, 14);
int_dynarr_push(s, 20);
if(int_dynarr_size(s) == 5 &&
int_dynarr_pop(s) == 20 &&
int_dynarr_size(s) == 4 &&
int_dynarr_pop(s) == 14 &&
int_dynarr_top(s) == 10 &&
int_dynarr_pop(s) == 10 &&
int_dynarr_pop(s) == 6 &&
int_dynarr_pop(s) == 3 &&
int_dynarr_size(s) == 0) {
} else {
return false;
}
int_dynarr_free(s);
return true;
}
static int test_dynarr_push_array(){
int_dynarr* s = int_dynarr_new();
int_dynarr_push_array(s, 3, (int[]){0,1,2});
if(int_dynarr_size(s) == 3 &&
int_dynarr_pop(s) == 2 &&
int_dynarr_pop(s) == 1 &&
int_dynarr_pop(s) == 0 &&
int_dynarr_size(s) == 0) {
} else {
return false;
}
int_dynarr_free(s);
return true;
}
static int test_dynarr_push_void(){
int_dynarr* s = int_dynarr_new();
int_dynarr_push(s, 0);
int* array = int_dynarr_push_void(s, 100);
for(int i = 0; i < 100;i++){
array[i] = i + 1;
}
int* whole_array = int_dynarr_current_raw_array(s);
for(int i = 0; i < 101;i++){
if(whole_array[i] != i){
return false;
}
}
if(int_dynarr_size(s) != 101){
return false;
}
int_dynarr_free(s);
return true;
}
static int test_dynarr_replace(){
int_dynarr* s = int_dynarr_new();
int_dynarr_push(s, 9);
int_dynarr_push(s, 10);
int_dynarr_push(s, 11);
int_dynarr_replace(s, 9, 0);
int_dynarr_replace(s, 10, 1);
int_dynarr_replace(s, 11, 2);
int* whole_array = int_dynarr_current_raw_array(s);
for(int i = 0; i < 3;i++){
if(whole_array[i] != i){
return false;
}
}
int_dynarr_free(s);
return true;
}
static int test_dynarr_insert_before_after_remove(){
int_dynarr* s = int_dynarr_new();
int_dynarr_push(s, 9);
int_dynarr_push(s, 10);
int_dynarr_push(s, 11);
int_dynarr_insert_before(s, 9, 0);
int_dynarr_insert_after(s, 11, 2);
int_dynarr_insert_before(s, 10, 1);
int_dynarr_insert_after(s, 10, 101);
int_dynarr_remove(s, 9);
int_dynarr_remove(s, 10);
int_dynarr_remove(s, 11);
int_dynarr_remove(s, 101);
int* whole_array = int_dynarr_current_raw_array(s);
for(int i = 0; i < int_dynarr_size(s); i++){
if(whole_array[i] != i){
return false;
}
}
int_dynarr_free(s);
return true;
}
int main(){
int res = true;
res = res && test(test_dynarr_as_stack, "test_dynarr_as_stack");
res = res && test(test_dynarr_push_array, "test_dynarr_push_array");
res = res && test(test_dynarr_push_void, "test_dynarr_push_void");
res = res && test(test_dynarr_replace, "test_dynarr_replace");
res = res && test(test_dynarr_insert_before_after_remove, "test_dynarr_insert_before_after_remove");
return !res;
}