diff options
Diffstat (limited to 'libraries/eina/src/tests/eina_test_error.c')
-rw-r--r-- | libraries/eina/src/tests/eina_test_error.c | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_error.c b/libraries/eina/src/tests/eina_test_error.c index 506f406..57f6950 100644 --- a/libraries/eina/src/tests/eina_test_error.c +++ b/libraries/eina/src/tests/eina_test_error.c | |||
@@ -33,6 +33,41 @@ | |||
33 | 33 | ||
34 | #define TEST_TEXT "The big test\n" | 34 | #define TEST_TEXT "The big test\n" |
35 | 35 | ||
36 | struct log_ctx { | ||
37 | const char *msg; | ||
38 | const char *fnc; | ||
39 | Eina_Bool did; | ||
40 | }; | ||
41 | |||
42 | /* tests should not output on success, just uncomment this for debugging */ | ||
43 | //#define SHOW_LOG 1 | ||
44 | |||
45 | static void | ||
46 | _eina_test_safety_print_cb(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args __UNUSED__) | ||
47 | { | ||
48 | struct log_ctx *ctx = data; | ||
49 | va_list cp_args; | ||
50 | const char *str; | ||
51 | |||
52 | va_copy(cp_args, args); | ||
53 | str = va_arg(cp_args, const char *); | ||
54 | va_end(cp_args); | ||
55 | |||
56 | ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR); | ||
57 | ck_assert_str_eq(fmt, "%s"); | ||
58 | ck_assert_str_eq(ctx->msg, str); | ||
59 | ck_assert_str_eq(ctx->fnc, fnc); | ||
60 | ctx->did = EINA_TRUE; | ||
61 | |||
62 | #ifdef SHOW_LOG | ||
63 | eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args); | ||
64 | #else | ||
65 | (void)d; | ||
66 | (void)file; | ||
67 | (void)line; | ||
68 | #endif | ||
69 | } | ||
70 | |||
36 | START_TEST(eina_error_errno) | 71 | START_TEST(eina_error_errno) |
37 | { | 72 | { |
38 | int test; | 73 | int test; |
@@ -52,8 +87,157 @@ START_TEST(eina_error_errno) | |||
52 | } | 87 | } |
53 | END_TEST | 88 | END_TEST |
54 | 89 | ||
90 | START_TEST(eina_error_test_find) | ||
91 | { | ||
92 | int test, r; | ||
93 | const char *str; | ||
94 | |||
95 | eina_init(); | ||
96 | |||
97 | test = eina_error_msg_register(TEST_TEXT); | ||
98 | ck_assert_int_ne(test, 0); | ||
99 | |||
100 | str = eina_error_msg_get(test); | ||
101 | fail_unless(str != NULL); | ||
102 | ck_assert_str_eq(str, TEST_TEXT); | ||
103 | |||
104 | eina_error_set(test); | ||
105 | fail_if(eina_error_get() != test); | ||
106 | |||
107 | r = eina_error_find(TEST_TEXT); | ||
108 | ck_assert_int_eq(r, test); | ||
109 | |||
110 | eina_shutdown(); | ||
111 | } | ||
112 | END_TEST | ||
113 | |||
114 | START_TEST(eina_error_test_modify) | ||
115 | { | ||
116 | int test, r; | ||
117 | const char *str, smsg[] = "Do not copy this string"; | ||
118 | |||
119 | eina_init(); | ||
120 | |||
121 | test = eina_error_msg_register("Some Test Error"); | ||
122 | ck_assert_int_ne(test, 0); | ||
123 | |||
124 | str = eina_error_msg_get(test); | ||
125 | fail_unless(str != NULL); | ||
126 | ck_assert_str_eq(str, "Some Test Error"); | ||
127 | |||
128 | eina_error_set(test); | ||
129 | fail_if(eina_error_get() != test); | ||
130 | |||
131 | fail_unless(eina_error_msg_modify(test, "ABCDE")); | ||
132 | |||
133 | r = eina_error_find("ABCDE"); | ||
134 | ck_assert_int_eq(r, test); | ||
135 | |||
136 | test = eina_error_msg_static_register(smsg); | ||
137 | ck_assert_int_ne(test, 0); | ||
138 | |||
139 | str = eina_error_msg_get(test); | ||
140 | fail_unless(str != NULL); | ||
141 | fail_unless(str == smsg); | ||
142 | |||
143 | fail_unless(eina_error_msg_modify(test, "Change that!")); | ||
144 | r = eina_error_find("Change that!"); | ||
145 | ck_assert_int_eq(r, test); | ||
146 | |||
147 | eina_shutdown(); | ||
148 | } | ||
149 | END_TEST | ||
150 | |||
151 | START_TEST(eina_error_test_lots) | ||
152 | { | ||
153 | char buf[64]; | ||
154 | int codes[512]; | ||
155 | unsigned int i; | ||
156 | |||
157 | eina_init(); | ||
158 | |||
159 | for (i = 0; i < sizeof(codes)/sizeof(codes[0]); i++) | ||
160 | { | ||
161 | snprintf(buf, sizeof(buf), "myerr-%d", i); | ||
162 | codes[i] = eina_error_msg_register(buf); | ||
163 | ck_assert_int_ne(codes[i], 0); | ||
164 | } | ||
165 | |||
166 | for (i = 0; i < sizeof(codes)/sizeof(codes[0]); i++) | ||
167 | { | ||
168 | int found; | ||
169 | |||
170 | snprintf(buf, sizeof(buf), "myerr-%d", i); | ||
171 | |||
172 | found = eina_error_find(buf); | ||
173 | ck_assert_int_eq(codes[i], found); | ||
174 | } | ||
175 | |||
176 | eina_shutdown(); | ||
177 | } | ||
178 | END_TEST | ||
179 | |||
180 | #ifdef EINA_SAFETY_CHECKS | ||
181 | START_TEST(eina_error_test_failures) | ||
182 | { | ||
183 | struct log_ctx ctx; | ||
184 | |||
185 | eina_init(); | ||
186 | |||
187 | eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx); | ||
188 | |||
189 | #define TEST_MAGIC_SAFETY(fn, _msg) \ | ||
190 | ctx.msg = _msg; \ | ||
191 | ctx.fnc = fn; \ | ||
192 | ctx.did = EINA_FALSE | ||
193 | |||
194 | TEST_MAGIC_SAFETY("eina_error_msg_register", | ||
195 | "safety check failed: msg == NULL"); | ||
196 | ck_assert_int_eq(eina_error_msg_register(NULL), 0); | ||
197 | fail_unless(ctx.did); | ||
198 | |||
199 | TEST_MAGIC_SAFETY("eina_error_msg_static_register", | ||
200 | "safety check failed: msg == NULL"); | ||
201 | ck_assert_int_eq(eina_error_msg_static_register(NULL), 0); | ||
202 | fail_unless(ctx.did); | ||
203 | |||
204 | ck_assert_int_eq(eina_error_msg_modify(0, "X"), EINA_FALSE); | ||
205 | ck_assert_int_eq(eina_error_msg_modify(4096, "X"), EINA_FALSE); | ||
206 | |||
207 | TEST_MAGIC_SAFETY("eina_error_msg_modify", | ||
208 | "safety check failed: msg == NULL"); | ||
209 | ck_assert_int_eq(eina_error_msg_modify(EINA_ERROR_OUT_OF_MEMORY, NULL), | ||
210 | EINA_FALSE); | ||
211 | fail_unless(ctx.did); | ||
212 | |||
213 | ck_assert_str_eq(eina_error_msg_get(EINA_ERROR_OUT_OF_MEMORY), | ||
214 | "Out of memory"); | ||
215 | |||
216 | TEST_MAGIC_SAFETY("eina_error_find", | ||
217 | "safety check failed: msg == NULL"); | ||
218 | ck_assert_int_eq(eina_error_find(NULL), 0); | ||
219 | fail_unless(ctx.did); | ||
220 | |||
221 | ck_assert_int_eq(eina_error_find("Non-existent Error..."), 0); | ||
222 | |||
223 | fail_if(eina_error_msg_get(0)); | ||
224 | fail_if(eina_error_msg_get(4096)); | ||
225 | |||
226 | eina_log_print_cb_set(eina_log_print_cb_stderr, NULL); | ||
227 | |||
228 | eina_shutdown(); | ||
229 | } | ||
230 | END_TEST | ||
231 | #endif | ||
232 | |||
55 | void | 233 | void |
56 | eina_test_error(TCase *tc) | 234 | eina_test_error(TCase *tc) |
57 | { | 235 | { |
58 | tcase_add_test(tc, eina_error_errno); | 236 | tcase_add_test(tc, eina_error_errno); |
237 | tcase_add_test(tc, eina_error_test_find); | ||
238 | tcase_add_test(tc, eina_error_test_modify); | ||
239 | tcase_add_test(tc, eina_error_test_lots); | ||
240 | #ifdef EINA_SAFETY_CHECKS | ||
241 | tcase_add_test(tc, eina_error_test_failures); | ||
242 | #endif | ||
59 | } | 243 | } |