-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_stack.c
192 lines (163 loc) · 3.72 KB
/
test_stack.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
/* ========== H03 - Personal Queue Implementation ============
*
* Student: UPDATE
* Semester: UPDATE
*
* A simple queue unit-tst implementation
*
*/
#include "mystack.h"
// Note that we are locating this file
// within the same directory, so we use quotations
// and provide the path to this file which is within
// our current directory.
// A sample test of your program
// You can add as many unit tests as you like
// We will be adding our own to test your program.
// Tests the capacity of a stack
int unitTest1(int status)
{
int passed = 0;
stack_t *test_s = create_stack(MAX_DEPTH);
if (MAX_DEPTH == test_s->capacity)
{
passed = 1;
}
free_stack(test_s);
return passed;
}
// Enqueu several items into a stack and test the size
int unitTest2(int status)
{
int passed = 0;
stack_t *test_s = create_stack(MAX_DEPTH);
stack_enqueue(test_s, 1);
stack_enqueue(test_s, 2);
stack_enqueue(test_s, 3);
stack_enqueue(test_s, 4);
stack_enqueue(test_s, 5);
stack_enqueue(test_s, 6);
stack_enqueue(test_s, 7);
stack_enqueue(test_s, 8);
stack_enqueue(test_s, 9);
stack_enqueue(test_s, 10);
if (10 == stack_size(test_s))
{
passed = 1;
}
free_stack(test_s);
return passed;
}
// Tests enqueuing and fully dequeing a stack
int unitTest3(int status)
{
int passed = 0;
stack_t *test_s = create_stack(MAX_DEPTH);
int i = 0;
for (i = 0; i < MAX_DEPTH; i++)
{
stack_enqueue(test_s, 1);
}
for (i = 0; i < MAX_DEPTH; i++)
{
stack_dequeue(test_s);
}
if (0 == stack_size(test_s))
{
passed = 1;
}
free_stack(test_s);
return passed;
}
// Tests enqueuing and fully dequeing a stack multiple times
int unitTest4(int status)
{
int passed = 0;
stack_t *test_s = create_stack(MAX_DEPTH);
int i = 0;
for (i = 0; i < MAX_DEPTH; i++)
{
stack_enqueue(test_s, 1);
}
for (i = 0; i < MAX_DEPTH; i++)
{
stack_dequeue(test_s);
}
for (i = 0; i < MAX_DEPTH; i++)
{
stack_enqueue(test_s, 1);
}
for (i = 0; i < MAX_DEPTH; i++)
{
stack_dequeue(test_s);
}
if (0 == stack_size(test_s))
{
passed = 1;
}
free_stack(test_s);
return passed;
}
// Simple enqueu and deque stack test
// Also confirms that a stack is full
int unitTest5(int status)
{
int passed = 0;
stack_t *test_s = create_stack(1);
stack_enqueue(test_s, 1);
if (1 == stack_full(test_s))
{
passed = 1;
}
else
{
free_stack(test_s);
return 0;
}
stack_dequeue(test_s);
if (0 == stack_full(test_s))
{
passed = 1;
}
else
{
passed = 0;
}
free_stack(test_s);
return passed;
}
// TODO: Add more tests here
// add your own, and uncomment the provided tests as
// things are implemented
int (*unitTests[])(int) = {
// unitTest1,
// unitTest2,
// unitTest3,
// unitTest4,
// unitTest5,
NULL};
// ====================================================
// ================== Program Entry ===================
// ====================================================
int main()
{
unsigned int testsPassed = 0;
// List of Unit Tests to test your data structure
int counter = 0;
while (unitTests[counter] != NULL)
{
printf("========unitTest %d========\n", counter);
if (1 == unitTests[counter](1))
{
printf("passed test\n");
testsPassed++;
}
else
{
printf("failed test, missing functionality, or incorrect test\n");
}
counter++;
}
printf("%d of %d tests passed\n", testsPassed, counter);
return 0;
}