aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-02-05 13:56:01 -0500
committerAlon Zakai <alonzakai@gmail.com>2014-02-05 15:16:05 -0500
commit0c9c8b4c524e599bc17a1a011c3ec2ec9c24b998 (patch)
treee1999a3fe69100d42c08d4109acb4d676c680fda /tests
parent9532e3875c691cc50efbee95475243eeb46e2552 (diff)
optimize out fround calls on clients that do not support fround
Diffstat (limited to 'tests')
-rw-r--r--tests/codemods.cpp21
-rw-r--r--tests/test_browser.py33
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/codemods.cpp b/tests/codemods.cpp
new file mode 100644
index 00000000..26712339
--- /dev/null
+++ b/tests/codemods.cpp
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <math.h>
+#include <emscripten.h>
+
+int main() {
+ volatile int x = 10;
+ float y = 123456789.123456789;
+ while (x-- > 0) {
+ y = (sqrtf(y) + y)/2;
+ }
+ double d = y;
+ double diff = fabs(d - 121376.4609375000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000);
+ int ok = fabs(diff) < 0.000001;
+ printf("%.20f : %d\n", diff, ok);
+
+ int result;
+ if (ok) result = 1;
+ else result = diff+2; // add two to this >= number to avoid conflicts with 1
+ REPORT_RESULT();
+}
+
diff --git a/tests/test_browser.py b/tests/test_browser.py
index f185c211..ef67aa20 100644
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@ -1815,3 +1815,36 @@ Module["preRun"].push(function () {
def test_html5(self):
self.btest(path_from_root('tests', 'test_html5.c'), expected='0')
+
+ def test_codemods(self):
+ for opt_level in [0, 2]:
+ print 'opt level', opt_level
+ opts = '-O' + str(opt_level)
+ # sanity checks, building with and without precise float semantics generates different results
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='2', args=[opts])
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='1', args=[opts, '-s', 'PRECISE_F32=1'])
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='1', args=[opts, '-s', 'PRECISE_F32=2']) # empty polyfill, but browser has support, so semantics are like float
+
+ # now use a shell to remove the browser's fround support
+ open(self.in_dir('shell.html'), 'w').write(open(path_from_root('src', 'shell.html')).read().replace('var Module = {', '''
+ Math.fround = null;
+ var Module = {
+ '''))
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='2', args=[opts, '--shell-file', 'shell.html'])
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='1', args=[opts, '--shell-file', 'shell.html', '-s', 'PRECISE_F32=1'])
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='2', args=[opts, '--shell-file', 'shell.html', '-s', 'PRECISE_F32=2']) # empty polyfill, no browser support, so semantics are like double
+
+ # finally, remove fround, patch up fround as the code executes (after polyfilling etc.), to verify that we got rid of it entirely on the client side
+ fixer = 'python fix.py'
+ open('fix.py', 'w').write(r'''
+import sys
+filename = sys.argv[1]
+js = open(filename).read()
+replaced = js.replace("var Math_fround = Math.fround;", "var Math_fround = Math.fround = function(x) { return 0; }")
+assert js != replaced
+open(filename, 'w').write(replaced)
+ ''')
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='2', args=[opts, '--shell-file', 'shell.html', '--js-transform', fixer]) # no fround anyhow
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='121378', args=[opts, '--shell-file', 'shell.html', '--js-transform', fixer, '-s', 'PRECISE_F32=1']) # proper polyfill was enstated, then it was replaced by the fix so 0 is returned all the time, hence a different result here
+ self.btest(path_from_root('tests', 'codemods.cpp'), expected='2', args=[opts, '--shell-file', 'shell.html', '--js-transform', fixer, '-s', 'PRECISE_F32=2']) # we should remove the calls to the polyfill ENTIRELY here, on the clientside, so we should NOT see any calls to fround here, and result should be like double
+