1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
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 <stdio.h>
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 <stdio.h>
#include <unistd.h>
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 && ball<previous_ball;
if(frames==2 || frames==3) ok=ok && paddle>previous_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 <stdio.h>
#include <unistd.h>
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<<step;
return 0;
}
static int32_t present(const uint32_t *p,uint32_t n) { return -1; }
static int32_t wait_events(int64_t ms) {
usleep(35000);
if(++step<6) return 0;
// Minimized windows need not repaint; every usable size must.
if((seen & 46)!=46) { printf("missing resize frames: %u\\n",seen); return -1; }
puts("resize passed"); return 1;
}
static int32_t poll_events(int32_t n) { return wait_events(n); }
static int32_t position(void) { return 0; }
static int32_t key(int32_t n) { return 0; }
static void close_window(void) {}
static const struct simple_graphics_api api={SIMPLE_GRAPHICS_VERSION,
open_window,present,poll_events,position,position,key,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());
for(String exe:new String[]{graphics,clock,game,breakout})
assertTrue(exe,run(exe,library.toAbsolutePath().toString(),0).contains("resize passed"));
}
@Test public void textIgnoresGraphicsConfiguration() throws Exception {
assertEquals("text still works\n",run(text,"off",0));
assertEquals("text still works\n",run(text,"no-such-backend",0));
}
@Test public void incompatibleLibrary() throws Exception {
Path source = Path.of("build/objs/graphics_incompatible.c");
Path library = Path.of("build/objs/graphics_incompatible" +
(TestC.OS.startsWith("Windows") ? ".dll" : ".so"));
Files.writeString(source,"""
#include "simple_graphics.h"
static const struct simple_graphics_api api = {999};
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());
assertTrue(run(graphics,library.toAbsolutePath().toString(),1).contains("incompatible MiniFB backend"));
}
}
|