Crossfire Server, Trunk
check_swap.cpp
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_swap_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 server/swap.c
31  */
32 
33 #include <stdlib.h>
34 #include <check.h>
35 
36 #include "global.h"
37 #include "sproto.h"
38 #include "map.h"
39 #include "toolkit_common.h"
40 
41 void setup(void) {
42  /* put any initialisation steps here, they will be run before each testcase */
43 }
44 
45 void teardown(void) {
46  /* put any cleanup steps here, they will be run after each testcase */
47 }
48 
49 static mapstruct *do_map(const char *tmpname, const char *reset_group) {
50  mapstruct *map = get_empty_map(4, 4);
51  FAIL_UNLESS(first_map == map, "Map not added on list");
52  map->in_memory = MAP_SWAPPED;
53  map->reset_time = 1;
54  map->tmpname = strdup_local(tmpname);
55  if (reset_group) {
56  map->reset_group = add_string(reset_group);
57  }
58  return map;
59 }
60 
61 START_TEST(test_simple_reset) {
62  do_map("map1", NULL);
64  FAIL_UNLESS(first_map == NULL, "Map wasn't reset");
65 }
66 END_TEST
67 
68 START_TEST(test_simple_reset_group) {
69  do_map("1", "rg");
71  FAIL_UNLESS(first_map == NULL, "Map wasn't reset");
72 }
73 END_TEST
74 
75 START_TEST(test_reset_group_other) {
76  do_map("1", "rg");
77  do_map("2", NULL)->reset_time = seconds() + 50;
79  FAIL_UNLESS(first_map != NULL, "Map was reset");
80  FAIL_UNLESS(first_map->next == NULL, "Map group wasn't reset");
81 }
82 END_TEST
83 
84 START_TEST(test_reset_group_all) {
85  do_map("1", "rg");
86  do_map("2", "rg");
88  FAIL_UNLESS(first_map == NULL, "Map weren't reset");
89 }
90 END_TEST
91 
92 START_TEST(test_reset_group_blocked_time) {
93  do_map("1", "rg");
94  mapstruct *block = do_map("2", "rg");
95  block->reset_time = seconds() + 50;
97  FAIL_UNLESS(first_map == block, "Map were reset");
98  FAIL_UNLESS(first_map->next, "Other map should not have been reset");
99 
100  block->reset_time = 1;
101  flush_old_maps();
102  FAIL_UNLESS(first_map == NULL, "Map weren't reset");
103 }
104 END_TEST
105 
106 START_TEST(test_reset_group_blocked_not_swapped) {
107  do_map("1", "rg");
108  mapstruct *block = do_map("2", "rg");
109  block->in_memory = MAP_IN_MEMORY;
110  flush_old_maps();
111  FAIL_UNLESS(first_map == block, "Map were reset");
112  FAIL_UNLESS(first_map->next, "Other map should not have been reset");
113 
114  block->in_memory = MAP_SWAPPED;
115  flush_old_maps();
116  FAIL_UNLESS(first_map == NULL, "Map weren't reset");
117 }
118 END_TEST
119 
120 Suite *swap_suite(void) {
121  Suite *s = suite_create("swap");
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_simple_reset);
129  tcase_add_test(tc_core, test_simple_reset_group);
130  tcase_add_test(tc_core, test_reset_group_other);
131  tcase_add_test(tc_core, test_reset_group_all);
132  tcase_add_test(tc_core, test_reset_group_blocked_time);
133  tcase_add_test(tc_core, test_reset_group_blocked_not_swapped);
134 
135  return s;
136 }
137 
138 int main(void) {
139  int nf;
140  Suite *s = swap_suite();
141  SRunner *sr = srunner_create(s);
142 
143  srunner_set_xml(sr, LOGDIR "/unit/server/swap.xml");
144  srunner_set_log(sr, LOGDIR "/unit/server/swap.out");
145  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
146  nf = srunner_ntests_failed(sr);
147  srunner_free(sr);
148  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
149 }
global.h
teardown
void teardown(void)
Definition: check_swap.cpp:45
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.cpp:842
strdup_local
#define strdup_local
Definition: compat.h:29
flush_old_maps
void flush_old_maps(void)
Definition: swap.cpp:291
FAIL_UNLESS
#define FAIL_UNLESS(expr,...)
Definition: toolkit_common.h:11
MAP_IN_MEMORY
#define MAP_IN_MEMORY
Definition: map.h:126
toolkit_common.h
disinfect.map
map
Definition: disinfect.py:4
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
first_map
mapstruct * first_map
Definition: init.cpp:107
setup
void setup(void)
Definition: check_swap.cpp:41
sproto.h
seconds
long seconds(void)
Definition: time.cpp:348
map.h
block
static blocks block[MAP_CLIENT_X][MAP_CLIENT_Y]
Definition: los.cpp:57
swap_suite
END_TEST Suite * swap_suite(void)
Definition: check_swap.cpp:120
mapstruct
Definition: map.h:313
MAP_SWAPPED
#define MAP_SWAPPED
Definition: map.h:127
main
int main(void)
Definition: check_swap.cpp:138
START_TEST
START_TEST(test_simple_reset)
Definition: check_swap.cpp:61
mapstruct::next
mapstruct * next
Definition: map.h:314
mapstruct::reset_time
uint32_t reset_time
Definition: map.h:320
do_map
static mapstruct * do_map(const char *tmpname, const char *reset_group)
Definition: check_swap.cpp:49