-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist_test.c
132 lines (116 loc) · 3.02 KB
/
linkedlist_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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.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;
}
// Test the values are stored properly
assert(val1->i == INT);
assert(!strcmp(val2->s, STR));
assert(val3->d == DOUBLE);
// Test that makeNull creates an empty list
Value *head = makeNull();
if (!head) {
printf("Out of memory!\n");
return 1;
}
assert(head->type == NULL_TYPE);
assert(isNull(head));
assert(length(head) == 0);
// Test the result of cons value with empty list
head = cons(val1, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
assert(length(head) == 1);
assert(head->type == CONS_TYPE);
assert(car(head) == val1);
assert(!isNull(head));
assert(isNull(cdr(head)));
display(head);
printf("------------------------\n");
// Test the result of cons value to non-emptylist
head = cons(val2, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
assert(length(head) == 2);
assert(head->type == CONS_TYPE);
assert(car(head) == val2);
assert(car(cdr(head)) == val1);
assert(isNull(cdr(cdr(head))));
display(head);
printf("------------------------\n");
// Add another value
head = cons(val3, head);
if (!head) {
printf("Out of memory!\n");
return 1;
}
assert(length(head) == 3);
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 deep copy, so changing head should
// not affect reversed
car(head)->d = 3.2;
strcpy(car(cdr(head))->s, "soup");
printf("head: \n");
display(head);
printf("reversed: \n");
display(reversed);
return 0;
<<<<<<< HEAD
}
=======
}
>>>>>>> f0b82c1980d608a5228f60d124664e15100d4454