forked from subailong/datalgo
/
list_tests.c
182 lines (123 loc) · 3.04 KB
/
list_tests.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
//
// list_tests.c
// datalgo
// Created by Iyed Bennour on 09/12/2014.
// Copyright (c) 2014 Iyed Bennour. All rights reserved.
//
//
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include "list.h"
#include "list_tests.h"
void print_node(node_t *n) {
printf("%d\n", n->val);
}
bool test_add_node() {
bool test_result = true;
node_t *head = NULL;
add_node(&head, 1);
assert(head->val == 1);
assert(list_len(head) == 1);
add_node(&head, 2);
assert(list_len(head) == 2);
assert(find_node(head, 1) != NULL);
assert(find_node(head, 2) != NULL);
assert(find_node(head, 3) == NULL);
add_node(&head, 3);
//traverse_node(head, print_node);
assert(find_node(head, 3) != NULL);
assert(find_node(head, 3)->next->val == 3);
return test_result && find_node(head, 3) != NULL;
}
bool test_find_node() {
node_t *head = NULL;
add_node(&head, 2);
add_node(&head, 1);
return find_node_rec(head, 1) != NULL && find_node_rec(head, 1) != NULL && find_node_rec(head, 3) == NULL;
}
void test_remove_dups() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 2);
node_t *new_head = remove_list_dups(&head);
assert(list_len(new_head) == 2);
traverse_node(new_head, print_node);
}
bool test_delete_list() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 2);
free_list(&head);
assert(list_len(head) == 0);
return head == NULL;
}
bool test_remove_dups2() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 2);
add_node(&head, 1);
remove_list_dups2(head);
assert(list_len(head) == 2);
//traverse_node(head, print_node);
return list_len(head) == 2;
}
bool test_reverser_list() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 3);
add_node(&head, 4);
add_node(&head, 5);
add_node(&head, 6);
reverse_list(&head);
traverse_node(head, print_node);
return list_len_rec(head) == 6 && head->val == 6;
}
bool test_remove_node() {
node_t *head = NULL;
add_node(&head, 2);
add_node(&head, 1);
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 4);
add_node(&head, 1);
add_node(&head, 1);
remove_node(&head, 1);
//traverse_node(head, print_node);
return list_len(head) == 3 && find_node(head, 1) == NULL;
}
bool test_find_last_n() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 3);
add_node(&head, 4);
add_node(&head, 5);
add_node(&head, 6);
node_t *n = find_last_n(head, 6);
return n->val == 1;
}
bool test_free_list() {
node_t *head = NULL;
add_node(&head, 1);
add_node(&head, 2);
add_node(&head, 3);
add_node(&head, 4);
add_node(&head, 5);
add_node(&head, 6);
free_list(&head);
if (head == NULL && list_len(head) == 0)
return 1;
else
return 0;
}