-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_two.c
242 lines (197 loc) · 7.55 KB
/
test_two.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "process.h"
int main(int argc, const char * argv[])
{
if(argc != 3)
{
printf("Please enter only the first PID and maximum size of memory.\n");
return 0;
}
else
{
// first tree
struct process* first = make_process(1, 1);
first->left = make_process(2, 2);
first->right = make_process(3, 3);
first->left->left = make_process(4, 4);
first->left->right = make_process(5, 5);
first->right->left = make_process(6, 6);
// second tree
struct process* second = make_process(7, 7);
second->right = make_process(8, 8);
second->right->left = make_process(9, 9);
second->right->right = make_process(10, 10);
second->right->left->right = make_process(11, 11);
// Test 1: contains_pid
printf("Testing contains_pid\n");
printf("%d\n", contains_pid(first, 2)); // 1
printf("%d\n", contains_pid(first, 6)); // 1
printf("%d\n", contains_pid(first, 23)); // 0
printf("%d\n", contains_pid(NULL, 2)); // 0
printf("%d\n", contains_pid(second, 11)); // 1
printf("%d\n", contains_pid(second, 223)); // 0
// Test 2: total_mem
printf("Testing total_mem\n");
printf("%d\n", total_mem(NULL)); // 0
printf("%d\n", total_mem(first)); // 21
printf("%d\n", total_mem(second)); // 45
// Test 3: can_add
printf("Testing can_add\n");
// unique PIDs
printf("%d\n", can_add(second, make_process(7, 7), 1000) ); // 0
printf("%d\n", can_add(second, make_process(11, 7), 1000) ); // 0
printf("%d\n", can_add(second, make_process(12, 7), 1000) ); // 1
// max memory
printf("%d\n", can_add(second, make_process(12, 1), 0) ); // 0
printf("%d\n", can_add(second, make_process(12, 1), 45) ); // 0
printf("%d\n", can_add(second, make_process(12, 1), 46) ); // 1
printf("%d\n", can_add(second, make_process(12, 1), 47) ); // 1
// adding NULL
// printf("%d\n", can_add(second, NULL, 46) ); // should abort -- can_add: Assertion `new_node' failed.
// Test 4: insert with level order
// To begin, we'll test this with two of our trees from part one.
// insert level order to first tree:
int max_mem = 56;
insert_levelorder(&first, make_process(7, 6), max_mem);
print_levelorder(first); // Levelorder: 1 2 3 4 5 6 7
print_inorder(first); // Inorder: 4 2 5 1 6 3 7
insert_levelorder(&first, make_process(8, 6), max_mem);
print_levelorder(first); // Levelorder: 1 2 3 4 5 6 7 8
print_inorder(first); // Inorder: 8 4 2 5 1 6 3 7
insert_levelorder(&first, make_process(9, 6), max_mem);
print_levelorder(first); // Levelorder: 1 2 3 4 5 6 7 8 9
print_inorder(first); // Inorder: 8 4 9 2 5 1 6 3 7
insert_levelorder(&first, make_process(10, 6), max_mem);
print_levelorder(first); // Levelorder: 1 2 3 4 5 6 7 8 9 10
print_inorder(first); // Inorder: 8 4 9 2 10 5 1 6 3 7
// now practice adding to a non-complete tree:
// insert_levelorder(&second, make_process(12, 12), 1); // should abort since it's not complete: Assertion `is_complete(*root)' failed.
// make the tree
struct process *third = NULL;
insert_levelorder(&third, make_process(20, 5), max_mem);
print_levelorder(third); // should make a tree with only 20 in it
print_inorder(third); // 20
// make a non-complete tree
third->right = make_process(25, 23);
print_levelorder(third); // Levelorder: 20 25
print_inorder(third); // Inorder: 20 25
// and add to it to complete it:
insert_levelorder(&third, make_process(24, 24), max_mem);
print_levelorder(third); // Levelorder: 20 24 25
print_inorder(third); // Inorder: 24 20 25
// and test the can_add assertion:
// insert_levelorder(&third, make_process(24, 24), max_mem); // should abort: Assertion `can_add(*root, to_add, max_mem)' failed.
// insert_levelorder(&third, make_process(284, 242222), max_mem); // should abort: Assertion `can_add(*root, to_add, max_mem)' failed.
// Test 5: create_tree
printf("Testing create_tree\n");
int first_pid = atoi(argv[1]);
int max_size = atoi(argv[2]);
// this is set up for specific entries from the command-line
assert(first_pid == 50);
assert(max_size == 24);
struct process *fourth = create_tree(first_pid, max_size, 1, 3);
print_levelorder(fourth);
print_inorder(fourth);
/* Levelorder: 50 51 52
Inorder: 51 50 52 */
fourth = create_tree(first_pid, max_size, 1, 24);
print_levelorder(fourth);
print_inorder(fourth);
/*
Levelorder: 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
Inorder: 65 57 66 53 67 58 68 51 69 59 70 54 71 60 72 50 73 61 55 62 52 63 56 64
*/
// fourth = create_tree(first_pid, max_size, 1, 25);
// two: process.c:244: insert_levelorder: Assertion `can_add(*root, to_add, max_mem)' failed.
// Test 6: is_sorted
printf("Testing is_sorted\n");
printf("%d\n", is_sorted(NULL)); // 1
printf("%d\n", is_sorted(first)); // 1
printf("%d\n", is_sorted(second)); // 1
first->right->left = make_process(12, 5);
printf("%d\n", is_sorted(first)); // 0
struct process *sixth = make_process(20, 6);
sixth->left = make_process(17, 6);
printf("%d\n", is_sorted(sixth)); // 0
// Test 7: get_min
printf("Testing get_min\n");
print_process(get_min(NULL, 0)); // NULL: blank line
printf("\n");
print_process(get_min(fourth, 0)); // 50
printf("\n");
print_process(get_min(fourth, 50)); // 50
printf("\n");
print_process(get_min(fourth, 51)); // 51
printf("\n");
print_process(get_min(fourth, 72)); // 72
printf("\n");
print_process(get_min(fourth, 73)); // 73
printf("\n");
print_process(get_min(fourth, 74)); // NULL: blank line
printf("\n");
// get_min(first, 0); // should abort: get_min: Assertion `is_sorted(root)' failed.
// Test 8: rebuild_tree
printf("Testing rebuild_tree\n");
// rebuild a tree that doesn't need it
printf("%d\n", rebuild_tree(&fourth)); // 0
printf("%d\n", rebuild_tree(NULL)); // 0
// make a small tree to rebuild
struct process *fifth = make_process(17, 3);
fifth->right = make_process(20, 4);
// when rebuilt, the 20 node should be on the left
rebuild_tree(&fifth);
print_levelorder(fifth); // 17 20
print_inorder(fifth); // 20 17
rebuild_tree(&sixth);
print_levelorder(sixth); // 17 20
print_inorder(sixth); // 20 17
// and finally, rebuild second
rebuild_tree(&second);
print_levelorder(second); // 7 8 9 10 11
print_inorder(second); // 10 8 11 7 9
second->right->right = make_process(20, 3);
print_levelorder(second); // 7 8 9 10 11 20
print_inorder(second); // 10 8 11 7 9 20
rebuild_tree(&second);
print_levelorder(second); // 7 8 9 10 11 20
print_inorder(second); // 10 8 11 7 20 9
// Test 9: kill
printf("Testing kill\n");
// kill on NULL
printf("%d\n", kill(NULL, 5)); // 0
// kill a PID not in tree
printf("%d\n", kill(&second, 5)); // 0
// kill a leaf
kill(&first, 8);
print_levelorder(first); // 1 2 3 4 5 7 9 10 12
print_preorder(first); // 1 2 4 10 12 5 3 7 9
// kill another leaf (left)
kill(&first, 9);
print_levelorder(first); // 1 2 3 4 5 7 10 12
print_preorder(first); // 1 2 4 12 5 3 7 10
// kill another leaf (right)
kill(&first, 12);
print_levelorder(first); // 1 2 3 4 5 7 10
print_preorder(first); // 1 2 4 5 3 7 10
// kill root
struct process *seventh = make_process(80, 1);
kill(&seventh, 80);
print_levelorder(seventh); // is null
// kill a different root
kill(&first, 1);
print_levelorder(first); // 2 3 4 5 7 10
print_preorder(first); // 2 3 5 7 4 10
// kill a parent with a right child
kill(&first, 3);
print_levelorder(first); // 2 4 5 7 10
print_preorder(first); // 2 4 7 10 5
// kill a parent with a left child and right child
kill(&first, 4);
print_levelorder(first); // 2 5 7 10
print_preorder(first); // 2 5 10 7
// kill a parent with only left child
kill(&first, 5);
print_levelorder(first); // 2 7 10
print_preorder(first); // 2 7 10
}
return 0;
}