Crossfire Server, Trunk
check_utils.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_utils_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/utils.c
31  */
32 
33 #include <check.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #include "global.h"
39 
40 
41 static void setup(void) {
42  /* put any initialisation steps here, they will be run before each testcase */
43 }
44 
45 static void teardown(void) {
46  /* put any cleanup steps here, they will be run after each testcase */
47 }
48 
49 static void check_split_string(const char *str, size_t array_size, ...) {
50  char tmp[256];
51  char *array[64];
52  size_t result;
53  size_t i;
54  va_list arg;
55 
56  snprintf(tmp, sizeof(tmp), "%s", str);
57  for(i = 0; i < sizeof(array)/sizeof(*array); i++)
58  array[i] = NULL;
59  result = split_string(tmp, array, array_size, ':');
60  fail_if(result > array_size, "result == %zu > %zu == array_size", result, array_size);
61  va_start(arg, array_size);
62  for(i = 0; i < sizeof(array)/sizeof(*array); i++) {
63  const char *expected_result;
64 
65  expected_result = va_arg(arg, const char *);
66  if (expected_result == NULL)
67  break;
68 
69  if (i >= array_size)
70  fail("internal error: too many arguments passed to check_split_string()");
71  if (i < result)
72  fail_if(strcmp(array[i], expected_result) != 0, "strcmp(array[%zu] == %s, %s) != 0", i, array[i], expected_result);
73  else
74  fail_if(array[i] != NULL, "array[%zu] == NULL", i);
75  }
76  va_end(arg);
77  fail_if(result != i, "%zu != %zu", result, i);
78 }
79 
80 START_TEST(test_split_string) {
81  check_split_string("", 0, NULL);
82  check_split_string("", 5, "", NULL);
83  check_split_string(":", 5, "", "", NULL);
84  check_split_string("::", 5, "", "", "", NULL);
85  check_split_string("abc:def:ghi", 0, NULL);
86  check_split_string("abc:def:ghi", 1, "abc:def:ghi", NULL);
87  check_split_string("abc:def:ghi", 2, "abc", "def:ghi", NULL);
88  check_split_string("abc:def:ghi", 3, "abc", "def", "ghi", NULL);
89  check_split_string("abc:def:ghi", 4, "abc", "def", "ghi", NULL);
90  check_split_string("::abc::def::", 0, NULL);
91  check_split_string("::abc::def::", 1, "::abc::def::", NULL);
92  check_split_string("::abc::def::", 2, "", ":abc::def::", NULL);
93  check_split_string("::abc::def::", 3, "", "", "abc::def::", NULL);
94  check_split_string("::abc::def::", 4, "", "", "abc", ":def::", NULL);
95  check_split_string("::abc::def::", 5, "", "", "abc", "", "def::", NULL);
96  check_split_string("::abc::def::", 6, "", "", "abc", "", "def", ":", NULL);
97  check_split_string("::abc::def::", 7, "", "", "abc", "", "def", "", "", NULL);
98  check_split_string("::abc::def::", 8, "", "", "abc", "", "def", "", "", NULL);
99 }
100 END_TEST
101 
102 START_TEST(test_replace) {
103  char init[MAX_BUF], replaced[MAX_BUF];
104 
105  snprintf(init, sizeof(init), "first test");
106  replace(init, "first", "other", replaced, sizeof(replaced));
107  fail_if(strcmp(replaced, "other test") != 0, "%s != other test", replaced);
108 
109  snprintf(init, sizeof(init), "second test");
110  replace(init, "e", "a", replaced, sizeof(replaced));
111  fail_if(strcmp(replaced, "sacond tast") != 0, "%s != sacond tast", replaced);
112 
113  /* corner case for length */
114  snprintf(init, sizeof(init), "long test");
115  replace(init, "long", "really long", replaced, strlen(init) + 1);
116  fail_if(strcmp(replaced, "really lo") != 0, "%s != really lo", replaced);
117 }
118 END_TEST
119 
120 static Suite *utils_suite(void) {
121  Suite *s = suite_create("utils");
122  TCase *tc_core = tcase_create("Core");
123 
124  /*setup and teardown will be called before each test in testcase 'tc_core' */
125  tcase_add_checked_fixture(tc_core, setup, teardown);
126 
127  suite_add_tcase(s, tc_core);
128  tcase_add_test(tc_core, test_split_string);
129  tcase_add_test(tc_core, test_replace);
130 
131  return s;
132 }
133 
134 int main(void) {
135  int nf;
136  Suite *s = utils_suite();
137  SRunner *sr = srunner_create(s);
138 
139  srunner_set_fork_status(sr, CK_NOFORK);
140  srunner_set_xml(sr, LOGDIR "/unit/common/utils.xml");
141  srunner_set_log(sr, LOGDIR "/unit/common/utils.out");
142  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
143  nf = srunner_ntests_failed(sr);
144  srunner_free(sr);
145  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
146 }
global.h
setup
static void setup(void)
Definition: check_utils.c:41
Ice.tmp
int tmp
Definition: Ice.py:207
check_split_string
static void check_split_string(const char *str, size_t array_size,...)
Definition: check_utils.c:49
main
int main(void)
Definition: check_utils.c:134
rotate-tower.result
bool result
Definition: rotate-tower.py:13
make_face_from_files.str
str
Definition: make_face_from_files.py:24
START_TEST
START_TEST(test_split_string)
Definition: check_utils.c:80
utils_suite
static END_TEST Suite * utils_suite(void)
Definition: check_utils.c:120
MAX_BUF
#define MAX_BUF
Definition: define.h:35
split_string
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Definition: utils.c:483
init
void init(int argc, char **argv)
Definition: init.c:1108
replace
void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
Definition: utils.c:337
teardown
static void teardown(void)
Definition: check_utils.c:45