Atrinik Server  4.0
stringbuffer.c
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
25 #include <global.h>
26 #include <check.h>
27 #include <checkstd.h>
28 #include <check_proto.h>
29 #include <toolkit/string.h>
30 
31 START_TEST(test_stringbuffer_new)
32 {
33  StringBuffer *sb;
34 
35  sb = stringbuffer_new();
36  ck_assert_ptr_ne(sb, NULL);
37  efree(stringbuffer_finish(sb));
38 }
39 END_TEST
40 
41 START_TEST(test_stringbuffer_free)
42 {
43  StringBuffer *sb = stringbuffer_new();
44  ck_assert_ptr_ne(sb, NULL);
45 
46  memory_status_t memory_status;
47  if (memory_get_status(sb, &memory_status)) {
48  ck_assert_uint_eq(memory_status, MEMORY_STATUS_OK);
49  }
50 
51  stringbuffer_free(sb);
52  if (memory_get_status(sb, &memory_status)) {
53  ck_assert_uint_eq(memory_status, MEMORY_STATUS_FREE);
54  }
55 }
56 END_TEST
57 
58 START_TEST(test_stringbuffer_finish)
59 {
60  StringBuffer *sb;
61  char *cp;
62 
63  sb = stringbuffer_new();
64  cp = stringbuffer_finish(sb);
65  ck_assert_ptr_ne(cp, NULL);
66  ck_assert_str_eq(cp, "");
67  efree(cp);
68 
69  sb = stringbuffer_new();
70  stringbuffer_append_string(sb, "hello");
71  cp = stringbuffer_finish(sb);
72  ck_assert_str_eq(cp, "hello");
73  efree(cp);
74 }
75 END_TEST
76 
77 START_TEST(test_stringbuffer_finish_shared)
78 {
79  StringBuffer *sb;
80  shstr *cp;
81 
82  sb = stringbuffer_new();
83  cp = stringbuffer_finish_shared(sb);
84  ck_assert_ptr_ne(cp, NULL);
85  ck_assert_str_eq(cp, "");
86  ck_assert_ptr_eq(cp, find_string(""));
87  free_string_shared(cp);
88 
89  sb = stringbuffer_new();
90  stringbuffer_append_string(sb, "hello");
91  cp = stringbuffer_finish_shared(sb);
92  ck_assert_str_eq(cp, "hello");
93  ck_assert_ptr_eq(cp, find_string("hello"));
94  free_string_shared(cp);
95 }
96 END_TEST
97 
98 START_TEST(test_stringbuffer_append_string_len)
99 {
100  StringBuffer *sb;
101  char *cp;
102 
103  sb = stringbuffer_new();
104  stringbuffer_append_string_len(sb, "hello", 5);
105  cp = stringbuffer_finish(sb);
106  ck_assert_str_eq(cp, "hello");
107  efree(cp);
108 
109  sb = stringbuffer_new();
110  stringbuffer_append_string_len(sb, "hello world", 7);
111  cp = stringbuffer_finish(sb);
112  ck_assert_str_eq(cp, "hello w");
113  efree(cp);
114 
115  sb = stringbuffer_new();
116  stringbuffer_append_string_len(sb, "hello world", 0);
117  cp = stringbuffer_finish(sb);
118  ck_assert_str_eq(cp, "");
119  efree(cp);
120 
121  sb = stringbuffer_new();
122  stringbuffer_append_string_len(sb, "hello ", 6);
123  stringbuffer_append_string_len(sb, "world", 5);
124  cp = stringbuffer_finish(sb);
125  ck_assert_str_eq(cp, "hello world");
126  efree(cp);
127 }
128 END_TEST
129 
130 START_TEST(test_stringbuffer_append_string)
131 {
132  StringBuffer *sb;
133  char *cp, *cp2;
134 
135  sb = stringbuffer_new();
136  stringbuffer_append_string(sb, " hello ");
137  cp = stringbuffer_finish(sb);
138  ck_assert_str_eq(cp, " hello ");
139  efree(cp);
140 
141  sb = stringbuffer_new();
142  stringbuffer_append_string(sb, "hello ");
143  stringbuffer_append_string(sb, "world");
144  cp = stringbuffer_finish(sb);
145  ck_assert_str_eq(cp, "hello world");
146  efree(cp);
147 
148  sb = stringbuffer_new();
149  cp2 = string_repeat("hello", 1000);
150  stringbuffer_append_string(sb, cp2);
151  cp = stringbuffer_finish(sb);
152  ck_assert_str_eq(cp, cp2);
153  efree(cp);
154  efree(cp2);
155 }
156 END_TEST
157 
158 START_TEST(test_stringbuffer_append_printf)
159 {
160  StringBuffer *sb;
161  char *cp, *cp2;
162 
163  sb = stringbuffer_new();
164  stringbuffer_append_printf(sb, "%s", "hello");
165  cp = stringbuffer_finish(sb);
166  ck_assert_str_eq(cp, "hello");
167  efree(cp);
168 
169  sb = stringbuffer_new();
170  stringbuffer_append_printf(sb, "%s", "hello world");
171  cp = stringbuffer_finish(sb);
172  ck_assert_str_eq(cp, "hello world");
173  efree(cp);
174 
175  sb = stringbuffer_new();
176  stringbuffer_append_printf(sb, "%s ", "hello");
177  stringbuffer_append_printf(sb, "%s", "world");
178  cp = stringbuffer_finish(sb);
179  ck_assert_str_eq(cp, "hello world");
180  efree(cp);
181 
182  sb = stringbuffer_new();
183  stringbuffer_append_printf(sb, "%s %d %u", "hello", 10, UINT32_MAX);
184  cp = stringbuffer_finish(sb);
185  ck_assert_str_eq(cp, "hello 10 4294967295");
186  efree(cp);
187 
188  sb = stringbuffer_new();
189  cp2 = string_repeat("hello", 1000);
190  stringbuffer_append_printf(sb, "%s", cp2);
191  cp = stringbuffer_finish(sb);
192  ck_assert_str_eq(cp, cp2);
193  efree(cp);
194  efree(cp2);
195 }
196 END_TEST
197 
198 START_TEST(test_stringbuffer_append_stringbuffer)
199 {
200  StringBuffer *sb, *sb2, *sb3;
201  char *cp;
202 
203  sb = stringbuffer_new();
204  sb2 = stringbuffer_new();
205  stringbuffer_append_string(sb2, "hello");
206  stringbuffer_append_stringbuffer(sb, sb2);
207  cp = stringbuffer_finish(sb);
208  ck_assert_str_eq(cp, "hello");
209  efree(cp);
210  efree(stringbuffer_finish(sb2));
211 
212  sb = stringbuffer_new();
213  sb2 = stringbuffer_new();
214  sb3 = stringbuffer_new();
215  stringbuffer_append_string(sb2, "hello ");
216  stringbuffer_append_string(sb3, "world");
217  stringbuffer_append_stringbuffer(sb, sb2);
218  stringbuffer_append_stringbuffer(sb, sb3);
219  cp = stringbuffer_finish(sb);
220  ck_assert_str_eq(cp, "hello world");
221  efree(cp);
222  efree(stringbuffer_finish(sb2));
223  efree(stringbuffer_finish(sb3));
224 }
225 END_TEST
226 
227 START_TEST(test_stringbuffer_append_char)
228 {
229  StringBuffer *sb;
230  char *cp;
231 
232  sb = stringbuffer_new();
233  stringbuffer_append_char(sb, 'a');
234  cp = stringbuffer_finish(sb);
235  ck_assert_str_eq(cp, "a");
236  efree(cp);
237 
238  sb = stringbuffer_new();
239  stringbuffer_append_char(sb, 'h');
240  stringbuffer_append_char(sb, 'e');
241  stringbuffer_append_char(sb, 'l');
242  stringbuffer_append_char(sb, 'l');
243  stringbuffer_append_char(sb, 'o');
244  cp = stringbuffer_finish(sb);
245  ck_assert_str_eq(cp, "hello");
246  efree(cp);
247 }
248 END_TEST
249 
250 START_TEST(test_stringbuffer_data)
251 {
252  StringBuffer *sb = stringbuffer_new();
253  ck_assert_ptr_ne(stringbuffer_data(sb), NULL);
254  stringbuffer_append_string(sb, "hello");
255  ck_assert(strncmp(stringbuffer_data(sb), "hello", 5) == 0);
256  char *cp = stringbuffer_finish(sb);
257  ck_assert_str_eq(cp, "hello");
258  efree(cp);
259 }
260 END_TEST
261 
262 START_TEST(test_stringbuffer_length)
263 {
264  StringBuffer *sb;
265  char *cp;
266 
267  sb = stringbuffer_new();
268  ck_assert_uint_eq(stringbuffer_length(sb), 0);
269  stringbuffer_append_string(sb, "hello");
270  ck_assert_uint_eq(stringbuffer_length(sb), 5);
271  cp = stringbuffer_finish(sb);
272  ck_assert_str_eq(cp, "hello");
273  efree(cp);
274 }
275 END_TEST
276 
277 START_TEST(test_stringbuffer_seek)
278 {
279  StringBuffer *sb = stringbuffer_new();
280  stringbuffer_append_string(sb, "hello");
281  stringbuffer_seek(sb, 0);
282  stringbuffer_append_string(sb, "world");
283  char *cp = stringbuffer_finish(sb);
284  ck_assert_str_eq(cp, "world");
285  efree(cp);
286 }
287 END_TEST
288 
289 START_TEST(test_stringbuffer_index)
290 {
291  StringBuffer *sb;
292  char *cp;
293 
294  sb = stringbuffer_new();
295  ck_assert_int_eq(stringbuffer_index(sb, 'l'), -1);
296  ck_assert_int_eq(stringbuffer_index(sb, 'a'), -1);
297  stringbuffer_append_string(sb, "hello");
298  ck_assert_int_eq(stringbuffer_index(sb, 'a'), -1);
299  ck_assert_int_eq(stringbuffer_index(sb, 'h'), 0);
300  ck_assert_int_eq(stringbuffer_index(sb, 'e'), 1);
301  ck_assert_int_eq(stringbuffer_index(sb, 'l'), 2);
302  ck_assert_int_eq(stringbuffer_index(sb, 'o'), 4);
303  cp = stringbuffer_finish(sb);
304  ck_assert_str_eq(cp, "hello");
305  efree(cp);
306 }
307 END_TEST
308 
309 START_TEST(test_stringbuffer_rindex)
310 {
311  StringBuffer *sb;
312  char *cp;
313 
314  sb = stringbuffer_new();
315  ck_assert_int_eq(stringbuffer_rindex(sb, 'l'), -1);
316  ck_assert_int_eq(stringbuffer_rindex(sb, 'a'), -1);
317  stringbuffer_append_string(sb, "hello");
318  ck_assert_int_eq(stringbuffer_rindex(sb, 'a'), -1);
319  ck_assert_int_eq(stringbuffer_rindex(sb, 'h'), 0);
320  ck_assert_int_eq(stringbuffer_rindex(sb, 'e'), 1);
321  ck_assert_int_eq(stringbuffer_rindex(sb, 'l'), 3);
322  ck_assert_int_eq(stringbuffer_rindex(sb, 'o'), 4);
323  cp = stringbuffer_finish(sb);
324  ck_assert_str_eq(cp, "hello");
325  efree(cp);
326 }
327 END_TEST
328 
329 START_TEST(test_stringbuffer_sub)
330 {
331  StringBuffer *sb;
332  char *cp;
333 
334  sb = stringbuffer_new();
335  stringbuffer_append_string(sb, "hello world");
336 
337  cp = stringbuffer_sub(sb, 1, -1);
338  ck_assert_str_eq(cp, "ello worl");
339  efree(cp);
340 
341  cp = stringbuffer_sub(sb, 0, 0);
342  ck_assert_str_eq(cp, "hello world");
343  efree(cp);
344 
345  cp = stringbuffer_sub(sb, 1, 0);
346  ck_assert_str_eq(cp, "ello world");
347  efree(cp);
348 
349  cp = stringbuffer_sub(sb, 0, -1);
350  ck_assert_str_eq(cp, "hello worl");
351  efree(cp);
352 
353  cp = stringbuffer_sub(sb, -1, -1);
354  ck_assert_str_eq(cp, "l");
355  efree(cp);
356 
357  cp = stringbuffer_sub(sb, 4, 0);
358  ck_assert_str_eq(cp, "o world");
359  efree(cp);
360 
361  cp = stringbuffer_sub(sb, -5, 0);
362  ck_assert_str_eq(cp, "world");
363  efree(cp);
364 
365  cp = stringbuffer_sub(sb, 20, 0);
366  ck_assert_str_eq(cp, "");
367  efree(cp);
368 
369  cp = stringbuffer_sub(sb, -20, -20);
370  ck_assert_str_eq(cp, "");
371  efree(cp);
372 
373  cp = stringbuffer_sub(sb, 0, -20);
374  ck_assert_str_eq(cp, "");
375  efree(cp);
376 
377  cp = stringbuffer_sub(sb, -20, 0);
378  ck_assert_str_eq(cp, "hello world");
379  efree(cp);
380 
381  cp = stringbuffer_sub(sb, -500, 2);
382  ck_assert_str_eq(cp, "he");
383  efree(cp);
384 
385  cp = stringbuffer_sub(sb, 0, -500);
386  ck_assert_str_eq(cp, "");
387  efree(cp);
388 
389  cp = stringbuffer_sub(sb, 5, -500);
390  ck_assert_str_eq(cp, "");
391  efree(cp);
392 
393  cp = stringbuffer_finish(sb);
394  ck_assert_str_eq(cp, "hello world");
395  efree(cp);
396 }
397 END_TEST
398 
399 START_TEST(test_stringbuffer_1)
400 {
401  StringBuffer *sb, *sb2;
402  char *cp;
403 
404  sb = stringbuffer_new();
405  stringbuffer_append_string(sb, "hello");
406  stringbuffer_append_char(sb, ' ');
407  stringbuffer_append_printf(sb, "world %d", 1);
408  stringbuffer_append_string_len(sb, " ", 1);
409  sb2 = stringbuffer_new();
410  stringbuffer_append_string(sb2, "test");
411  stringbuffer_append_stringbuffer(sb, sb2);
412  ck_assert_uint_eq(stringbuffer_length(sb), 18);
413  cp = stringbuffer_finish(sb);
414  ck_assert_str_eq(cp, "hello world 1 test");
415  ck_assert_uint_eq(stringbuffer_length(sb2), 4);
416  efree(cp);
417  efree(stringbuffer_finish(sb2));
418 }
419 END_TEST
420 
421 static Suite *suite(void)
422 {
423  Suite *s = suite_create("stringbuffer");
424  TCase *tc_core = tcase_create("Core");
425 
426  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
427  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
428 
429  suite_add_tcase(s, tc_core);
430  tcase_add_test(tc_core, test_stringbuffer_new);
431  tcase_add_test(tc_core, test_stringbuffer_free);
432  tcase_add_test(tc_core, test_stringbuffer_finish);
433  tcase_add_test(tc_core, test_stringbuffer_finish_shared);
434  tcase_add_test(tc_core, test_stringbuffer_append_string_len);
435  tcase_add_test(tc_core, test_stringbuffer_append_string);
436  tcase_add_test(tc_core, test_stringbuffer_append_printf);
437  tcase_add_test(tc_core, test_stringbuffer_append_stringbuffer);
438  tcase_add_test(tc_core, test_stringbuffer_append_char);
439  tcase_add_test(tc_core, test_stringbuffer_data);
440  tcase_add_test(tc_core, test_stringbuffer_length);
441  tcase_add_test(tc_core, test_stringbuffer_seek);
442  tcase_add_test(tc_core, test_stringbuffer_index);
443  tcase_add_test(tc_core, test_stringbuffer_rindex);
444  tcase_add_test(tc_core, test_stringbuffer_sub);
445  tcase_add_test(tc_core, test_stringbuffer_1);
446 
447  return s;
448 }
449 
450 void check_server_stringbuffer(void)
451 {
452  check_run_suite(suite(), __FILE__);
453 }