[307a732] | 1 | // Draft of tests for exception handling.
|
---|
| 2 |
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <stdbool.h>
|
---|
| 5 |
|
---|
| 6 | struct signal_exit {
|
---|
| 7 | const char * area;
|
---|
| 8 | };
|
---|
| 9 |
|
---|
| 10 | void ?{}(signal_exit * this, const char * area) {
|
---|
| 11 | this->area = area;
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | void ^?{}(signal_exit * this) {
|
---|
| 15 | printf("Exiting: %s\n", this->area);
|
---|
| 16 | // sout | "Exiting:" | this->area | endl;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | void terminate(int except_value) {
|
---|
| 20 | signal_exit a = {"terminate function"};
|
---|
| 21 | throw except_value;
|
---|
| 22 | printf("terminate returned\n");
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | void resume(int except_value) {
|
---|
| 26 | signal_exit a = {"resume function"};
|
---|
| 27 | throwResume except_value;
|
---|
| 28 | printf("resume returned\n");
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // Termination Test: Two handlers: no catch, catch
|
---|
| 32 | void bar() {
|
---|
| 33 | signal_exit a = {"bar function"};
|
---|
| 34 | try {
|
---|
| 35 | terminate(4);
|
---|
| 36 | } catch (3) {
|
---|
| 37 | printf("bar caught exception 3.\n");
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void foo() {
|
---|
| 42 | signal_exit a = {"foo function"};
|
---|
| 43 | try {
|
---|
| 44 | bar();
|
---|
| 45 | } catch (4) {
|
---|
| 46 | printf("foo caught exception 4.\n");
|
---|
| 47 | } catch (2) {
|
---|
| 48 | printf("foo caught exception 2.\n");
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | // Resumption Two Handler Test: no catch, catch.
|
---|
| 53 | void beta() {
|
---|
| 54 | signal_exit a = {"beta function"};
|
---|
| 55 | try {
|
---|
| 56 | resume(4);
|
---|
| 57 | } catchResume (3) {
|
---|
| 58 | printf("beta caught exception 3\n");
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void alpha() {
|
---|
| 63 | signal_exit a = {"alpha function"};
|
---|
| 64 | try {
|
---|
| 65 | beta();
|
---|
| 66 | } catchResume (2) {
|
---|
| 67 | printf("alpha caught exception 2\n");
|
---|
| 68 | } catchResume (4) {
|
---|
| 69 | printf("alpha caught exception 4\n");
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // Finally Test:
|
---|
| 74 | void farewell(bool jump) {
|
---|
| 75 | try {
|
---|
| 76 | if (jump) {
|
---|
| 77 | printf("jump out of farewell\n");
|
---|
| 78 | goto endoffunction;
|
---|
| 79 | } else {
|
---|
| 80 | printf("walk out of farewell\n");
|
---|
| 81 | }
|
---|
| 82 | } finally {
|
---|
| 83 | printf("See you next time\n");
|
---|
| 84 | }
|
---|
| 85 | endoffunction:
|
---|
| 86 | printf("leaving farewell\n");
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // Resume-to-Terminate Test:
|
---|
| 90 | void fallback() {
|
---|
| 91 | try {
|
---|
| 92 | resume(2);
|
---|
| 93 | } catch (2) {
|
---|
| 94 | printf("fallback caught termination 2\n");
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Terminate Throw New Exception:
|
---|
| 99 | void terminate_swap() {
|
---|
| 100 | signal_exit a = {"terminate_swap"};
|
---|
| 101 | try {
|
---|
| 102 | terminate(2);
|
---|
| 103 | } catch (2) {
|
---|
| 104 | terminate(3);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void terminate_swapped() {
|
---|
| 109 | signal_exit a = {"terminate_swapped"};
|
---|
| 110 | try {
|
---|
| 111 | terminate_swap();
|
---|
| 112 | } catch (3) {
|
---|
| 113 | printf("terminate_swapped caught exception 3\n");
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | // Resume Throw New Exception:
|
---|
| 118 | void resume_swap() {
|
---|
| 119 | signal_exit a = {"terminate_swap"};
|
---|
| 120 | try {
|
---|
| 121 | resume(2);
|
---|
| 122 | } catchResume (2) {
|
---|
| 123 | resume(3);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void resume_swapped() {
|
---|
| 128 | try {
|
---|
| 129 | resume_swap();
|
---|
| 130 | } catchResume (3) {
|
---|
| 131 | printf("resume_swapped caught exception 3\n");
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // Terminate Rethrow:
|
---|
| 136 | void reterminate() {
|
---|
| 137 | try {
|
---|
| 138 | try {
|
---|
| 139 | terminate(2);
|
---|
| 140 | } catch (2) {
|
---|
| 141 | printf("reterminate 2 caught and "
|
---|
| 142 | "will rethrow exception 2\n");
|
---|
| 143 | throw;
|
---|
| 144 | }
|
---|
| 145 | } catch (2) {
|
---|
| 146 | printf("reterminate 1 caught exception 2\n");
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | // Resume Rethrow:
|
---|
| 151 | void reresume() {
|
---|
| 152 | try {
|
---|
| 153 | try {
|
---|
| 154 | resume(2);
|
---|
| 155 | } catchResume (2) {
|
---|
| 156 | printf("reresume 2 caught and rethrows exception 2\n");
|
---|
| 157 | throwResume;
|
---|
| 158 | }
|
---|
| 159 | } catchResume (2) {
|
---|
| 160 | printf("reresume 1 caught exception 2\n");
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | // Terminate-Resume interaction:
|
---|
| 165 | void fum() {
|
---|
| 166 | // terminate block, call resume
|
---|
| 167 | try {
|
---|
| 168 | resume(3);
|
---|
| 169 | } catch (3) {
|
---|
| 170 | printf("fum caught exception 3\n");
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void foe() {
|
---|
| 175 | // resume block, call terminate
|
---|
| 176 | try {
|
---|
| 177 | terminate(3);
|
---|
| 178 | } catchResume (3) {
|
---|
| 179 | printf("foe caught exception 3\n");
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void fy() {
|
---|
| 184 | // terminate block calls fum, call foe
|
---|
| 185 | try {
|
---|
| 186 | foe();
|
---|
| 187 | } catch (3) {
|
---|
| 188 | printf("fy caught exception 3\n");
|
---|
| 189 | fum();
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | void fee() {
|
---|
| 194 | // resume block, call fy
|
---|
| 195 | try {
|
---|
| 196 | fy();
|
---|
| 197 | } catchResume (3) {
|
---|
| 198 | printf("fee caught exception 3\n");
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | // main: choose which tests to run
|
---|
| 204 | int main(int argc, char * argv[]) {
|
---|
| 205 | signal_exit a = {"main function"};
|
---|
| 206 |
|
---|
| 207 | foo(); printf("\n");
|
---|
| 208 | alpha(); printf("\n");
|
---|
| 209 | farewell(false); printf("\n");
|
---|
| 210 | farewell(true); printf("\n");
|
---|
| 211 | fallback(); printf("\n");
|
---|
| 212 | terminate_swapped(); printf("\n");
|
---|
| 213 | resume_swapped(); printf("\n");
|
---|
| 214 | reterminate(); printf("\n");
|
---|
| 215 | reresume(); printf("\n");
|
---|
| 216 | fee(); printf("\n");
|
---|
| 217 | // Uncaught termination test.
|
---|
| 218 | terminate(7);
|
---|
| 219 | }
|
---|