-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
175 lines (150 loc) · 3.62 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
#include "main.c"
void test1() {
printf("Test 1: Objects on stack are preserved.\n");
VM* vm = newVM();
pushInt(vm, 1);
pushInt(vm, 2);
gc(vm);
assert(vm->numObjects == 2, "Should have preserved objects.\n");
freeVM(vm);
}
void test2() {
printf("Test 2: Unreached objects are collected.\n");
VM* vm = newVM();
pushInt(vm, 1);
pushInt(vm, 2);
pop(vm);
pop(vm);
gc(vm);
assert(vm->numObjects == 0, "Should have collected objects.\n");
freeVM(vm);
}
void test3() {
printf("Test 3: Reach nested objects.\n");
VM* vm = newVM();
pushInt(vm, 1);
pushInt(vm, 2);
pushPair(vm);
pushInt(vm, 3);
pushInt(vm, 4);
pushPair(vm);
pushPair(vm);
gc(vm);
assert(vm->numObjects == 7, "Should have collected objects.\n");
freeVM(vm);
}
void test4() {
printf("Test 4: Handle cycles.\n");
VM* vm = newVM();
pushInt(vm, 1);
pushInt(vm, 2);
Object* a = pushPair(vm);
pushInt(vm, 3);
pushInt(vm, 4);
Object* b = pushPair(vm);
a->tail = b;
b->tail = a;
gc(vm);
assert(vm->numObjects == 4, "Should have collected objects.\n");
freeVM(vm);
}
void test5() {
printf("Test 5: Handle large number of objects.\n");
printf("Insert number of objects: ");
int nOb = 0;
scanf("%d", &nOb);
VM* vm = newVM();
for (int i = 0; i < nOb; i++) {
pushInt(vm, i);
}
for (int i = 0; i < nOb; i++) {
pop(vm);
}
gc(vm);
assert(vm->numObjects == 0, "Should have collected objects.\n");
freeVM(vm);
}
void test6(){
printf("Print objects in the list (int).\n");
VM* vm = newVM();
Object* object_int_1 = newObject(vm, OBJ_INT);
object_int_1->value = 1;
push(vm, object_int_1);
Object* object_int_2 = newObject(vm, OBJ_INT);
object_int_2->value = 2;
push(vm, object_int_2);
ObjectPrint(object_int_1);
ObjectPrint(object_int_2);
gc(vm);
assert(vm->numObjects == 2, "Should have preserved objects.\n");
freeVM(vm);
}
void test7(){
printf("Print objects in the list (pair).\n");
VM* vm = newVM();
//first int
Object* object_int_1 = newObject(vm, OBJ_INT);
object_int_1->value = 1;
push(vm, object_int_1);
//second int
Object* object_int_2 = newObject(vm, OBJ_INT);
object_int_2->value = 2;
push(vm, object_int_2);
//first pair
Object* object_pair_1 = newObject(vm, OBJ_PAIR);
object_pair_1->head = pop(vm);
object_pair_1->tail = pop(vm);
push(vm, object_pair_1);
ObjectPrint(object_pair_1);
gc(vm);
assert(vm->numObjects == 3, "Should have preserved objects.\n");
freeVM(vm);
}
void perfTest() {
printf("Performance Test: Create lots of objects.\n");
VM* vm = newVM();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 20; j++) {
pushInt(vm, i);
}
for (int j = 0; j < 20; j++) {
pop(vm);
}
}
freeVM(vm);
}
int main(int argc, const char * argv[]) {
int test = 0;
printf("Insert test here: \n");
scanf("%d", &test);
switch(test){
case 1:
test1();
break;
case 2:
test2();
break;
case 3:
test3();
break;
case 4:
test4();
break;
case 5:
test5();
break;
case 6:
test6();
break;
case 7:
test7();
break;
case 8:
perfTest();
break;
default:
printf("Invalid test number");
break;
}
return 0;
}