diff options
Diffstat (limited to 'chapter17/src/test/java/com')
| -rw-r--r-- | chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java | 10 | ||||
| -rw-r--r-- | chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java | 37 |
2 files changed, 41 insertions, 6 deletions
diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java index 0a48d2e7..88dccc49 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java @@ -23,10 +23,8 @@ public class BrainFuckTest { Parser parser = new Parser( encoded + """ -int pc = 0; -int d = 0; -u8[] data = new u8[100]; -u8[] output = new u8[0]; +int pc = 0, d = 0; +u8[] data = new u8[100], output = new u8[0]; while (pc < program#) { int command = program[pc]; @@ -35,9 +33,9 @@ while (pc < program#) { } else if (command == 60) { d--; } else if (command == 43) { - data[d] = data[d] + 1; + data[d]++; } else if (command == 45) { - data[d] = data[d] - 1; + data[d]--; } else if (command == 46) { u8[] old = output; output = new u8[output# + 1]; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java index 91c275ce..45e3ef0f 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java @@ -49,6 +49,43 @@ return -arg---arg--; assertEquals(1L, Evaluator.evaluate(stop, 0)); } + @Test + public void testInc3() { + Parser parser = new Parser(""" +int[] xs = new int[arg]; +xs[0]++; +xs[1]++; +return xs[0]; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 1;", stop.toString()); + assertEquals(1L, Evaluator.evaluate(stop, 2)); + } + + @Test + public void testInc4() { + Parser parser = new Parser(""" +u8[] xs = new u8[1]; +xs[0]--; +return xs[0]; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 255;", stop.toString()); + assertEquals(255L, Evaluator.evaluate(stop, 2)); + } + + @Test + public void testInc5() { + Parser parser = new Parser(""" +struct S { u16 x; }; +S s = new S; +s.x--; +return s.x; +"""); + StopNode stop = parser.parse().iterate(); + assertEquals("return 65535;", stop.toString()); + assertEquals(65535L, Evaluator.evaluate(stop, 2)); + } @Test public void testLinkedList2() { |
