Crossfire Server, Trunk
check_region.cpp
Go to the documentation of this file.
1 /*
2  * CrossFire, A Multiplayer game for X-windows
3  *
4  * Copyright (C) 2022 the Crossfire Development Team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * The authors can be reached via e-mail at crossfire-devel@real-time.com
21  */
22 
23 /*
24  * This is the unit tests file for common/region.c
25  */
26 
27 #include <stdlib.h>
28 #include <check.h>
29 #include <global.h>
30 #include <assert.h>
31 #include <toolkit_common.h>
32 
33 void setup(void) {
34  cctk_setdatadir(SOURCE_ROOT "lib");
35  cctk_setlog(LOGDIR "/unit/common/region.out");
37 }
38 
39 void teardown(void) {
40  /* put any cleanup steps here, they will be run after each testcase */
41 }
42 
43 START_TEST(test_get_by_unknown_name) {
44  region *reg = get_region_by_name("undefined?!");
45  FAIL_UNLESS(reg != NULL, "Must get a region for all names");
46  FAIL_UNLESS(reg->fallback, "Region must be fallback");
47 }
48 END_TEST
49 
50 START_TEST(test_get_by_name) {
51  region *reg = get_region_by_name("scorn");
52  FAIL_UNLESS(reg != NULL, "Must get a region for scorn");
53  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
54 }
55 END_TEST
56 
57 START_TEST(test_get_region_from_string_empty) {
58  region *reg = get_region_from_string("");
59  FAIL_UNLESS(reg != NULL, "Must get a region for empty");
60  FAIL_UNLESS(!reg->parent, "Region must not have a parent");
61 }
62 END_TEST
63 
64 START_TEST(test_get_region_from_string_exact_name) {
65  region *reg = get_region_from_string("darcapcircus");
66  FAIL_UNLESS(reg != NULL, "Must get a region for darcapcircus");
67  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
68 }
69 END_TEST
70 
71 START_TEST(test_get_region_from_string_exact_name_wrong_case) {
72  region *reg = get_region_from_string("darcapciRcus");
73  FAIL_UNLESS(reg != NULL, "Must not get a region for darcapciRcus");
74  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
75 }
76 END_TEST
77 
78 START_TEST(test_get_region_from_string_partial_name) {
79  region *reg = get_region_from_string("uthv");
80  FAIL_UNLESS(reg != NULL, "Must get a region for uthv");
81  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
82 }
83 END_TEST
84 
85 START_TEST(test_get_region_from_string_exact_long_name) {
86  region *reg = get_region_from_string("Team Arena Volcano");
87  FAIL_UNLESS(reg != NULL, "Must get a region for Team Arena Volcano");
88  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
89 }
90 END_TEST
91 
92 START_TEST(test_get_region_from_string_partial_long_name) {
93  region *reg = get_region_from_string("lord marksel");
94  FAIL_UNLESS(reg != NULL, "Must get a region for lord marksel");
95  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
96 }
97 END_TEST
98 
99 START_TEST(test_check_region_parent) {
100  region *reg = get_region_from_string("scorncounty");
101  FAIL_UNLESS(reg != NULL, "Must get a region for scorncounty");
102  FAIL_UNLESS(!reg->fallback, "Region must not be fallback");
103  FAIL_UNLESS(reg->parent != NULL, "scorncounty must have a parent");
104  FAIL_UNLESS(strcmp(reg->parent->name, "scorn") == 0, "scorncounty must be child of scorn");
105  FAIL_UNLESS(reg->parent->parent != NULL, "Scorn must be a child too");
106  FAIL_UNLESS(strcmp(reg->parent->parent->name, "world") == 0, "scorn must be child of world");
107 }
108 END_TEST
109 
110 START_TEST(test_get_region_is_child_of_region) {
111  region *reg1 = get_region_from_string("scorn");
112  region *reg2 = get_region_from_string("scorncounty");
113  FAIL_UNLESS(region_is_child_of_region(reg2, reg1), "scorncountry should be child of scorn");
114 }
115 END_TEST
116 
117 Suite *region_suite(void) {
118  Suite *s = suite_create("region");
119  TCase *tc_core = tcase_create("Core");
120  tcase_set_timeout(tc_core, 60);
121 
122  /*setup and teardown will be called before each test in testcase 'tc_core' */
123  tcase_add_checked_fixture(tc_core, setup, teardown);
124 
125  suite_add_tcase(s, tc_core);
126  tcase_add_test(tc_core, test_get_by_unknown_name);
127  tcase_add_test(tc_core, test_get_by_name);
128  tcase_add_test(tc_core, test_get_region_from_string_empty);
129  tcase_add_test(tc_core, test_get_region_from_string_exact_name);
130  tcase_add_test(tc_core, test_get_region_from_string_exact_name_wrong_case);
131  tcase_add_test(tc_core, test_get_region_from_string_partial_name);
132  tcase_add_test(tc_core, test_get_region_from_string_exact_long_name);
133  tcase_add_test(tc_core, test_get_region_from_string_partial_long_name);
134  tcase_add_test(tc_core, test_check_region_parent);
135  tcase_add_test(tc_core, test_get_region_is_child_of_region);
136 
137  return s;
138 }
139 
140 int main(void) {
141  int nf;
142  Suite *s = region_suite();
143  SRunner *sr = srunner_create(s);
144 
145  srunner_set_xml(sr, LOGDIR "/unit/common/region.xml");
146  srunner_set_log(sr, LOGDIR "/unit/common/region.out");
147  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
148  srunner_set_fork_status(sr, CK_NOFORK);
149  nf = srunner_ntests_failed(sr);
150  srunner_free(sr);
151  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
152 }
global.h
region::fallback
int8_t fallback
Definition: map.h:283
get_region_by_name
region * get_region_by_name(const char *region_name)
Definition: region.cpp:46
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.cpp:69
region_is_child_of_region
int region_is_child_of_region(const region *child, const region *r)
Definition: region.cpp:183
START_TEST
START_TEST(test_get_by_unknown_name)
Definition: check_region.cpp:43
region::name
char * name
Definition: map.h:273
main
int main(void)
Definition: check_region.cpp:140
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
toolkit_common.h
cctk_setlog
void cctk_setlog(const char *logfile)
Definition: toolkit_common.cpp:64
setup
void setup(void)
Definition: check_region.cpp:33
region_suite
END_TEST Suite * region_suite(void)
Definition: check_region.cpp:117
region::parent
region * parent
Definition: map.h:274
teardown
void teardown(void)
Definition: check_region.cpp:39
region
Definition: map.h:272
cctk_init_std_archetypes
void cctk_init_std_archetypes(void)
Definition: toolkit_common.cpp:83
get_region_from_string
region * get_region_from_string(const char *name)
Definition: region.cpp:117