Skip to content

Commit 48ea251

Browse files
committed
[test] Use assert and zero exit code in more browser tests. NFC
1 parent 8278ad1 commit 48ea251

7 files changed

+67
-81
lines changed

test/core/test_em_asm_signatures.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include <assert.h>
12
#include <emscripten.h>
23
#include <stdio.h>
34

4-
int main()
5-
{
5+
int main() {
66
int ret = MAIN_THREAD_EM_ASM_INT(return 1);
77
ret += MAIN_THREAD_EM_ASM_INT(return $0, 1);
88
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1, 1, 2);
@@ -13,5 +13,6 @@ int main()
1313
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1 + $2 + $3 + $4 + $5 + $6, 1, 2, 3, 4, 5, 6, 7);
1414
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1 + $2 + $3 + $4 + $5 + $6 + $7, 1, 2, 3, 4, 5, 6, 7, 8);
1515
printf("ret: %d\n", ret);
16-
return ret;
16+
assert(ret == 121);
17+
return 0;
1718
}

test/emscripten_api_browser_infloop.cpp

Lines changed: 8 additions & 3 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 <assert.h>
67
#include <stdio.h>
78
#include <stdlib.h>
89
#include <string.h>
@@ -14,14 +15,18 @@ struct Class {
1415
int x;
1516

1617
Class() : x(0) {}
17-
~Class() { x = -9999; }
18+
~Class() {
19+
// Destructor should never be called.
20+
assert(false);
21+
x = -9999;
22+
}
1823

1924
void print() {
2025
printf("waka %d\n", x++);
2126

2227
if (x == 7 || x < 0) {
2328
emscripten_cancel_main_loop();
24-
exit(x);
29+
exit(x == 7 ? 0 : 1);
2530
}
2631
}
2732

@@ -52,6 +57,6 @@ Class *Class::instance = NULL;
5257

5358
int main() {
5459
Class().start();
55-
return 1;
60+
return 99;
5661
}
5762

test/emscripten_fs_api_browser.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <sys/stat.h>
1515
#include <unistd.h>
1616

17-
int result = 1;
1817
int get_count = 0;
1918
int data_ok = 0;
2019
int data_bad = 0;
@@ -65,43 +64,33 @@ void wait_wgets() {
6564
assert(IMG_Load("/tmp/screen_shot.png"));
6665
assert(data_ok == 1 && data_bad == 1);
6766
emscripten_cancel_main_loop();
68-
exit(result);
67+
exit(0);
6968
}
7069
assert(get_count <= 8);
7170
}
7271

7372
void onLoaded(const char* file) {
74-
if (strcmp(file, "/tmp/test.html") && strcmp(file, "/tmp/screen_shot.png")
75-
&& strcmp(file, "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html")
76-
&& strcmp(file, "/path/this_directory_is_relative_to_cwd/test.html")) {
77-
result = 0;
78-
}
73+
assert(strcmp(file, "/tmp/test.html") == 0 ||
74+
strcmp(file, "/tmp/screen_shot.png") == 0 ||
75+
strcmp(file, "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html") == 0 ||
76+
strcmp(file, "/path/this_directory_is_relative_to_cwd/test.html") == 0);
7977

8078
FILE * f = fopen(file, "r");
81-
if (f) {
82-
printf("exists: %s\n", file);
83-
int c = fgetc (f);
84-
if (c == EOF) {
85-
printf("file empty: %s\n", file);
86-
result = 0;
87-
}
88-
fclose(f);
89-
} else {
90-
result = 0;
91-
printf("!exists: %s\n", file);
92-
}
79+
assert(f);
80+
printf("exists: %s\n", file);
81+
int c = fgetc (f);
82+
assert(c != EOF && "file empty");
83+
fclose(f);
9384

9485
get_count++;
9586
printf("onLoaded %s\n", file);
9687
}
9788

9889
void onError(const char* file) {
99-
if (strcmp(file, "/tmp/null")) {
100-
result = 0;
101-
}
90+
printf("onError %s\n", file);
91+
assert(strcmp(file, "/tmp/null") == 0);
10292

10393
get_count++;
104-
printf("onError %s\n", file);
10594
}
10695

10796
int main() {
@@ -152,5 +141,5 @@ int main() {
152141

153142
emscripten_set_main_loop(wait_wgets, 0, 0);
154143

155-
return 0;
144+
return 99;
156145
}

test/emscripten_fs_api_browser2.c

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

6+
#include <assert.h>
7+
#include <stdbool.h>
68
#include <stdio.h>
79
#include <stdlib.h>
810
#include <emscripten.h>
@@ -11,40 +13,28 @@
1113
#include <SDL/SDL.h>
1214
#include "SDL/SDL_image.h"
1315

14-
int result = 1;
1516
int get_count = 0;
1617

1718
void onLoaded(const char* file) {
18-
if (strcmp(file, "/tmp/test.html")) {
19-
printf("what?\n");
20-
result = 0;
21-
}
22-
23-
FILE * f = fopen(file, "r");
24-
if (f) {
25-
printf("exists: %s\n", file);
26-
int c = fgetc (f);
27-
if (c == EOF) {
28-
printf("file empty: %s\n", file);
29-
result = 0;
30-
}
31-
fclose(f);
32-
} else {
33-
result = 0;
34-
printf("!exists: %s\n", file);
35-
}
36-
37-
get_count++;
3819
printf("onLoaded %s\n", file);
3920

40-
if (get_count == 2) {
41-
exit(result);
21+
assert(strcmp(file, "/tmp/test.html") == 0);
22+
23+
FILE * f = fopen(file, "r");
24+
assert(f);
25+
printf("exists: %s\n", file);
26+
int c = fgetc(f);
27+
assert(c != EOF && "file empty!");
28+
fclose(f);
29+
30+
if (++get_count == 2) {
31+
exit(0);
4232
}
4333
}
4434

4535
void onError(const char* file) {
4636
printf("error...\n");
47-
result = 0;
37+
assert(false);
4838
}
4939

5040
int main() {
@@ -61,5 +51,5 @@ int main() {
6151
onLoaded,
6252
onError);
6353

64-
return 0;
54+
return 99;
6555
}

test/pthread/test_pthread_cancel_cond_wait.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@
55

66
#include <pthread.h>
77
#include <sys/types.h>
8+
#include <stdbool.h>
89
#include <stdio.h>
910
#include <stdlib.h>
1011
#include <assert.h>
1112
#include <unistd.h>
1213
#include <errno.h>
13-
#include <emscripten.h>
14+
#include <emscripten/console.h>
1415

1516
pthread_barrier_t barrier;
1617
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1718
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;
1819

19-
int th_cancelled = 0;
20-
21-
volatile int res = 43;
20+
_Atomic bool th_cancelled = false;
21+
_Atomic int result = 0;
2222

2323
static void cleanup_handler(void *arg) {
24-
emscripten_log(EM_LOG_CONSOLE, "Called clean-up handler with arg %p", arg);
25-
int a = (intptr_t)(arg);
26-
res -= a;
24+
emscripten_outf("Called clean-up handler with arg %p", arg);
25+
result = (intptr_t)(arg);
26+
assert(result == 42);
2727

2828
pthread_mutex_unlock(&mutex);
2929
pthread_barrier_wait(&barrier);
3030
}
3131

3232
static void *thread_start(void *arg) {
3333
pthread_cleanup_push(cleanup_handler, (void*)42);
34-
emscripten_log(EM_LOG_CONSOLE, "Thread started!");
34+
emscripten_outf("Thread started!");
3535
pthread_mutex_lock(&mutex);
3636
pthread_barrier_wait(&barrier);
3737

3838
int ret = 0;
3939
do {
40-
emscripten_log(EM_LOG_CONSOLE, "Waiting on conditional variable");
40+
emscripten_outf("Waiting on conditional variable");
4141
ret = pthread_cond_wait(&condvar, &mutex);
42-
} while (ret == 0 && th_cancelled == 0);
42+
} while (ret == 0 && !th_cancelled);
4343

4444
if (ret != 0) {
45-
emscripten_log(EM_LOG_CONSOLE, "Cond wait failed ret: %d", ret);
45+
emscripten_outf("Cond wait failed ret: %d", ret);
4646
}
4747

48-
res = 1000; // Shouldn't ever reach here.
48+
assert(false); // Shouldn't ever reach here.
4949
pthread_cleanup_pop(0);
5050

5151
pthread_mutex_unlock(&mutex);
@@ -59,23 +59,24 @@ int main() {
5959
pthread_t thr;
6060
int s = pthread_create(&thr, NULL, thread_start, (void*)0);
6161
assert(s == 0);
62-
emscripten_log(EM_LOG_CONSOLE, "Thread created");
62+
emscripten_outf("Thread created");
6363

6464
pthread_barrier_wait(&barrier);
6565

6666
// Lock mutex to ensure that thread is waiting
6767
pthread_mutex_lock(&mutex);
6868

69-
emscripten_log(EM_LOG_CONSOLE, "Canceling thread..");
69+
emscripten_outf("Canceling thread..");
7070
s = pthread_cancel(thr);
7171
assert(s == 0);
72-
th_cancelled = 1;
72+
th_cancelled = true;
7373
pthread_mutex_unlock(&mutex);
7474

75-
emscripten_log(EM_LOG_CONSOLE, "Main thread waitnig for side-thread");
75+
emscripten_outf("Main thread waitnig for side-thread");
7676
pthread_barrier_wait(&barrier);
7777
pthread_barrier_destroy(&barrier);
7878

79-
emscripten_log(EM_LOG_CONSOLE, "Test finished result: %d", res);
80-
return res;
79+
emscripten_outf("Test finished result: %d", result);
80+
assert(result == 42);
81+
return 0;
8182
}

test/test_browser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,16 +1874,16 @@ def setup():
18741874
self.btest_exit('test_emscripten_async_load_script.c', args=['-sFORCE_FILESYSTEM'])
18751875

18761876
def test_emscripten_api_infloop(self):
1877-
self.btest_exit('emscripten_api_browser_infloop.cpp', assert_returncode=7)
1877+
self.btest_exit('emscripten_api_browser_infloop.cpp')
18781878

18791879
@also_with_wasmfs
18801880
def test_emscripten_fs_api(self):
18811881
shutil.copy(test_file('screenshot.png'), '.') # preloaded *after* run
1882-
self.btest_exit('emscripten_fs_api_browser.c', assert_returncode=1, args=['-lSDL'])
1882+
self.btest_exit('emscripten_fs_api_browser.c', args=['-lSDL'])
18831883

18841884
def test_emscripten_fs_api2(self):
1885-
self.btest_exit('emscripten_fs_api_browser2.c', assert_returncode=1, args=['-sASSERTIONS=0'])
1886-
self.btest_exit('emscripten_fs_api_browser2.c', assert_returncode=1, args=['-sASSERTIONS=1'])
1885+
self.btest_exit('emscripten_fs_api_browser2.c', args=['-sASSERTIONS=0'])
1886+
self.btest_exit('emscripten_fs_api_browser2.c', args=['-sASSERTIONS=1'])
18871887

18881888
@parameterized({
18891889
'': ([],),
@@ -3852,7 +3852,7 @@ def test_pthread_cancel(self):
38523852

38533853
# Test that pthread_cancel() cancels pthread_cond_wait() operation
38543854
def test_pthread_cancel_cond_wait(self):
3855-
self.btest_exit('pthread/test_pthread_cancel_cond_wait.c', assert_returncode=1, args=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
3855+
self.btest_exit('pthread/test_pthread_cancel_cond_wait.c', args=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
38563856

38573857
# Test pthread_kill() operation
38583858
@no_chrome('pthread_kill hangs chrome renderer, and keep subsequent tests from passing')
@@ -4116,10 +4116,10 @@ def test_pthread_trap(self):
41164116

41174117
# Tests MAIN_THREAD_EM_ASM_INT() function call signatures.
41184118
def test_main_thread_em_asm_signatures(self):
4119-
self.btest_exit('core/test_em_asm_signatures.cpp', assert_returncode=121, args=[])
4119+
self.btest_exit('core/test_em_asm_signatures.cpp')
41204120

41214121
def test_main_thread_em_asm_signatures_pthreads(self):
4122-
self.btest_exit('core/test_em_asm_signatures.cpp', assert_returncode=121, args=['-O3', '-pthread', '-sPROXY_TO_PTHREAD', '-sASSERTIONS'])
4122+
self.btest_exit('core/test_em_asm_signatures.cpp', args=['-O3', '-pthread', '-sPROXY_TO_PTHREAD', '-sASSERTIONS'])
41234123

41244124
def test_main_thread_async_em_asm(self):
41254125
self.btest_exit('core/test_main_thread_async_em_asm.cpp', args=['-O3', '-pthread', '-sPROXY_TO_PTHREAD', '-sASSERTIONS'])

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ def test_main_thread_async_em_asm(self, args, force_c=False):
18881888

18891889
# Tests MAIN_THREAD_EM_ASM_INT() function call with different signatures.
18901890
def test_main_thread_em_asm_signatures(self):
1891-
self.do_core_test('test_em_asm_signatures.cpp', assert_returncode=NON_ZERO)
1891+
self.do_core_test('test_em_asm_signatures.cpp')
18921892

18931893
@crossplatform
18941894
def test_em_asm_unicode(self):

0 commit comments

Comments
 (0)