10 void log(void* ignored
)
12 //printf("Cleanup called on %s\n", *(char**)ignored);
15 __attribute__((cleanup(log))) __attribute__((unused))\
16 const char *f = __func__;
19 * Simple struct to test throwing.
33 * Non-pod type to test throwing
37 non_pod(int i
): x(i
) {}
42 static int cleanup_count
;
44 * Simple structure declared with a destructor. Destroying this object will
45 * increment cleanup count. The destructor should be called automatically if
46 * an instance of cl is allocated with automatic storage.
51 ~cl() { fprintf(stderr
, "cl destroyed: %d\n", i
); cleanup_count
++; }
54 * Test that one cl was destroyed when running the argument.
56 #define TEST_CLEANUP(x) do {\
57 int cleanups = cleanup_count;\
59 TEST(cleanup_count == cleanups+1, "Cleanup ran correctly");\
67 case 0: throw (int)1.0;
68 case 1: throw (float)1.0;
69 case 2: fprintf(stderr
, "Throwing int64_t\n");throw (int64_t)1;
70 case 3: { foo f
= {2} ; throw f
; }
71 case 4: { bar f
; f
.i
= 2 ; f
.bar
=1 ; throw f
; }
72 case 5: throw non_pod(3);
77 int outer(int i
) throw(float, int, foo
, non_pod
)
84 static void test_const(void)
93 TEST(a
== b
, "Caught int as const int");
97 TEST(0, "Failed to catch int as const int");
105 TEST(&a
== b
, "Caught int* as const int*");
109 TEST(0, "Failed to catch int* as const int*");
113 static void test_catch(int s
)
117 fprintf(stderr
, "Entering try\n");
124 fprintf(stderr
, "Caught int %d in test %d\n", i
, s
);
125 TEST((s
== 0 && i
== 1) || (s
== 2 && i
== 0), "Caught int");
130 fprintf(stderr
, "Caught float %f!\n", f
);
131 TEST(s
== 1 && f
== 1, "Caught float");
136 fprintf(stderr
, "Caught struct {%d}!\n", f
.i
);
137 TEST((s
== 3 || s
== 4) && f
.i
== 2, "Caught struct");
141 fprintf(stderr
, "Caught non_pod {%d}!\n", np
.x
);
142 TEST(s
== 5 && np
.x
== 3, "Caught non_pod");
146 TEST(0, "Unreachable line reached");
149 void test_nested1(void)
162 TEST(a
== 1, "Caught int");
167 TEST(f
== 1, "Caught float inside outer catch block");
181 fprintf(stderr
, "Caught re-thrown float\n");
182 TEST(f
== 1, "Caught re-thrown float");
186 static int violations
= 0;
187 static void throw_zero()
190 fprintf(stderr
, "Throwing 0\n");
194 extern "C" void __cxa_bad_cast();
196 void test_exceptions(void)
198 std::set_unexpected(throw_zero
);
199 TEST_CLEANUP(test_catch(0));
200 TEST_CLEANUP(test_catch(1));
201 TEST_CLEANUP(test_catch(3));
202 TEST_CLEANUP(test_catch(4));
203 TEST_CLEANUP(test_catch(5));
204 TEST_CLEANUP(test_nested());
207 TEST(violations
== 1, "Exactly one exception spec violation");
210 TEST(0, "Caught int64_t, but that violates an exception spec");
218 TEST(&a
==b
, "Caught const int from thrown int");
225 TEST(&a
==b
, "Caught int from thrown int");
231 catch (std::exception b
)
233 TEST(1, "Caught bad cast");
237 TEST(0, "Bad cast was not caught correctly");
242 //printf("Test: %s\n",