Crossfire Server, Trunk
check_loader.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_loader_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/loader.c
31  */
32 
33 #include <global.h>
34 #include <stdlib.h>
35 #include <check.h>
36 #include <loader.h>
37 #include <toolkit_common.h>
38 #include <object.h>
39 #include <stringbuffer.h>
40 
41 static void setup(void) {
42  cctk_setdatadir(SOURCE_ROOT "lib");
43  cctk_setlog(LOGDIR "/unit/common/loader.out");
45 }
46 
47 static void teardown(void) {
48  /* put any cleanup steps here, they will be run after each testcase */
49 }
50 
51 START_TEST(test_get_ob_diff) {
53  object *orc;
54  archetype *arch;
55  char *result;
56 
57  arch = find_archetype("orc");
58  fail_unless(arch != NULL, "Can't find 'orc' archetype!");
59  orc = arch_to_object(arch);
60  fail_unless(orc != NULL, "Couldn't create first orc!");
61 
63  get_ob_diff(buf, orc, &arch->clone);
65  fail_unless(result && result[0] == '\0', "diff obj/clone was %s!", result);
66  free(result);
67 
68  orc->speed = 0.5;
69  orc->speed_left = arch->clone.speed_left;
70  FREE_AND_COPY(orc->name, "Orc chief");
71 
73  get_ob_diff(buf, orc, &arch->clone);
75  fail_unless(result && strcmp(result, "name Orc chief\nspeed 0.500000\n") == 0, "diff modified obj/clone was %s!", result);
76  free(result);
77 
78  orc->stats.hp = 50;
79  orc->stats.Wis = 59;
80  orc->expmul = 8.5;
81  orc->stats.dam = 168;
82 
84  get_ob_diff(buf, orc, &arch->clone);
86  fail_unless(result && strcmp(result, "name Orc chief\nWis 59\nhp 50\nexpmul 8.500000\ndam 168\nspeed 0.500000\n") == 0, "2n diff modified obj/clone was %s!", result);
87  free(result);
88 }
89 END_TEST
90 
91 START_TEST(test_object_dump) {
94  object *empty;
95  char *result;
96  char expect[10000];
97 
98  /* Basic */
100  fail_unless(empty != NULL, "Couldn't create empty archetype!");
101 
102  snprintf(expect, sizeof(expect), "arch empty_archetype\nend\n");
103  buf = stringbuffer_new();
104  object_dump(empty, buf);
106  fail_unless(result && strcmp(result, expect) == 0, "object_dump was \"%s\" instead of \"%s\"!", result, expect);
107  free(result);
108 
109  /* With more things */
114  fail_unless(empty->head != NULL, "Couldn't create empty archetype as head!");
115  fail_unless(empty->inv != NULL, "Couldn't create empty archetype as inv!");
116  fail_unless(empty->more != NULL, "Couldn't create empty archetype as more!");
117  fail_unless(empty->env != NULL, "Couldn't create empty archetype as env!");
118 
119  snprintf(expect, sizeof(expect), "arch empty_archetype\nmore %d\nhead %d\nenv %d\ninv %d\nend\n", empty->more->count, empty->head->count, empty->env->count, empty->inv->count);
120  buf = stringbuffer_new();
121  object_dump(empty, buf);
123  fail_unless(result && strcmp(result, expect) == 0, "object_dump was \"%s\" instead of \"%s\"!", result, expect);
124  free(result);
125 }
126 END_TEST
127 
128 static Suite *loader_suite(void) {
129  Suite *s = suite_create("loader");
130  TCase *tc_core = tcase_create("Core");
131  tcase_set_timeout(tc_core, 60);
132 
133  /*setup and teardown will be called before each test in testcase 'tc_core' */
134  tcase_add_checked_fixture(tc_core, setup, teardown);
135 
136  suite_add_tcase(s, tc_core);
137  tcase_add_test(tc_core, test_get_ob_diff);
138  tcase_add_test(tc_core, test_object_dump);
139 
140  return s;
141 }
142 
143 int main(void) {
144  int nf;
145  Suite *s = loader_suite();
146  SRunner *sr = srunner_create(s);
147 
148  srunner_set_xml(sr, LOGDIR "/unit/common/loader.xml");
149  srunner_set_log(sr, LOGDIR "/unit/common/loader.out");
150  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
151  nf = srunner_ntests_failed(sr);
152  srunner_free(sr);
153  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
154 }
empty_archetype
EXTERN archetype * empty_archetype
Definition: global.h:140
global.h
liv::dam
int16_t dam
Definition: living.h:46
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
loader_suite
static END_TEST Suite * loader_suite(void)
Definition: check_loader.c:128
obj::count
tag_t count
Definition: object.h:302
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
cctk_setdatadir
void cctk_setdatadir(const char *datadir)
Definition: toolkit_common.c:69
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4956
liv::hp
int16_t hp
Definition: living.h:40
obj::expmul
double expmul
Definition: object.h:400
archt
Definition: object.h:469
stringbuffer.h
toolkit_common.h
obj::name
sstring name
Definition: object.h:314
cctk_setlog
void cctk_setlog(const char *logfile)
Definition: toolkit_common.c:64
rotate-tower.result
bool result
Definition: rotate-tower.py:13
teardown
static void teardown(void)
Definition: check_loader.c:47
object_dump
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:649
obj::speed_left
float speed_left
Definition: object.h:333
FREE_AND_COPY
#define FREE_AND_COPY(sv, nv)
Definition: global.h:201
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
obj::speed
float speed
Definition: object.h:332
obj::env
struct obj * env
Definition: object.h:296
StringBuffer
Definition: stringbuffer.c:25
cctk_init_std_archetypes
void cctk_init_std_archetypes(void)
Definition: toolkit_common.c:83
obj::stats
living stats
Definition: object.h:373
START_TEST
START_TEST(test_get_ob_diff)
Definition: check_loader.c:51
setup
static void setup(void)
Definition: check_loader.c:41
liv::Wis
int8_t Wis
Definition: living.h:36
find_archetype
archetype * find_archetype(const char *name)
Definition: assets.cpp:284
main
int main(void)
Definition: check_loader.c:143
buf
StringBuffer * buf
Definition: readable.c:1610
obj::head
struct obj * head
Definition: object.h:299
obj::more
struct obj * more
Definition: object.h:298
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
loader.h
object.h
obj::inv
struct obj * inv
Definition: object.h:293