summaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2016-06-10 13:48:38 -0700
committerCedric BAIL <cedric@osg.samsung.com>2016-06-10 14:09:41 -0700
commitedc753d1a2e84a499a2c7e746f75776f7c0002e0 (patch)
tree5f6b40f7db932f7fd043fd311f1d72a7e33305b0 /src/tests
parent692b2c9fc9b347fc0be80b674d4580648e2f6541 (diff)
eina: add tests suite for Eina_Safepointer.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/eina/eina_suite.c1
-rw-r--r--src/tests/eina/eina_suite.h1
-rw-r--r--src/tests/eina/eina_test_safepointer.c188
3 files changed, 190 insertions, 0 deletions
diff --git a/src/tests/eina/eina_suite.c b/src/tests/eina/eina_suite.c
index 3af23dac6a..e4b6fbdff9 100644
--- a/src/tests/eina/eina_suite.c
+++ b/src/tests/eina/eina_suite.c
@@ -79,6 +79,7 @@ static const Efl_Test_Case etc[] = {
79/* { "Vector", eina_test_vector }, */ 79/* { "Vector", eina_test_vector }, */
80 { "Promise", eina_test_promise }, 80 { "Promise", eina_test_promise },
81 /* { "Bezier", eina_test_bezier }, */ 81 /* { "Bezier", eina_test_bezier }, */
82 { "SafePointer", eina_test_safepointer },
82 { NULL, NULL } 83 { NULL, NULL }
83}; 84};
84 85
diff --git a/src/tests/eina/eina_suite.h b/src/tests/eina/eina_suite.h
index 5e0a9e89a6..b11c0ad705 100644
--- a/src/tests/eina/eina_suite.h
+++ b/src/tests/eina/eina_suite.h
@@ -71,5 +71,6 @@ void eina_test_quaternion(TCase *tc);
71void eina_test_vector(TCase *tc); 71void eina_test_vector(TCase *tc);
72void eina_test_promise(TCase *tc); 72void eina_test_promise(TCase *tc);
73void eina_test_bezier(TCase *tc); 73void eina_test_bezier(TCase *tc);
74void eina_test_safepointer(TCase *tc);
74 75
75#endif /* EINA_SUITE_H_ */ 76#endif /* EINA_SUITE_H_ */
diff --git a/src/tests/eina/eina_test_safepointer.c b/src/tests/eina/eina_test_safepointer.c
new file mode 100644
index 0000000000..543f77e1ec
--- /dev/null
+++ b/src/tests/eina/eina_test_safepointer.c
@@ -0,0 +1,188 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2016 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 <stdio.h>
24
25#include <Eina.h>
26
27#include "eina_suite.h"
28
29static int test_array[1024] = { 0, 1, 2, 3, 4, 5, 6, 7 };
30static int test_array2[1024] = { 8, 9, 10, 11, 12, 13, 14 };
31static const void *pointers[2048] = { NULL };
32
33START_TEST(eina_test_reusable)
34{
35 unsigned int i;
36
37 eina_init();
38
39 for (i = 0; i < sizeof (test_array) / sizeof (test_array[0]); i++)
40 {
41 pointers[i] = eina_safepointer_register(&test_array[i]);
42 fail_if(pointers[i] == NULL);
43 fail_if(pointers[i] == &test_array[i]);
44 fail_if(&test_array[i] != eina_safepointer_get(pointers[i]));
45 }
46
47 for (i = 0; i < sizeof (test_array) / sizeof (test_array[0]); i++)
48 {
49 eina_safepointer_unregister(pointers[i]);
50 }
51
52 for (i = 0; i < sizeof (test_array2) / sizeof (test_array2[0]); i++)
53 {
54 pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] = eina_safepointer_register(&test_array2[i]);
55 fail_if(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] == NULL);
56 fail_if(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] == &test_array2[i]);
57 fail_if(&test_array2[i] != eina_safepointer_get(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))]));
58 eina_safepointer_unregister(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))]);
59 }
60
61 for (i = 0; i < sizeof (pointers) / sizeof (pointers[0]); i++)
62 {
63 unsigned int j;
64
65 for (j = i + 1; j < sizeof (pointers) / sizeof (pointers[0]); j++)
66 {
67 fail_if(pointers[j] == pointers[i]);
68 }
69 }
70
71 eina_shutdown();
72}
73END_TEST
74
75static Eina_Barrier b;
76
77static void *
78_thread1(void *data EINA_UNUSED, Eina_Thread t EINA_UNUSED)
79{
80 unsigned int i;
81
82 fail_if(!eina_barrier_wait(&b));
83
84 for (i = 0; i < sizeof (test_array) / sizeof (test_array[0]); i++)
85 {
86 pointers[i] = eina_safepointer_register(&test_array[i]);
87 fail_if(pointers[i] == NULL);
88 fail_if(pointers[i] == &test_array[i]);
89 fail_if(&test_array[i] != eina_safepointer_get(pointers[i]));
90 }
91
92 for (i = 0; i < sizeof (test_array) / sizeof (test_array[0]); i++)
93 {
94 eina_safepointer_unregister(pointers[i]);
95 }
96
97 return NULL;
98}
99
100static void *
101_thread2(void *data EINA_UNUSED, Eina_Thread t EINA_UNUSED)
102{
103 unsigned int i;
104
105 fail_if(!eina_barrier_wait(&b));
106
107 for (i = 0; i < sizeof (test_array2) / sizeof (test_array2[0]); i++)
108 {
109 pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] = eina_safepointer_register(&test_array2[i]);
110 fail_if(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] == NULL);
111 fail_if(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))] == &test_array2[i]);
112 fail_if(&test_array2[i] != eina_safepointer_get(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))]));
113 eina_safepointer_unregister(pointers[i + (sizeof (test_array) / sizeof (test_array[0]))]);
114 }
115
116 return NULL;
117}
118
119START_TEST(eina_test_threading)
120{
121 Eina_Thread t1, t2;
122 unsigned int i;
123
124 eina_init();
125
126 // We need a barrier so that both thread are more likely
127 // to start running in parallel
128 fail_if(!eina_barrier_new(&b, 2));
129
130 // Spawn them
131 fail_if(!eina_thread_create(&t1, 0, 0, _thread1, NULL));
132 fail_if(!eina_thread_create(&t2, 0, 0, _thread2, NULL));
133
134 // And wait for the outcome !
135 eina_thread_join(t1);
136 eina_thread_join(t2);
137
138 eina_barrier_free(&b);
139
140 for (i = 0; i < sizeof (pointers) / sizeof (pointers[0]); i++)
141 {
142 unsigned int j;
143
144 for (j = i + 1; j < sizeof (pointers) / sizeof (pointers[0]); j++)
145 {
146 fail_if(pointers[j] == pointers[i]);
147 }
148 }
149
150 eina_shutdown();
151}
152END_TEST
153
154START_TEST(eina_test_lowestbit)
155{
156 unsigned int i;
157
158 eina_init();
159
160 for (i = 0; i < sizeof (test_array) / sizeof (test_array[0]); i++)
161 {
162 pointers[i] = eina_safepointer_register(&test_array[i]);
163 fail_if(pointers[i] == NULL);
164 fail_if(pointers[i] == &test_array[i]);
165 fail_if(&test_array[i] != eina_safepointer_get(pointers[i]));
166
167 // We do guaranty that the two lower bit are always zero and will be internally ignored
168 fail_if((((uintptr_t) pointers[i]) & 0x3) != 0);
169 pointers[i] = (void*)(((uintptr_t) pointers[i]) | 0x3);
170
171 fail_if(&test_array[i] != eina_safepointer_get(pointers[i]));
172
173 eina_safepointer_unregister(pointers[i]);
174
175 fail_if(eina_safepointer_get(pointers[i]) != NULL);
176 }
177
178 eina_shutdown();
179}
180END_TEST
181
182void
183eina_test_safepointer(TCase *tc)
184{
185 tcase_add_test(tc, eina_test_reusable);
186 tcase_add_test(tc, eina_test_threading);
187 tcase_add_test(tc, eina_test_lowestbit);
188}