source: tests/exceptions/defaults.cfa@ 9c65169

Last change on this file since 9c65169 was d3cf623, checked in by Andrew Beach <ajbeach@…>, 9 months ago

Solved the requested warning with exceptions. Also went through the exceptions tests and managed to remove about 2/3rds of them from the lax list that now either didn't have any warnings or warnings because of the test itself.

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