-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJohnnyScriptTest.java
189 lines (151 loc) · 5.35 KB
/
JohnnyScriptTest.java
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
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/**
* Testing class for JohnnyScript
*/
public class JohnnyScriptTest {
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
// Valid file for testing
private final String validFilename = "jUnit";
private final String validExtension = ".jns";
private final String validFile = validFilename + validExtension;
private final Path inputPath = FileSystems.getDefault().getPath(validFile);
private final String validOutputExtension = ".ram";
private final String outputFile = validFilename + validOutputExtension;
private final Path outputPath = FileSystems.getDefault().getPath(outputFile);
@Before
public void setUp() throws Exception {
System.setErr(new PrintStream(errContent));
Files.createFile(inputPath);
}
@After
public void tearDown() throws Exception {
Files.deleteIfExists(inputPath);
Files.deleteIfExists(outputPath);
}
@Test(expected = IllegalArgumentException.class)
public void argumentMissing() throws Exception {
JohnnyScript.main(new String[]{});
assertEquals("No argument given.\nUsage: java JohnnyScript filename",errContent.toString());
}
@Test(expected = IllegalArgumentException.class)
public void invalidFilename() throws Exception {
String filename = "invalid";
JohnnyScript.main(new String[]{filename});
assertEquals("Invalid Filename: " + filename, errContent.toString());
}
@Test
public void validInput() throws Exception {
JohnnyScript.main(new String[]{validFile});
assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath));
}
@Test
public void commentLine() throws Exception {
List<String> testCode = new ArrayList<>();
testCode.add("add 0");
testCode.add("//Test comment");
testCode.add("add 0");
Files.write(inputPath, testCode);
JohnnyScript.main(new String[]{validFile});
assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath));
List<String> outLines = Files.readAllLines(outputPath);
testCode = new ArrayList<>();
testCode.add("5001");
testCode.add("2000");
testCode.add("2000");
for (int i = 0; i < testCode.size(); i++) {
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
}
}
@Test
public void commentInline() throws Exception {
List<String> testCode = new ArrayList<>();
testCode.add("add 0");
testCode.add("add 1 //Test comment");
testCode.add("add 0");
Files.write(inputPath, testCode);
JohnnyScript.main(new String[]{validFile});
assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath));
List<String> outLines = Files.readAllLines(outputPath);
testCode = new ArrayList<>();
testCode.add("5001");
testCode.add("2000");
testCode.add("2001");
testCode.add("2000");
for (int i = 0; i < testCode.size(); i++) {
assertEquals("Line " + i, testCode.get(i),outLines.get(i));
}
}
@Test
public void testMaxLines() throws Exception {
List<String> testCode = new ArrayList<>();
Files.write(inputPath, testCode);
JohnnyScript.main(new String[]{validFile});
List<String> outLines = Files.readAllLines(outputPath);
assertEquals(1000, outLines.size());
}
@Test
public void testTake() throws Exception {
exhTest("TAKE");
}
@Test
public void testAdd() throws Exception {
exhTest("ADD");
}
@Test
public void testSub() throws Exception {
exhTest("SUB");
}
@Test
public void testSave() throws Exception {
exhTest("SAVE");
}
@Test
public void testTst() throws Exception {
exhTest("TST");
}
@Test
public void testInc() throws Exception {
exhTest("INC");
}
@Test
public void testDec() throws Exception {
exhTest("DEC");
}
@Test
public void testNull() throws Exception {
exhTest("NULL");
}
@Test
public void testHlt() throws Exception {
exhTest("HLT");
}
/**
* Tests conversion of all possible addresses for a code
* @param code Code to test
*/
private void exhTest(String code) throws Exception {
for (int i = 0; i < 999; i++) {
List<String> testCode = new ArrayList<>();
testCode.add(code + " " + i);
Files.write(inputPath, testCode);
JohnnyScript.main(new String[]{validFile});
List<String> outLines = Files.readAllLines(outputPath);
testCode = new ArrayList<>();
testCode.add(JohnnyScript.Codes.valueOf(code).getCode()+ String.format("%03d",i));
assertEquals("Line " + 0, "5001",outLines.get(0));
for (int j = 1; j < testCode.size(); j++) {
assertEquals("Line " + j, testCode.get(j),outLines.get(j));
}
}
}
}