Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JUnit to tests the programs #82

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions License.txt → LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

===

Copyright (c) 2012-2015, The Regents of the University of California (Regents).
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the Regents nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

===

Copyright (C) 1999 Slava Pestov

You may use and modify this package for any purpose. Redistribution is
Expand Down
2 changes: 1 addition & 1 deletion src/rars/venus/HelpHelpAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private JPanel createCopyrightInfoPanel() {
JScrollPane copyrightScrollPane;
JEditorPane copyrightDisplay;
try {
StringBuilder text = loadFiletoStringBuilder("/License.txt").append("</pre>");
StringBuilder text = loadFiletoStringBuilder("/LICENSE").append("</pre>");
copyrightDisplay = new JEditorPane("text/html", "<pre>" + text.toString());
copyrightDisplay.setEditable(false);
copyrightDisplay.setCaretPosition(0); // assure top of document displayed
Expand Down
2 changes: 1 addition & 1 deletion test/RarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void checkPrograms() {
// 32-bit tests
Program p = setupProgram(false);
runDirectory("./test", p);
runDirectory("./test/riscv-tests", p);
runDirectory("./test/riscv-tests-32", p);

// 64-bit tests
p = setupProgram(true);
Expand Down
128 changes: 123 additions & 5 deletions test/RarsUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,129 @@
import org.junit.jupiter.api.Test;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import rars.AssemblyException;
import rars.ErrorMessage;
import rars.SimulationException;
import rars.api.Program;
import rars.simulator.ProgramArgumentList;
import rars.simulator.Simulator;

import java.io.*;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;

public class RarsUnitTest extends RarsTest {
@ParameterizedTest
@MethodSource({"agnosticTestFiles", "rv32TestFiles"})
public void testRunRV32(String path) throws IOException {
Program p = setupProgram(false);
testFile(path, p);
}

@ParameterizedTest
@MethodSource({"agnosticTestFiles", "rv64TestFiles"})
public void testRunRV64(String path) throws IOException {
Program p = setupProgram(true);
testFile(path, p);
}

static Stream<String> agnosticTestFiles() {
File[] tests = new File("./test").listFiles();
assumeTrue(tests != null && Arrays.stream(tests).anyMatch(t -> t.isFile() && t.getName().endsWith(".s")));
return Arrays.stream(tests).filter(t -> t.isFile() && t.getName().endsWith(".s")).map(File::getPath);
}
static Stream<String> rv32TestFiles() {
File[] tests = new File("./test/riscv-tests-32").listFiles();
assumeTrue(tests != null && Arrays.stream(tests).anyMatch(t -> t.isFile() && t.getName().endsWith(".s")));
return Arrays.stream(tests).filter(t -> t.isFile() && t.getName().endsWith(".s")).map(File::getPath);
}
static Stream<String> rv64TestFiles() {
File[] tests = new File("./test/riscv-tests-64").listFiles();
assumeTrue(tests != null && Arrays.stream(tests).anyMatch(t -> t.isFile() && t.getName().endsWith(".s")));
return Arrays.stream(tests).filter(t -> t.isFile() && t.getName().endsWith(".s")).map(File::getPath);
}

public void testFile(String path, Program p) throws IOException {
int[] errorlines = null;
String stdin = "", stdout = "", stderr ="", errorMessage = "", exitReason = "";
ArrayList<String> programArgumentList = null;
ArrayList<String> fileList = new ArrayList<>();
fileList.add(path);
int exitCode = 0;
p.getOptions().selfModifyingCode = false;
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
while(line != null){
if (line.startsWith("#error on lines:")) {
String[] linenumbers = line.replaceFirst("#error on lines:", "").split(",");
errorlines = new int[linenumbers.length];
for(int i = 0; i < linenumbers.length; i++){
errorlines[i] = Integer.parseInt(linenumbers[i].trim());
}
} else if (line.startsWith("#lib:")) {
String lib = line.replaceFirst("#lib:", "");
fileList.add(Paths.get(path).getParent().resolve(lib).toString());
} else if (line.startsWith("#stdin:")) {
stdin = line.replaceFirst("#stdin:", "").replaceAll("\\\\n","\n");
} else if (line.startsWith("#stdout:")) {
stdout = line.replaceFirst("#stdout:", "").replaceAll("\\\\n","\n").trim();
} else if (line.startsWith("#stderr:")) {
stderr = line.replaceFirst("#stderr:", "").replaceAll("\\\\n","\n").trim();
} else if (line.startsWith("#exit:")) {
exitReason = line.replaceFirst("#exit:", "");
try {
exitCode = Integer.parseInt(exitReason);
} catch (NumberFormatException nfe) {
exitCode = -1;
}
} else if (line.startsWith("#args:")) {
String args = line.replaceFirst("#args:", "");
programArgumentList = new ProgramArgumentList(args).getProgramArgumentList();
} else if (line.startsWith("#error:")) {
errorMessage = line.replaceFirst("#error:", "");
} else if (line.startsWith("#selfmod:")) {
String selfmod = line.replaceFirst("#selfmod:", "");
if (selfmod.equals("true")) {
p.getOptions().selfModifyingCode = true;
}
}
line = br.readLine();
}

try {
p.assemble(fileList, path);
assertNull(errorlines,"Expected assembly error, but successfully assembled " + path);

p.setup(programArgumentList, stdin);
Simulator.Reason r = p.simulate();
if(r != Simulator.Reason.NORMAL_TERMINATION){
assertEquals(r.toString().toLowerCase(),exitReason,"Ended abnormally " + r + " while executing " + path);
}else{
assertEquals(p.getExitCode(), exitCode, "Final exit code was wrong for " + path);
assertEquals(p.getSTDOUT().trim(),stdout, "STDOUT was wrong for " + path);
assertEquals(p.getSTDERR().trim(), stderr, "STDERR was wrong for " + path);
}
} catch (AssemblyException ae){
assertNotNull(errorlines, "Failed to assemble " + path);
assertEquals(ae.errors().errorCount(), errorlines.length, "Mismatched number of assembly errors in" + path);

Iterator<ErrorMessage> errors = ae.errors().getErrorMessages().iterator();
for(int number : errorlines){
ErrorMessage error = errors.next();
while(error.isWarning()) error = errors.next();
assertEquals(error.getLine(), number, "Expected error on line " + number + ". Found error on line " + error.getLine()+" in " + path);
}
} catch (SimulationException se){
assertEquals(se.error().getMessage(), errorMessage, "Crashed while executing " + path + "; " + se.error().generateReport());
}
}
@Override
@Test
public void checkBinary() {
Expand All @@ -13,9 +136,4 @@ public void checkPseudo() {
super.checkPseudo();
}

@Override
@Test
public void checkPrograms() {
super.checkPrograms();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 0 additions & 24 deletions test/riscv-tests/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion test/riscv-tests/readme.txt

This file was deleted.

Loading