aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authormax99x <max99x@gmail.com>2011-09-13 02:07:59 +0300
committermax99x <max99x@gmail.com>2011-09-13 02:07:59 +0300
commit91e29b31f280b879b10debf2194c2b87d94857cf (patch)
tree3e264215977d5a384b58b08a8f6a979ae3254da4 /tests
parente3e357f06ea64e577bb7f4ac99b9d034de953baa (diff)
Basic support for typed exceptions.
Diffstat (limited to 'tests')
-rw-r--r--tests/exceptions/output.txt54
-rw-r--r--tests/exceptions/typed.cpp91
-rw-r--r--tests/runner.py9
3 files changed, 153 insertions, 1 deletions
diff --git a/tests/exceptions/output.txt b/tests/exceptions/output.txt
new file mode 100644
index 00000000..9f6961e3
--- /dev/null
+++ b/tests/exceptions/output.txt
@@ -0,0 +1,54 @@
+*CREATING A FOO
+*CREATING A BAR
+*CREATING A QUUX
+*CREATING A QUUX
+*CREATING A CHILD
+start
+
+
+ throwing ExFooInstance
+*COPYING A FOO
+*COPYING A FOO
+outer catch foo: 11
+*DESTROYING A FOO
+*DESTROYING A FOO
+
+
+ throwing ExBarInstance
+*COPYING A BAR
+*COPYING A BAR
+inner re-throw: 22
+*DESTROYING A BAR
+outer catch bar-ref: 22
+*DESTROYING A BAR
+
+
+ throwing ExQuuxInstance
+*COPYING A QUUX
+*COPYING A QUUX
+inner catch quux: 33
+*DESTROYING A QUUX
+*DESTROYING A QUUX
+
+
+
+
+
+
+ throwing 42
+outer catch int: 42
+
+
+ throwing NULL
+outer catch-all
+
+
+ not throwing
+
+
+end
+*DESTROYING A CHILD
+*DESTROYING A QUUX
+*DESTROYING A QUUX
+*DESTROYING A BAR
+*DESTROYING A FOO
diff --git a/tests/exceptions/typed.cpp b/tests/exceptions/typed.cpp
new file mode 100644
index 00000000..dd509bdf
--- /dev/null
+++ b/tests/exceptions/typed.cpp
@@ -0,0 +1,91 @@
+#include <stdio.h>
+
+class ExFoo {
+public:
+ int x;
+ ExFoo(int x) { this->x = x; printf("*CREATING A FOO\n"); }
+ ExFoo(const ExFoo& other) { x=other.x; printf("*COPYING A FOO\n"); }
+ ~ExFoo() { printf("*DESTROYING A FOO\n"); }
+} ExFooInstance(11);
+class ExBar {
+public:
+ int x;
+ ExBar(int x) { this->x = x; printf("*CREATING A BAR\n"); }
+ ExBar(const ExBar& other) { x=other.x; printf("*COPYING A BAR\n"); }
+ ~ExBar() { printf("*DESTROYING A BAR\n"); }
+} ExBarInstance(22);
+class ExQuux {
+public:
+ int x;
+ ExQuux(int x) { this->x = x; printf("*CREATING A QUUX\n"); }
+ ExQuux(const ExQuux& other) { x=other.x; printf("*COPYING A QUUX\n"); }
+ ~ExQuux() { printf("*DESTROYING A QUUX\n"); }
+} ExQuuxInstance(33);
+class ExChild : public ExQuux {
+public:
+ int x;
+ ExChild(int x) : ExQuux(x) { this->x = x; printf("*CREATING A CHILD\n"); }
+ ExChild(const ExChild& other) : ExQuux(x) { x=other.x; printf("*COPYING CHILD\n"); }
+ ~ExChild() { printf("*DESTROYING A CHILD\n"); }
+} ExChildInstance(44);
+
+void magic(int which) {
+ try {
+ switch (which) {
+ case 0:
+ printf(" throwing ExFooInstance\n");
+ throw ExFooInstance;
+ case 1:
+ printf(" throwing ExBarInstance\n");
+ throw ExBarInstance;
+ case 2:
+ printf(" throwing ExQuuxInstance\n");
+ throw ExQuuxInstance;
+// NOTE: Throwing pointers and polymorphic matching not supported.
+// case 3:
+// printf(" throwing ExQuux ptr\n");
+// throw &ExQuuxInstance;
+// case 4:
+// printf(" throwing ExChildInstance\n");
+// throw ExChildInstance;
+ case 5:
+ printf(" throwing 42\n");
+ throw 42;
+ case 6:
+ printf(" throwing NULL\n");
+ throw (void*)0;
+ case 7:
+ printf(" not throwing\n");
+ }
+ } catch (ExQuux e1) {
+ printf("inner catch quux: %d\n", e1.x);
+ } catch (ExBar e2) {
+ printf("inner re-throw: %d\n", e2.x);
+ throw;
+ }
+}
+
+int main() {
+ printf("start\n\n\n");
+ for (int i = 0; i < 8; i++) {
+ try {
+ magic(i);
+ } catch (ExFoo e1) {
+ printf("outer catch foo: %d\n", e1.x);
+ } catch (ExBar& e2) {
+ printf("outer catch bar-ref: %d\n", e2.x);
+// NOTE: Throwing pointers and polymorphic matching not supported.
+// } catch (ExQuux& e3) {
+// printf("outer catch quux-ref: %d\n", e3.x);
+// } catch (ExQuux* e4) {
+// printf("outer catch quux-ptr: %d\n", e4->x);
+ } catch (int e5) {
+ printf("outer catch int: %d\n", e5);
+ } catch (...) {
+ printf("outer catch-all\n");
+ }
+ printf("\n\n");
+ }
+ printf("end\n");
+ return 0;
+}
diff --git a/tests/runner.py b/tests/runner.py
index 09ea6745..c52027a5 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -178,7 +178,7 @@ class RunnerCore(unittest.TestCase):
def do_emscripten(self, filename, output_processor=None, append_ext=True, extra_args=[]):
# Run Emscripten
exported_settings = {}
- for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE', 'EXPORTED_FUNCTIONS', 'EXPORTED_GLOBALS', 'BUILD_AS_SHARED_LIB', 'INCLUDE_FULL_LIBRARY', 'RUNTIME_TYPE_INFO', 'DISABLE_EXCEPTIONS']:
+ for setting in ['QUANTUM_SIZE', 'RELOOP', 'OPTIMIZE', 'ASSERTIONS', 'USE_TYPED_ARRAYS', 'SAFE_HEAP', 'CHECK_OVERFLOWS', 'CORRECT_OVERFLOWS', 'CORRECT_SIGNS', 'CHECK_SIGNS', 'CORRECT_OVERFLOWS_LINES', 'CORRECT_SIGNS_LINES', 'CORRECT_ROUNDINGS', 'CORRECT_ROUNDINGS_LINES', 'INVOKE_RUN', 'SAFE_HEAP_LINES', 'INIT_STACK', 'AUTO_OPTIMIZE', 'EXPORTED_FUNCTIONS', 'EXPORTED_GLOBALS', 'BUILD_AS_SHARED_LIB', 'INCLUDE_FULL_LIBRARY', 'RUNTIME_TYPE_INFO', 'DISABLE_EXCEPTIONS', 'EXCEPTION_DEBUG']:
try:
value = eval(setting)
exported_settings[setting] = value
@@ -966,6 +966,13 @@ if 'benchmark' not in sys.argv:
DISABLE_EXCEPTIONS = 1
self.do_test(src, 'Compiled code throwing an exception')
+ def test_typed_exceptions(self):
+ global SAFE_HEAP; SAFE_HEAP = 0 # Throwing null will cause an ignorable null pointer access.
+ global EXCEPTION_DEBUG; EXCEPTION_DEBUG = 0 # Messes up expected output.
+ src = open(path_from_root('tests', 'exceptions', 'typed.cpp'), 'r').read()
+ expected = open(path_from_root('tests', 'exceptions', 'output.txt'), 'r').read()
+ self.do_test(src, expected)
+
def test_class(self):
src = '''
#include <stdio.h>