1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
27 #define EINA_MAGIC_DEBUG
29 #include "eina_suite.h"
31 #include "eina_safety_checks.h"
33 #define EINA_MAGIC_TEST 0x7781fee7
34 #define EINA_MAGIC_TEST2 0x42241664
35 #define EINA_MAGIC_STRING "Eina Magic Test"
37 typedef struct _Eina_Magic_Struct Eina_Magic_Struct;
38 struct _Eina_Magic_Struct
49 /* tests should not output on success, just uncomment this for debugging */
53 _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__)
55 struct log_ctx *ctx = data;
59 va_copy(cp_args, args);
60 str = va_arg(cp_args, const char *);
63 ck_assert_int_eq(level, EINA_LOG_LEVEL_ERR);
64 ck_assert_str_eq(fmt, "%s");
65 ck_assert_str_eq(ctx->msg, str);
66 ck_assert_str_eq(ctx->fnc, fnc);
70 eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
79 _eina_test_magic_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__)
81 struct log_ctx *ctx = data;
83 ck_assert_int_eq(level, EINA_LOG_LEVEL_CRITICAL);
84 ck_assert_str_eq(ctx->msg, fmt);
85 ck_assert_str_eq(ctx->fnc, fnc);
89 eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
97 #ifdef EINA_SAFETY_CHECKS
98 #define TEST_MAGIC_SAFETY(fn, _msg) \
104 START_TEST(eina_magic_simple)
106 Eina_Magic_Struct *ems = NULL;
111 eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
113 eina_magic_string_set(EINA_MAGIC_TEST, EINA_MAGIC_STRING);
115 #ifdef EINA_SAFETY_CHECKS
117 fprintf(stderr, "you should have a safety check failure below:\n");
119 TEST_MAGIC_SAFETY("eina_magic_string_set",
120 "safety check failed: magic_name == NULL");
121 eina_magic_string_set(EINA_MAGIC_TEST2, NULL);
122 fail_if(eina_error_get() != EINA_ERROR_SAFETY_FAILED);
123 fail_unless(ctx.did);
126 fprintf(stderr, "you should have a safety check failure below:\n");
128 TEST_MAGIC_SAFETY("eina_magic_string_set",
129 "safety check failed: magic_name == NULL");
130 eina_magic_string_set(EINA_MAGIC_TEST2, NULL);
131 fail_unless(ctx.did);
132 fail_if(eina_error_get() != EINA_ERROR_SAFETY_FAILED);
135 eina_magic_string_set(EINA_MAGIC_TEST2, EINA_MAGIC_STRING);
137 fail_if(eina_magic_string_get(EINA_MAGIC_TEST) == NULL);
138 fail_if(strcmp(eina_magic_string_get(
139 EINA_MAGIC_TEST), EINA_MAGIC_STRING) != 0);
141 eina_log_print_cb_set(_eina_test_magic_print_cb, &ctx);
143 #ifdef EINA_MAGIC_DEBUG
144 fail_if(EINA_MAGIC_CHECK(ems, EINA_MAGIC_TEST));
146 fprintf(stderr, "you should see 'Input handle pointer is NULL' below\n");
148 TEST_MAGIC_SAFETY(__FUNCTION__,
149 "*** Eina Magic Check Failed !!!\n"
150 " Input handle pointer is NULL !\n"
151 "*** NAUGHTY PROGRAMMER!!!\n"
152 "*** SPANK SPANK SPANK!!!\n"
153 "*** Now go fix your code. Tut tut tut!\n"
155 EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
156 fail_unless(ctx.did);
158 ems = malloc(sizeof (Eina_Magic_Struct));
160 EINA_MAGIC_SET(ems, EINA_MAGIC_TEST);
162 fail_if(!EINA_MAGIC_CHECK(ems, EINA_MAGIC_TEST));
164 EINA_MAGIC_SET(ems, EINA_MAGIC_NONE);
167 "you should see 'Input handle has already been freed' below\n");
169 TEST_MAGIC_SAFETY(__FUNCTION__,
170 "*** Eina Magic Check Failed !!!\n"
171 " Input handle has already been freed!\n"
172 "*** NAUGHTY PROGRAMMER!!!\n"
173 "*** SPANK SPANK SPANK!!!\n"
174 "*** Now go fix your code. Tut tut tut!\n"
176 EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
177 fail_unless(ctx.did);
179 EINA_MAGIC_SET(ems, 42424242);
181 fprintf(stderr, "you should see 'Input handle is wrong type' below\n");
183 TEST_MAGIC_SAFETY(__FUNCTION__,
184 "*** Eina Magic Check Failed !!!\n"
185 " Input handle is wrong type\n"
186 " Expected: %08x - %s\n"
187 " Supplied: %08x - %s\n"
188 "*** NAUGHTY PROGRAMMER!!!\n"
189 "*** SPANK SPANK SPANK!!!\n"
190 "*** Now go fix your code. Tut tut tut!\n"
192 EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
193 fail_unless(ctx.did);
196 eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
202 void eina_test_magic(TCase *tc)
204 tcase_add_test(tc, eina_magic_simple);