source: tests/exceptions/conditional.cfa @ 6d43cdde

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-expr
Last change on this file since 6d43cdde was 6d43cdde, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Rework of exception tests. Includes a new disabled failing test case.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1// Conditional Catch Test
2
3// I may fold this back into terminate.cfa and resume.cfa once setting
4// up the non-trivial exception is reasonable to do.
5
6#include "except-mac.hfa"
7#include <stdio.h>
8
9DECLARE_EXCEPT(num_error, BASE_EXCEPT,
10        int (*code)(num_error *this);
11);
12
13struct num_error {
14        VTABLE_FIELD(num_error);
15    char * msg;
16    int num;
17};
18
19void num_error_msg(num_error * this) {
20    if ( ! this->msg ) {
21        static const char * base = "Num Error with code: X";
22        this->msg = (char *)malloc(22);
23        for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
24    }
25    this->msg[21] = '0' + this->num;
26    return this->msg;
27}
28void ?{}(num_error & this, int num) {
29        VTABLE_INIT(this, num_error);
30    this.msg = 0;
31    this.num = num;
32}
33void ?{}(num_error & this, num_error & other) {
34    this.virtual_table = other.virtual_table;
35    this.msg = 0;
36    this.num = other.num;
37}
38void copy(num_error * this, num_error * other) {
39        *this = *other;
40}
41void ^?{}(num_error & this) {
42    if( this.msg ) free( this.msg );
43}
44int num_error_code( num_error * this ) {
45    return this->num;
46}
47
48VTABLE_INSTANCE(num_error, BASE_EXCEPT, copy, ^?{},
49        num_error_msg, num_error_code
50);
51
52void caught_num_error(int expect, num_error * actual) {
53        printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num);
54}
55
56int main(int argc, char * argv[]) {
57        num_error exc = 2;
58
59        try {
60                THROW(&exc);
61        } catch (num_error * error ; 3 == error->virtual_table->code( error )) {
62                caught_num_error(3, error);
63        } catch (num_error * error ; 2 == error->virtual_table->code( error )) {
64                caught_num_error(2, error);
65        }
66
67        try {
68                THROW_RESUME(&exc);
69        } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) {
70                caught_num_error(3, error);
71        } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) {
72                caught_num_error(2, error);
73        }
74}
Note: See TracBrowser for help on using the repository browser.