Atrinik Server  4.0
shstr.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 
30 START_TEST(test_add_string)
31 {
32  shstr *str1, *str2, *str3;
33  char *temp;
34 
35  str1 = add_string("Hello world");
36  ck_assert_ptr_ne(str1, NULL);
37  temp = emalloc(strlen(str1) + 1);
38  strcpy(temp, str1);
39  str2 = add_string(temp);
40  ck_assert_ptr_eq(str2, str1);
41  str3 = add_string("");
42  ck_assert_ptr_ne(str3, NULL);
43  efree(temp);
44 
45  free_string_shared(str1);
46  free_string_shared(str2);
47  free_string_shared(str3);
48 }
49 
50 END_TEST
51 
52 START_TEST(test_add_refcount)
53 {
54  shstr *str1, *str2;
55 
56  str1 = add_string("Refcount testing//..");
57  str2 = add_refcount(str1);
58  ck_assert_ptr_eq(str1, str2);
59  ck_assert_int_eq(query_refcount(str1), 2);
60 
61  free_string_shared(str1);
62  free_string_shared(str2);
63 }
64 
65 END_TEST
66 
67 START_TEST(test_query_refcount)
68 {
69  shstr *str1;
70 
71  str1 = add_string("Hello World");
72  ck_assert_int_eq(query_refcount(str1), 1);
73  add_string("Hello World");
74  ck_assert_int_eq(query_refcount(str1), 2);
75  add_refcount(str1);
76  ck_assert_int_eq(query_refcount(str1), 3);
77 
78  free_string_shared(str1);
79  free_string_shared(str1);
80  free_string_shared(str1);
81 }
82 
83 END_TEST
84 
85 START_TEST(test_find_string)
86 {
87  shstr *str1, *str2, *str3, *result;
88 
89  str1 = add_string("Hello world");
90  str2 = add_string("Bonjour le monde");
91  result = find_string("Hello world");
92  ck_assert_ptr_eq(str1, result);
93  result = find_string("Bonjour le monde");
94  ck_assert_ptr_eq(str2, result);
95  result = find_string("Hola mundo");
96  ck_assert_ptr_eq(result, NULL);
97  str3 = add_string("");
98  result = find_string("");
99  ck_assert_ptr_eq(result, str3);
100  free_string_shared(str2);
101  result = find_string("Bonjour le monde");
102  ck_assert_ptr_eq(result, NULL);
103 
104  free_string_shared(str1);
105  free_string_shared(str3);
106 }
107 
108 END_TEST
109 
110 START_TEST(test_free_string_shared)
111 {
112  shstr *str1, *str2;
113 
114  str1 = add_string("l33t");
115  free_string_shared(str1);
116  str2 = find_string("l33t");
117  ck_assert_ptr_eq(str2, NULL);
118  str1 = add_string("bleh");
119  add_string("bleh");
120  free_string_shared(str1);
121  str2 = find_string("bleh");
122  ck_assert_ptr_eq(str2, str1);
123  free_string_shared(str1);
124  str2 = find_string("bleh");
125  ck_assert_ptr_eq(str2, NULL);
126 }
127 
128 END_TEST
129 
130 static Suite *suite(void)
131 {
132  Suite *s = suite_create("shstr");
133  TCase *tc_core = tcase_create("Core");
134 
135  tcase_add_unchecked_fixture(tc_core, check_setup, check_teardown);
136  tcase_add_checked_fixture(tc_core, check_test_setup, check_test_teardown);
137 
138  suite_add_tcase(s, tc_core);
139  tcase_add_test(tc_core, test_add_string);
140  tcase_add_test(tc_core, test_add_refcount);
141  tcase_add_test(tc_core, test_query_refcount);
142  tcase_add_test(tc_core, test_find_string);
143  tcase_add_test(tc_core, test_free_string_shared);
144 
145  return s;
146 }
147 
148 void check_server_shstr(void)
149 {
150  check_run_suite(suite(), __FILE__);
151 }