File: check_c_object.c

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (205 lines) | stat: -rw-r--r-- 6,857 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * static char *rcsid_check_c_object_c =
 *   "$Id$";
 */

/*
 * CrossFire, A Multiplayer game for X-windows
 *
 * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
 * Copyright (C) 1992 Frank Tore Johansen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * The authors can be reached via e-mail at crossfire-devel@real-time.com
 */

/*
 * This is the unit tests file for server/c_object.c
 */

#include <global.h>
#include <stdlib.h>
#include <check.h>
#include <loader.h>
#include <toolkit_common.h>
#include <sproto.h>

static void setup(void) {
}

static void teardown(void) {
    /* put any cleanup steps here, they will be run after each testcase */
}

static object *find_best_apply_object_match(object *start, object *pl, const char *params, int aflag) {
    object *tmp, *best = NULL;
    int match_val = 0, tmpmatch;

    for (tmp = start; tmp; tmp = tmp->below) {
        if (tmp->invisible)
            continue;
        if ((tmpmatch = object_matches_string(pl, tmp, params)) > match_val) {
            if ((aflag == AP_APPLY) && (QUERY_FLAG(tmp, FLAG_APPLIED)))
                continue;
            if ((aflag == AP_UNAPPLY) && (!QUERY_FLAG(tmp, FLAG_APPLIED)))
                continue;
            match_val = tmpmatch;
            best = tmp;
        }
    }
    return best;
}

START_TEST(test_find_best_apply_object_match) {
    object *pl, *found;
    object *gorokh, *cloak, *other;

    pl = create_archetype("kobold");
    fail_unless(pl != NULL, "can't find kobold archetype.");

    gorokh = create_archetype("cloak");
    gorokh->title = add_string("of Gorokh");
    CLEAR_FLAG(gorokh, FLAG_IDENTIFIED);
    object_insert_in_ob(gorokh, pl);

    cloak = create_archetype("cloak");
    object_insert_in_ob(cloak, pl);

    other = create_archetype("gem");
    object_insert_in_ob(other, pl);

    found = find_best_apply_object_match(pl->inv, pl, "all", 0);
    fail_unless(found == other, "not found gem but %s", found ? found->name : "nothing");

    found = find_best_apply_object_match(pl->inv, pl, "cloak", 0);
    fail_unless(found == cloak, "didn't find cloak but %s", found ? found->name : "nothing");

    found = find_best_apply_object_match(pl->inv, pl, "Gorokh", 0);
    fail_unless(found == NULL, "Gorokh found %s instead of nothing", found ? found->name : "nothing??");
}
END_TEST

START_TEST(test_put_object_in_sack) {
    mapstruct *test_map;
    object *sack, *obj, *sack2, *dummy;

    dummy = create_archetype("orc");

    test_map = get_empty_map(5, 5);
    fail_unless(test_map != NULL, "can't create test map");

    sack = create_archetype("gem");
    object_insert_in_map_at(sack, test_map, NULL, 0, 0, 0);
    fail_unless(GET_MAP_OB(test_map, 0, 0) == sack);

    obj = create_archetype("gem");
    obj->nrof = 1;
    object_insert_in_map_at(obj, test_map, NULL, 0, 1, 0);
    put_object_in_sack(dummy, sack, obj, 1);
    fail_unless(GET_MAP_OB(test_map, 1, 0) == obj, "object was removed from map?");
    fail_unless(sack->inv == NULL, "sack's inventory isn't null?");

    object_remove(sack);
    object_free_drop_inventory(sack);

    /* basic insertion */
    sack = create_archetype("sack");
    sack->nrof = 1;
    fail_unless(sack->type == CONTAINER, "sack isn't a container?");
    object_insert_in_map_at(sack, test_map, NULL, 0, 0, 0);
    fail_unless(GET_MAP_OB(test_map, 0, 0) == sack, "sack not put on map?");

    SET_FLAG(sack, FLAG_APPLIED);
    put_object_in_sack(dummy, sack, obj, 1);
    fail_unless(sack->inv == obj, "object not inserted into sack?");
    fail_unless(GET_MAP_OB(test_map, 1, 0) == NULL, "object wasn't removed from map?");

    object_remove(obj);
    object_insert_in_map_at(obj, test_map, NULL, 0, 1, 0);
    sack->weight_limit = 1;
    obj->weight = 5;

    put_object_in_sack(dummy, sack, obj, 1);
    fail_unless(sack->inv == NULL, "item was put in sack even if too heavy?");
    fail_unless(GET_MAP_OB(test_map, 1, 0) == obj, "object was removed from map?");

    /* now for sack splitting */
    sack->nrof = 2;
    obj->weight = 1;

    put_object_in_sack(dummy, sack, obj, 1);
    fail_unless(sack->nrof == 1, "sack wasn't split?");
    fail_unless(sack->above != NULL, "no new sack created?");
    fail_unless(sack->inv == obj, "object not inserted in old sack?");
    fail_unless(sack == obj->env, "object's env not updated?");

    /* now moving to/from containers */
    obj->nrof = 2;
    sack2 = sack->above;
    SET_FLAG(sack2, FLAG_APPLIED);
    dummy->container = sack;
    put_object_in_sack(dummy, sack, sack2, 1);
    fail_unless(sack2->inv == NULL, "sack2's not empty?");
    fail_unless(sack->inv == obj, "obj wasn't transferred?");

    /* move between containers and split containers */
    object_remove(sack2);
    object_insert_in_map_at(sack2, test_map, NULL, 0, 2, 0);
    SET_FLAG(sack2, FLAG_APPLIED);
    sack2->nrof = 2;
    dummy->container = sack2;
    put_object_in_sack(dummy, sack2, sack, 0);
    fail_unless(sack->inv == NULL, "sack wasn't put into sack2?");
    fail_unless(sack2->inv != NULL, "sack2 wasn't filled?");
    fail_unless(sack2->above != NULL, "sack2 wasn't split?");
    fail_unless(sack2->above->inv == NULL, "sack2's split was filled?");

    free_map(test_map);
}
END_TEST

static Suite *c_object_suite(void) {
    Suite *s = suite_create("c_object");
    TCase *tc_core = tcase_create("Core");

    /*setup and teardown will be called before each test in testcase 'tc_core' */
    tcase_add_checked_fixture(tc_core, setup, teardown);

    suite_add_tcase(s, tc_core);
    tcase_add_test(tc_core, test_find_best_apply_object_match);
    tcase_add_test(tc_core, test_put_object_in_sack);

    return s;
}

int main(void) {
    int nf;
    Suite *s = c_object_suite();
    SRunner *sr = srunner_create(s);

    settings.debug = 0;
    settings.logfilename = "c_object.out";
    init(0, NULL);

    /* srunner_set_fork_status (sr, CK_NOFORK); */

    srunner_set_xml(sr, LOGDIR "/unit/server/c_object.xml");
    srunner_set_log(sr, LOGDIR "/unit/server/c_object.out");
    srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
    nf = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}