source: tests/exceptions/defaults.cfa @ 8ad5752

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8ad5752 was 8ad5752, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added tests for default exception handlers. Fixed a memory leak they revealed.

  • Property mode set to 100644
File size: 1.6 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
15char * 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){(char *)"Should be printed.\n"};
31        } catchResume (log_message * this) {
32                printf(this->virtual_table->msg(this));
33        }
34        // But we don't have to:
35        throwResume (log_message){(char *)"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(dtype T | is_exception(T))
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
71int main(int argc, char * argv[]) {
72        log_test();
73        jump_test();
74        unhandled_test();
75}
Note: See TracBrowser for help on using the repository browser.