diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/runner.py | 127 | ||||
-rw-r--r-- | tests/sdl_image.c | 8 | ||||
-rw-r--r-- | tests/sdl_image_jpeg.c | 45 |
3 files changed, 110 insertions, 70 deletions
diff --git a/tests/runner.py b/tests/runner.py index 6734c7e6..67b81214 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -16,7 +16,7 @@ so you may prefer to use fewer cores here. ''' from subprocess import Popen, PIPE, STDOUT -import os, unittest, tempfile, shutil, time, inspect, sys, math, glob, re, difflib, webbrowser, hashlib, threading, platform, BaseHTTPServer, multiprocessing, functools, stat +import os, unittest, tempfile, shutil, time, inspect, sys, math, glob, re, difflib, webbrowser, hashlib, threading, platform, BaseHTTPServer, multiprocessing, functools, stat, string if len(sys.argv) == 1: print ''' @@ -355,6 +355,9 @@ process(sys.argv[1]) cache_name = name + str(Building.COMPILER_TEST_OPTS) + cache_name_extra + (self.env.get('EMCC_LLVM_TARGET') or '') + valid_chars = "_%s%s" % (string.ascii_letters, string.digits) + cache_name = ''.join([(c if c in valid_chars else '_') for c in cache_name]) + if self.library_cache is not None: if cache and self.library_cache.get(cache_name): print >> sys.stderr, '<load %s from cache> ' % cache_name, @@ -1984,6 +1987,26 @@ Succeeded! self.do_run(src, "1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 " "1 2 -1 -2 2 2 -2 -2") + # This example borrowed from MSDN documentation + def test_fcvt(self): + if self.emcc_args is None: return self.skip('requires emcc') + + src = ''' + #include <stdlib.h> + #include <stdio.h> + + int main() { + int decimal, sign; + char *buffer; + double source = 3.1415926535; + + buffer = fcvt(source, 7, &decimal, &sign); + printf("source: %2.10f buffer: '%s' decimal: %d sign: %d\\n", + source, buffer, decimal, sign); + } + ''' + self.do_run(src, "source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0"); + def test_llrint(self): if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') src = r''' @@ -4062,17 +4085,11 @@ def process(filename): self.do_run(src, 'Inline JS is very cool\n3.64') - def zzztest_inlinejs2(self): + def test_inlinejs2(self): if Settings.ASM_JS: return self.skip('asm does not support random code, TODO: something that works in asm') src = r''' #include <stdio.h> - double get() { - double ret = 0; - __asm __volatile__("Math.abs(-12/3.3)":"=r"(ret)); // write to a variable - return ret; - } - int mix(int x, int y) { int ret; asm("Math.pow(2, %0+%1+1)" : "=r"(ret) : "r"(x), "r"(y)); // read and write @@ -4085,15 +4102,13 @@ def process(filename): } int main(int argc, char **argv) { - asm("Module.print('Inline JS is very cool')"); - printf("%.2f\n", get()); printf("%d\n", mix(argc, argc/2)); mult(); return 0; } ''' - self.do_run(src, 'Inline JS is very cool\n3.64\nwaka\nzakai\n') + self.do_run(src, '4\n200\n') def test_memorygrowth(self): if Settings.USE_TYPED_ARRAYS == 0: return self.skip('memory growth is only supported with typed arrays') @@ -7089,7 +7104,7 @@ def process(filename): other.close() src = open(path_from_root('tests', 'files.cpp'), 'r').read() - self.do_run(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\ntexte\n', + self.do_run(src, ('size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\ntexte\n', 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\n'), post_build=post, extra_emscripten_args=['-H', 'libc/fcntl.h']) def test_files_m(self): @@ -7121,7 +7136,7 @@ def process(filename): return 0; } ''' - self.do_run(src, 'got: 35\ngot: 45\ngot: 25\ngot: 15\nisatty? 0,0,1\n', post_build=post) + self.do_run(src, ('got: 35\ngot: 45\ngot: 25\ngot: 15\nisatty? 0,0,1\n', 'isatty? 0,0,1\ngot: 35\ngot: 45\ngot: 25\ngot: 15\n'), post_build=post) def test_fwrite_0(self): src = r''' @@ -7764,6 +7779,8 @@ def process(filename): self.do_run(src, '120.86.52.18\n120.86.52.18\n') def test_inet4(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') + src = r''' #include <stdio.h> #include <arpa/inet.h> @@ -11041,6 +11058,24 @@ f.close() voidfunc sidey(voidfunc f) { return f; } ''', 'hello from funcptr\n') + # function pointers with 'return' in the name + test('fp2', 'typedef void (*voidfunc)();', r''' + #include <stdio.h> + #include "header.h" + int sidey(voidfunc f); + void areturn0() { printf("hello 0\n"); } + void areturn1() { printf("hello 1\n"); } + void areturn2() { printf("hello 2\n"); } + int main(int argc, char **argv) { + voidfunc table[3] = { areturn0, areturn1, areturn2 }; + table[sidey(NULL)](); + return 0; + } + ''', ''' + #include "header.h" + int sidey(voidfunc f) { if (f) f(); return 1; } + ''', 'hello 1\n') + # Global initializer test('global init', '', r''' #include <stdio.h> @@ -11416,6 +11451,8 @@ int main(int argc, char const *argv[]) for args, expected in [(['-I/usr/something'], True), (['-L/usr/something'], True), + (['-I/usr/something', '-Wno-warn-absolute-paths'], False), + (['-L/usr/something', '-Wno-warn-absolute-paths'], False), (['-Isubdir/something'], False), (['-Lsubdir/something'], False), ([], False)]: @@ -11644,6 +11681,39 @@ int main(int argc, char const *argv[]) self.assertContained('a\nb\n', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_export_in_a(self): + export_name = 'this_is_an_entry_point' + + open('export.c', 'w').write(r''' + #include <stdio.h> + void %s(void) { + printf("Hello, world!\n"); + } + ''' % export_name) + Popen([PYTHON, EMCC, 'export.c', '-c', '-o', 'export.o']).communicate() + Popen([PYTHON, EMAR, 'rc', 'libexport.a', 'export.o']).communicate() + + open('main.c', 'w').write(r''' + int main() { + return 0; + } + ''') + + definition = 'function _%s(' % export_name + + # Sanity check: the symbol should not be linked in if not requested. + Popen([PYTHON, EMCC, 'main.c', '-L.', '-lexport']).communicate() + self.assertNotContained(definition, open(os.path.join(self.get_dir(), 'a.out.js')).read()) + + # Sanity check: exporting without a definition does not cause it to appear. + # Note: exporting main prevents emcc from warning that it generated no code. + Popen([PYTHON, EMCC, 'main.c', '-s', '''EXPORTED_FUNCTIONS=['_main', '_%s']''' % export_name]).communicate() + self.assertNotContained(definition, open(os.path.join(self.get_dir(), 'a.out.js')).read()) + + # Actual test: defining symbol in library and exporting it causes it to appear in the output. + Popen([PYTHON, EMCC, 'main.c', '-L.', '-lexport', '-s', '''EXPORTED_FUNCTIONS=['_%s']''' % export_name]).communicate() + self.assertContained(definition, open(os.path.join(self.get_dir(), 'a.out.js')).read()) + def test_embed_file(self): open(os.path.join(self.get_dir(), 'somefile.txt'), 'w').write('''hello from a file with lots of data and stuff in it thank you very much''') open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(r''' @@ -13055,13 +13125,21 @@ Press any key to continue.''' open(os.path.join(self.get_dir(), 'sdl_image.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image.c')).read())) for mem in [0, 1]: - Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '-O2', '--preload-file', 'screenshot.jpg', '-o', 'page.html', '--memory-init-file', str(mem)]).communicate() - self.run_browser('page.html', '', '/report_result?600') + for dest, dirname, basename in [('screenshot.jpg', '/', 'screenshot.jpg'), + ('screenshot.jpg@/assets/screenshot.jpg', '/assets', 'screenshot.jpg')]: + Popen([ + PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '-o', 'page.html', '-O2', '--memory-init-file', str(mem), + '--preload-file', dest, '-DSCREENSHOT_DIRNAME="' + dirname + '"', '-DSCREENSHOT_BASENAME="' + basename + '"' + ]).communicate() + self.run_browser('page.html', '', '/report_result?600') def test_sdl_image_jpeg(self): shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.jpeg')) - open(os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image_jpeg.c')).read())) - Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), '--preload-file', 'screenshot.jpeg', '-o', 'page.html']).communicate() + open(os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image.c')).read())) + Popen([ + PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), '-o', 'page.html', + '--preload-file', 'screenshot.jpeg', '-DSCREENSHOT_DIRNAME="/"', '-DSCREENSHOT_BASENAME="screenshot.jpeg"' + ]).communicate() self.run_browser('page.html', '', '/report_result?600') def test_sdl_image_compressed(self): @@ -13072,13 +13150,16 @@ Press any key to continue.''' basename = os.path.basename(image) shutil.copyfile(image, os.path.join(self.get_dir(), basename)) - open(os.path.join(self.get_dir(), 'sdl_image.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image.c')).read()).replace('screenshot.jpg', basename)) + open(os.path.join(self.get_dir(), 'sdl_image.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image.c')).read())) self.build_native_lzma() - Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '--preload-file', basename, '-o', 'page.html', - '--compression', '%s,%s,%s' % (path_from_root('third_party', 'lzma.js', 'lzma-native'), - path_from_root('third_party', 'lzma.js', 'lzma-decoder.js'), - 'LZMA.decompress')]).communicate() + Popen([ + PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '-o', 'page.html', + '--preload-file', basename, '-DSCREENSHOT_DIRNAME="/"', '-DSCREENSHOT_BASENAME="' + basename + '"', + '--compression', '%s,%s,%s' % (path_from_root('third_party', 'lzma.js', 'lzma-native'), + path_from_root('third_party', 'lzma.js', 'lzma-decoder.js'), + 'LZMA.decompress') + ]).communicate() shutil.move(os.path.join(self.get_dir(), basename), basename + '.renamedsoitcannotbefound'); self.run_browser('page.html', '', '/report_result?' + str(width)) @@ -14982,7 +15063,7 @@ fi print os.stat(os.path.join(EMCC_CACHE, libname + '.bc')).st_size, os.stat(basebc_name).st_size, os.stat(dcebc_name).st_size assert os.stat(os.path.join(EMCC_CACHE, libname + '.bc')).st_size > 1000000, 'libc++ is big' assert os.stat(basebc_name).st_size > 1000000, 'libc++ is indeed big' - assert os.stat(dcebc_name).st_size < 500000, 'Dead code elimination must remove most of libc++' + assert os.stat(dcebc_name).st_size < os.stat(basebc_name).st_size/2, 'Dead code elimination must remove most of libc++' # should only have metadata in -O0, not 1 and 2 if i > 0: for ll_name in ll_names: diff --git a/tests/sdl_image.c b/tests/sdl_image.c index 9d8c36f2..523f8903 100644 --- a/tests/sdl_image.c +++ b/tests/sdl_image.c @@ -3,6 +3,7 @@ #include <SDL/SDL_image.h> #include <assert.h> #include <emscripten.h> +#include <unistd.h> int testImage(SDL_Surface* screen, const char* fileName) { SDL_Surface *image = IMG_Load(fileName); @@ -27,9 +28,12 @@ int main() { SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); int result = 0; - result = testImage(screen, "screenshot.jpg"); // relative path + + result |= testImage(screen, SCREENSHOT_DIRNAME "/" SCREENSHOT_BASENAME); // absolute path assert(result != 0); - result |= testImage(screen, "/screenshot.jpg"); // absolute path + + chdir(SCREENSHOT_DIRNAME); + result = testImage(screen, "./" SCREENSHOT_BASENAME); // relative path assert(result != 0); SDL_Flip(screen); diff --git a/tests/sdl_image_jpeg.c b/tests/sdl_image_jpeg.c deleted file mode 100644 index 10619dad..00000000 --- a/tests/sdl_image_jpeg.c +++ /dev/null @@ -1,45 +0,0 @@ -#include <stdio.h> -#include <SDL/SDL.h> -#include <SDL/SDL_image.h> -#include <assert.h> -#include <emscripten.h> - -int testImage(SDL_Surface* screen, const char* fileName) { - SDL_Surface *image = IMG_Load(fileName); - if (!image) - { - printf("IMG_Load: %s\n", IMG_GetError()); - return 0; - } - assert(image->format->BitsPerPixel == 32); - assert(image->format->BytesPerPixel == 4); - assert(image->pitch == 4*image->w); - int result = image->w; - - SDL_BlitSurface (image, NULL, screen, NULL); - SDL_FreeSurface (image); - - return result; -} - -int main() { - SDL_Init(SDL_INIT_VIDEO); - SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); - - int result = 0; - result = testImage(screen, "screenshot.jpeg"); // relative path - assert(result != 0); - result |= testImage(screen, "/screenshot.jpeg"); // absolute path - assert(result != 0); - - SDL_Flip(screen); - - printf("you should see an image.\n"); - - SDL_Quit(); - - REPORT_RESULT(); - - return 0; -} - |