Skip to content

Commit 9dd9c35

Browse files
authored
Convert more btests to btest_exit. NFC (#14415)
There are still many more to convert.. perhaps we should be more systematic about it. For now I'm just doing them in batches by hand to keep the reviews of reasonable size.
1 parent 74d0c9d commit 9dd9c35

32 files changed

+182
-275
lines changed
Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
#include <emscripten/html5.h>
22
#include <stdio.h>
3+
#include <stdlib.h>
34

4-
void timeout(void *userData)
5-
{
6-
printf("Got timeout handler\n");
7-
#ifdef REPORT_RESULT
8-
// Test passed
9-
REPORT_RESULT(1);
10-
#endif
5+
void timeout(void *userData) {
6+
printf("Got timeout handler\n");
7+
// Test passed
8+
exit(0);
119
}
1210

13-
int main()
14-
{
15-
emscripten_set_timeout(timeout, 2000, 0);
16-
emscripten_unwind_to_js_event_loop();
17-
printf("This should not be called!\n");
18-
#ifdef REPORT_RESULT
19-
// Should not reach here
20-
REPORT_RESULT(-1);
21-
#endif
11+
int main() {
12+
emscripten_set_timeout(timeout, 2000, 0);
13+
emscripten_unwind_to_js_event_loop();
14+
// emscripten_unwind_to_js_event_loop should never return
15+
__builtin_unreachable();
2216
}

tests/canvas_animate_resize.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ void tick()
8181
emscripten_webgl_make_context_current(0);
8282
emscripten_webgl_destroy_context(ctx);
8383
printf("quit\n");
84-
#ifdef REPORT_RESULT
85-
REPORT_RESULT(1);
86-
#endif
8784
exit(0);
8885
}
8986
}
@@ -154,4 +151,5 @@ int main()
154151
usleep(16*1000);
155152
}
156153
#endif
154+
return 99;
157155
}

tests/cstdio/test_remove.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,5 @@ int main() {
6464
atexit(cleanup);
6565
setup();
6666
test();
67-
68-
#ifdef REPORT_RESULT
69-
REPORT_RESULT(0);
70-
#endif
71-
return EXIT_SUCCESS;
67+
return 0;
7268
}

tests/emscripten_api_browser.cpp renamed to tests/emscripten_api_browser.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6+
#include <stdbool.h>
67
#include <stdio.h>
78
#include <math.h>
89
#include <stdlib.h>
910
#include <SDL.h>
1011
#include <emscripten.h>
1112
#include <assert.h>
1213

14+
bool exit_ok = false;
1315
int last = 0;
1416

15-
extern "C" {
16-
1717
bool pre1ed = false;
1818
bool pre2ed = false;
1919
void pre1(void *arg) {
@@ -43,7 +43,9 @@ void argey(void* arg) {
4343
printf("argey: %d\n", counter);
4444
if (counter == 5) {
4545
emscripten_cancel_main_loop();
46-
REPORT_RESULT(1);
46+
// The main loop is now done so its ok to run atexit handlers.
47+
exit_ok = true;
48+
exit(0);
4749
}
4850
}
4951

@@ -75,22 +77,22 @@ void four(void *arg) {
7577
void __attribute__((used)) third() {
7678
int now = SDL_GetTicks();
7779
printf("thard! %d\n", now);
78-
assert(fabs(now - last - 1000) < 500);
80+
assert(abs(now - last - 1000) < 500);
7981
emscripten_async_call(four, (void*)43, -1); // triggers requestAnimationFrame
8082
}
8183

8284
void second(void *arg) {
8385
int now = SDL_GetTicks();
8486
printf("sacond! %d\n", now);
85-
assert(fabs(now - last - 500) < 250);
87+
assert(abs(now - last - 500) < 250);
8688
last = now;
8789
emscripten_async_run_script("Module._third()", 1000);
8890
}
8991

90-
}
91-
92-
void never() {
93-
REPORT_RESULT(0);
92+
// Should not be called when main return but only once the
93+
// main loops is stopped and the runtime shuts down.
94+
void check_exit_ok() {
95+
assert(exit_ok == true);
9496
}
9597

9698
int main() {
@@ -105,7 +107,7 @@ int main() {
105107

106108
assert(ratio == ratio2);
107109

108-
atexit(never); // should never be called - it is wrong to exit the runtime orderly if we have async calls!
110+
atexit(check_exit_ok);
109111

110112
emscripten_async_call(second, (void*)0, 500);
111113

tests/emscripten_api_browser2.cpp renamed to tests/emscripten_api_browser2.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
// found in the LICENSE file.
55

66
#include <stdio.h>
7+
#include <stdlib.h>
78
#include <string.h>
89
#include <assert.h>
910

1011
#include <emscripten.h>
1112

1213
int value = 0;
1314

14-
extern "C" {
15-
void set(int x) {
16-
printf("set! %d\n", x);
17-
value = x;
18-
}
15+
void set(int x) {
16+
printf("set! %d\n", x);
17+
value = x;
1918
}
2019

2120
void load2() {
@@ -34,8 +33,9 @@ void load2() {
3433
fclose(f);
3534
assert(strcmp(buffer, "second") == 0);
3635

37-
REPORT_RESULT(1);
36+
exit(0);
3837
}
38+
3939
void error2() {
4040
printf("fail2\n");
4141
}
@@ -45,13 +45,12 @@ void load1() {
4545
assert(value == 456);
4646
emscripten_async_load_script("script2.js", load2, error2);
4747
}
48+
4849
void error1() {
4950
printf("fail1\n");
5051
}
5152

5253
int main() {
5354
emscripten_async_load_script("script1.js", load1, error1);
54-
55-
return 1;
55+
return 99;
5656
}
57-

tests/emscripten_api_browser_infloop.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// found in the LICENSE file.
55

66
#include <stdio.h>
7+
#include <stdlib.h>
78
#include <string.h>
89
#include <emscripten.h>
910

@@ -19,8 +20,8 @@ struct Class {
1920
printf("waka %d\n", x++);
2021

2122
if (x == 7 || x < 0) {
22-
REPORT_RESULT(x);
2323
emscripten_cancel_main_loop();
24+
exit(x);
2425
}
2526
}
2627

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1+
#include <assert.h>
12
#include <stdio.h>
23
#include <emscripten/emscripten.h>
34

4-
int main()
5-
{
6-
double devicePixelRatio = emscripten_get_device_pixel_ratio();
7-
printf("window.devicePixelRatio = %f.\n", devicePixelRatio);
8-
int result = (devicePixelRatio > 0) ? 1 : 0;
9-
if (result) {
10-
printf("Test succeeded!\n");
11-
}
12-
#ifdef REPORT_RESULT
13-
REPORT_RESULT(result);
14-
#endif
5+
int main() {
6+
double devicePixelRatio = emscripten_get_device_pixel_ratio();
7+
printf("window.devicePixelRatio = %f.\n", devicePixelRatio);
8+
assert(devicePixelRatio > 0);
9+
return 0;
1510
}

tests/emscripten_main_loop.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ int frame = 0;
1313

1414
void final(void*) {
1515
assert(frame == 110);
16-
#ifdef REPORT_RESULT
1716
printf("Test passed.\n");
18-
REPORT_RESULT(0);
19-
#endif
17+
exit(0);
2018
}
2119

2220
void looper() {
@@ -29,11 +27,8 @@ void looper() {
2927
timesTooSoon++;
3028
if (timesTooSoon >= 10) {
3129
printf("Abort: main loop tick was called too quickly after the previous frame, too many times!\n");
32-
#ifdef REPORT_RESULT
33-
REPORT_RESULT(1);
34-
#endif
3530
emscripten_cancel_main_loop();
36-
exit(0);
31+
exit(1);
3732
}
3833
}
3934
prevTime = curTime;
@@ -42,11 +37,8 @@ void looper() {
4237
timesTooSoon++;
4338
if (timesTooSoon >= 2) {
4439
printf("Abort: With swap interval of 4, we should be running at most 15fps! (or 30fps on 120Hz displays) but seems like swap control is not working and we are running at 60fps!\n");
45-
#ifdef REPORT_RESULT
46-
REPORT_RESULT(2);
47-
#endif
4840
emscripten_cancel_main_loop();
49-
exit(0);
41+
exit(2);
5042
}
5143
}
5244
if (frame > 0 && frame < 90 && frame % 10 == 0) {
@@ -79,4 +71,5 @@ void looper() {
7971

8072
int main() {
8173
emscripten_set_main_loop(looper, 5, 1);
74+
return 99;
8275
}

tests/emscripten_main_loop_and_blocker.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@ bool blockerExecuted = false;
1414

1515
void final(void*) {
1616
assert(frame == 20);
17-
#ifdef REPORT_RESULT
18-
REPORT_RESULT(0);
19-
#else
2017
exit(0);
21-
#endif
2218
}
2319

2420
void looper() {
2521
if (blockerExecuted == false) {
26-
#ifdef REPORT_RESULT
27-
REPORT_RESULT(1);
28-
#else
2922
exit(1);
30-
#endif
3123
}
3224

3325
frame++;

tests/emscripten_main_loop_setimmediate.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6+
#include <stdbool.h>
67
#include <stdlib.h>
78
#include <stdio.h>
89
#include <assert.h>
@@ -21,15 +22,16 @@ void looper() {
2122
double now = emscripten_get_now();
2223
double msecsPerFrame = (now - frame0) / (numFrames-1); // Sub one to account for intervals vs endpoints
2324
printf("Avg. msecs/frame: %f\n", msecsPerFrame);
24-
#ifdef REPORT_RESULT
25-
int result = (msecsPerFrame < 5); // Expecting to run extremely fast unthrottled, and certainly not bounded by vsync, so less than common 16.667 msecs per frame (this is assuming 60hz display)
26-
REPORT_RESULT(result);
27-
#endif
2825
emscripten_cancel_main_loop();
26+
// Expecting to run extremely fast unthrottled, and certainly not bounded by
27+
// vsync, so less than common 16.667 msecs per frame (this is assuming 60hz
28+
// display)
29+
exit((msecsPerFrame < 5) ? 0 : 1);
2930
}
3031
}
3132

3233
int main() {
3334
emscripten_set_main_loop(looper, 1, 0);
3435
emscripten_set_main_loop_timing(EM_TIMING_SETIMMEDIATE, 0);
36+
return 99;
3537
}

0 commit comments

Comments
 (0)