diff options
| author | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
| commit | 9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch) | |
| tree | d92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java | |
| parent | f0a6c6757a795b8e186de0076836a87acef59fa7 (diff) | |
| download | Simple-ch32.tar.gz Simple-ch32.zip | |
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java')
| -rw-r--r-- | chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java b/chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java new file mode 100644 index 00000000..f374d397 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/BreakoutTest.java @@ -0,0 +1,216 @@ +package com.seaofnodes.simple; + +import java.awt.image.BufferedImage; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.file.Files; +import java.nio.file.Path; +import javax.imageio.ImageIO; +import org.junit.Test; +import static org.junit.Assert.*; + +// Exercise the actual Simple game and rasterizer in native code, without a window. +public class BreakoutTest { + private static String game() throws Exception { + String source = Files.readString(Path.of("docs/examples/Breakout.smp")); + // Physics/rendering tests supply their own entry point. Input and + // sound are exercised by GraphicsTest with the complete program. + int end = source.indexOf("// Short, fading triangle waves"); + assertTrue("Breakout physics/rendering section",end >= 0); + String physics = source.substring(0,end); + // The shared square-root helper now follows the input code. + int sqrt = source.indexOf("val _sqrt ="); + if( sqrt >= end ) { + int close = source.indexOf("\n};",sqrt); + assertTrue("Square-root helper end",close >= 0); + physics += source.substring(sqrt,close+4); + } + 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; g._reset(); + if( g.remaining!=192 || g.score!=0 || g.lives!=3 || g.phase!=READY ) return 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; + 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; g._rebound(); + if( g.dx < 0.78 || g.dy>=0.0 ) return 6; + g.x=g.paddle; g._rebound(); + if( g.dy > -0.99 || g.dx*g.dx+g.dy*g.dy<0.999999 ) return 7; + g._serve(); g.paused=true; + f64 x=g.x, y=g.y, paddle=g.paddle; + 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++ ) { + 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; + 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; + }; + ""","breakoutControls","",-1); + } + + @Test public void collisionsAndBoost() throws Exception { + TestC.runSYS(game()+""" + val main={ -> + val g=new Game; g._reset(); + g._serve(); + g.x=29.1; g.y=400.0; g.dx=-1.0; g.dy=0.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; 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; + 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; + 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; + 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; + 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; + 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; + 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; + 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; + 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; + 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; + 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; + 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; g._reset(); + val c=new sys.draw.Canvas(WIDTH,HEIGHT); + 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; + // Save a native-rendered preview for manual visual review. + int fd=sys.io.create("build/objs/breakout-preview.bgra"); + i64 pixels=c.pixels; + if( fd<0 || sys.libc.write(fd,pixels,c.pixels#*4)!=c.pixels#*4 ) return 3; + sys.io.close(fd); + 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%COLS)*BRICK_DX; + g.y=BRICK_TOP+1.0+(i/COLS)*BRICK_DY+BRICK_H+RADIUS; + g.dx=0.0; g.dy=-1.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; g._tick(0); + if( g.y!=y ) return 6; + 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; + }; + ""","breakoutClear","",-1); + byte[] pixels=Files.readAllBytes(Path.of("build/objs/breakout-preview.bgra")); + assertEquals(1008*760*4,pixels.length); + var image=new BufferedImage(1008,760,BufferedImage.TYPE_INT_RGB); + var bytes=ByteBuffer.wrap(pixels).order(ByteOrder.LITTLE_ENDIAN); + for(int y=0;y<760;y++) for(int x=0;x<1008;x++) image.setRGB(x,y,bytes.getInt()); + ImageIO.write(image,"png",Path.of("build/objs/breakout-preview.png").toFile()); + } +} |
