diff options
Diffstat (limited to 'libraries/eina/src/tests/eina_test_binshare.c')
-rw-r--r-- | libraries/eina/src/tests/eina_test_binshare.c | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/libraries/eina/src/tests/eina_test_binshare.c b/libraries/eina/src/tests/eina_test_binshare.c new file mode 100644 index 0000000..b2e77ae --- /dev/null +++ b/libraries/eina/src/tests/eina_test_binshare.c | |||
@@ -0,0 +1,199 @@ | |||
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 | #include <time.h> | ||
27 | |||
28 | #include "eina_suite.h" | ||
29 | #include "Eina.h" | ||
30 | |||
31 | #define TEST0 "te\0st/0" | ||
32 | #define TEST0_SIZE 7 | ||
33 | #define TEST1 "te\0st/11" | ||
34 | #define TEST1_SIZE 8 | ||
35 | |||
36 | START_TEST(eina_binshare_simple) | ||
37 | { | ||
38 | const char *t0; | ||
39 | const char *t1; | ||
40 | |||
41 | eina_init(); | ||
42 | |||
43 | t0 = eina_binshare_add_length(TEST0, TEST0_SIZE); | ||
44 | t1 = eina_binshare_add_length(TEST1, TEST1_SIZE); | ||
45 | |||
46 | fail_if(t0 == NULL); | ||
47 | fail_if(t1 == NULL); | ||
48 | fail_if(memcmp(t0, TEST0, TEST0_SIZE) != 0); | ||
49 | fail_if(memcmp(t1, TEST1, TEST1_SIZE) != 0); | ||
50 | |||
51 | t0 = eina_binshare_ref(t0); | ||
52 | fail_if(t0 == NULL); | ||
53 | fail_if(memcmp(t0, TEST0, TEST0_SIZE) != 0); | ||
54 | |||
55 | eina_binshare_del(t0); | ||
56 | eina_binshare_del(t0); | ||
57 | eina_binshare_del(t1); | ||
58 | |||
59 | eina_shutdown(); | ||
60 | } | ||
61 | END_TEST | ||
62 | |||
63 | START_TEST(eina_binshare_small) | ||
64 | { | ||
65 | char buf[4]; | ||
66 | int i; | ||
67 | |||
68 | eina_init(); | ||
69 | |||
70 | for (i = 1; i < 3; i++) | ||
71 | { | ||
72 | const char *t0, *t1; | ||
73 | int j; | ||
74 | |||
75 | for (j = 0; j < i; j++) | ||
76 | { | ||
77 | char c; | ||
78 | for (c = 'a'; c <= 'z'; c++) | ||
79 | buf[j] = c; | ||
80 | } | ||
81 | buf[i] = '\0'; | ||
82 | t0 = eina_binshare_add_length(buf, i); | ||
83 | t1 = eina_binshare_add_length(buf, i); | ||
84 | |||
85 | fail_if(t0 == NULL); | ||
86 | fail_if(t1 == NULL); | ||
87 | fail_if(t0 != t1); | ||
88 | fail_if(memcmp(t0, buf, i) != 0); | ||
89 | |||
90 | eina_binshare_del(t0); | ||
91 | eina_binshare_del(t1); | ||
92 | } | ||
93 | eina_shutdown(); | ||
94 | } | ||
95 | END_TEST | ||
96 | |||
97 | |||
98 | START_TEST(eina_binshare_test_share) | ||
99 | { | ||
100 | const char *t0; | ||
101 | const char *t1; | ||
102 | |||
103 | eina_init(); | ||
104 | |||
105 | t0 = eina_binshare_add_length(TEST0, TEST0_SIZE); | ||
106 | t1 = eina_binshare_add_length(TEST0, TEST0_SIZE); | ||
107 | |||
108 | fail_if(t0 == NULL); | ||
109 | fail_if(t1 == NULL); | ||
110 | fail_if(memcmp(t0, TEST0, TEST0_SIZE) != 0); | ||
111 | fail_if(memcmp(t1, TEST0, TEST0_SIZE) != 0); | ||
112 | fail_if(t0 != t1); | ||
113 | fail_if(TEST0_SIZE != eina_binshare_length(t0)); | ||
114 | |||
115 | eina_binshare_del(t0); | ||
116 | eina_binshare_del(t1); | ||
117 | eina_shutdown(); | ||
118 | } | ||
119 | END_TEST | ||
120 | |||
121 | START_TEST(eina_binshare_putstuff) | ||
122 | { | ||
123 | const char *tmp; | ||
124 | int i; | ||
125 | |||
126 | eina_init(); | ||
127 | |||
128 | for (i = 10000; i > 0; --i) | ||
129 | { | ||
130 | char build[64] = "string_"; | ||
131 | |||
132 | eina_convert_xtoa(i, build + 7); | ||
133 | tmp = eina_binshare_add_length(build, strlen(build)); | ||
134 | fail_if(tmp != eina_binshare_add_length(build, strlen(build))); | ||
135 | fail_if((int)strlen(build) != eina_binshare_length(tmp)); | ||
136 | } | ||
137 | eina_shutdown(); | ||
138 | } | ||
139 | END_TEST | ||
140 | |||
141 | START_TEST(eina_binshare_collision) | ||
142 | { | ||
143 | Eina_Array *ea; | ||
144 | char buffer[50]; | ||
145 | int i; | ||
146 | |||
147 | srand(time(NULL)); | ||
148 | |||
149 | eina_init(); | ||
150 | |||
151 | ea = eina_array_new(256); | ||
152 | fail_if(!ea); | ||
153 | |||
154 | for (i = 0; i < 10000; ++i) | ||
155 | { | ||
156 | eina_convert_itoa(rand(), buffer); | ||
157 | eina_array_push(ea, | ||
158 | (void *)eina_binshare_add_length(buffer, strlen(buffer))); | ||
159 | if (rand() > RAND_MAX / 2) | ||
160 | { | ||
161 | const char *r = eina_binshare_add_length(buffer, strlen(buffer)); | ||
162 | fail_if(r == NULL); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | for (i = 0; i < 10000; ++i) | ||
167 | { | ||
168 | const char *r; | ||
169 | |||
170 | eina_convert_itoa(60000 - i, buffer); | ||
171 | eina_array_push(ea, | ||
172 | (void *)eina_binshare_add_length(buffer, strlen(buffer))); | ||
173 | r = eina_binshare_add_length(buffer, strlen(buffer)); | ||
174 | fail_if(r == NULL); | ||
175 | r = eina_binshare_add_length(buffer, strlen(buffer)); | ||
176 | fail_if(r == NULL); | ||
177 | } | ||
178 | |||
179 | for (i = 0; i < 200; ++i) | ||
180 | eina_binshare_del(eina_array_data_get(ea, i)); | ||
181 | |||
182 | for (i = 0; i < 1000; ++i) | ||
183 | eina_binshare_del(eina_array_pop(ea)); | ||
184 | |||
185 | eina_shutdown(); | ||
186 | |||
187 | eina_array_free(ea); | ||
188 | } | ||
189 | END_TEST | ||
190 | |||
191 | void | ||
192 | eina_test_binshare(TCase *tc) | ||
193 | { | ||
194 | tcase_add_test(tc, eina_binshare_simple); | ||
195 | tcase_add_test(tc, eina_binshare_small); | ||
196 | tcase_add_test(tc, eina_binshare_test_share); | ||
197 | tcase_add_test(tc, eina_binshare_collision); | ||
198 | tcase_add_test(tc, eina_binshare_putstuff); | ||
199 | } | ||