Debian Codegraph @

test/unit/common/check_loader.c

raw symbols SCIP
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 [email protected]
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");
44 cctk_init_std_archetypes();
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) {
92 /** we only test specific things like env/more/head/..., the rest is in test_get_ob_diff(). */
93 StringBuffer *buf;
94 object *empty;
95 char *result;
96 char expect[10000];
97
98 /* Basic */
99 empty = arch_to_object(empty_archetype);
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 */
110 empty->head = arch_to_object(empty_archetype);
111 empty->inv = arch_to_object(empty_archetype);
112 empty->more = arch_to_object(empty_archetype);
113 empty->env = arch_to_object(empty_archetype);
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 }