Crossfire Server, Trunk
check_swap.c
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 
40 void setup(void) {
41  /* put any initialisation steps here, they will be run before each testcase */
42 }
43 
44 void teardown(void) {
45  /* put any cleanup steps here, they will be run after each testcase */
46 }
47 
48 static mapstruct *do_map(const char *tmpname, const char *reset_group) {
49  mapstruct *map = get_empty_map(4, 4);
50  fail_unless(first_map == map, "Map not added on list");
51  map->in_memory = MAP_SWAPPED;
52  map->reset_time = 1;
53  map->tmpname = strdup_local(tmpname);
54  if (reset_group) {
55  map->reset_group = add_string(reset_group);
56  }
57  return map;
58 }
59 
60 START_TEST(test_simple_reset) {
61  do_map("map1", NULL);
63  fail_unless(first_map == NULL, "Map wasn't reset");
64 }
65 END_TEST
66 
67 START_TEST(test_simple_reset_group) {
68  do_map("1", "rg");
70  fail_unless(first_map == NULL, "Map wasn't reset");
71 }
72 END_TEST
73 
74 START_TEST(test_reset_group_other) {
75  do_map("1", "rg");
76  do_map("2", NULL)->reset_time = seconds() + 50;
78  fail_unless(first_map != NULL, "Map was reset");
79  fail_unless(first_map->next == NULL, "Map group wasn't reset");
80 }
81 END_TEST
82 
83 START_TEST(test_reset_group_all) {
84  do_map("1", "rg");
85  do_map("2", "rg");
87  fail_unless(first_map == NULL, "Map weren't reset");
88 }
89 END_TEST
90 
91 START_TEST(test_reset_group_blocked_time) {
92  do_map("1", "rg");
93  mapstruct *block = do_map("2", "rg");
94  block->reset_time = seconds() + 50;
96  fail_unless(first_map == block, "Map were reset");
97  fail_unless(first_map->next, "Other map should not have been reset");
98 
99  block->reset_time = 1;
100  flush_old_maps();
101  fail_unless(first_map == NULL, "Map weren't reset");
102 }
103 END_TEST
104 
105 START_TEST(test_reset_group_blocked_not_swapped) {
106  do_map("1", "rg");
107  mapstruct *block = do_map("2", "rg");
108  block->in_memory = MAP_IN_MEMORY;
109  flush_old_maps();
110  fail_unless(first_map == block, "Map were reset");
111  fail_unless(first_map->next, "Other map should not have been reset");
112 
113  block->in_memory = MAP_SWAPPED;
114  flush_old_maps();
115  fail_unless(first_map == NULL, "Map weren't reset");
116 }
117 END_TEST
118 
119 Suite *swap_suite(void) {
120  Suite *s = suite_create("swap");
121  TCase *tc_core = tcase_create("Core");
122 
123  /*setup and teardown will be called before each test in testcase 'tc_core' */
124  tcase_add_checked_fixture(tc_core, setup, teardown);
125 
126  suite_add_tcase(s, tc_core);
127  tcase_add_test(tc_core, test_simple_reset);
128  tcase_add_test(tc_core, test_simple_reset_group);
129  tcase_add_test(tc_core, test_reset_group_other);
130  tcase_add_test(tc_core, test_reset_group_all);
131  tcase_add_test(tc_core, test_reset_group_blocked_time);
132  tcase_add_test(tc_core, test_reset_group_blocked_not_swapped);
133 
134  return s;
135 }
136 
137 int main(void) {
138  int nf;
139  Suite *s = swap_suite();
140  SRunner *sr = srunner_create(s);
141 
142  srunner_set_xml(sr, LOGDIR "/unit/server/swap.xml");
143  srunner_set_log(sr, LOGDIR "/unit/server/swap.out");
144  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
145  nf = srunner_ntests_failed(sr);
146  srunner_free(sr);
147  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
148 }
teardown
void teardown(void)
Definition: check_swap.c:44
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
strdup_local
#define strdup_local
Definition: compat.h:29
flush_old_maps
void flush_old_maps(void)
Definition: swap.c:289
swap_suite
END_TEST Suite * swap_suite(void)
Definition: check_swap.c:119
setup
void setup(void)
Definition: check_swap.c:40
do_map
static mapstruct * do_map(const char *tmpname, const char *reset_group)
Definition: check_swap.c:48
first_map
EXTERN mapstruct * first_map
Definition: global.h:116
MAP_IN_MEMORY
#define MAP_IN_MEMORY
Definition: map.h:131
main
int main(void)
Definition: check_swap.c:137
block
static blocks block[MAP_CLIENT_X][MAP_CLIENT_Y]
Definition: los.c:39
disinfect.map
map
Definition: disinfect.py:4
sproto.h
mapdef
Definition: map.h:317
map.h
MAP_SWAPPED
#define MAP_SWAPPED
Definition: map.h:132
mapdef::reset_time
uint32_t reset_time
Definition: map.h:324
seconds
long seconds(void)
Definition: time.c:344
get_empty_map
mapstruct * get_empty_map(int sizex, int sizey)
Definition: map.c:869
mapdef::next
struct mapdef * next
Definition: map.h:318
START_TEST
START_TEST(test_simple_reset)
Definition: check_swap.c:60