aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/life.c87
-rwxr-xr-xtests/runner.py60
2 files changed, 144 insertions, 3 deletions
diff --git a/tests/life.c b/tests/life.c
new file mode 100644
index 00000000..08742f64
--- /dev/null
+++ b/tests/life.c
@@ -0,0 +1,87 @@
+// From http://rosettacode.org/wiki/Conway%27s_Game_of_Life#C
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define for_x for (int x = 0; x < w; x++)
+#define for_y for (int y = 0; y < h; y++)
+#define for_xy for_x for_y
+
+void show(void *u, int w, int h)
+{
+ int (*univ)[w] = u;
+ for_x printf("-"); printf("\n");
+ for_y {
+ for_x printf(univ[y][x] ? "[]" : " ");
+ printf("\n");
+ }
+ for_x printf("-"); printf("\n");
+ fflush(stdout);
+}
+
+void evolve(void *u, int w, int h)
+{
+ unsigned (*univ)[w] = u;
+ unsigned new[h][w];
+
+ for_y for_x {
+ int n = 0;
+ for (int y1 = y - 1; y1 <= y + 1; y1++)
+ for (int x1 = x - 1; x1 <= x + 1; x1++)
+ if (univ[(y1 + h) % h][(x1 + w) % w])
+ n++;
+
+ if (univ[y][x]) n--;
+ new[y][x] = (n == 3 || (n == 2 && univ[y][x]));
+ }
+ for_y for_x univ[y][x] = new[y][x];
+}
+
+void nudge(void *u, int w, int h)
+{
+ unsigned (*univ)[w] = u;
+ int sum = 0;
+ for_xy sum += univ[y][x];
+ while (sum < (w*h)/8) {
+ int x = sum & (w-1);
+ int y = (sum*sum) & (h-1);
+ univ[y][x] = 1;
+ sum++;
+ }
+}
+
+void game(int w, int h, int i)
+{
+ unsigned univ[h][w];
+ //for_xy univ[y][x] = rand() < RAND_MAX / 10 ? 1 : 0;
+ int acc = 0; // nonrandom generation, for benchmarking
+ for_xy {
+ acc += (x*17) % (y*3 + 1);
+ univ[y][x] = acc & 1;
+ }
+ while (i != 0) {
+ //show(univ, w, h);
+ evolve(univ, w, h);
+ if (i > 0) {
+ i--;
+ nudge(univ, w, h); // keep it interesting for benchmark
+ } else {
+ usleep(20000);
+ show(univ, w, h);
+ }
+ }
+ show(univ, w, h);
+}
+
+int main(int c, char **v)
+{
+ int w = 0, h = 0, i = -1; // i = -1 means run forever, normal. otherwise, run in benchmark mode
+ if (c > 1) w = atoi(v[1]);
+ if (c > 2) h = atoi(v[2]);
+ if (c > 3) i = atoi(v[3]);
+ if (w <= 0) w = 32;
+ if (h <= 0) h = 32;
+ game(w, h, i);
+}
+
diff --git a/tests/runner.py b/tests/runner.py
index fd5b3358..1ed063b7 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -3047,6 +3047,22 @@ Exiting setjmp function, level: 0, prev_jmp: -1
'''
self.do_run(src, '.ok.\n')
+ def test_life(self):
+ if self.emcc_args is None: return self.skip('need c99')
+ self.emcc_args += ['-std=c99']
+ src = open(path_from_root('tests', 'life.c'), 'r').read()
+ self.do_run(src, '''--------
+ [][][]
+[][]
+ []
+ []
+[][]
+
+
+
+--------
+''', ['8', '8', '25000'], force_c=True)
+
def test_array2(self):
src = '''
#include <stdio.h>
@@ -10683,7 +10699,7 @@ elif 'benchmark' in str(sys.argv):
print ' JavaScript: mean: %.3f (+-%.3f) secs median: %.3f range: %.3f-%.3f (noise: %3.3f%%) (%d runs)' % (mean, std, median, min(times), max(times), 100*std/mean, TEST_REPS)
print ' Native : mean: %.3f (+-%.3f) secs median: %.3f range: %.3f-%.3f (noise: %3.3f%%) JS is %.2f X slower' % (mean_native, std_native, median_native, min(native_times), max(native_times), 100*std_native/mean_native, final)
- def do_benchmark(self, name, src, args=[], expected_output='FAIL', emcc_args=[], native_args=[], force_c=False, reps=TEST_REPS):
+ def do_benchmark(self, name, src, args=[], expected_output='FAIL', emcc_args=[], native_args=[], shared_args=[], force_c=False, reps=TEST_REPS):
dirname = self.get_dir()
filename = os.path.join(dirname, name + '.c' + ('' if force_c else 'pp'))
f = open(filename, 'w')
@@ -10696,7 +10712,7 @@ elif 'benchmark' in str(sys.argv):
'-O2', '-s', 'INLINING_LIMIT=0', '-s', 'DOUBLE_MODE=0', '-s', 'PRECISE_I64_MATH=0',
'-s', 'ASM_JS=1',# '-s', 'USE_MATH_IMUL=1',
'-s', 'TOTAL_MEMORY=128*1024*1024', '-s', 'FAST_MEMORY=10*1024*1024',
- '-o', final_filename] + emcc_args, stdout=PIPE, stderr=self.stderr_redirect).communicate()
+ '-o', final_filename] + shared_args + emcc_args, stdout=PIPE, stderr=self.stderr_redirect).communicate()
assert os.path.exists(final_filename), 'Failed to compile file: ' + output[0]
# Run JS
@@ -10715,7 +10731,7 @@ elif 'benchmark' in str(sys.argv):
self.assertContained(expected_output, js_output)
# Run natively
- self.build_native(filename, native_args)
+ self.build_native(filename, shared_args + native_args)
global total_native_times
native_times = []
for i in range(reps):
@@ -10910,6 +10926,44 @@ elif 'benchmark' in str(sys.argv):
src = open(path_from_root('tests', 'skinning_test_no_simd.cpp'), 'r').read()
self.do_benchmark('skinning', src, ['9500', '10000'], 'blah=0.000000')
+ def test_life(self):
+ src = open(path_from_root('tests', 'life.c'), 'r').read()
+ self.do_benchmark('life', src, ['32', '32', '7100'], '''--------------------------------
+
+ [][][][][] []
+ []
+ []
+ []
+ [][][][] []
+ [][][][][] []
+ [] [] []
+ [][][] [] [] [][]
+[][][] [][][] [] [][] []
+[][][] [][] [][] [][][]
+ [][][][][] []
+ [][][] [] [] [] []
+ [] [][] [] [][] [] [][]
+ [] [] [][][] [] []
+ [][] [] [][]
+[][] [] [] [][] []
+[][] [] []
+[][] [][]
+ [] []
+ [][][] [][][]
+ [] [] []
+ [][][][][] [][]
+ [][] [][]
+ [][] []
+ [] []
+ [] [] [][] [][]
+ [][] [][]
+ [] [] [] []
+ [] []
+ []
+ [] []
+--------------------------------
+''', shared_args=['-std=c99'], force_c=True)
+
def test_dlmalloc(self):
# XXX This seems to have regressed slightly with emcc. Are -g and the signs lines passed properly?
src = open(path_from_root('system', 'lib', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read()