[0ec9229] | 1 | // New draft of exception tests.
|
---|
| 2 |
|
---|
| 3 |
|
---|
[73abe95] | 4 | #include <stdlib.hfa>
|
---|
[dc8511c] | 5 | #include "except-mac.hfa"
|
---|
[0ec9229] | 6 |
|
---|
| 7 | TRIVIAL_EXCEPTION(yin)
|
---|
| 8 | TRIVIAL_EXCEPTION(yang)
|
---|
| 9 |
|
---|
| 10 | struct num_error;
|
---|
| 11 | struct num_error_vtable {
|
---|
[406a6e6] | 12 | struct TABLE(BASE_EXCEPT) const * parent;
|
---|
[0ec9229] | 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);
|
---|
[e9145a3] | 20 |
|
---|
[0ec9229] | 21 | struct num_error {
|
---|
| 22 | struct num_error_vtable const * virtual_table;
|
---|
| 23 | char * msg;
|
---|
| 24 | int num;
|
---|
| 25 | };
|
---|
[e9145a3] | 26 |
|
---|
[0ec9229] | 27 | void num_error_msg(num_error * this) {
|
---|
| 28 | if ( ! this->msg ) {
|
---|
[e9145a3] | 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);
|
---|
[0ec9229] | 32 | }
|
---|
| 33 | this->msg[21] = '0' + this->num;
|
---|
| 34 | return this->msg;
|
---|
| 35 | }
|
---|
| 36 | void ?{}(num_error * this, int num) {
|
---|
[406a6e6] | 37 | this->virtual_table = &INSTANCE(num_error);
|
---|
[0ec9229] | 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 @= {
|
---|
[fcc88a4] | 53 | &INSTANCE(BASE_EXCEPT),
|
---|
[0ec9229] | 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;
|
---|
[e9145a3] | 63 | THROW(&black);
|
---|
[fcc88a4] | 64 | } catch ( yin * error ) {
|
---|
[0ec9229] | 65 | printf("throw yin caught.\n");
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | try {
|
---|
| 69 | yang white;
|
---|
[e9145a3] | 70 | THROW_RESUME(&white);
|
---|
[0ec9229] | 71 | printf("> throwResume returned.\n");
|
---|
[fcc88a4] | 72 | } catchResume ( yang * error ) {
|
---|
[0ec9229] | 73 | printf("throwResume yang caught <");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | try {
|
---|
| 77 | num_error x = { 2 };
|
---|
[e9145a3] | 78 | THROW(&x);
|
---|
[0ec9229] | 79 | }
|
---|
[fcc88a4] | 80 | catch (num_error * error ; 3 == error->virtual_table->code( error ) ) {
|
---|
| 81 | printf("exception at %p\n", error );
|
---|
[0ec9229] | 82 | printf("Should not be printed.\n");
|
---|
| 83 | }
|
---|
[fcc88a4] | 84 | catch (num_error * error ; 2 == error->virtual_table->code( error ) ) {
|
---|
[0ec9229] | 85 | printf("Should be printed.\n");
|
---|
[fcc88a4] | 86 | }
|
---|
[0ec9229] | 87 | }
|
---|
| 88 |
|
---|
| 89 | int main (int argc, char * argv[]) {
|
---|
| 90 | throw_catch();
|
---|
| 91 | return 0;
|
---|
| 92 | }
|
---|