Crossfire Server, Branch 1.12  R12190
check_c_object.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_check_c_object_c =
00003  *   "$Id: check_c_object.c 11578 2009-02-23 22:02:27Z lalo $";
00004  */
00005 
00006 /*
00007  * CrossFire, A Multiplayer game for X-windows
00008  *
00009  * Copyright (C) 2002 Mark Wedel & Crossfire Development Team
00010  * Copyright (C) 1992 Frank Tore Johansen
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  *
00026  * The authors can be reached via e-mail at crossfire-devel@real-time.com
00027  */
00028 
00029 /*
00030  * This is the unit tests file for server/c_object.c
00031  */
00032 
00033 #include <global.h>
00034 #include <stdlib.h>
00035 #include <check.h>
00036 #include <loader.h>
00037 #include <toolkit_common.h>
00038 #include <sproto.h>
00039 
00040 void setup(void) {
00041 }
00042 
00043 void teardown(void) {
00044     /* put any cleanup steps here, they will be run after each testcase */
00045 }
00046 
00047 static object *find_best_apply_object_match(object *start, object *pl, const char *params, int aflag) {
00048     object *tmp, *best = NULL;
00049     int match_val = 0, tmpmatch;
00050 
00051     for (tmp = start; tmp; tmp = tmp->below) {
00052         if (tmp->invisible)
00053             continue;
00054         if ((tmpmatch = item_matched_string(pl, tmp, params)) > match_val) {
00055             if ((aflag == AP_APPLY) && (QUERY_FLAG(tmp, FLAG_APPLIED)))
00056                 continue;
00057             if ((aflag == AP_UNAPPLY) && (!QUERY_FLAG(tmp, FLAG_APPLIED)))
00058                 continue;
00059             match_val = tmpmatch;
00060             best = tmp;
00061         }
00062     }
00063     return best;
00064 }
00065 
00066 START_TEST(test_find_best_apply_object_match) {
00067     object *pl, *found;
00068     object *gorokh, *cloak, *other;
00069 
00070     pl = create_archetype("kobold");
00071     fail_unless(pl != NULL, "can't find kobold archetype.");
00072 
00073     gorokh = create_archetype("cloak");
00074     gorokh->title = add_string("of Gorokh");
00075     CLEAR_FLAG(gorokh, FLAG_IDENTIFIED);
00076     insert_ob_in_ob(gorokh, pl);
00077 
00078     cloak = create_archetype("cloak");
00079     insert_ob_in_ob(cloak, pl);
00080 
00081     other = create_archetype("gem");
00082     insert_ob_in_ob(other, pl);
00083 
00084     found = find_best_apply_object_match(pl->inv, pl, "all", 0);
00085     fail_unless(found == other, "not found gem but %s", found ? found->name : "nothing");
00086 
00087     found = find_best_apply_object_match(pl->inv, pl, "cloak", 0);
00088     fail_unless(found == cloak, "didn't find cloak but %s", found ? found->name : "nothing");
00089 
00090     found = find_best_apply_object_match(pl->inv, pl, "Gorokh", 0);
00091     fail_unless(found == NULL, "Gorokh found %s instead of nothing", found ? found->name : "nothing??");
00092 }
00093 END_TEST
00094 
00095 START_TEST(test_put_object_in_sack) {
00096     mapstruct *test_map;
00097     object *sack, *obj, *sack2, *dummy;
00098 
00099     dummy = create_archetype("orc");
00100 
00101     test_map = get_empty_map(5, 5);
00102     fail_unless(test_map != NULL, "can't create test map");
00103 
00104     sack = create_archetype("gem");
00105     insert_ob_in_map_at(sack, test_map, NULL, 0, 0, 0);
00106     fail_unless(GET_MAP_OB(test_map, 0, 0) == sack);
00107 
00108     obj = create_archetype("gem");
00109     obj->nrof = 1;
00110     insert_ob_in_map_at(obj, test_map, NULL, 0, 1, 0);
00111     put_object_in_sack(dummy, sack, obj, 1);
00112     fail_unless(GET_MAP_OB(test_map, 1, 0) == obj, "object was removed from map?");
00113     fail_unless(sack->inv == NULL, "sack's inventory isn't null?");
00114 
00115     remove_ob(sack);
00116     free_object(sack);
00117 
00118     /* basic insertion */
00119     sack = create_archetype("sack");
00120     sack->nrof = 1;
00121     fail_unless(sack->type == CONTAINER, "sack isn't a container?");
00122     insert_ob_in_map_at(sack, test_map, NULL, 0, 0, 0);
00123     fail_unless(GET_MAP_OB(test_map, 0, 0) == sack, "sack not put on map?");
00124 
00125     SET_FLAG(sack, FLAG_APPLIED);
00126     put_object_in_sack(dummy, sack, obj, 1);
00127     fail_unless(sack->inv == obj, "object not inserted into sack?");
00128     fail_unless(GET_MAP_OB(test_map, 1, 0) == NULL, "object wasn't removed from map?");
00129 
00130     remove_ob(obj);
00131     insert_ob_in_map_at(obj, test_map, NULL, 0, 1, 0);
00132     sack->weight_limit = 1;
00133     obj->weight = 5;
00134 
00135     put_object_in_sack(dummy, sack, obj, 1);
00136     fail_unless(sack->inv == NULL, "item was put in sack even if too heavy?");
00137     fail_unless(GET_MAP_OB(test_map, 1, 0) == obj, "object was removed from map?");
00138 
00139     /* now for sack splitting */
00140     sack->nrof = 2;
00141     obj->weight = 1;
00142 
00143     put_object_in_sack(dummy, sack, obj, 1);
00144     fail_unless(sack->nrof == 1, "sack wasn't split?");
00145     fail_unless(sack->above != NULL, "no new sack created?");
00146     fail_unless(sack->inv == obj, "object not inserted in old sack?");
00147     fail_unless(sack == obj->env, "object's env not updated?");
00148 
00149     /* now moving to/from containers */
00150     obj->nrof = 2;
00151     sack2 = sack->above;
00152     SET_FLAG(sack2, FLAG_APPLIED);
00153     dummy->container = sack;
00154     put_object_in_sack(dummy, sack, sack2, 1);
00155     fail_unless(sack2->inv == NULL, "sack2's not empty?");
00156     fail_unless(sack->inv == obj, "obj wasn't transferred?");
00157 
00158     /* move between containers and split containers */
00159     remove_ob(sack2);
00160     insert_ob_in_map_at(sack2, test_map, NULL, 0, 2, 0);
00161     SET_FLAG(sack2, FLAG_APPLIED);
00162     sack2->nrof = 2;
00163     dummy->container = sack2;
00164     put_object_in_sack(dummy, sack2, sack, 0);
00165     fail_unless(sack->inv == NULL, "sack wasn't put into sack2?");
00166     fail_unless(sack2->inv != NULL, "sack2 wasn't filled?");
00167     fail_unless(sack2->above != NULL, "sack2 wasn't split?");
00168     fail_unless(sack2->above->inv == NULL, "sack2's split was filled?");
00169 
00170     free_map(test_map);
00171 }
00172 END_TEST
00173 
00174 Suite *c_object_suite(void) {
00175     Suite *s = suite_create("c_object");
00176     TCase *tc_core = tcase_create("Core");
00177 
00178     /*setup and teardown will be called before each test in testcase 'tc_core' */
00179     tcase_add_checked_fixture(tc_core, setup, teardown);
00180 
00181     suite_add_tcase(s, tc_core);
00182     tcase_add_test(tc_core, test_find_best_apply_object_match);
00183     tcase_add_test(tc_core, test_put_object_in_sack);
00184 
00185     return s;
00186 }
00187 
00188 int main(void) {
00189     int nf;
00190     Suite *s = c_object_suite();
00191     SRunner *sr = srunner_create(s);
00192 
00193     settings.debug = 0;
00194     settings.logfilename = "c_object.out";
00195     init(0, NULL);
00196 
00197     /* srunner_set_fork_status (sr, CK_NOFORK); */
00198 
00199     srunner_set_xml(sr, LOGDIR "/unit/server/c_object.xml");
00200     srunner_set_log(sr, LOGDIR "/unit/server/c_object.out");
00201     srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
00202     nf = srunner_ntests_failed(sr);
00203     srunner_free(sr);
00204     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00205 }