Skip to content

Commit

Permalink
[test] Use assert and zero exit code in more browser tests. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Feb 5, 2025
1 parent 8278ad1 commit 48ea251
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 81 deletions.
7 changes: 4 additions & 3 deletions test/core/test_em_asm_signatures.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <assert.h>
#include <emscripten.h>
#include <stdio.h>

int main()
{
int main() {
int ret = MAIN_THREAD_EM_ASM_INT(return 1);
ret += MAIN_THREAD_EM_ASM_INT(return $0, 1);
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1, 1, 2);
Expand All @@ -13,5 +13,6 @@ int main()
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1 + $2 + $3 + $4 + $5 + $6, 1, 2, 3, 4, 5, 6, 7);
ret += MAIN_THREAD_EM_ASM_INT(return $0 + $1 + $2 + $3 + $4 + $5 + $6 + $7, 1, 2, 3, 4, 5, 6, 7, 8);
printf("ret: %d\n", ret);
return ret;
assert(ret == 121);
return 0;
}
11 changes: 8 additions & 3 deletions test/emscripten_api_browser_infloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -14,14 +15,18 @@ struct Class {
int x;

Class() : x(0) {}
~Class() { x = -9999; }
~Class() {
// Destructor should never be called.
assert(false);
x = -9999;
}

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

if (x == 7 || x < 0) {
emscripten_cancel_main_loop();
exit(x);
exit(x == 7 ? 0 : 1);
}
}

Expand Down Expand Up @@ -52,6 +57,6 @@ Class *Class::instance = NULL;

int main() {
Class().start();
return 1;
return 99;
}

37 changes: 13 additions & 24 deletions test/emscripten_fs_api_browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <sys/stat.h>
#include <unistd.h>

int result = 1;
int get_count = 0;
int data_ok = 0;
int data_bad = 0;
Expand Down Expand Up @@ -65,43 +64,33 @@ void wait_wgets() {
assert(IMG_Load("/tmp/screen_shot.png"));
assert(data_ok == 1 && data_bad == 1);
emscripten_cancel_main_loop();
exit(result);
exit(0);
}
assert(get_count <= 8);
}

void onLoaded(const char* file) {
if (strcmp(file, "/tmp/test.html") && strcmp(file, "/tmp/screen_shot.png")
&& strcmp(file, "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html")
&& strcmp(file, "/path/this_directory_is_relative_to_cwd/test.html")) {
result = 0;
}
assert(strcmp(file, "/tmp/test.html") == 0 ||
strcmp(file, "/tmp/screen_shot.png") == 0 ||
strcmp(file, "/this_directory_does_not_exist_and_should_be_created_by_wget/test.html") == 0 ||
strcmp(file, "/path/this_directory_is_relative_to_cwd/test.html") == 0);

FILE * f = fopen(file, "r");
if (f) {
printf("exists: %s\n", file);
int c = fgetc (f);
if (c == EOF) {
printf("file empty: %s\n", file);
result = 0;
}
fclose(f);
} else {
result = 0;
printf("!exists: %s\n", file);
}
assert(f);
printf("exists: %s\n", file);
int c = fgetc (f);
assert(c != EOF && "file empty");
fclose(f);

get_count++;
printf("onLoaded %s\n", file);
}

void onError(const char* file) {
if (strcmp(file, "/tmp/null")) {
result = 0;
}
printf("onError %s\n", file);
assert(strcmp(file, "/tmp/null") == 0);

get_count++;
printf("onError %s\n", file);
}

int main() {
Expand Down Expand Up @@ -152,5 +141,5 @@ int main() {

emscripten_set_main_loop(wait_wgets, 0, 0);

return 0;
return 99;
}
40 changes: 15 additions & 25 deletions test/emscripten_fs_api_browser2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <emscripten.h>
Expand All @@ -11,40 +13,28 @@
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"

int result = 1;
int get_count = 0;

void onLoaded(const char* file) {
if (strcmp(file, "/tmp/test.html")) {
printf("what?\n");
result = 0;
}

FILE * f = fopen(file, "r");
if (f) {
printf("exists: %s\n", file);
int c = fgetc (f);
if (c == EOF) {
printf("file empty: %s\n", file);
result = 0;
}
fclose(f);
} else {
result = 0;
printf("!exists: %s\n", file);
}

get_count++;
printf("onLoaded %s\n", file);

if (get_count == 2) {
exit(result);
assert(strcmp(file, "/tmp/test.html") == 0);

FILE * f = fopen(file, "r");
assert(f);
printf("exists: %s\n", file);
int c = fgetc(f);
assert(c != EOF && "file empty!");
fclose(f);

if (++get_count == 2) {
exit(0);
}
}

void onError(const char* file) {
printf("error...\n");
result = 0;
assert(false);
}

int main() {
Expand All @@ -61,5 +51,5 @@ int main() {
onLoaded,
onError);

return 0;
return 99;
}
37 changes: 19 additions & 18 deletions test/pthread/test_pthread_cancel_cond_wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@

#include <pthread.h>
#include <sys/types.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <emscripten.h>
#include <emscripten/console.h>

pthread_barrier_t barrier;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;

int th_cancelled = 0;

volatile int res = 43;
_Atomic bool th_cancelled = false;
_Atomic int result = 0;

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

pthread_mutex_unlock(&mutex);
pthread_barrier_wait(&barrier);
}

static void *thread_start(void *arg) {
pthread_cleanup_push(cleanup_handler, (void*)42);
emscripten_log(EM_LOG_CONSOLE, "Thread started!");
emscripten_outf("Thread started!");
pthread_mutex_lock(&mutex);
pthread_barrier_wait(&barrier);

int ret = 0;
do {
emscripten_log(EM_LOG_CONSOLE, "Waiting on conditional variable");
emscripten_outf("Waiting on conditional variable");
ret = pthread_cond_wait(&condvar, &mutex);
} while (ret == 0 && th_cancelled == 0);
} while (ret == 0 && !th_cancelled);

if (ret != 0) {
emscripten_log(EM_LOG_CONSOLE, "Cond wait failed ret: %d", ret);
emscripten_outf("Cond wait failed ret: %d", ret);
}

res = 1000; // Shouldn't ever reach here.
assert(false); // Shouldn't ever reach here.
pthread_cleanup_pop(0);

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

pthread_barrier_wait(&barrier);

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

emscripten_log(EM_LOG_CONSOLE, "Canceling thread..");
emscripten_outf("Canceling thread..");
s = pthread_cancel(thr);
assert(s == 0);
th_cancelled = 1;
th_cancelled = true;
pthread_mutex_unlock(&mutex);

emscripten_log(EM_LOG_CONSOLE, "Main thread waitnig for side-thread");
emscripten_outf("Main thread waitnig for side-thread");
pthread_barrier_wait(&barrier);
pthread_barrier_destroy(&barrier);

emscripten_log(EM_LOG_CONSOLE, "Test finished result: %d", res);
return res;
emscripten_outf("Test finished result: %d", result);
assert(result == 42);
return 0;
}
14 changes: 7 additions & 7 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,16 +1874,16 @@ def setup():
self.btest_exit('test_emscripten_async_load_script.c', args=['-sFORCE_FILESYSTEM'])

def test_emscripten_api_infloop(self):
self.btest_exit('emscripten_api_browser_infloop.cpp', assert_returncode=7)
self.btest_exit('emscripten_api_browser_infloop.cpp')

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

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

@parameterized({
'': ([],),
Expand Down Expand Up @@ -3852,7 +3852,7 @@ def test_pthread_cancel(self):

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

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

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

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

def test_main_thread_async_em_asm(self):
self.btest_exit('core/test_main_thread_async_em_asm.cpp', args=['-O3', '-pthread', '-sPROXY_TO_PTHREAD', '-sASSERTIONS'])
Expand Down
2 changes: 1 addition & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ def test_main_thread_async_em_asm(self, args, force_c=False):

# Tests MAIN_THREAD_EM_ASM_INT() function call with different signatures.
def test_main_thread_em_asm_signatures(self):
self.do_core_test('test_em_asm_signatures.cpp', assert_returncode=NON_ZERO)
self.do_core_test('test_em_asm_signatures.cpp')

@crossplatform
def test_em_asm_unicode(self):
Expand Down

0 comments on commit 48ea251

Please sign in to comment.