source: tests/exceptions/defaults.cfa @ fd54fef

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since fd54fef was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 9 months ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 2.1 KB
Line 
1// Tests for providing new default operations.
2
3#include <string.h>
4#include <exception.hfa>
5
6DATA_EXCEPTION(log_message)(
7        char * msg;
8);
9
10void ?{}(log_message & this, char * msg) {
11        VTABLE_INIT(this, log_message);
12        this.msg = msg;
13}
14
15const char * get_log_message(log_message * this) {
16        return this->msg;
17}
18
19VTABLE_INSTANCE(log_message)(get_log_message);
20
21// Logging messages don't have to be handled.
22void defaultResumptionHandler(log_message &) {}
23
24// And should never be used to terminate computation.
25void defaultTerminationHandler(log_message &) = void;
26
27void log_test(void) {
28        // We can catch log:
29        try {
30                throwResume (log_message){"Should be printed.\n"};
31        } catchResume (log_message * this) {
32                printf("%s", this->virtual_table->msg(this));
33        }
34        // But we don't have to:
35        throwResume (log_message){"Should not be printed.\n"};
36}
37
38// I don't have a good use case for doing the same with termination.
39TRIVIAL_EXCEPTION(jump);
40void defaultTerminationHandler(jump &) {
41        printf("jump default handler.\n");
42}
43
44void jump_test(void) {
45        try {
46                throw (jump){};
47        } catch (jump * this) {
48                printf("jump catch handler.\n");
49        }
50        throw (jump){};
51}
52
53TRIVIAL_EXCEPTION(first);
54TRIVIAL_EXCEPTION(unhandled_exception);
55
56void unhandled_test(void) {
57        forall(T &, V & | is_exception(T, V))
58        void defaultTerminationHandler(T &) {
59                throw (unhandled_exception){};
60        }
61        void defaultTerminationHandler(unhandled_exception &) {
62                abort();
63        }
64        try {
65                throw (first){};
66        } catch (unhandled_exception * t) {
67                printf("Catch unhandled_exception.\n");
68        }
69}
70
71TRIVIAL_EXCEPTION(second);
72
73void cross_test(void) {
74        void defaultTerminationHandler(first &) {
75                printf("cross terminate default\n");
76                throw (second){};
77        }
78        void defaultResumptionHandler(first &) {
79                printf("cross resume default\n");
80                throwResume (second){};
81        }
82        try {
83                printf("cross terminate throw\n");
84                throw (first){};
85        } catch (second *) {
86                printf("cross terminate catch\n");
87        }
88        try {
89                printf("cross resume throw\n");
90                throwResume (first){};
91        } catchResume (second *) {
92                printf("cross resume catch\n");
93        }
94}
95
96int main(int argc, char * argv[]) {
97        log_test();
98        jump_test();
99        unhandled_test();
100        cross_test();
101}
Note: See TracBrowser for help on using the repository browser.