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