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