EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_error.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 #ifdef HAVE_EVIL
28 # include <Evil.h>
29 #endif
30
31 #include "eina_suite.h"
32 #include "Eina.h"
33
34 #define TEST_TEXT "The big test\n"
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
71 START_TEST(eina_error_errno)
72 {
73    int test;
74
75    setenv("EINA_ERROR_LEVEL", "1", 0);
76
77    eina_init();
78
79    test = eina_error_msg_register(TEST_TEXT);
80    fail_if(!eina_error_msg_get(test));
81    fail_if(strcmp(eina_error_msg_get(test), TEST_TEXT) != 0);
82
83    eina_error_set(test);
84    fail_if(eina_error_get() != test);
85
86    eina_shutdown();
87 }
88 END_TEST
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
233 void
234 eina_test_error(TCase *tc)
235 {
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
243 }