-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInterpreterTests.java
More file actions
72 lines (71 loc) · 1.96 KB
/
Copy pathInterpreterTests.java
File metadata and controls
72 lines (71 loc) · 1.96 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
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.*;
import java.util.*;
public class InterpreterTests {
Stack<Integer> execStack;
@Before
public void init() {
execStack = new Stack<>();
}
@Test
public void BUPUSH10() {
BIPUSH bipush10 = new BIPUSH(10);
int result = bipush10.exec(execStack);
assertEquals(10, result);
}
@Test
public void IADD() {
IADD iadd = new IADD();
BIPUSH bipush10 = new BIPUSH(10);
BIPUSH bipush20 = new BIPUSH(20);
bipush10.exec(execStack);
bipush20.exec(execStack);
int result = iadd.exec(execStack);
assertEquals(result, 30);
}
@Test
public void IMUL() {
IMUL imul = new IMUL();
BIPUSH bipush10 = new BIPUSH(10);
BIPUSH bipush20 = new BIPUSH(20);
bipush10.exec(execStack);
bipush20.exec(execStack);
int result = imul.exec(execStack);
assertEquals(result, 200);
}
@Test
public void IDIV() {
IDIV idiv = new IDIV();
BIPUSH bipush10 = new BIPUSH(10);
BIPUSH bipush20 = new BIPUSH(20);
bipush10.exec(execStack);
bipush20.exec(execStack);
int result = idiv.exec(execStack);
assertEquals(result, 0);
}
@Test(expected = DivByZeroException.class)
public void IDIVByZero() {
IDIV idiv = new IDIV();
BIPUSH bipush10 = new BIPUSH(10);
BIPUSH bipush0 = new BIPUSH(0);
bipush10.exec(execStack);
bipush0.exec(execStack);
idiv.exec(execStack);
}
@Test
public void InterpreterBytecodeSequence1() {
List<ByteCode> byteCodes = Arrays.asList(new BIPUSH(10), new BIPUSH(20), new IADD());
Interpreter interpret = new Interpreter(execStack);
int result = interpret.interpret(byteCodes);
assertEquals(30, result);
}
@Test
public void InterpreterBytecodeSequence2() {
List<ByteCode> byteCodes = Arrays.asList(new BIPUSH(10), new BIPUSH(20), new IMUL(), new BIPUSH(30), new IADD());
Interpreter interpret = new Interpreter(execStack);
int result = interpret.interpret(byteCodes);
assertEquals(230, result);
}
}