source: tests/exceptions/conditional.cfa @ 481cf3a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 481cf3a was afe2939, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Removed #include <stdio.h> from tests that only use printf.

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[6d43cdde]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
[e68d092]6#include <exception.hfa>
[6d43cdde]7
[e68d092]8VTABLE_DECLARATION(num_error)(
[6d43cdde]9        int (*code)(num_error *this);
10);
11
12struct num_error {
13        VTABLE_FIELD(num_error);
14    char * msg;
15    int num;
16};
17
[b81fd95]18const char * num_error_msg(num_error * this) {
[6d43cdde]19    if ( ! this->msg ) {
20        static const char * base = "Num Error with code: X";
21        this->msg = (char *)malloc(22);
22        for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
23    }
24    this->msg[21] = '0' + this->num;
25    return this->msg;
26}
27void ?{}(num_error & this, int num) {
28        VTABLE_INIT(this, num_error);
29    this.msg = 0;
30    this.num = num;
31}
32void ?{}(num_error & this, num_error & other) {
33    this.virtual_table = other.virtual_table;
34    this.msg = 0;
35    this.num = other.num;
36}
37void ^?{}(num_error & this) {
38    if( this.msg ) free( this.msg );
39}
40int num_error_code( num_error * this ) {
41    return this->num;
42}
43
[e68d092]44VTABLE_INSTANCE(num_error)(
45        num_error_msg,
46        num_error_code,
[6d43cdde]47);
48
49void caught_num_error(int expect, num_error * actual) {
50        printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num);
51}
52
53int main(int argc, char * argv[]) {
54        num_error exc = 2;
55
56        try {
[046a890]57                throw exc;
[6d43cdde]58        } catch (num_error * error ; 3 == error->virtual_table->code( error )) {
59                caught_num_error(3, error);
60        } catch (num_error * error ; 2 == error->virtual_table->code( error )) {
61                caught_num_error(2, error);
62        }
63
64        try {
[046a890]65                throwResume exc;
[6d43cdde]66        } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) {
67                caught_num_error(3, error);
68        } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) {
69                caught_num_error(2, error);
70        }
71}
Note: See TracBrowser for help on using the repository browser.