-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbtests.c
161 lines (138 loc) · 4.28 KB
/
btests.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
#include "btests.h"
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
#include "blex.h"
#include "barray.h"
#include "bstring.h"
#include "bhash.h"
#include "bstate.h"
#include "bparser.h"
#include "math.h"
bean_State * get_state() {
bean_State * B = malloc(sizeof(B));
B -> allocateBytes = 0;
global_init(B);
beanX_init(B);
return B;
}
void test_barray_pop_and_push(bean_State * B) {
Array * array = init_array(B);
TValue * v1 = malloc(sizeof(TValue));
TValue * v2 = malloc(sizeof(TValue));
setnvalue(v1, 100);
setnvalue(v2, 300);
array_push(B, array, v1);
array_push(B, array, v2);
assert(check_equal(array_get(B, array, 0), v1));
assert(check_equal(array_get(B, array, 1), v2));
assert(array->count == 2);
assert(array->capacity == ARRAY_MIN_CAPACITY);
TValue * popValue1 = array_pop(B, array);
TValue * popValue2 = array_pop(B, array);
assert(check_equal(popValue1, v2));
assert(check_equal(popValue2, v1));
assert(array->count == 0);
}
void test_barray_shift_and_unshift(bean_State * B) {
Array * array = init_array(B);
TValue * v1 = malloc(sizeof(TValue));
TValue * v2 = malloc(sizeof(TValue));
TValue * v3 = malloc(sizeof(TValue));
array_unshift(B, array, v1);
array_unshift(B, array, v2);
array_unshift(B, array, v3);
assert(check_equal(array_get(B, array, 0), v3));
assert(check_equal(array_get(B, array, 1), v2));
assert(check_equal(array_get(B, array, 2), v1));
assert(array->count == 3);
TValue * shiftValue1 = array_shift(B, array);
TValue * shiftValue2 = array_shift(B, array);
TValue * shiftValue3 = array_shift(B, array);
assert(check_equal(shiftValue1, v3));
assert(check_equal(shiftValue2, v2));
assert(check_equal(shiftValue3, v1));
assert(array->count == 0);
}
void test_barray_set_value(bean_State * B) {
Array * array = init_array(B);
TValue * v1 = malloc(sizeof(TValue));
TValue * v2 = malloc(sizeof(TValue));
TValue * v3 = malloc(sizeof(TValue));
array_set(B, array, 10, v1);
assert(array->count == 11);
assert(array->capacity == 16);
array_set(B, array, 60, v2);
assert(array->count == 61);
assert(array->capacity == 64);
array_set(B, array, 80, v3);
assert(array->count == 81);
assert(array->capacity == 96);
assert(check_equal(array_get(B, array, 80), v3));
assert(check_equal(array_get(B, array, 60), v2));
assert(check_equal(array_get(B, array, 10), v1));
}
static TValue * getValue(bean_State * B, uint32_t i) {
TValue * value = malloc(sizeof(TValue));
setnvalue(value, i);
return value;
}
static TValue * getKey(bean_State * B, uint32_t i) {
TValue * key = malloc(sizeof(TValue));
char keyBuffer[HASH_MAX_LEN_OF_KEY];
sprintf(keyBuffer, "key-%d", i);
TString * sKey = beanS_newlstr(B, keyBuffer, strlen(keyBuffer));
setsvalue(key, sKey);
return key;
}
static void testing_value_by_count (bean_State * B, Hash * hash, uint32_t count) {
for (uint32_t i = 0; i < count; i ++) {
TValue * key = getKey(B, i);
TValue * value = getValue(B, i);
hash_set(B, hash, key, value);
}
for (uint32_t i = 0; i < count; i ++) {
TValue * key = getKey(B, i);
TValue * value = getValue(B, i);
assert(check_equal(hash_get(B, hash, key), value));
}
}
void test_hash_set(bean_State * B) {
Hash * hash = init_hash(B);
assert(hash->count == 0);
assert(hash->capacity == 16);
testing_value_by_count(B, hash, 10);
assert(hash->count == 10);
assert(hash->capacity == 16);
testing_value_by_count(B, hash, 100);
assert(hash->count == 100);
assert(hash->capacity == 256);
}
void test_bhash() {
bean_State * B = get_state();
test_hash_set(B);
printf("==============================\n");
printf("All tests of hash passed!\n");
printf("==============================\n");
}
void test_barray() {
bean_State * B = get_state();
test_barray_pop_and_push(B);
test_barray_shift_and_unshift(B);
test_barray_set_value(B);
printf("==============================\n");
printf("All tests of array passed!\n");
printf("==============================\n");
}
void test_tokens() {
uint32_t size = TK_STRING - TK_AND + 1;
for (uint32_t i = 0; i < size; i++) {
assert(memcmp(symbol_table[i].id, beanX_tokens[i], strlen(symbol_table[i].id)) == 0);
}
printf("Length of tokens area %d.\n", size);
}
void test() {
test_barray();
test_bhash();
test_tokens();
}