diff options
| author | XmiliaH <xmilia.hermit@gmail.com> | 2024-10-03 22:22:38 +0200 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-10-06 08:56:22 -0700 |
| commit | ea91f6c27a2a640c89386bb8a41914c0b748ef4e (patch) | |
| tree | 9700ff816aba01f3704d2d7c9e50c15cc95a704c /chapter15/src/test/java/com | |
| parent | a5fb3fb98c67ff0d2803ba7b6c0cd9933e7d7001 (diff) | |
| download | Simple-ea91f6c27a2a640c89386bb8a41914c0b748ef4e.tar.gz Simple-ea91f6c27a2a640c89386bb8a41914c0b748ef4e.zip | |
Add Brainfuck test
Diffstat (limited to 'chapter15/src/test/java/com')
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java index ad3c6b3f..00ed6c6c 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -5,6 +5,8 @@ import com.seaofnodes.simple.node.StopNode; import org.junit.Test; import org.junit.Ignore; +import java.nio.charset.StandardCharsets; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -265,4 +267,86 @@ return is[1]; catch( NegativeArraySizeException e ) { assertEquals("-1",e.getMessage()); } } + @Test + public void testBrainfuck() { + var program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.".getBytes(StandardCharsets.UTF_8); + var encoded = new StringBuilder("u8[] program = new u8[").append(program.length).append("];"); + for (int i = 0; i < program.length; i++) { + int value = program[i] & 0xFF; + encoded.append("program[").append(i).append("] = ").append(value).append(";"); + } + + Parser parser = new Parser( + encoded + """ +int pc = 0; +int d = 0; +u8[] data = new u8[30000]; +int i=0; +while(i < data#) { + data[i] = 0; + i = i + 1; +} + +u8[] output = new u8[0]; + +while (pc < program#) { + int command = program[pc]; + if (command == 62) { + d = d + 1; + } else if (command == 60) { + d = d - 1; + } else if (command == 43) { + data[d] = data[d] + 1; + } else if (command == 45) { + data[d] = data[d] - 1; + } else if (command == 46) { + u8[] old = output; + output = new u8[output# + 1]; + i = 0; + while (i < old#) { + output[i] = old[i]; + i = i + 1; + } + output[i] = data[d]; + } else if (command == 44) { + data[d] = 42; + } else if (command == 91) { + if (data[d] == 0) { + int d = 1; + while (d > 0) { + pc = pc + 1; + command = program[pc]; + if (command == 91) d = d + 1; + if (command == 93) d = d - 1; + } + } + } else if (command == 93) { + if (data[d] != 0) { + int d = 1; + while (d > 0) { + pc = pc - 1; + command = program[pc]; + if (command == 93) d = d + 1; + if (command == 91) d = d - 1; + } + } + } + pc = pc + 1; +} +int result = 0; +i = 0; +while (i < 8) { + result = (result << 8) | data[i]; + i = i + 1; +} +return result; + """); + StopNode stop = parser.parse(false).iterate(false); + long expected = 0; + for (byte b : "Hello Wo".getBytes(StandardCharsets.UTF_8)) { + expected = (expected << 8) | b; + } + assertEquals(expected, Evaluator.evaluate(stop, 1000000)); + } + } |
