source: tests/exceptions/defaults.cfa @ 26d944c

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 26d944c was ecfd758, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Major exception update, seperating type-ids from virtual tables. The major interface changes are done. There is a regression of ?Cancelled(T) to Some?Cancelled. There is some bits of code for the new verion of the ?Cancelled(T) interface already there. Not connected yet but I just reached the limit of what I wanted to do in one commit and then spent over a day cleaning up, so it will replace Some?Cancelled in a future commit.

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