-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from schemil053/dev
Merge upstream
- Loading branch information
Showing
6 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...eCPU/tests/CPUCompilerDecompilerTest.java → ...s/emulator/CPUCompilerDecompilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/test/java/de/emilschlampp/scheCPU/tests/emulator/CPUExecuteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package de.emilschlampp.scheCPU.tests.emulator; | ||
|
||
import de.emilschlampp.scheCPU.compile.Compiler; | ||
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CPUExecuteTest { | ||
@Test | ||
public void testExecute() { | ||
ProcessorEmulator processorEmulator = new ProcessorEmulator(5, 5, new Compiler("OUTW 16 5").compile()); | ||
processorEmulator.execute(); | ||
|
||
assertEquals(processorEmulator.getIo()[1], 0); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/de/emilschlampp/scheCPU/tests/emulator/CPUProtectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package de.emilschlampp.scheCPU.tests.emulator; | ||
|
||
import de.emilschlampp.scheCPU.compile.Compiler; | ||
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator; | ||
import de.emilschlampp.scheCPU.util.EmulatorSandboxRestrictions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.PrintStream; | ||
|
||
public class CPUProtectionTest { | ||
@Test | ||
public void testCPUProtection() { | ||
String program = "OUTW 34 72\n" + //H | ||
"OUTW 34 105"; // i | ||
|
||
|
||
PrintStream printStream = System.out; | ||
|
||
System.setOut(new PrintStream(System.out) { | ||
@Override | ||
public void print(char c) { | ||
throw new IllegalStateException("should not print!"); | ||
} | ||
}); | ||
|
||
try { | ||
ProcessorEmulator processorEmulator = new ProcessorEmulator(5, 5, new Compiler(program).compile()); | ||
processorEmulator.setRestrictions(new EmulatorSandboxRestrictions().setAllowOutput(false)); | ||
|
||
while (processorEmulator.canExecute()) { | ||
processorEmulator.execute(); | ||
} | ||
} catch (Throwable throwable) { | ||
System.setOut(printStream); | ||
throw throwable; | ||
} | ||
System.setOut(printStream); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...lschlampp/scheCPU/tests/CPUStateTest.java → .../scheCPU/tests/emulator/CPUStateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/de/emilschlampp/scheCPU/tests/highlang/HighlangCompileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.emilschlampp.scheCPU.tests.highlang; | ||
|
||
import de.emilschlampp.scheCPU.emulator.ProcessorEmulator; | ||
import de.emilschlampp.scheCPU.high.HighProgramCompiler; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static de.emilschlampp.scheCPU.tests.highlang.HighlangIOCompileProcessor.IO_PRINT; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class HighlangCompileTest { | ||
@Test | ||
public void testHighlangCompiler() { | ||
String test = "Hallo!"; | ||
|
||
HighProgramCompiler compiler = new HighProgramCompiler( | ||
"reserve t "+test.length()+"\n" + | ||
"var t \""+test+"\"\n" + | ||
"print t" | ||
); | ||
|
||
compiler.setCompileProcessor(new HighlangIOCompileProcessor()); | ||
|
||
|
||
byte[] program = compiler.compile().toBytecode(); | ||
|
||
ProcessorEmulator emulator = new ProcessorEmulator(64, 10, program); | ||
|
||
StringBuffer buffer = new StringBuffer(); | ||
|
||
while (emulator.canExecute()) { | ||
emulator.execute(); | ||
|
||
if(emulator.getIo()[IO_PRINT] != 0) { | ||
buffer.append((char) emulator.getIo()[IO_PRINT]); | ||
emulator.getIo()[IO_PRINT] = 0; | ||
} | ||
} | ||
|
||
assertEquals(buffer.toString(), test); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/de/emilschlampp/scheCPU/tests/highlang/HighlangIOCompileProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.emilschlampp.scheCPU.tests.highlang; | ||
|
||
import de.emilschlampp.scheCPU.high.processor.CompileContext; | ||
import de.emilschlampp.scheCPU.high.processor.CompileProcessor; | ||
|
||
public class HighlangIOCompileProcessor extends CompileProcessor { | ||
public static final int IO_PRINT = 129; | ||
|
||
|
||
@Override | ||
public void startCompile(CompileContext compileContext) { | ||
compileContext.getOptions().put("io-print-port", ""+IO_PRINT); | ||
compileContext.getProtectedOptions().add("io-print-port"); | ||
} | ||
|
||
@Override | ||
public String generatePreCompileHigh() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String generatePreCompileHighEnd() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String generatePreCompileSCHESEM() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String generateSchesemForInstruction(CompileContext compileContext, String[] instruction) { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String generateAfterCompileSCHESEM(CompileContext compileContext) { | ||
return ""; | ||
} | ||
} |