forked from IshitaTakeshi/Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.c
155 lines (122 loc) · 3.23 KB
/
queue_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
#include <stdio.h>
#include "queue.h"
int size = 4;
char items[] = {'A', 'B', 'C', 'D'};
int test_enqueue_and_deque() {
int i;
init_queue(size);
for(i=0; i<size; i++) {
enqueue(&items[i]);
printf("inserted %c\n", items[i]);
}
//all items should be gotten in the insertion order
char *item;
int success = 1;
for(i=0; i<size; i++) {
item = (char *)dequeue();
success &= (items[i] == *item);
printf("got %c, should be gotten %c\n", *item, items[i]);
}
free_queue();
return success;
}
int test_is_full() {
int success = 1;
int i, is_full;
init_queue(size);
//The queue should not be full when immidiately after initialization
is_full = queue_is_full();
success &= !is_full;
printf("is_full:%d\n", is_full);
for(i=0; i<size; i++) {
enqueue(&items[i]);
printf("inserted %c\n", items[i]);
}
//The queue should be full when all items inserted.
is_full = queue_is_full();
success &= is_full;
printf("is_full:%d\n", is_full);
free_queue();
return success;
}
int test_is_empty() {
int success = 1;
int i, is_empty;
init_queue(size);
//The queue should be empty when immidiately after initialization
is_empty = queue_is_empty();
success &= is_empty;
printf("is_empty:%d\n", is_empty);
for(i=0; i<size; i++) {
enqueue(&items[i]);
printf("inserted %c\n", items[i]);
}
//The queue should not be empty when all items inserted.
is_empty = queue_is_empty();
success &= !is_empty;
printf("is_empty:%d\n", is_empty);
free_queue();
return success;
}
int test_get_n_items() {
int success = 1;
int i;
int s;
init_queue(size);
for(i=0; i<size; i++) {
enqueue(&items[i]);
s = get_n_items();
printf("get_n_items(): %d the real size: %d\n", s, i+1);
success &= (get_n_items() == i+1);
}
free_queue();
return success;
}
void should_exit_when_too_many_items_inserted() {
int success = 1;
int i;
int s;
init_queue(size);
//insert items more than the size of the queue
for(i=0; i<size+1; i++) {
enqueue(&items[0]);
}
free_queue();
}
void should_exit_when_dequeue_called_if_empty() {
int i;
init_queue(size);
for(i=0; i<size; i++) {
enqueue(&items[i]);
}
//pop items more than kept in the queue
for(i=0; i<size+1; i++) {
dequeue();
}
free_queue();
}
int main(void) {
if(test_get_n_items()) {
printf("get_n_items works correctly.\n\n");
} else {
printf("get_n_items doesn't work correctly.\n\n");
}
if(test_is_empty()) {
printf("is_empty works correctly.\n\n");
} else {
printf("is_empty doesn't work correctly.\n\n");
}
if(test_is_full()) {
printf("is_full works correctly.\n\n");
} else {
printf("is_full doesn't work correctly.\n\n");
}
if(test_enqueue_and_deque()) {
printf("enqueue and dequeue works correctly. \n\n");
} else {
printf("enqueue and dequeue doesn't work correctly.\n\n");
}
//should_exit_when_too_many_items_inserted();
//should_exit_when_dequeue_called_if_empty();
return 0;
}