-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStack.java
More file actions
188 lines (162 loc) · 5.03 KB
/
TestStack.java
File metadata and controls
188 lines (162 loc) · 5.03 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
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
public class TestStack {
private static int[] readTests(String[] args, int numTests) {
int[] tests = new int[numTests];
int value;
if (args.length == 0)
for (int i = 0; i < numTests; ++i) tests[i] = i + 1;
else
for (int i = 0; i < args.length; ++i) {
value = Integer.parseInt(args[i]);
if (value >= 1 && value <= numTests)
tests[value-1] = value;
else System.out.println("ERROR: Test "+value+ "does not exist");
}
return tests;
}
public static void main(String[] args) {
boolean testPassed;
Integer val = null;
int[] tests = readTests(args,7);
ArrayStack<Integer> s = new ArrayStack<Integer>(30);
System.out.println("TESTS FOR CLASS ArrayStack");
System.out.println("==========================\n");
// Test 1. Test push and peek on a stack with few elements
if (tests[0] == 1 || tests[1] == 2 || tests[2] == 3) {
try {
for (int i = 0; i < 11; ++i)
s.push(Integer.valueOf(i));
val = s.peek();
if ((val.intValue() == 10) && (s.size() == 11))
testPassed = true;
else testPassed = false;
} catch (Exception e) {testPassed = false;}
if (testPassed)
System.out.println("Test 1 passed: push() and peek()");
else
System.out.println("Test 1 failed: push() and peek()");
if (tests[1] == 2 || tests[2] == 3) {
// Test 2. Test pop on a stack with few elements.
try {
testPassed = true;
for (int i = 0; i < 5; ++i)
val = s.pop();
if ((val.intValue() == 6) && !s.isEmpty())
testPassed = true;
else testPassed = false;
} catch (Exception e) {testPassed = false;}
if (testPassed)
System.out.println("Test 2 passed: pop()");
else
System.out.println("Test 2 failed: pop()");
}
if (tests[2] == 3) {
// Test 3. Pop on an empty stack
try {
for (int i = 0; i < 7; ++i)
val = s.pop();
testPassed = false;
} catch (EmptyStackException e) {testPassed = true;}
catch (Exception e) {testPassed = false;}
if (testPassed)
System.out.println("Test 3 passed: pop() empty");
else
System.out.println("Test 3 failed: pop() empty");
}
}
if (tests[3] == 4) {
// Test 4. Test that size of stack is increased correctly
s = new ArrayStack<Integer>(25);
try {
testPassed = true;
for (int i = 0; i < 226; ++i) {
s.push(Integer.valueOf(i));
if ((i == 25) && (s.length() != 35)) {
testPassed = false;
break;
}
else if ((i == 75) && (s.length() != 110)) {
testPassed = false;
break;
}
else if ((i == 225) && (s.length() != 440)) {
testPassed = false;
break;
}
}
} catch (Exception e) {testPassed = false; System.out.println(e);}
// System.out.println(s.length());
if (testPassed)
System.out.println("Test 4 passed: increase stack");
else System.out.println("Test 4 failed: increase stack");
}
if (tests[4] == 5) {
// Test 5. Test that the size of the stack is decreased correctly.
s = new ArrayStack<Integer>(200);
int result;
try {
testPassed = true;
for (int i = 0; i <= 25; ++i)
s.push(Integer.valueOf(i));
for (int i = 25; i > 1; --i) {
result = s.pop();
if ((i == 24) && (s.length() != 50)) {
System.out.println("19: "+s.length());
testPassed = false;
break;
}
else if ((i == 3) && (s.length() != 14)) {
System.out.println("9: "+s.length());
testPassed = false;
break;
}
}
if (s.length() != 14) testPassed = false;
} catch (Exception e) {testPassed = false;}
if (!testPassed)
System.out.println("Test 5 failed: decrease stack");
else System.out.println("Test 5 passed: decrease stack");
}
if (tests[5] == 6) {
// Test 6. Test push, pop, size
testPassed = true;
try {
s = new ArrayStack<Integer>();
for (int i = 0; i < 990; ++i)
s.push(Integer.valueOf(i));
if (s.size() != 990)
testPassed = false;
for (int i = 989; i >= 0; --i) {
val = s.pop();
if (val.intValue() != i) {
testPassed = false;
break;
}
}
} catch (Exception e) {testPassed = false;}
if (testPassed)
System.out.println("Test 6 passed: push(), pop(), size()");
else
System.out.println("Test 6 failed: push(), pop(), size()");
}
if (tests[6] == 7) {
// Test 7. Test toString
s = new ArrayStack<Integer>(3);
testPassed = true;
try {
for (int i = 0; i < 3; ++i)
s.push(Integer.valueOf(i));
String out = s.toString();
if (out.equals("Stack: 0, 1, 2")) testPassed = true;
else testPassed = false;
}
catch(Exception e) {testPassed = false;}
if (testPassed)
System.out.println("Test 7 passed: toString()");
else {
System.out.println("Test 7 failed: toString()");
System.out.println("Expected Result: Stack: 0, 1, 2");
System.out.println("Actual Result: " + s.toString());
}
}
}
}