aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-01-07 18:01:09 -0800
committerAlon Zakai <alonzakai@gmail.com>2012-01-07 18:01:09 -0800
commit5c02f40d3422617d0be4f0542b8b5281cae7883b (patch)
tree0a486e9da09016e3c47981d88844f3ccbc65acab /tests
parent16230c8cf1e947e03714da3fd3307a9eff41405a (diff)
automatic malloc need detection for new
Diffstat (limited to 'tests')
-rw-r--r--tests/new.cpp38
-rw-r--r--tests/runner.py12
2 files changed, 49 insertions, 1 deletions
diff --git a/tests/new.cpp b/tests/new.cpp
new file mode 100644
index 00000000..3e09fbc1
--- /dev/null
+++ b/tests/new.cpp
@@ -0,0 +1,38 @@
+// Emscripten tests
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+struct Structy { char data[100]; int x; };
+
+int main() {
+ int NUM = 100;
+ char* allocations[NUM];
+ for (int i = 0; i < NUM/2; i++) {
+ allocations[i] = (char*){{{ NEW }}};
+ assert(allocations[i]);
+ if (i > 10 && i%4 == 1 && allocations[i-10]) {
+ {{{ DELETE }}}(allocations[i-10]);
+ allocations[i-10] = NULL;
+ }
+ }
+ for (int i = NUM/2; i < NUM; i++) {
+ allocations[i] = (char*){{{ NEW }}};
+ assert(allocations[i]);
+ if (i > 10 && i%4 != 1 && allocations[i-10]) {
+ {{{ DELETE }}}(allocations[i-10]);
+ allocations[i-10] = NULL;
+ }
+ }
+ char* first = allocations[0];
+ for (int i = 0; i < NUM; i++) {
+ if (allocations[i]) {
+ {{{ DELETE }}}(allocations[i]);
+ }
+ }
+ char *last = (char*){{{ NEW }}}; /* should be identical, as we free'd it all */
+ char *newer = (char*){{{ NEW }}}; /* should be different */
+ printf("*%d,%d*\n", first == last, first == newer);
+}
+
diff --git a/tests/runner.py b/tests/runner.py
index 86d37e8e..34071a91 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -3844,11 +3844,21 @@ def process(filename):
try_delete(os.path.join(self.get_dir(), 'src.cpp.o.js'))
output = Popen([EMCC, path_from_root('tests', 'dlmalloc_test.c'),
'-o', os.path.join(self.get_dir(), 'src.cpp.o.js')], stdout=PIPE, stderr=self.stderr_redirect).communicate()
- #print output
self.do_run('x', '*1,0*', ['200', '1'], no_build=True)
self.do_run('x', '*400,0*', ['400', '400'], no_build=True)
+ # The same for new and all its variants
+ src = open(path_from_root('tests', 'new.cpp')).read()
+ for new, delete in [
+ ('malloc(100)', 'free'),
+ ('new char[100]', 'delete[]'),
+ ('new Structy', 'delete'),
+ ('new int', 'delete'),
+ ('new Structy[10]', 'delete[]'),
+ ]:
+ self.do_run(src.replace('{{{ NEW }}}', new).replace('{{{ DELETE }}}', delete), '*1,0*')
+
def test_libcxx(self):
self.do_run(path_from_root('tests', 'libcxx'),
'june -> 30\nPrevious (in alphabetical order) is july\nNext (in alphabetical order) is march',