Crossfire Server, Trunk  R20513
check_loader.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_check_loader_c =
3  * "$Id: check_loader.c 15379 2011-11-01 19:40:45Z ryo_saeba $";
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(BUILD_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) {
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_object_dump) {
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  object_dump(empty, buf);
105  result = stringbuffer_finish(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);
122  result = stringbuffer_finish(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 
132  /*setup and teardown will be called before each test in testcase 'tc_core' */
133  tcase_add_checked_fixture(tc_core, setup, teardown);
134 
135  suite_add_tcase(s, tc_core);
136  tcase_add_test(tc_core, test_get_ob_diff);
137  tcase_add_test(tc_core, test_object_dump);
138 
139  return s;
140 }
141 
142 int main(void) {
143  int nf;
144  Suite *s = loader_suite();
145  SRunner *sr = srunner_create(s);
146 
147  srunner_set_xml(sr, LOGDIR "/unit/common/loader.xml");
148  srunner_set_log(sr, LOGDIR "/unit/common/loader.out");
149  srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
150  nf = srunner_ntests_failed(sr);
151  srunner_free(sr);
152  return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
153 }
archetype * find_archetype(const char *name)
Finds, using the hashtable, which archetype matches the given name.
Definition: arch.c:695
void cctk_setdatadir(const char *datadir)
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.c:57
object clone
An object from which to do object_copy()
Definition: object.h:470
static END_TEST Suite * loader_suite(void)
Definition: check_loader.c:128
double expmul
needed experience = (calc_exp*expmul) - means some races/classes can need less/more exp to gain level...
Definition: object.h:395
Global type definitions and header inclusions.
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:465
int16_t hp
Hit Points.
Definition: living.h:39
void object_dump(const object *op, StringBuffer *sb)
Dumps an object.
Definition: object.c:394
EXTERN archetype * empty_archetype
Nice to have fast access to it.
Definition: global.h:151
float speed_left
How much speed is left to spend this round.
Definition: object.h:329
Defines for loader.l / loader.c.
int8_t Wis
Definition: living.h:35
#define snprintf
Definition: win32.h:46
static void teardown(void)
Definition: check_loader.c:47
int16_t dam
How much damage this object does when hitting.
Definition: living.h:45
const char * name
The name of the object, obviously...
Definition: object.h:311
struct obj * env
Pointer to the object which is the environment.
Definition: object.h:293
START_TEST(test_get_ob_diff)
Definition: check_loader.c:51
void cctk_setlog(const char *logfile)
set the logdir to use
float speed
The overall speed of this object.
Definition: object.h:328
int main(void)
Definition: check_loader.c:142
Implements a general string buffer: it builds a string by concatenating.
Object structure, the core of Crossfire.
#define FREE_AND_COPY(sv, nv)
Release the shared string if not NULL, and make it a reference to nv.
Definition: global.h:213
tag_t count
Unique object number for this object.
Definition: object.h:299
living stats
Str, Con, Dex, etc.
Definition: object.h:368
static void setup(void)
Definition: check_loader.c:41
struct obj * inv
Pointer to the first object in the inventory.
Definition: object.h:290
struct obj * head
Points to the main object of a large body.
Definition: object.h:296
void cctk_init_std_archetypes(void)
Loads up to archetype initialisation using standard crossfire files in source tree.
A buffer that will be expanded as content is added to it.
Definition: stringbuffer.c:25
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Returns a pointer to a static string which contains all variables which are different in the two give...
Definition: object.c:4845
struct obj * more
Pointer to the rest of a large body of objects.
Definition: object.h:295
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.c:571
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.c:76