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
RevLine 
[8ad5752]1// Tests for providing new default operations.
2
3#include <string.h>
4
[d00d581]5exception log_message {
[8ad5752]6        char * msg;
[d00d581]7};
[8ad5752]8
[c715e5f]9void copy(log_message * this, log_message * that) {
10        *this = *that;
11}
12
[ecfd758]13const char * msg(log_message * this) {
[8ad5752]14        return this->msg;
15}
[c715e5f]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};
[8ad5752]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 {
[ecfd758]34                throwResume (log_message){&log_vt, "Should be printed.\n"};
[8ad5752]35        } catchResume (log_message * this) {
[381132b]36                printf("%s", this->virtual_table->msg(this));
[8ad5752]37        }
38        // But we don't have to:
[ecfd758]39        throwResume (log_message){&log_vt, "Should not be printed.\n"};
[8ad5752]40}
41
42// I don't have a good use case for doing the same with termination.
[d00d581]43exception jump {};
[8ad5752]44void defaultTerminationHandler(jump &) {
45        printf("jump default handler.\n");
46}
47
[d00d581]48vtable(jump) jump_vt;
[ecfd758]49
[8ad5752]50void jump_test(void) {
51        try {
[ecfd758]52                throw (jump){&jump_vt};
[8ad5752]53        } catch (jump * this) {
54                printf("jump catch handler.\n");
55        }
[ecfd758]56        throw (jump){&jump_vt};
[8ad5752]57}
58
[d00d581]59exception first {};
60vtable(first) first_vt;
[ecfd758]61
[d00d581]62exception unhandled_exception {};
63vtable(unhandled_exception) unhandled_vt;
[8ad5752]64
65void unhandled_test(void) {
[fd54fef]66        forall(T &, V & | is_exception(T, V))
[8ad5752]67        void defaultTerminationHandler(T &) {
[ecfd758]68                throw (unhandled_exception){&unhandled_vt};
[8ad5752]69        }
70        void defaultTerminationHandler(unhandled_exception &) {
71                abort();
72        }
73        try {
[ecfd758]74                throw (first){&first_vt};
[8ad5752]75        } catch (unhandled_exception * t) {
76                printf("Catch unhandled_exception.\n");
77        }
78}
79
[d00d581]80exception second {};
81vtable(second) second_vt;
[381132b]82
83void cross_test(void) {
84        void defaultTerminationHandler(first &) {
85                printf("cross terminate default\n");
[ecfd758]86                throw (second){&second_vt};
[381132b]87        }
88        void defaultResumptionHandler(first &) {
89                printf("cross resume default\n");
[ecfd758]90                throwResume (second){&second_vt};
[381132b]91        }
92        try {
93                printf("cross terminate throw\n");
[ecfd758]94                throw (first){&first_vt};
[381132b]95        } catch (second *) {
96                printf("cross terminate catch\n");
97        }
98        try {
99                printf("cross resume throw\n");
[ecfd758]100                throwResume (first){&first_vt};
[381132b]101        } catchResume (second *) {
102                printf("cross resume catch\n");
103        }
104}
105
[8ad5752]106int main(int argc, char * argv[]) {
107        log_test();
108        jump_test();
109        unhandled_test();
[381132b]110        cross_test();
[8ad5752]111}
Note: See TracBrowser for help on using the repository browser.