package com.seaofnodes.simple; import com.seaofnodes.simple.util.Ary; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.TimeUnit; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; // These tests never open a window and do not need MiniFB installed. public class GraphicsTest { private static String graphics, text, game, clock, breakout; @BeforeClass public static void compilePrograms() throws Exception { var paths = new Ary<>(new String[]{TestC.RELEASE_SYS_DIR}); graphics = TestC.compile(Files.readString(Path.of("docs/examples/Graphics.smp")), "graphics_missing",paths,TestC.CALL_CONVENTION,null,null,-1); text = TestC.compile("val main={ -> sys.io.p(\"text still works\\n\"); return 0; };", "graphics_text",paths,TestC.CALL_CONVENTION,null,null,-1); game = TestC.compile(Files.readString(Path.of("docs/examples/TicTacToeGraphical.smp")), "graphics_game",paths,TestC.CALL_CONVENTION,null,null,-1); clock = TestC.compile(Files.readString(Path.of("docs/examples/Clock.smp")), "graphics_clock",paths,TestC.CALL_CONVENTION,null,null,-1); breakout = TestC.compile(Files.readString(Path.of("docs/examples/Breakout.smp")), "graphics_breakout",paths,TestC.CALL_CONVENTION,null,null,-1); } private static String run(String exe, String backend, int exit) throws Exception { return run(exe,backend,exit,null); } private static String run(String exe, String backend, int exit, Path cwd) throws Exception { ProcessBuilder pb = new ProcessBuilder(Path.of(exe).toAbsolutePath().toString()).redirectErrorStream(true); pb.environment().put("SIMPLE_MINIAUDIO","off"); if (cwd!=null) pb.directory(cwd.toFile()); if( backend == null ) pb.environment().remove("SIMPLE_MINIFB"); else pb.environment().put("SIMPLE_MINIFB",backend); Process child = pb.start(); try { assertTrue("native program timed out",child.waitFor(10,TimeUnit.SECONDS)); String output = new String(child.getInputStream().readAllBytes(),java.nio.charset.StandardCharsets.UTF_8); assertEquals(output,exit,child.exitValue()); return output; } finally { if( child.isAlive() ) child.destroyForcibly(); } } @Test public void missingConfiguration() throws Exception { Path dir=Files.createTempDirectory(Path.of("build/objs"),"graphics-missing-"); Path bin=Files.createDirectory(dir.resolve("bin")); Path exe=Files.copy(Path.of(graphics),bin.resolve(Path.of(graphics).getFileName())); String output = run(exe.toString(),null,1,dir); assertTrue(output,output.contains("MiniFB backend not found")); assertTrue(output,output.contains("make graphics")); } @Test public void discoverBackendWithoutConfiguration() throws Exception { Path root=Files.createTempDirectory(Path.of("build/objs"),"graphics-discovery-"); String abi=TestC.OS.startsWith("Windows") ? "x86_64_v2_win64" : "x86_64_v2_SystemV"; String name=TestC.OS.startsWith("Windows") ? "simple_minifb.dll" : "libsimple_minifb.so"; Path source=root.resolve("backend.c"); Files.writeString(source,""" #include "simple_graphics.h" #include static int32_t open_window(const char *s,uint32_t w,uint32_t h) { puts("discovered mock MiniFB"); return -1; } static int32_t present(const uint32_t *p,uint32_t n) { return 0; } static int32_t poll_events(int32_t n) { return 0; } static int32_t position(void) { return 0; } static int32_t wait_events(int64_t n) { return 0; } static uint32_t size(void) { return 0; } static int32_t present_size(const uint32_t *p,uint32_t n,uint32_t w,uint32_t h) { return 0; } static void close_window(void) {} static const struct simple_graphics_api api={SIMPLE_GRAPHICS_VERSION, open_window,present,poll_events,position,position,poll_events,poll_events,close_window,wait_events, size,size,present_size}; const struct simple_graphics_api *simple_minifb_api(void) { return &api; } """); Path mock=root.resolve(name); TestC.exec(30,"gcc","-shared","-fPIC","-Isrc/main/c",source.toString(),"-o",mock.toString()); Path cwd=Files.createDirectory(root.resolve("empty")); Path bin=Files.createDirectory(root.resolve("bin")); Path exe=Files.copy(Path.of(graphics),bin.resolve(Path.of(graphics).getFileName())); Path beside=Files.copy(mock,bin.resolve(name)); assertTrue(run(exe.toString(),null,1,cwd).contains("discovered mock MiniFB")); // Explicit bad overrides must not silently fall back to the mock. assertTrue(run(exe.toString(),"no-such-backend",1,cwd).contains("cannot load MiniFB")); Files.delete(beside); Path release=Files.createDirectories(root.resolve("build/release/"+abi)); Files.copy(mock,release.resolve(name)); Path examples=Files.createDirectories(root.resolve("build/examples/"+abi)); Path example=Files.copy(exe,examples.resolve(exe.getFileName())); assertTrue(run(example.toString(),null,1,cwd).contains("discovered mock MiniFB")); assertTrue(run(exe.toString(),null,1,root).contains("discovered mock MiniFB")); } @Test public void disabledGraphics() throws Exception { assertTrue(run(graphics,"off",1).contains("disabled")); } @Test public void gameWithoutBackend() throws Exception { assertTrue(run(game,"off",1).contains("disabled")); } @Test public void clockWithoutBackend() throws Exception { assertTrue(run(clock,"off",1).contains("disabled")); } @Test public void breakoutWithoutBackend() throws Exception { assertTrue(run(breakout,"off",1).contains("disabled")); } @Test public void breakoutInputAndFrames() throws Exception { Path source=Path.of("build/objs/breakout_input.c"); Path library=Path.of("build/objs/breakout_input"+(TestC.OS.startsWith("Windows") ? ".dll" : ".so")); Files.writeString(source,""" #include "simple_graphics.h" #include #include static int frames, previous_ball, previous_paddle; static int32_t open_window(const char *s,uint32_t w,uint32_t h) { return w==1008 && h==760 ? 0 : -1; } static int32_t present(const uint32_t *p,uint32_t n) { if(n!=1008*760) return -1; int paddle=-1,ball=-1; for(int x=24;x<984;x++) if(p[675*1008+x]==0x4ce0ef) { paddle=x; break; } for(int y=560;y<674 && ball<0;y++) for(int x=24;x<984;x++) if(p[y*1008+x]==0xeef7ff) { ball=y; break; } int ok=paddle>=0 && ball>=0; if(frames==0) ok=ok && paddle==448 && ball==658; if(frames==1 || frames==6 || frames==9) ok=ok && ballprevious_paddle; if(frames==4 || frames==5) ok=ok && paddle==previous_paddle && ball==previous_ball; if(frames==7) ok=ok && paddle==584; if(frames==8) ok=ok && paddle==448 && ball==658; if(!ok) { printf("bad frame %d: paddle %d (was %d), ball %d (was %d)\\n", frames,paddle,previous_paddle,ball,previous_ball); return -1; } previous_ball=ball; previous_paddle=paddle; if(++frames==10) puts("breakout input smoke passed"); return 0; } static int32_t poll_events(int32_t n) { return 0; } static int32_t mouse_x(void) { return frames>=7 ? 640 : 504; } static int32_t mouse_y(void) { return 680; } static int32_t key(int32_t k) { return (k==' ' && (frames==1 || frames==9)) || (k==262 && (frames==2 || frames==3)) || (k=='P' && (frames==4 || frames==6)) || (k=='R' && (frames==8 || frames==9)); } static int32_t wait_events(int64_t ms) { usleep(ms*1000); return 0; } static uint32_t width(void) { return 1008; } static uint32_t height(void) { return 760; } static int32_t present_size(const uint32_t *p,uint32_t n,uint32_t w,uint32_t h) { return w==1008 && h==760 ? present(p,n) : -1; } static void close_window(void) {} static const struct simple_graphics_api api={SIMPLE_GRAPHICS_VERSION, open_window,present,poll_events,mouse_x,mouse_y,poll_events,key,close_window,wait_events, width,height,present_size}; const struct simple_graphics_api *simple_minifb_api(void) { return &api; } """); TestC.exec(30,"gcc","-shared","-fPIC","-Isrc/main/c",source.toString(),"-o",library.toString()); var pb=new ProcessBuilder(Path.of(breakout).toAbsolutePath().toString(),"smoke").redirectErrorStream(true); pb.environment().put("SIMPLE_MINIFB",library.toAbsolutePath().toString()); pb.environment().put("SIMPLE_MINIAUDIO","off"); Process child=pb.start(); try { assertTrue("Breakout timed out",child.waitFor(10,TimeUnit.SECONDS)); String output=new String(child.getInputStream().readAllBytes(),java.nio.charset.StandardCharsets.UTF_8); assertEquals(output,0,child.exitValue()); assertEquals("breakout input smoke passed\n",output); } finally { if(child.isAlive()) child.destroyForcibly(); } } @Test public void missingLibrary() throws Exception { String missing = Path.of("build","no-such-minifb-backend").toAbsolutePath().toString(); String output = run(graphics,missing,1); assertTrue(output,output.contains("cannot load MiniFB backend")); } @Test public void demosResizeWithoutDisplay() throws Exception { Path source=Path.of("build/objs/graphics_resize.c"); Path library=Path.of("build/objs/graphics_resize"+(TestC.OS.startsWith("Windows") ? ".dll" : ".so")); Files.writeString(source,""" #include "simple_graphics.h" #include #include static unsigned step, seen, base_w, base_h; static uint32_t width(void) { return step==1 ? base_w/2 : step==2 ? base_w+123 : step==3 ? 1 : step==4 ? 0 : base_w; } static uint32_t height(void) { return step==1 ? base_h/2 : step==2 ? base_h/2+17 : step==3 ? 1 : step==4 ? 0 : base_h; } static int32_t open_window(const char *s,uint32_t w,uint32_t h) { base_w=w; base_h=h; return 0; } static int32_t present_size(const uint32_t *p,uint32_t n,uint32_t w,uint32_t h) { if(!p || n!=(uint64_t)w*h || (step!=4 && (w!=width() || h!=height()))) return -1; seen |= 1u<