source: tests/except-2.cfa @ 35a2d47

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 35a2d47 was dc8511c, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first attempt at updating suffixes for test programs

  • Property mode set to 100644
File size: 2.0 KB
Line 
1// New draft of exception tests.
2
3
4#include <stdlib.hfa>
5#include "except-mac.hfa"
6
7TRIVIAL_EXCEPTION(yin)
8TRIVIAL_EXCEPTION(yang)
9
10struct num_error;
11struct 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};
19extern num_error_vtable INSTANCE(num_error);
20
21struct num_error {
22        struct num_error_vtable const * virtual_table;
23        char * msg;
24        int num;
25};
26
27void 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}
36void ?{}(num_error * this, int num) {
37        this->virtual_table = &INSTANCE(num_error);
38        this->msg = 0;
39        this->num = num;
40}
41void ?{}(num_error * this, num_error * other) {
42        this->virtual_table = other->virtual_table;
43        this->msg = 0;
44        this->num = other->num;
45}
46void ^?{}(num_error * this) {
47        if( this->msg ) free( this->msg );
48}
49int num_error_code( num_error * this ) {
50        return this->num;
51}
52num_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.
60void 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
89int main (int argc, char * argv[]) {
90        throw_catch();
91        return 0;
92}
Note: See TracBrowser for help on using the repository browser.