Crossfire Server, Trunk
check_shstr.cpp
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_shstr_c =
3  * "$Id$";
4  */
5 
6 /*
7  * CrossFire, A Multiplayer game for X-windows
8  *
9  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  * Copyright (C) 1992 Frank Tore Johansen
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * The authors can be reached via e-mail at crossfire-devel@real-time.com
27  */
28 
29 /*
30  * This is the unit tests file for common/shstr.c
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <check.h>
36 #include "global.h"
37 #include "toolkit_common.h"
38 
39 /*
40 extern const char *add_string(const char *str);
41 extern const char *add_refcount(const char *str);
42 extern int query_refcount(const char *str);
43 extern const char *find_string(const char *str);
44 extern void free_string(const char *str);
45 extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
46 */
47 
48 static void setup(void) {
50 }
51 
52 static void teardown(void) {
53  /* nothing to do */
54 }
55 
56 START_TEST(test_add_string) {
57  const char *str1;
58  const char *str2;
59  const char *str3;
60  char *temp;
61 #ifndef MANY_CORES
62  FAIL_UNLESS(add_string(NULL) == NULL, "add_string should null when receiving a null as parameter.");
63 #endif
64  str1 = add_string("Hello world");
65  FAIL_UNLESS(str1 != NULL, "add_string should not return null when receiving content.");
66  temp = static_cast<char *>(malloc(strlen(str1)+1));
67  strcpy(temp, str1);
68  str2 = add_string(temp);
69  FAIL_UNLESS(str2 == str1, "add_string should return same pointer for 2 same strings but str1 (%p -> '%s') != str2 (%p -> '%s').", str1, str1, str2, str2);
70  str3 = add_string("");
71  FAIL_UNLESS(str3 != NULL, "add_string should handle gracefully empty non null strings.");
72  free(temp);
73 }
74 END_TEST
75 
76 START_TEST(test_add_refcount) {
77  const char *str1;
78  const char *str2;
79 
80  str1 = add_string("Crossfire Rulez");
81  str2 = add_refcount(str1);
82  FAIL_UNLESS(str1 == str2, "result of add_refcount (%p) should be the same as original pointer (%p).", str2, str1);
83  FAIL_UNLESS(query_refcount(str1) == 2, "add_refcount (%p) should have made refcount to value 2 but was %d", str1, query_refcount(str1));
84 }
85 END_TEST
86 
87 START_TEST(test_query_refcount) {
88  const char *str1;
89 
90  str1 = add_string("Hello World");
91  FAIL_UNLESS(query_refcount(str1) == 1, "After add_string, query_refcount should return 1 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
92  add_string("Hello World");
93  FAIL_UNLESS(query_refcount(str1) == 2, "After twice add_string with same string, query_refcount should return 2 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
94  add_refcount(str1);
95  FAIL_UNLESS(query_refcount(str1) == 3, "After call to add_refcount, query_refcount should now return 3 but returned %d(0x%X) for %s", query_refcount(str1), query_refcount(str1), str1);
96 }
97 END_TEST
98 
99 START_TEST(test_find_string) {
100  const char *str1;
101  const char *str2;
102  const char *result;
103 
104  str1 = add_string("Hello world");
105  str2 = add_string("Bonjour le monde");
106  result = find_string("Hello world");
107  FAIL_UNLESS(str1 == result, "find_string for %s should return %p but returned %p(%s).", str1, str1, result, result);
108  result = find_string("Bonjour le monde");
109  FAIL_UNLESS(str2 == result, "find_string for %s should return %p but returned %p(%s).", str2, str2, result, result);
110  result = find_string("Hola mundo");
111  FAIL_UNLESS(result == NULL, "Searching for an inexistant string should return NULL but returned %p(%s)", result, result);
112  str1 = add_string("");
113  result = find_string("");
114  FAIL_UNLESS(result == str1, "Search for empty string should return it(%p), but returned %p", str1, result);
115  free_string(str2);
116  result = find_string("Bonjour le monde");
117  FAIL_UNLESS(result == NULL, "add_string + free_string should mean i can't find the string anymore but find string returned %p(%s)", result, result);
118 }
119 END_TEST
120 
121 START_TEST(test_free_string) {
122  const char *str1;
123  const char *str2;
124 
125  str1 = add_string("Cr0ssf1r3 r|_|1z");
126  free_string(str1);
127  str2 = find_string("Cr0ssf1r3 r|_|1z");
128  FAIL_UNLESS(str2 == NULL, "find_String should return null after a free_string but it returned %p (%s)", str2, str2);
129  str1 = add_string("bleh");
130  add_string("bleh");
131  free_string(str1);
132  str2 = find_string("bleh");
133  FAIL_UNLESS(str2 == str1, "find_string should return the string(%p) after a add_string, add_string, free_string but returned %p", str1, str2);
134  free_string(str1);
135  str2 = find_string("bleh");
136  FAIL_UNLESS(str2 == NULL, "find_string should return null after add_string, add_string, free_string, free_string but returned %p", str2);
137 }
138 END_TEST
139 
140 START_TEST(test_buf_overflow) {
141  int i;
142 
143  i = buf_overflow("1", "22", 3);
144  FAIL_UNLESS(i, "'1' +'22' can't fit in a 3 char buffer but buf_overflow told us there won't be any overflow");
145  i = buf_overflow("1", NULL, 1);
146  FAIL_UNLESS(i, "'1' +NULL can't fit in a 1 char buffer but buf_overflow told us there won't be any overflow");
147  i = buf_overflow("1", NULL, 2);
148  FAIL_UNLESS(!i, "'1' +NULL can fit in a 2 char buffer but buf_overflow told us it won't");
149  i = buf_overflow("", NULL, 1);
150  FAIL_UNLESS(!i, "EMPTY +NULL can fit in a 1 char buffer but buf_overflow told us it won't");
151  i = buf_overflow("", NULL, 0);
152  FAIL_UNLESS(i, "EMPTY +NULL can't fit in a 0 char buffer but buf_overflow told us there won't be any overflow");
153 }
154 END_TEST
155 
156 static Suite *shstr_suite(void) {
157  Suite *s = suite_create("shstr");
158  TCase *tc_core = tcase_create("Core");
159 
160  /*setup and teardown will be called before each test in testcase 'tc_core' */
161  tcase_add_checked_fixture(tc_core, setup, teardown);
162 
163  suite_add_tcase(s, tc_core);
164  tcase_add_test(tc_core, test_add_string);
165  tcase_add_test(tc_core, test_add_refcount);
166  tcase_add_test(tc_core, test_query_refcount);
167  tcase_add_test(tc_core, test_find_string);
168  tcase_add_test(tc_core, test_free_string);
169  tcase_add_test(tc_core, test_buf_overflow);
170 
171  return s;
172 }
173 
174 int main(void) {
175  int nf;
176  Suite *s = shstr_suite();
177  SRunner *sr = srunner_create(s);
178 
179  srunner_set_xml(sr, LOGDIR "/unit/common/shstr.xml");
180  srunner_set_log(sr, LOGDIR "/unit/common/shstr.out");
181  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
182  nf = srunner_ntests_failed(sr);
183  srunner_free(sr);
184  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
185 }
main
int main(void)
Definition: check_shstr.cpp:174
global.h
setup
static void setup(void)
Definition: check_shstr.cpp:48
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
add_refcount
sstring add_refcount(sstring str)
Definition: shstr.cpp:210
rotate-tower.result
bool result
Definition: rotate-tower.py:13
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
find_string
sstring find_string(const char *str)
Definition: shstr.cpp:236
query_refcount
int query_refcount(sstring str)
Definition: shstr.cpp:224
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
teardown
static void teardown(void)
Definition: check_shstr.cpp:52
init_hash_table
void init_hash_table(void)
Definition: shstr.cpp:55
buf_overflow
int buf_overflow(const char *buf1, const char *buf2, size_t bufsize)
Definition: shstr.cpp:398
guildbuy.temp
def temp
Definition: guildbuy.py:26
shstr_suite
static END_TEST Suite * shstr_suite(void)
Definition: check_shstr.cpp:156
START_TEST
START_TEST(test_add_string)
Definition: check_shstr.cpp:56