aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/tests/eina_test_ustr.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/tests/eina_test_ustr.c')
-rw-r--r--libraries/eina/src/tests/eina_test_ustr.c130
1 files changed, 120 insertions, 10 deletions
diff --git a/libraries/eina/src/tests/eina_test_ustr.c b/libraries/eina/src/tests/eina_test_ustr.c
index eaeba9d..75126cc 100644
--- a/libraries/eina/src/tests/eina_test_ustr.c
+++ b/libraries/eina/src/tests/eina_test_ustr.c
@@ -28,6 +28,43 @@
28#include "eina_suite.h" 28#include "eina_suite.h"
29#include "Eina.h" 29#include "Eina.h"
30 30
31#ifdef EINA_SAFETY_CHECKS
32struct log_ctx {
33 const char *msg;
34 const char *fnc;
35 Eina_Bool did;
36};
37
38/* tests should not output on success, just uncomment this for debugging */
39//#define SHOW_LOG 1
40
41static void
42_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__)
43{
44 struct log_ctx *ctx = data;
45 va_list cp_args;
46 const char *str;
47
48 va_copy(cp_args, args);
49 str = va_arg(cp_args, const char *);
50 va_end(cp_args);
51
52 ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR);
53 ck_assert_str_eq(fmt, "%s");
54 ck_assert_str_eq(ctx->msg, str);
55 ck_assert_str_eq(ctx->fnc, fnc);
56 ctx->did = EINA_TRUE;
57
58#ifdef SHOW_LOG
59 eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
60#else
61 (void)d;
62 (void)file;
63 (void)line;
64#endif
65}
66#endif
67
31static const Eina_Unicode STR1[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0}; 68static const Eina_Unicode STR1[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
32static const Eina_Unicode STR2[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'f', 'f', 0}; 69static const Eina_Unicode STR2[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'f', 'f', 0};
33static const Eina_Unicode STR3[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0}; 70static const Eina_Unicode STR3[] = {'P', 'a', 'n', 't', 's',' ', 'O', 'n', 0};
@@ -126,14 +163,42 @@ START_TEST(eina_unicode_strncpy_test)
126 rv = eina_unicode_strncpy(buf, STR1, 0); 163 rv = eina_unicode_strncpy(buf, STR1, 0);
127 fail_if(buf[0] != '7'); 164 fail_if(buf[0] != '7');
128 165
129 /* may segfault */ 166#ifdef EINA_SAFETY_CHECKS
130 buf[0] = '7'; 167 {
131 rv = eina_unicode_strncpy(buf, NULL, 0); 168 struct log_ctx ctx;
132 fail_if(buf[0] != '7'); 169
170#define TEST_MAGIC_SAFETY(fn, _msg) \
171 ctx.msg = _msg; \
172 ctx.fnc = fn; \
173 ctx.did = EINA_FALSE
174
175 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
133 176
134 /* Hopefully won't segfault */ 177 /* may segfault */
135 rv = eina_unicode_strncpy(NULL, STR1, 0); 178 buf[0] = '7';
136 fail_if(rv != NULL); 179#ifdef SHOW_LOG
180 fprintf(stderr, "you should have a safety check failure below:\n");
181#endif
182 TEST_MAGIC_SAFETY("eina_unicode_strncpy",
183 "safety check failed: source == NULL");
184 rv = eina_unicode_strncpy(buf, NULL, 0);
185 fail_if(buf[0] != '7');
186 fail_unless(ctx.did);
187
188 /* Hopefully won't segfault */
189#ifdef SHOW_LOG
190 fprintf(stderr, "you should have a safety check failure below:\n");
191#endif
192 TEST_MAGIC_SAFETY("eina_unicode_strncpy",
193 "safety check failed: dest == NULL");
194 rv = eina_unicode_strncpy(NULL, STR1, 0);
195 fail_if(rv != NULL);
196 fail_unless(ctx.did);
197
198 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
199#undef TEST_MAGIC_SAFETY
200 }
201#endif
137 202
138 eina_shutdown(); 203 eina_shutdown();
139} 204}
@@ -151,8 +216,30 @@ START_TEST(eina_ustr_strlen_test)
151 fail_if(eina_unicode_strlen(STR3) != 8); 216 fail_if(eina_unicode_strlen(STR3) != 8);
152 fail_if(eina_unicode_strlen(STR4) != 1); 217 fail_if(eina_unicode_strlen(STR4) != 1);
153 fail_if(eina_unicode_strlen(EMPTYSTR) != 0); 218 fail_if(eina_unicode_strlen(EMPTYSTR) != 0);
154 /* Eina unicode doesn't take NULL */ 219
155 // fail_if(eina_unicode_strlen(NULL)); 220#ifdef EINA_SAFETY_CHECKS
221 {
222 struct log_ctx ctx;
223
224#define TEST_MAGIC_SAFETY(fn, _msg) \
225 ctx.msg = _msg; \
226 ctx.fnc = fn; \
227 ctx.did = EINA_FALSE
228
229 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
230
231#ifdef SHOW_LOG
232 fprintf(stderr, "you should have a safety check failure below:\n");
233#endif
234 TEST_MAGIC_SAFETY("eina_unicode_strlen",
235 "safety check failed: ustr == NULL");
236 fail_if(eina_unicode_strlen(NULL));
237 fail_unless(ctx.did);
238
239 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
240#undef TEST_MAGIC_SAFETY
241 }
242#endif
156 243
157 eina_shutdown(); 244 eina_shutdown();
158} 245}
@@ -174,7 +261,30 @@ START_TEST(eina_unicode_strnlen_test)
174 fail_if(eina_unicode_strnlen(STR2,3) != 3); 261 fail_if(eina_unicode_strnlen(STR2,3) != 3);
175 fail_if(eina_unicode_strnlen(STR3,3) != 3); 262 fail_if(eina_unicode_strnlen(STR3,3) != 3);
176 fail_if(eina_unicode_strnlen(EMPTYSTR,1) != 0); 263 fail_if(eina_unicode_strnlen(EMPTYSTR,1) != 0);
177 fail_if(eina_unicode_strnlen(NULL,0) != 0); 264
265#ifdef EINA_SAFETY_CHECKS
266 {
267 struct log_ctx ctx;
268
269#define TEST_MAGIC_SAFETY(fn, _msg) \
270 ctx.msg = _msg; \
271 ctx.fnc = fn; \
272 ctx.did = EINA_FALSE
273
274 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
275
276#ifdef SHOW_LOG
277 fprintf(stderr, "you should have a safety check failure below:\n");
278#endif
279 TEST_MAGIC_SAFETY("eina_unicode_strnlen",
280 "safety check failed: ustr == NULL");
281 fail_if(eina_unicode_strnlen(NULL,0) != 0);
282 fail_unless(ctx.did);
283
284 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
285#undef TEST_MAGIC_SAFETY
286 }
287#endif
178 288
179 eina_shutdown(); 289 eina_shutdown();
180} 290}