-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStackTest.java
More file actions
205 lines (159 loc) · 6.6 KB
/
ArrayStackTest.java
File metadata and controls
205 lines (159 loc) · 6.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
public class ArrayStackTest {
/**
* Calculates the value of a given postfix expression.
* @param postfixString The postfix expression to be evaluated.
* @param valueOfVar The values from left to right, of each variable a-e.
* @return The integer value of the evaluated postfix expression.
*/
public static int evaluatePostfix(String postfixString, int[] valueOfVar ) {
StackInterface<Integer> valueStackInterface = new ResizeableArrayStack<>() ;
char nextItem ;
int operand1 ;
int operand2 ;
int result = 0 ;
int i = 0 ;
if (valueOfVar != null && postfixString != null) {
while(i < postfixString.length()){
nextItem = postfixString.charAt(i);
if (charVariable(nextItem)){
valueStackInterface.push(determineValue(nextItem, valueOfVar));
} else {
operand2 = valueStackInterface.pop();
operand1= valueStackInterface.pop();
switch(nextItem){
case '+':result = operand1 + operand2;
break;
case '*':result = operand1 * operand2;
break;
case '-':result = operand1 - operand2;
break;
case '/':result = operand1 / operand2;
break;
default:break;
}
valueStackInterface.push(result);
}
i++ ;
}
return valueStackInterface.pop();
} else {
// Returns -1 if either postFixString or valueOfVar are null
return -1;
}
}
/**
* Checks whether input is one of the five variable characters.
* @param ch The character that is being checked for being a variable.
* @return True if the character is a variable, false if not.
*/
// Unit Testing for this method is in this file, since it is private
private static boolean charVariable(char ch){
return(ch == 'a') || (ch == 'b') || (ch == 'c') || (ch == 'd') || (ch == 'e');
}
/**
* Determines the value of a given variable character.
* @param nextItem The variable whos value will be found.
* @param valuesArray The values for all five possible variables.
* @return The variable's correct value, or -1 if there is no correct value.
*/
// Unit Testing for this method is in this file, since it is private
private static int determineValue(char nextItem, int[] valuesArray) {
int result = -1;
switch(nextItem){
case'a':
if (valuesArray.length >= 1)
result = valuesArray[0];
break;
case'b':
if (valuesArray.length >= 2)
result = valuesArray[1];
break;
case'c':
if (valuesArray.length >= 3)
result = valuesArray[2];
break;
case'd':
if (valuesArray.length >= 4)
result = valuesArray[3];
break;
case'e':
if (valuesArray.length >= 5)
result = valuesArray[4];
break;
default:
break;
}
return result;
}
public static void main(String[] args) {
int[] numbers = {2,3,4,5,6};
String postfixStr = "ab*ca-/de*+";
System.out.println(evaluatePostfix(postfixStr,numbers));
}
/**
* Testing charVariable (Private method)
*/
// Testing charVariable with valid input
@Test
public void charVariable_ValidTest() {
char input1 = 'a';
char input2 = 'b';
char input3 = 'c';
char input4 = 'd';
char input5 = 'e';
assertTrue(charVariable(input1));
assertTrue(charVariable(input2));
assertTrue(charVariable(input3));
assertTrue(charVariable(input4));
assertTrue(charVariable(input5));
}
// Testing charVariable with invalid input
@Test
public void charVariable_InvalidTest() {
char invalidVar1 = 'g';
char invalidVar2 = 'o';
char invalidVar3 = '=';
assertFalse(charVariable(invalidVar1));
assertFalse(charVariable(invalidVar2));
assertFalse(charVariable(invalidVar3));
}
/**
* Testing determingValue (Private method)
*/
// Testing determineValue with valid input
@Test
public void determineValue_ValidInput() {
int[] valueArray = {2,3,4,5,6};
int[] shorterArray = {2,3,4};
char var1 = 'a';
char var2 = 'b';
char var3 = 'c';
char var4 = 'd';
char var5 = 'e';
assertEquals(valueArray[0], determineValue(var1,valueArray));
assertEquals(valueArray[1], determineValue(var2,valueArray));
assertEquals(valueArray[2], determineValue(var3,valueArray));
assertEquals(valueArray[3], determineValue(var4,valueArray));
assertEquals(valueArray[4], determineValue(var5,valueArray));
assertEquals(shorterArray[0], determineValue(var1,shorterArray));
assertEquals(shorterArray[1], determineValue(var2,shorterArray));
assertEquals(shorterArray[2], determineValue(var3,shorterArray));
assertEquals(-1, determineValue(var4, shorterArray));
assertEquals(-1, determineValue(var5, shorterArray));
}
// Testing determineValue with invalid input
@Test
public void determineValue_InvalidInput() {
int[] valuesArray = {2,3,4,5,6};
char var1 = 'a';
int[] invalidArray = {};
char invalidChar = 'p';
int expectedResult = -1;
assertEquals(expectedResult, determineValue(invalidChar,valuesArray));
assertEquals(expectedResult, determineValue(var1,invalidArray));
}
}