source: tests/exceptions/defaults.cfa @ c715e5f

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since c715e5f was c715e5f, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Removed most of the exception macros (EHM_ group). Made changes to the exception declaration pass to do so.

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