- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstacktest.c
More file actions
Latest commit
122 lines (97 loc) · 2.66 KB
/
Copy pathstacktest.c
File metadata and controls
122 lines (97 loc) · 2.66 KB
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
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#defineMAX 100
voidcheckEmpty(Stacks) {
if(isEmpty(&s)) {
fprintf(stdout, "Stack is empty.\n");
}
else {
fprintf(stderr, "Error: Stack should be empty.\n");
exit(1);
}
}
voidcheckSize(Stacks, intn) {
if(size(&s) ==n) {
fprintf(stdout, "Size of stack is %d.\n", n);
}
else {
fprintf(stderr, "Error: Stack size is not equal to number of elements pushed.\n");
}
}
intmain() {
Stacks;
fprintf(stdout, "Running tests for stack of int...\n\n");
/* Make a stack of int */
init(&s, sizeof(int), MAX);
/* Initially stack should be empty */
checkEmpty(s);
/* Now insert n integers */
inti, n=10;
for(i=1; i <= n; i++) {
push(&s, (void*)&i);
}
/* size of stack should be n */
checkSize(s, n);
/* top element should be n */
intt;
t=*((int*)top(&s));
if(t==n) {
fprintf(stdout, "Top element is %d.\n", t);
}
else {
fprintf(stderr, "Error: Top element is not equal to last pushed element.\n");
fprintf(stderr, "Expected %d, got %d.\n", n, t);
exit(1);
}
/* pop all elements */
for(i=n; i >= 1; i--) {
t=*((int*)pop(&s));
if(i!=t) {
fprintf(stderr, "Error: Elements not being popped in LIFO manner.\n");
fprintf(stderr, "Expected %d but got %d.\n", i , t);
exit(1);
}
}
/* Stack should be empty now */
checkEmpty(s);
destroy(&s);
fprintf(stdout, "All tests passed for stack of int.\n\n");
fprintf(stdout, "Running tests for stack of double...\n\n");
/* Make a stack of double */
init(&s, sizeof(double), MAX);
/* Initially stack should be empty */
checkEmpty(s);
/* Now insert n double values */
doublevalues[] = {1.1, -0.5, 9.7, 11.0, 15.1, -72.7, 45.6, 10.1, 5.5, 8.3};
for(i=0; i<n; i++) {
push(&s, (void*)(values+i));
}
/* size of stack should be n */
checkSize(s, n);
/* top element should be last pushed element */
doubled;
d=*((double*)top(&s));
if(d==values[n-1]) {
fprintf(stdout, "Top element is %.1lf.\n", d);
}
else {
fprintf(stderr, "Error: Top element is not equal to last pushed element.\n");
fprintf(stderr, "Expected %.1lf, got %.1lf.\n", values[n-1], d);
exit(1);
}
/* pop all elements */
for(i=n-1; i >= 0; i--) {
d=*((double*)pop(&s));
if(values[i] !=d) {
fprintf(stderr, "Error: Elements not being popped in LIFO manner.\n");
fprintf(stderr, "Expected %.1lf but got %.1lf.\n", values[i] , d);
exit(1);
}
}
/* Stack should be empty now */
checkEmpty(s);
destroy(&s);
fprintf(stdout, "All tests passed for stack of double.\n\n");
return0;
}