Crossfire Server, Trunk
check_shstr.c
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 
38 /*
39 extern const char *add_string(const char *str);
40 extern const char *add_refcount(const char *str);
41 extern int query_refcount(const char *str);
42 extern const char *find_string(const char *str);
43 extern void free_string(const char *str);
44 extern int buf_overflow(const char *buf1, const char *buf2, int bufsize);
45 */
46 
47 static void setup(void) {
49 }
50 
51 static void teardown(void) {
52  /* nothing to do */
53 }
54 
55 START_TEST(test_add_string) {
56  const char *str1;
57  const char *str2;
58  const char *str3;
59  char *temp;
60 #ifndef MANY_CORES
61  fail_unless(add_string(NULL) == NULL, "add_string should null when receiving a null as parameter.");
62 #endif
63  str1 = add_string("Hello world");
64  fail_unless(str1 != NULL, "add_string should not return null when receiving content.");
65  temp = malloc(strlen(str1)+1);
66  strcpy(temp, str1);
67  str2 = add_string(temp);
68  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);
69  str3 = add_string("");
70  fail_unless(str3 != NULL, "add_string should handle gracefully empty non null strings.");
71  free(temp);
72 }
73 END_TEST
74 
75 START_TEST(test_add_refcount) {
76  const char *str1;
77  const char *str2;
78 
79  str1 = add_string("Crossfire Rulez");
80  str2 = add_refcount(str1);
81  fail_unless(str1 == str2, "result of add_refcount (%p) should be the same as original pointer (%p).", str2, str1);
82  fail_unless(query_refcount(str1) == 2, "add_refcount (%p) should have made refcount to value 2 but was %d", str1, query_refcount(str1));
83 }
84 END_TEST
85 
86 START_TEST(test_query_refcount) {
87  const char *str1;
88 
89  str1 = add_string("Hello World");
90  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);
91  add_string("Hello World");
92  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);
93  add_refcount(str1);
94  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);
95 }
96 END_TEST
97 
98 START_TEST(test_find_string) {
99  const char *str1;
100  const char *str2;
101  const char *result;
102 
103  str1 = add_string("Hello world");
104  str2 = add_string("Bonjour le monde");
105  result = find_string("Hello world");
106  fail_unless(str1 == result, "find_string for %s should return %p but returned %p(%s).", str1, str1, result, result);
107  result = find_string("Bonjour le monde");
108  fail_unless(str2 == result, "find_string for %s should return %p but returned %p(%s).", str2, str2, result, result);
109  result = find_string("Hola mundo");
110  fail_unless(result == NULL, "Searching for an inexistant string should return NULL but returned %p(%s)", result, result);
111  str1 = add_string("");
112  result = find_string("");
113  fail_unless(result == str1, "Search for empty string should return it(%p), but returned %p", str1, result);
114  free_string(str2);
115  result = find_string("Bonjour le monde");
116  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);
117 }
118 END_TEST
119 
120 START_TEST(test_free_string) {
121  const char *str1;
122  const char *str2;
123 
124  str1 = add_string("Cr0ssf1r3 r|_|1z");
125  free_string(str1);
126  str2 = find_string("Cr0ssf1r3 r|_|1z");
127  fail_unless(str2 == NULL, "find_String should return null after a free_string but it returned %p (%s)", str2, str2);
128  str1 = add_string("bleh");
129  add_string("bleh");
130  free_string(str1);
131  str2 = find_string("bleh");
132  fail_unless(str2 == str1, "find_string should return the string(%p) after a add_string, add_string, free_string but returned %p", str1, str2);
133  free_string(str1);
134  str2 = find_string("bleh");
135  fail_unless(str2 == NULL, "find_string should return null after add_string, add_string, free_string, free_string but returned %p", str2);
136 }
137 END_TEST
138 
139 START_TEST(test_buf_overflow) {
140  int i;
141 
142  i = buf_overflow("1", "22", 3);
143  fail_unless(i, "'1' +'22' can't fit in a 3 char buffer but buf_overflow told us there won't be any overflow");
144  i = buf_overflow("1", NULL, 1);
145  fail_unless(i, "'1' +NULL can't fit in a 1 char buffer but buf_overflow told us there won't be any overflow");
146  i = buf_overflow("1", NULL, 2);
147  fail_unless(!i, "'1' +NULL can fit in a 2 char buffer but buf_overflow told us it won't");
148  i = buf_overflow("", NULL, 1);
149  fail_unless(!i, "EMPTY +NULL can fit in a 1 char buffer but buf_overflow told us it won't");
150  i = buf_overflow("", NULL, 0);
151  fail_unless(i, "EMPTY +NULL can't fit in a 0 char buffer but buf_overflow told us there won't be any overflow");
152 }
153 END_TEST
154 
155 static Suite *shstr_suite(void) {
156  Suite *s = suite_create("shstr");
157  TCase *tc_core = tcase_create("Core");
158 
159  /*setup and teardown will be called before each test in testcase 'tc_core' */
160  tcase_add_checked_fixture(tc_core, setup, teardown);
161 
162  suite_add_tcase(s, tc_core);
163  tcase_add_test(tc_core, test_add_string);
164  tcase_add_test(tc_core, test_add_refcount);
165  tcase_add_test(tc_core, test_query_refcount);
166  tcase_add_test(tc_core, test_find_string);
167  tcase_add_test(tc_core, test_free_string);
168  tcase_add_test(tc_core, test_buf_overflow);
169 
170  return s;
171 }
172 
173 int main(void) {
174  int nf;
175  Suite *s = shstr_suite();
176  SRunner *sr = srunner_create(s);
177 
178  srunner_set_xml(sr, LOGDIR "/unit/common/shstr.xml");
179  srunner_set_log(sr, LOGDIR "/unit/common/shstr.out");
180  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
181  nf = srunner_ntests_failed(sr);
182  srunner_free(sr);
183  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
184 }
init_hash_table
void init_hash_table(void)
Definition: shstr.c:55
shstr_suite
static END_TEST Suite * shstr_suite(void)
Definition: check_shstr.c:155
global.h
add_refcount
sstring add_refcount(sstring str)
Definition: shstr.c:210
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
teardown
static void teardown(void)
Definition: check_shstr.c:51
free_string
void free_string(sstring str)
Definition: shstr.c:280
rotate-tower.result
bool result
Definition: rotate-tower.py:13
setup
static void setup(void)
Definition: check_shstr.c:47
find_string
sstring find_string(const char *str)
Definition: shstr.c:236
buf_overflow
int buf_overflow(const char *buf1, const char *buf2, size_t bufsize)
Definition: shstr.c:398
START_TEST
START_TEST(test_add_string)
Definition: check_shstr.c:55
main
int main(void)
Definition: check_shstr.c:173
query_refcount
int query_refcount(sstring str)
Definition: shstr.c:224
guildbuy.temp
def temp
Definition: guildbuy.py:26