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