summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_slstr.c
blob: 170fc282109935bad0c501b7f93fe70eb5c5e819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define EINA_SLSTR_INTERNAL
#include <Eina.h>

#include "eina_suite.h"

static Eina_Slstr *
_slstr_copy(void)
{
   const char local[] = "Hello world 1";

   return eina_slstr_copy_new(local);
}

EFL_START_TEST(slstr_copy)
{
   Eina_Slstr *str;


   str = _slstr_copy();
   ck_assert_str_eq(str, "Hello world 1");

}
EFL_END_TEST

static Eina_Slstr *
_slstr_steal(void)
{
   char *local = strdup("Hello world 2");

   return eina_slstr_steal_new(local);
}

EFL_START_TEST(slstr_steal)
{
   Eina_Slstr *str;


   str = _slstr_steal();
   ck_assert_str_eq(str, "Hello world 2");

}
EFL_END_TEST

static Eina_Slstr *
_slstr_stringshare(void)
{
   Eina_Stringshare *str = eina_stringshare_add("Hello world 3");

   return eina_slstr_stringshare_new(str);
}

EFL_START_TEST(slstr_stringshare)
{
   Eina_Stringshare *ss;
   Eina_Slstr *str;


   str = _slstr_stringshare();
   ss = eina_stringshare_add("Hello world 3");
   fail_if(ss != str);

}
EFL_END_TEST

static Eina_Slstr *
_slstr_tmpstr(void)
{
   Eina_Tmpstr *str = eina_tmpstr_add("Hello world 4");

   return eina_slstr_tmpstr_new(str);
}

EFL_START_TEST(slstr_tmpstr)
{
   Eina_Slstr *str;


   str = _slstr_tmpstr();
   ck_assert_str_eq(str, "Hello world 4");

}
EFL_END_TEST

static Eina_Slstr *
_slstr_strbuf(void)
{
   Eina_Strbuf *str = eina_strbuf_new();

   eina_strbuf_append(str, "Hello ");
   eina_strbuf_append(str, "world ");
   eina_strbuf_append_printf(str, "%d", 5);

   return eina_slstr_strbuf_new(str);
}

EFL_START_TEST(slstr_strbuf)
{
   Eina_Slstr *str;


   str = _slstr_strbuf();
   ck_assert_str_eq(str, "Hello world 5");

}
EFL_END_TEST

static Eina_Slstr *
_slstr_printf(int val)
{
   return eina_slstr_printf("Hello %s %d", "world", val);
}

EFL_START_TEST(slstr_slstr_printf)
{
   Eina_Slstr *str;


   str = _slstr_printf(6);
   ck_assert_str_eq(str, "Hello world 6");

}
EFL_END_TEST

static void
_many_do(Eina_Bool threaded)
{
   const int many = threaded ? 256 : 2048;
   Eina_Slstr *str;
   int k;

   for (k = 0; k < many; k++)
     {
        char local[64];

        str = _slstr_printf(k);
        sprintf(local, "Hello world %d", k);
        ck_assert_str_eq(str, local);
     }
}

EFL_START_TEST(slstr_many)
{

   _many_do(0);

   eina_slstr_local_clear();

}
EFL_END_TEST

static void *
_thread_cb(void *data EINA_UNUSED, Eina_Thread th EINA_UNUSED)
{
   _many_do(1);

   return NULL;
}

EFL_START_TEST(slstr_thread)
{
   const int threads = 8;
   Eina_Thread th[threads];
   int k;


   for (k = 0; k < threads; k++)
     fail_if(!eina_thread_create(&th[k], EINA_THREAD_NORMAL, -1, _thread_cb, NULL));

   for (k = 0; k < threads; k++)
     eina_thread_join(th[k]);

   eina_slstr_local_clear();

}
EFL_END_TEST

#define STRL "Hello world 7"

static Eina_Slstr *
_slstr_length(unsigned int length)
{
   Eina_Tmpstr *str = eina_tmpstr_add(STRL);

   return eina_slstr_copy_new_length(str, length);
}

EFL_START_TEST(slstr_length)
{
   unsigned int i;

   for (i = 0; i < strlen(STRL); i++)
     {
        Eina_Slstr *slstr;
        char *str;

        slstr = _slstr_length(i);
        str = eina_strndup(STRL, i);
        ck_assert_str_eq(str, slstr);

        free(str);
     }

   eina_slstr_local_clear();
}
EFL_END_TEST

void
eina_test_slstr(TCase *tc)
{
   tcase_add_test(tc, slstr_copy);
   tcase_add_test(tc, slstr_steal);
   tcase_add_test(tc, slstr_stringshare);
   tcase_add_test(tc, slstr_tmpstr);
   tcase_add_test(tc, slstr_strbuf);
   tcase_add_test(tc, slstr_slstr_printf);
   tcase_add_test(tc, slstr_many);
   tcase_add_test(tc, slstr_thread);
   tcase_add_test(tc, slstr_length);
}