-
Notifications
You must be signed in to change notification settings - Fork 0
/
talloc_test.c
98 lines (88 loc) · 2.19 KB
/
talloc_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
#include <stdio.h>
#include <string.h>
#include "value.h"
#include "talloc.h"
#include "linkedlist.h"
#define INT 23
#define DOUBLE 2.3
#define STR "tofu"
int main(void) {
Value *val1 = talloc(sizeof(Value));
if (!val1) {
printf("Out of memory!\n");
return 1;
}
val1->type = INT_TYPE;
val1->i = INT;
Value *val2 = talloc(sizeof(Value));
if (!val2) {
printf("Out of memory!\n");
return 1;
}
val2->type = STR_TYPE;
val2->s = talloc(10 * sizeof(char));
if (!(val2->s)) {
printf("Out of memory!\n");
return 1;
}
strcpy(val2->s, STR);
Value *val3 = talloc(sizeof(Value));
if (!val3) {
printf("Out of memory!\n");
return 1;
}
val3->type = DOUBLE_TYPE;
val3->d = DOUBLE;
Value *nullHead = makeNull();
if (!nullHead) {
printf("Out of memory!\n");
return 1;
}
// Switching to talloc should not affect the behavior
// of the methods in Linkedlist
Value *head = makeNull();
if (!head) {
printf("Out of memory!\n");
return 1;
}
head = cons(val1, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
head = cons(val2, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
head = cons(val3, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
display(head);
printf("------------------------\n");
// Test the reverse method
Value *reversed = reverse(head);
if (!reversed) {
printf("Out of memory!\n");
return 1;
}
assert(!isNull(reversed));
assert(length(reversed) == 3);
// reversed actually reverse the list
assert(car(head)->d == DOUBLE);
assert(!strcmp(car(cdr(head))->s, STR));
assert(car(cdr(cdr(head)))->i == INT);
// reversed should be a shallow copy, so changing head should
// affect reversed
car(head)->d = 3.2;
strcpy(car(cdr(head))->s, "soup");
printf("head: \n");
display(head);
printf("reversed: \n");
display(reversed);
// cleanup the original list and the reversed list
texit(0);
return 0;
}