EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_magic.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
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.
8  *
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.
13  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #define EINA_MAGIC_DEBUG
28
29 #include "eina_suite.h"
30 #include "Eina.h"
31 #include "eina_safety_checks.h"
32
33 #define EINA_MAGIC_TEST 0x7781fee7
34 #define EINA_MAGIC_TEST2 0x42241664
35 #define EINA_MAGIC_STRING "Eina Magic Test"
36
37 typedef struct _Eina_Magic_Struct Eina_Magic_Struct;
38 struct _Eina_Magic_Struct
39 {
40    EINA_MAGIC
41 };
42
43 struct log_ctx {
44    const char *msg;
45    const char *fnc;
46    Eina_Bool did;
47 };
48
49 /* tests should not output on success, just uncomment this for debugging */
50 //#define SHOW_LOG 1
51
52 static void
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__)
54 {
55    struct log_ctx *ctx = data;
56    va_list cp_args;
57    const char *str;
58
59    va_copy(cp_args, args);
60    str = va_arg(cp_args, const char *);
61    va_end(cp_args);
62
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);
67    ctx->did = EINA_TRUE;
68
69 #ifdef SHOW_LOG
70    eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
71 #else
72    (void)d;
73    (void)file;
74    (void)line;
75 #endif
76 }
77
78 static void
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__)
80 {
81    struct log_ctx *ctx = data;
82
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);
86    ctx->did = EINA_TRUE;
87
88 #ifdef SHOW_LOG
89    eina_log_print_cb_stderr(d, level, file, fnc, line, fmt, NULL, args);
90 #else
91    (void)d;
92    (void)file;
93    (void)line;
94 #endif
95 }
96
97 #ifdef EINA_SAFETY_CHECKS
98 #define TEST_MAGIC_SAFETY(fn, _msg)              \
99   ctx.msg = _msg;                                \
100   ctx.fnc = fn;                                  \
101   ctx.did = EINA_FALSE
102 #endif
103
104 START_TEST(eina_magic_simple)
105 {
106    Eina_Magic_Struct *ems = NULL;
107    struct log_ctx ctx;
108
109    eina_init();
110
111    eina_log_print_cb_set(_eina_test_safety_print_cb, &ctx);
112
113    eina_magic_string_set(EINA_MAGIC_TEST, EINA_MAGIC_STRING);
114
115 #ifdef EINA_SAFETY_CHECKS
116 #ifdef SHOW_LOG
117    fprintf(stderr, "you should have a safety check failure below:\n");
118 #endif
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);
124
125 #ifdef SHOW_LOG
126    fprintf(stderr, "you should have a safety check failure below:\n");
127 #endif
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);
133 #endif
134
135    eina_magic_string_set(EINA_MAGIC_TEST2, EINA_MAGIC_STRING);
136
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);
140
141    eina_log_print_cb_set(_eina_test_magic_print_cb, &ctx);
142
143 #ifdef EINA_MAGIC_DEBUG
144    fail_if(EINA_MAGIC_CHECK(ems, EINA_MAGIC_TEST));
145 #ifdef SHOW_LOG
146    fprintf(stderr, "you should see 'Input handle pointer is NULL' below\n");
147 #endif
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"
154                      "\n");
155    EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
156    fail_unless(ctx.did);
157
158    ems = malloc(sizeof (Eina_Magic_Struct));
159    fail_if(!ems);
160    EINA_MAGIC_SET(ems, EINA_MAGIC_TEST);
161
162    fail_if(!EINA_MAGIC_CHECK(ems, EINA_MAGIC_TEST));
163
164    EINA_MAGIC_SET(ems, EINA_MAGIC_NONE);
165 #ifdef SHOW_LOG
166    fprintf(stderr,
167            "you should see 'Input handle has already been freed' below\n");
168 #endif
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"
175                      "\n");
176    EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
177    fail_unless(ctx.did);
178
179    EINA_MAGIC_SET(ems, 42424242);
180 #ifdef SHOW_LOG
181    fprintf(stderr, "you should see 'Input handle is wrong type' below\n");
182 #endif
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"
191                      "\n");
192    EINA_MAGIC_FAIL(ems, EINA_MAGIC_TEST);
193    fail_unless(ctx.did);
194 #endif
195
196    eina_log_print_cb_set(eina_log_print_cb_stderr, NULL);
197
198    eina_shutdown();
199 }
200 END_TEST
201
202 void eina_test_magic(TCase *tc)
203 {
204    tcase_add_test(tc, eina_magic_simple);
205 }
206