Crossfire Server, Branches 1.12  R18729
check_loader.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_loader_c =
3  * "$Id: check_loader.c 11578 2009-02-23 22:02:27Z lalo $";
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 void setup(void) {
42  cctk_setdatadir(BUILD_ROOT "lib");
43  cctk_setlog(LOGDIR "/unit/common/loader.out");
45 }
46 
47 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) {
52  StringBuffer *buf;
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 
62  buf = stringbuffer_new();
63  get_ob_diff(buf, orc, &arch->clone);
64  result = stringbuffer_finish(buf);
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 
72  buf = stringbuffer_new();
73  get_ob_diff(buf, orc, &arch->clone);
74  result = stringbuffer_finish(buf);
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 
83  buf = stringbuffer_new();
84  get_ob_diff(buf, orc, &arch->clone);
85  result = stringbuffer_finish(buf);
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_dump_object) {
93  StringBuffer *buf;
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  dump_object(empty, buf);
105  result = stringbuffer_finish(buf);
106  fail_unless(result && strcmp(result, expect) == 0, "dump_object was \"%s\" instead of \"%s\"!", result, expect);
107  free(result);
108 
109  /* With more things */
115  fail_unless(empty->head != NULL, "Couldn't create empty archetype as head!");
116  fail_unless(empty->inv != NULL, "Couldn't create empty archetype as inv!");
117  fail_unless(empty->more != NULL, "Couldn't create empty archetype as more!");
118  fail_unless(empty->owner != NULL, "Couldn't create empty archetype as owner!");
119  fail_unless(empty->env != NULL, "Couldn't create empty archetype as env!");
120 
121  snprintf(expect, sizeof(expect), "arch empty_archetype\nmore %d\nhead %d\nenv %d\ninv %d\nowner %d\nend\n", empty->more->count, empty->head->count, empty->env->count, empty->inv->count, empty->owner->count);
122  buf = stringbuffer_new();
123  dump_object(empty, buf);
124  result = stringbuffer_finish(buf);
125  fail_unless(result && strcmp(result, expect) == 0, "dump_object was \"%s\" instead of \"%s\"!", result, expect);
126  free(result);
127 }
128 END_TEST
129 
130 Suite *loader_suite(void) {
131  Suite *s = suite_create("loader");
132  TCase *tc_core = tcase_create("Core");
133 
134  /*setup and teardown will be called before each test in testcase 'tc_core' */
135  tcase_add_checked_fixture(tc_core, setup, teardown);
136 
137  suite_add_tcase(s, tc_core);
138  tcase_add_test(tc_core, test_get_ob_diff);
139  tcase_add_test(tc_core, test_dump_object);
140 
141  return s;
142 }
143 
144 int main(void) {
145  int nf;
146  Suite *s = loader_suite();
147  SRunner *sr = srunner_create(s);
148 
149  srunner_set_xml(sr, LOGDIR "/unit/common/loader.xml");
150  srunner_set_log(sr, LOGDIR "/unit/common/loader.out");
151  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
152  nf = srunner_ntests_failed(sr);
153  srunner_free(sr);
154  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
155 }
archetype * find_archetype(const char *name)
Definition: arch.c:700
void teardown(void)
Definition: check_loader.c:47
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:64
object clone
Definition: object.h:326
double expmul
Definition: object.h:246
Definition: object.h:321
sint16 hp
Definition: living.h:81
EXTERN archetype * empty_archetype
Definition: global.h:227
float speed_left
Definition: object.h:182
sint8 Wis
Definition: living.h:78
void setup(void)
Definition: check_loader.c:41
void dump_object(object *op, StringBuffer *sb)
Definition: object.c:372
sint16 dam
Definition: living.h:87
const char * name
Definition: object.h:167
struct obj * env
Definition: object.h:151
START_TEST(test_get_ob_diff)
Definition: check_loader.c:51
float speed
Definition: object.h:181
int main(void)
Definition: check_loader.c:144
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
void get_ob_diff(StringBuffer *sb, object *op, object *op2)
END_TEST Suite * loader_suite(void)
Definition: check_loader.c:130
struct obj * owner
Definition: object.h:228
#define FREE_AND_COPY(sv, nv)
Definition: global.h:288
tag_t count
Definition: object.h:157
living stats
Definition: object.h:219
void cctk_setlog(char *logfile)
struct obj * inv
Definition: object.h:148
struct obj * head
Definition: object.h:154
void cctk_init_std_archetypes(void)
void cctk_setdatadir(char *datadir)
struct obj * more
Definition: object.h:153
object * arch_to_object(archetype *at)
Definition: arch.c:576
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:78