diff options
Diffstat (limited to 'chapter31/src/test')
6 files changed, 392 insertions, 40 deletions
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/BreakoutTest.java b/chapter31/src/test/java/com/seaofnodes/simple/BreakoutTest.java index efdb1dd7..7f3e56d4 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/BreakoutTest.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/BreakoutTest.java @@ -28,35 +28,51 @@ public class BreakoutTest { return physics; } + @Test public void firstServeMovesBall() throws Exception { + TestC.runSYS(game()+""" + val main = { -> + val g = new Game; g._reset(); + if( g.speed != 320.0 || g.paddle != 504.0 ) return 1; + g._serve(); + f64 x = g.x, y = g.y; + g._tick(0); + if( g.phase != PLAY || g.x <= x || g.y >= y ) return 2; + g._paddle(2000.0); + if( g.paddle != RIGHT-PADDLE_HALF ) return 3; + return 0; + }; + ""","breakoutServe","",-1); + } + @Test public void paddleLivesAndPause() throws Exception { TestC.runSYS(game()+""" val main={ -> - val g=new Game; + val g=new Game; g._reset(); if( g.remaining!=192 || g.score!=0 || g.lives!=3 || g.phase!=READY ) return 1; - _tick(g,1); + g._tick(1); if( g.paddle<510.39 || g.paddle>510.41 || g.x!=g.paddle ) return 2; g._paddle(-100.0); if( g.paddle != 80.0 ) return 3; g._paddle(2000.0); if( g.paddle != 928.0 ) return 4; - _reset(g); - g.x=g.paddle-50.0; _rebound(g); + g._reset(); + g.x=g.paddle-50.0; g._rebound(); if( g.dx > -0.78 || g.dy>=0.0 ) return 5; - g.x=g.paddle+50.0; _rebound(g); + g.x=g.paddle+50.0; g._rebound(); if( g.dx < 0.78 || g.dy>=0.0 ) return 6; - g.x=g.paddle; _rebound(g); + g.x=g.paddle; g._rebound(); if( g.dy > -0.99 || g.dx*g.dx+g.dy*g.dy<0.999999 ) return 7; - _serve(g); g.paused=true; + g._serve(); g.paused=true; f64 x=g.x, y=g.y, paddle=g.paddle; - _tick(g,1); + g._tick(1); if( g.x!=x || g.y!=y || g.paddle!=paddle ) return 8; g.paused=false; for( int i=0; i<3; i++ ) { - _serve(g); g.x=40.0; g.y=723.0; g.dx=0.0; g.dy=1.0; - _tick(g,0); + g._serve(); g.x=40.0; g.y=723.0; g.dx=0.0; g.dy=1.0; + g._tick(0); if( g.lives!=2-i || g.backWall || g.sound!=5 ) return 9; } if( g.phase!=LOST ) return 10; - _serve(g); if( g.phase!=LOST ) return 11; - _reset(g); + g._serve(); if( g.phase!=LOST ) return 11; + g._reset(); if( g.phase!=READY || g.lives!=3 || g.boosted || g.paused || g.speed!=320.0 ) return 12; return 0; }; @@ -66,67 +82,104 @@ public class BreakoutTest { @Test public void collisionsAndBoost() throws Exception { TestC.runSYS(game()+""" val main={ -> - val g=new Game; - _serve(g); + val g=new Game; g._reset(); + g._serve(); g.x=29.1; g.y=400.0; g.dx=-1.0; g.dy=0.0; - _tick(g,0); + g._tick(0); if( g.dx<=0.0 || g.x<29.0 || g.sound!=1 ) return 1; - g.x=978.9; g.dx=1.0; _tick(g,0); + g.x=978.9; g.dx=1.0; g._tick(0); if( g.dx>=0.0 || g.x>979.0 ) return 2; // Front of the bottom row: one point, no acceleration or boost. g.x=44.0; g.y=330.0; g.dx=0.0; g.dy=-1.0; - _tick(g,0); + g._tick(0); if( g.bricks[168]!=0 || g.score!=1 || g.dy<=0.0 || g.boosted || g.soundDepth!=1 ) return 3; // Top of a brick alone does not arm the back-wall bonus. g.x=44.0; g.y=146.0; g.dy=1.0; - _tick(g,0); + g._tick(0); if( g.bricks[0]!=0 || g.score!=9 || g.speed!=565.0 || g.boosted || g.soundDepth!=8 ) return 4; // Back wall arms it; hitting a side still must not trigger it. g.x=84.0; g.y=89.1; g.dy=-1.0; - _tick(g,0); + g._tick(0); if( !g.backWall || g.dy<=0.0 || g.boosted ) return 5; g.x=60.0; g.y=160.0; g.dx=1.0; g.dy=0.0; - _tick(g,0); + g._tick(0); if( g.bricks[1]!=0 || g.dx>=0.0 || g.boosted ) return 6; g.x=124.0; g.y=146.0; g.dx=0.0; g.dy=1.0; - _tick(g,0); - if( !g.boosted || g.speed<932.24 || g.speed>932.26 || g.dy>=0.0 || g.sound!=4 ) return 7; + g._tick(0); + if( !g.boosted || g.speed<932.24 || g.speed>932.26 || g.dy>=0.0 || g.sound!=3 || g.soundDepth!=8 ) return 7; // Another back hit cannot multiply the speed a second time. g.x=164.0; g.y=146.0; g.dy=1.0; - _tick(g,0); + g._tick(0); if( g.speed>932.26 || g.bricks[3]!=0 ) return 8; // A boosted front hit cannot tunnel through the brick either. g.x=84.0; g.y=331.0; g.dy=-1.0; - _tick(g,0); + g._tick(0); if( g.bricks[169]!=0 || g.dy<=0.0 || g.speed<932.24 ) return 9; // Actual paddle contact disarms the trip and chooses its angle. g.x=g.paddle+50.0; g.y=668.0; g.dy=1.0; g.dx=0.0; - _tick(g,0); + g._tick(0); if( g.dy>=0.0 || g.dx<0.78 || g.backWall || !g.boosted ) return 10; // A lost ball clears both kinds of acceleration, preserving the wall/score. int score=g.score, remaining=g.remaining; g.x=40.0; g.y=723.0; g.dx=0.0; g.dy=1.0; - _tick(g,0); + g._tick(0); if( g.phase!=READY || g.lives!=2 || g.speed!=320.0 || g.depth!=1 || g.boosted ) return 11; if( g.score!=score || g.remaining!=remaining ) return 12; - _serve(g); + g._serve(); if( g.speed!=320.0 ) return 13; // The new ball can earn its own boost. g.sound=0; g.backWall=true; g.x=204.0; g.y=146.0; g.dx=0.0; g.dy=1.0; - _tick(g,0); - if( !g.boosted || g.speed<932.24 || g.sound!=4 ) return 14; + g._tick(0); + if( !g.boosted || g.speed<932.24 || g.sound!=3 || g.soundDepth!=8 ) return 14; return 0; }; ""","breakoutCollisions","",-1); } + @Test public void gridEdgesAndGaps() throws Exception { + TestC.runSYS(game()+""" + val main = { -> + val g = new Game; g._reset(); + val c = new sys.draw.Canvas(WIDTH,HEIGHT); + g._draw(c); + g._serve(); + // The center lies below the last grid row and between columns. + // Both neighboring bricks overlap the ball; the left one wins. + g.x = 64.0; g.y = 330.0; g.dx = 0.0; g.dy = -1.0; + g._tick(0); + if( g.bricks[168] != 0 || g.bricks[169] != 1 || g.score != 1 || g.dy <= 0.0 ) return 1; + // A cleared neighbor must not hide the other candidate. + g.x = 64.0; g.y = 330.0; g.dx = 0.0; g.dy = -1.0; + g._tick(0); + if( g.bricks[169] != 0 || g.score != 2 || g.dy <= 0.0 ) return 2; + // Rightmost column, then a position entirely below the wall. + g.x = 978.0; g.y = 330.0; g.dx = 0.0; g.dy = -1.0; + g._tick(0); + if( g.bricks[191] != 0 || g.score != 3 || g.dy <= 0.0 ) return 3; + g.x = 44.0; g.y = 400.0; g.dx = 0.0; g.dy = -1.0; + g._tick(0); + if( g.score != 3 || g.dy >= 0.0 ) return 4; + // Overlapping rows: the first box contains the center but no + // face was crossed; continue to the next row's top face. + g.x = 44.0; g.y = 168.0; g.dx = 0.0; g.dy = 1.0; + g._tick(0); + if( g.bricks[0] != 1 || g.bricks[24] != 0 || g.score != 10 || g.dy >= 0.0 ) return 5; + // Contact just above the first row still belongs to that row. + g.x = 44.0; g.y = 146.0; g.dx = 0.0; g.dy = 1.0; + g._tick(0); + if( g.bricks[0] != 0 || g.score != 18 || g.dy >= 0.0 ) return 6; + return 0; + }; + ""","breakoutGrid","",-1); + } + @Test public void clearEveryBrickAndRender() throws Exception { TestC.runSYS(game()+""" val main={ -> - val g=new Game; + val g=new Game; g._reset(); val c=new sys.draw.Canvas(WIDTH,HEIGHT); - _draw(c,g); + g._draw(c); for( int row=0; row<ROWS; row++ ) if( c.pixels[(BRICK_TOP+row*BRICK_DY+4)*WIDTH+28]!=COLORS[row] ) return 1; if( c.pixels[675*WIDTH+504]!=CYAN || c.pixels[663*WIDTH+504]!=WHITE ) return 2; @@ -135,19 +188,19 @@ public class BreakoutTest { i64 pixels=c.pixels; if( fd<0 || sys.libc.write(fd,pixels,c.pixels#*4)!=c.pixels#*4 ) return 3; sys.io.close(fd); - _serve(g); + g._serve(); // Drive through every brick's front, bottom row first. for( int i=COLS*ROWS-1; i>=0; i-- ) { g.x=LEFT+20.0+(i-i/COLS*COLS)*BRICK_DX; g.y=BRICK_TOP+1.0+(i/COLS)*BRICK_DY+BRICK_H+RADIUS; g.dx=0.0; g.dy=-1.0; - _tick(g,0); + g._tick(0); if( g.bricks[i]!=0 ) return 4; } if( g.phase!=WON || g.remaining!=0 || g.score!=864 || g.sound!=6 ) return 5; - f64 y=g.y; _tick(g,0); + f64 y=g.y; g._tick(0); if( g.y!=y ) return 6; - _reset(g); + g._reset(); for( int i=0; i<g.bricks#; i++ ) if( g.bricks[i]!=1 ) return 7; if( g.score!=0 || g.remaining!=192 ) return 8; return 0; diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter17bTest.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter17bTest.java index 12e54193..8a66dcc5 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter17bTest.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter17bTest.java @@ -568,8 +568,8 @@ struct _A { return new _A(new !B?[0]).array_of_b; """); code.driver(CodeGen.Phase.TypeCheck); - assertEquals("return []*B?;", code.print()); - assertEquals("*B {}?[]", Eval2.eval(code,0)); + assertEquals("return []*Test.B?;", code.print()); + assertEquals("*Test.B {}?[]", Eval2.eval(code,0)); } // --------------------------------------------------------------- diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java index 89e41efa..8667c0bd 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java @@ -279,7 +279,7 @@ return _s.peek('q'); CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck); assertEquals("return Phi(Region,0,1);", code.print()); assertEquals("1", Eval2.eval(code, 0)); - testCPU(src,"x86_64_v2", "win64",31,null); + testCPU(src,"x86_64_v2", "win64",23,null); }; @Test @@ -303,8 +303,7 @@ return s.skip().x; try { new CodeGen(src).driver(CodeGen.Phase.TypeCheck); fail(); } catch( Exception e ) { - // Bad error message, but basically requires a mutable 'x' field. - org.junit.Assert.assertTrue(e.getMessage().startsWith("Argument #0 isa")); + assertEquals("Receiver for method 'skip' is read-only Test._Scan; expected writable Test._Scan",e.getMessage()); } }; diff --git a/chapter31/src/test/java/com/seaofnodes/simple/MethodAccessTest.java b/chapter31/src/test/java/com/seaofnodes/simple/MethodAccessTest.java new file mode 100644 index 00000000..7275b151 --- /dev/null +++ b/chapter31/src/test/java/com/seaofnodes/simple/MethodAccessTest.java @@ -0,0 +1,118 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.parser.Parser; +import com.seaofnodes.simple.type.TypeInteger; +import com.seaofnodes.simple.util.Ary; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MethodAccessTest { + @Test public void readersAcceptReadOnlyReceivers() throws Exception { + String source = """ + struct Child { int n = 7; }; + struct Box { + !Child child = new Child; + int[] values = new int[]{3,5}; + int count = 2; + { i64 } read = { -> count+child.n+values[1]; }; + val delegate = { -> read(); }; + val recurse = { i64 n -> n == 0 ? read() : recurse(n-1); }; + val draw = { !Child canvas -> canvas.n = count; }; + val outer = { -> later(count); }; + }; + val later = { int n -> n+1; }; + val check = { Box box -> + val canvas = new Child; + box.draw(canvas); + return box.read()+box.delegate()+box.recurse(3)+canvas.n+box.outer(); + }; + return check(new Box); + """; + assertEquals("47",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0)); + TestC.runSF("val main = { -> "+source.replace("return check(new Box);","return check(new Box) != 47;")+" };", + "readonlyMethods","",-1); + } + + @Test public void writesThroughReceiverRequireAccess() { + String[] methods = { + "val act = { -> x = 2; };", + "val act = { -> self.x++; };", + "val act = { -> val nested = { -> x++; }; nested(); };", + "val act = { -> val alias = self; alias.x += 1; };", + "val act = { -> child.n = 2; };", + "val act = { -> values[0]++; };", + "val act = { -> write(); }; val write = { -> x++; };", + "val act = { -> val f = x == 0 ? left : right; f(); }; val left = { -> x++; }; val right = { -> x--; };", + "val act = { -> child.write(); };", + "val act = { -> write(self); };", + "{ int } act = \"C\";" + }; + for( String method : methods ) { + String source = """ + struct Child { int n; val write = { -> n++; }; }; + struct Box { + int x; + !Child child = new Child; + int[] values = new int[1]; + """+method+""" + }; + val write = { !Box box -> box.x++; }; + val check = { Box box -> box.act(); return 0; }; + return check(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Accepted writer: "+method); + } catch( Parser.ParseException e ) { + assertTrue(method+": "+e.getMessage(),e.getMessage().contains("read-only")); + } + new CodeGen(source.replace("val check = { Box box", "val check = { !Box box")) + .driver(CodeGen.Phase.TypeCheck); + } + } + + @Test public void returningWritableAliasesRequiresAccess() { + String source = """ + struct Box { int x; val alias = { -> self; }; }; + val check = { Box box -> box.alias(); }; + return check(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Read-only receiver exposed as writable"); + } catch( Parser.ParseException e ) { + assertTrue(e.getMessage(),e.getMessage().contains("read-only")); + } + source = source.replace("val alias = { -> self; }", "val alias = { -> Box view = self; view; }"); + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + } + + @Test public void importedReceiverAccess() throws Exception { + var root = Files.createDirectories(Path.of("build/objs/separate")); + var lib = Files.createTempDirectory(root,"method-access-"); + new CodeGen(null,lib.toString(),null,"Counters",""" + struct Counter { + int n = 7; + val read = { -> n; }; + val write = { -> n++; }; + }; + val make = { -> new Counter; }; + """,126,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + var paths = new Ary<>(new String[]{lib.toString()}); + String source = "val box = Counters.make(); val check = { Counters.Counter c -> c.read(); }; return check(box);"; + var code = new CodeGen(null,"build/objs",paths,"CounterReader",source,126,true, + TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck); + assertEquals("7",Eval2.eval(code,0)); + try { + new CodeGen(null,"build/objs",paths,"CounterWriter",source.replace("c.read()","c.write()"),126,true, + TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck); + fail("Imported writer accepted a read-only receiver"); + } catch( Parser.ParseException e ) { + assertTrue(e.getMessage(),e.getMessage().contains("read-only")); + } + } +} diff --git a/chapter31/src/test/java/com/seaofnodes/simple/MethodFieldsTest.java b/chapter31/src/test/java/com/seaofnodes/simple/MethodFieldsTest.java new file mode 100644 index 00000000..3fe5c28b --- /dev/null +++ b/chapter31/src/test/java/com/seaofnodes/simple/MethodFieldsTest.java @@ -0,0 +1,131 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.parser.Parser; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MethodFieldsTest { + @Test public void nestedFunctionStillRejectsLocalCapture() { + String source = """ + struct Game { + int count; + val input = { int amount -> + val action = { -> count += amount; }; + action(); + }; + }; + return 0; + """; + try { + new CodeGen(source).driver(CodeGen.Phase.Parse); + fail("Captured a nonconstant local parameter"); + } catch( Parser.ParseException e ) { + assertEquals("Variable 'amount' is out of function scope and must be a final constant",e.getMessage()); + } + } + + @Test public void nestedFunctionReceiver() throws Exception { + String source = """ + struct Game { + bool held, paused; + int count; + val debounce = { bool pressed, bool wasHeld, { bool } action -> + if( pressed && !wasHeld ) action(); + return pressed; + }; + val bump = { -> count++; return false; }; + val input = { bool pressed -> + if( true ) { + held = debounce(pressed,held,{ -> + paused = !paused; + bump(); + val again = { -> count += 2; return false; }; + again(); + return false; + }); + } + }; + val named = { -> debounce(true,false,bump); }; + }; + val a = new Game, b = new Game; + a.input(true); a.input(true); + if( a.count != 3 || !a.paused || b.count != 0 || b.paused ) return 1; + a.input(false); a.input(true); + if( a.count != 6 || a.paused ) return 2; + b.input(true); b.named(); + if( b.count != 4 || !b.paused || a.count != 6 ) return 3; + return 0; + """; + assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0)); + TestC.runSF("val main = { -> "+source+" };","nestedFunctionReceiver","",-1); + } + + @Test public void laterSiblingParameterType() throws Exception { + String source = """ + struct Game { + val input = { !Controls keys -> keys.presses++; return keys.presses; }; + val read = { Controls keys -> keys.presses; }; + }; + struct Controls { int presses = 4; }; + val game = new Game, keys = new Controls; + if( game.input(keys) != 5 || game.read(keys) != 5 ) return 1; + return 0; + """; + assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0)); + TestC.runSF("val main = { -> "+source+" };","forwardMethodType","",-1); + } + + private static final String FIELDS = """ + int count = 100; + struct Counter { + val step = { int n -> + count = count+n; + ++count; + count += 2; + speed *= 2.0; + return read(); + }; + val local = { int count -> count+1; }; + val read = { -> count; }; + int count = 3; + f64 speed = 1.5; + }; + val a = new Counter, b = new Counter; + if( a.step(4) != 10 || b.read() != 3 || a.speed != 3.0 ) return 1; + if( a.local(20) != 21 || a.read() != 10 || count != 100 ) return 2; + if( b.step(1) != 7 || a.read() != 10 ) return 3; + return 0; + """; + + @Test public void laterFieldsAndMethods() throws Exception { + assertEquals("0",Eval2.eval(new CodeGen(FIELDS).driver(CodeGen.Phase.LocalSched),0)); + TestC.runSF("val main = { -> "+FIELDS+" };","methodFields","",-1); + } + + @Test public void laterGlobalAndRecursiveMethod() { + String source = """ + struct Counter { + val step = { i64 n -> + if( n == 0 ) return value; + value += increment; + return step(n-1); + }; + int value = 1; + }; + val increment = 2; + return new Counter.step(3); + """; + assertEquals("7",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0)); + } + + @Test public void laterFinalFieldStillRejectsWrites() { + String source = "struct C { val change = { -> x = 2; }; int ~x = 1; }; return new C.change();"; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Method wrote a final field"); + } catch( Parser.ParseException e ) { + assertEquals("Cannot reassign final 'x'",e.getMessage()); + } + } +} diff --git a/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java b/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java index a1253d05..d342892c 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java @@ -10,6 +10,57 @@ import org.junit.Test; import static org.junit.Assert.*; public class ParserDiagnosticTest { + @Test public void testMissingForwardTypeDiagnostic() { + String source = """ + val read = { Missing value -> + return value.field; + }; + return 0; + """; + var error = rejected(source); + assertEquals("Unknown type 'Test.Missing'",error.getMessage()); + assertTrue(error.diagnostic(),error.diagnostic().startsWith("Test.smp:2:")); + assertTrue(error.diagnostic(),error.diagnostic().contains("return value.field;")); + } + + @Test public void testReadOnlyReceiverDiagnostic() { + String source = """ + struct Box { + int x = 1; + val bump = { -> x++; x; }; + }; + val draw = { Box b -> + return b.bump(); + }; + return draw(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Read-only receiver accepted by a writable method"); + } catch( Parser.ParseException e ) { + assertEquals("Receiver for method 'bump' is read-only Test.Box; expected writable Test.Box",e.getMessage()); + assertTrue(e.diagnostic(),e.diagnostic().startsWith("Test.smp:6:18: error: ")); + assertFalse(e.getMessage().contains("{i32")); + } + } + + @Test public void testReadOnlyArgumentDiagnostic() { + String source = """ + struct Box { int x = 1; }; + val write = { !Box b -> b.x = 2; }; + val draw = { Box b -> write(b); }; + return draw(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Read-only argument accepted by a writable parameter"); + } catch( Parser.ParseException e ) { + assertEquals("Argument #1 is read-only Test.Box; expected writable Test.Box",e.getMessage()); + } + new CodeGen(source.replace("val draw = { Box b", "val draw = { !Box b")) + .driver(CodeGen.Phase.TypeCheck); + } + private static Parser.ParseException rejected(String source) { try { new CodeGen(source).driver(CodeGen.Phase.Parse); |
