1 | // New draft of exception tests. |
---|
2 | |
---|
3 | |
---|
4 | #include <stdlib> |
---|
5 | #include "except-mac.h" |
---|
6 | |
---|
7 | TRIVIAL_EXCEPTION(yin) |
---|
8 | TRIVIAL_EXCEPTION(yang) |
---|
9 | |
---|
10 | struct num_error; |
---|
11 | struct num_error_vtable { |
---|
12 | struct TABLE(BASE_EXCEPT) const * parent; |
---|
13 | size_t size; |
---|
14 | void (*copy)(num_error *this, num_error * other); |
---|
15 | void (*free)(num_error *this); |
---|
16 | const char * (*msg)(num_error *this); |
---|
17 | int (*code)(num_error *this); |
---|
18 | }; |
---|
19 | extern num_error_vtable INSTANCE(num_error); |
---|
20 | |
---|
21 | struct num_error { |
---|
22 | struct num_error_vtable const * virtual_table; |
---|
23 | char * msg; |
---|
24 | int num; |
---|
25 | }; |
---|
26 | |
---|
27 | void num_error_msg(num_error * this) { |
---|
28 | if ( ! this->msg ) { |
---|
29 | static const char * base = "Num Error with code: X"; |
---|
30 | this->msg = malloc(22); |
---|
31 | for (int i = 0 ; (this->msg[i] = base[i]) ; ++i); |
---|
32 | } |
---|
33 | this->msg[21] = '0' + this->num; |
---|
34 | return this->msg; |
---|
35 | } |
---|
36 | void ?{}(num_error * this, int num) { |
---|
37 | this->virtual_table = &INSTANCE(num_error); |
---|
38 | this->msg = 0; |
---|
39 | this->num = num; |
---|
40 | } |
---|
41 | void ?{}(num_error * this, num_error * other) { |
---|
42 | this->virtual_table = other->virtual_table; |
---|
43 | this->msg = 0; |
---|
44 | this->num = other->num; |
---|
45 | } |
---|
46 | void ^?{}(num_error * this) { |
---|
47 | if( this->msg ) free( this->msg ); |
---|
48 | } |
---|
49 | int num_error_code( num_error * this ) { |
---|
50 | return this->num; |
---|
51 | } |
---|
52 | num_error_vtable _num_error_vtable_instance @= { |
---|
53 | &INSTANCE(BASE_EXCEPT), |
---|
54 | sizeof(num_error), ?{}, ^?{}, |
---|
55 | num_error_msg, num_error_code |
---|
56 | }; |
---|
57 | |
---|
58 | |
---|
59 | // Test simple throwing, matching and catching. |
---|
60 | void throw_catch() { |
---|
61 | try { |
---|
62 | yin black; |
---|
63 | THROW(&black); |
---|
64 | } catch ( yin * error ) { |
---|
65 | printf("throw yin caught.\n"); |
---|
66 | } |
---|
67 | |
---|
68 | try { |
---|
69 | yang white; |
---|
70 | THROW_RESUME(&white); |
---|
71 | printf("> throwResume returned.\n"); |
---|
72 | } catchResume ( yang * error ) { |
---|
73 | printf("throwResume yang caught <"); |
---|
74 | } |
---|
75 | |
---|
76 | try { |
---|
77 | num_error x = { 2 }; |
---|
78 | THROW(&x); |
---|
79 | } |
---|
80 | catch (num_error * error ; 3 == error->virtual_table->code( error ) ) { |
---|
81 | printf("exception at %p\n", error ); |
---|
82 | printf("Should not be printed.\n"); |
---|
83 | } |
---|
84 | catch (num_error * error ; 2 == error->virtual_table->code( error ) ) { |
---|
85 | printf("Should be printed.\n"); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | int main (int argc, char * argv[]) { |
---|
90 | throw_catch(); |
---|
91 | return 0; |
---|
92 | } |
---|