Crossfire Server, Branch 1.12  R12190
check_arch.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_check_arch_c =
00003  *   "$Id: check_arch.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 common/arch.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 
00039 void setup(void) {
00040     cctk_setdatadir(BUILD_ROOT"lib");
00041     cctk_setlog(LOGDIR"/unit/common/arch.out");
00042     cctk_init_std_archetypes();
00043 }
00044 
00045 void teardown(void) {
00046     /* put any cleanup steps here, they will be run after each testcase */
00047 }
00048 
00049 START_TEST(test_find_archetype_by_object_name) {
00050     archetype *arch;
00051 
00052     arch = find_archetype_by_object_name("large city");
00053     fail_unless(arch != NULL, "Searching for an existing arch name (large city) should work");
00054     fail_unless(!strcmp(arch->clone.name, "large city"), "Searching for an existing arch name shoud have returned us large city but returned %s", arch->clone.name);
00055     arch = find_archetype_by_object_name("Cloak of Magic Resistance");
00056     fail_unless(arch != NULL, "Searching for an existing arch name (Cloak of Magic Resistance) should work");
00057     fail_unless(!strcmp(arch->clone.name, "Cloak of Magic Resistance"), "Searching for an existing arch name shoud have returned us Cloak of Magic Resistance but returned %s", arch->clone.name);
00058     arch = find_archetype_by_object_name("Cloak of Magic Resistanc");
00059     fail_unless(arch == NULL, "Searching for an inexistant arch name (Cloak of Magic Resistanc) should return NULL");
00060     arch = find_archetype_by_object_name("some really non existant archetype");
00061     fail_unless(arch == NULL, "Searching for an inexistant arch name (some really non existant archetype) should return NULL");
00062     arch = find_archetype_by_object_name("");
00063     fail_unless(arch == NULL, "Searching for empty arch name should return NULL");
00064     arch = find_archetype_by_object_name(NULL);
00065     fail_unless(arch == NULL, "Searching for NULL arch name should return NULL");
00066 }
00067 END_TEST
00068 
00069 START_TEST(test_find_archetype_by_object_type_name) {
00070     archetype *arch;
00071 
00072     arch = find_archetype_by_object_type_name(66, "large city");
00073     fail_unless(arch != NULL, "Searching for an existing arch name (large city) + type (66) should work");
00074     fail_unless(arch->clone.type == 66, "Requested type 66 but got %d", arch->clone.type);
00075     fail_unless(!strcmp(arch->clone.name, "large city"), "Searching for an existing arch name shoud have returned us large city but returned %s", arch->clone.name);
00076     arch = find_archetype_by_object_type_name(87, "Cloak of Magic Resistance");
00077     fail_unless(arch != NULL, "Searching for an existing arch name (Cloak of Magic Resistance) + type (87) should work");
00078     fail_unless(arch->clone.type == 87, "Requested type 87 but got %d", arch->clone.type);
00079     fail_unless(!strcmp(arch->clone.name, "Cloak of Magic Resistance"), "Searching for an existing arch name shoud have returned us Cloak of Magic Resistance but returned %s", arch->clone.name);
00080     arch = find_archetype_by_object_type_name(87, "Cloak of Magic Resistanc");
00081     fail_unless(arch == NULL, "Searching for an inexistant arch name (Cloak of Magic Resistanc) should return NULL");
00082     arch = find_archetype_by_object_type_name(88, "Cloak of Magic Resistance");
00083     fail_unless(arch == NULL, "Searching for an existing arch name (Cloak of Magic Resistance) but with wrong type (88) should return NULL");
00084 }
00085 END_TEST
00086 
00087 
00088 START_TEST(test_get_archetype_by_skill_name) {
00089     archetype *arch;
00090 
00091     arch = get_archetype_by_skill_name("alchemy", SKILL);
00092     fail_unless(arch != NULL, "Should be able to discover the alchemy skill");
00093     fail_unless(!strcmp(arch->name, "skill_alchemy"), "should have returned skill_alchemy but returned %s", arch->name);
00094     arch = get_archetype_by_skill_name("alchemy", SKILLSCROLL);
00095     fail_unless(arch != NULL, "Should be able to discover the scroll of alchemy skill or something similar");
00096     arch = get_archetype_by_skill_name("one handed weapons", -1);
00097     fail_unless(arch != NULL, "Should be able to discover something related to the 'one handed weapons' skill");
00098     arch = get_archetype_by_skill_name(NULL, -1);
00099     fail_unless(arch == NULL, "Asking for null skill should return null");
00100 }
00101 END_TEST
00102 
00103 START_TEST(test_get_archetype_by_type_subtype) {
00104     archetype *arch;
00105 
00106     arch = get_archetype_by_type_subtype(SKILL, SK_LITERACY);
00107     fail_unless(arch != NULL, "Should be able to find an arch of type SKILL, subtype SK_LITERACY");
00108     fail_unless(arch->clone.type == SKILL, "Arch of type SKILL, subtype SK_LITERACY shoud have type %d but has type %d", SKILL, arch->clone.type);
00109     fail_unless(arch->clone.subtype == SK_LITERACY, "Arch of type SKILL, subtype SK_LITERACY shoud have subtype %d but has subtype %d", SK_LITERACY, arch->clone.subtype);
00110     fail_unless(arch != NULL, "Should be able to find an arch of type quest, subtype SK_LITERACY");
00111     arch = get_archetype_by_type_subtype(SKILL, -1);
00112     fail_unless(arch != NULL, "Should be able to find an arch of type SKILL, no subtype");
00113     fail_unless(arch->clone.type == SKILL, "arch of type SKILL, no subtype should have type %d but has %d", SKILL, arch->clone.type);
00114     arch = get_archetype_by_type_subtype(-1, SK_LITERACY);
00115     fail_unless(arch != NULL, "Should be able to find an arch of type unknown, SK_LITERACY");
00116     fail_unless(arch->clone.subtype == SK_LITERACY, "arch of type unknown, subtype quest in progress shoud have subtype %d but has subtype %d", SK_LITERACY, arch->clone.subtype);
00117     arch = get_archetype_by_type_subtype(-1, -1);
00118     fail_unless(arch != NULL, "Should be able to find arch of type unknown, subtype unknown, despite this being useless");
00119     arch = get_archetype_by_type_subtype(OBJECT_TYPE_MAX+1, -1);
00120     if (arch != NULL)
00121         fail("Should be not able to find arch of inexistant type but got %p (%s)", arch, arch->name);
00122 }
00123 END_TEST
00124 
00125 
00126 START_TEST(test_create_archetype_by_object_name) {
00127     object *ob;
00128 
00129     ob = create_archetype_by_object_name("writing pen");
00130     fail_unless(ob != NULL, "Should never return null");
00131     fail_unless(strncmp(ob->name, ARCH_SINGULARITY, strlen(ARCH_SINGULARITY)), "Searching for writing pen should NOT have returned a singularity");
00132     fail_unless(!strncmp(ob->name, "writing pen", strlen(ob->name)), "Searching for writing pen should have returned something with same base name but returned '%s'", ob->name);
00133     ob = create_archetype_by_object_name("writing pen of hell raiser +3");
00134     fail_unless(ob != NULL, "Should never return null");
00135     fail_unless(strncmp(ob->name, ARCH_SINGULARITY, strlen(ARCH_SINGULARITY)), "Searching for writing pen of hell raiser +3 should NOT have returned a singularity");
00136     fail_unless(!strncmp(ob->name, "writing pen of hell raiser +3", strlen(ob->name)), "Searching for writing pen of hell raiser +3 should have returned something with same base name but returned %s", ob->name);
00137     ob = create_archetype_by_object_name("%*");
00138     fail_unless(ob != NULL, "Inexistent item shuold return a singularity");
00139     fail_unless(!strncmp(ob->name, ARCH_SINGULARITY, strlen(ARCH_SINGULARITY)), "Searching for %* should have returned a singularity");
00140     ob = create_archetype_by_object_name("");
00141     fail_unless(ob != NULL, "Inexistent item shuold return a singularity");
00142     fail_unless(!strncmp(ob->name, ARCH_SINGULARITY, strlen(ARCH_SINGULARITY)), "Searching for \"\" should have returned a singularity");
00143 }
00144 END_TEST
00145 
00146 START_TEST(test_init_archetypes) {
00147     /*init_archetypes is used by setup, just check it created the empty_archetype*/
00148     archetype *arch = find_archetype("empty_archetype");
00149 
00150     fail_unless(arch != NULL, "init_archetype should have an 'empty_archetype' loaded");
00151 }
00152 END_TEST
00153 
00154 START_TEST(test_clear_archetable) {
00155     clear_archetable(); /*should just not fail :p*/
00156 }
00157 END_TEST
00158 
00159 START_TEST(test_free_all_archs) {
00160     archetype *arch;
00161 
00162     free_all_archs();
00163     arch = find_archetype("empty_archetype");
00164     fail_unless(arch == NULL, "init_archetype should not have an 'empty_archetype' loaded after call to free_all_archs");
00165     init_archetypes();
00166     arch = find_archetype("empty_archetype");
00167     fail_unless(arch != NULL, "init_archetype should have an 'empty_archetype' loaded");
00168 }
00169 END_TEST
00170 
00171 START_TEST(test_get_archetype_struct) {
00172     archetype *arch = get_archetype_struct();
00173 
00174     fail_unless(arch != NULL, "get_archetype_struct should not return NULL");
00175     fail_unless(arch->name == NULL, "arch->name of get_archetype_struct should be inited to NULL");
00176     fail_unless(arch->head == NULL, "arch->head of get_archetype_struct should be inited to NULL");
00177     fail_unless(arch->next == NULL, "arch->next of get_archetype_struct should be inited to NULL");
00178     fail_unless(arch->more == NULL, "arch->more of get_archetype_struct should be inited to NULL");
00179     fail_unless(arch->clone.other_arch == NULL, "arch->clone.other_arch of get_archetype_struct should be inited to NULL");
00180     fail_unless(arch->clone.contr == NULL, "arch->clone.contr of get_archetype_struct should be inited to NULL");
00181     fail_unless(arch->clone.next == NULL, "arch->clone.next of get_archetype_struct should be inited to NULL");
00182     fail_unless(arch->clone.prev == NULL, "arch->clone.prev of get_archetype_struct should be inited to NULL");
00183     fail_unless(arch->clone.active_next == NULL, "arch->clone.active_next of get_archetype_struct should be inited to NULL");
00184     fail_unless(arch->clone.active_prev == NULL, "arch->clone.active_prev of get_archetype_struct should be inited to NULL");
00185     fail_unless(arch->clone.below == NULL, "arch->clone.below of get_archetype_struct should be inited to NULL");
00186     fail_unless(arch->clone.above == NULL, "arch->clone.above of get_archetype_struct should be inited to NULL");
00187     fail_unless(arch->clone.inv == NULL, "arch->clone.inv of get_archetype_struct should be inited to NULL");
00188     fail_unless(arch->clone.container == NULL, "arch->clone.container of get_archetype_struct should be inited to NULL");
00189     fail_unless(arch->clone.env == NULL, "arch->clone.env of get_archetype_struct should be inited to NULL");
00190     fail_unless(arch->clone.more == NULL, "arch->clone.more of get_archetype_struct should be inited to NULL");
00191     fail_unless(arch->clone.head == NULL, "arch->clone.head of get_archetype_struct should be inited to NULL");
00192     fail_unless(arch->clone.map == NULL, "arch->clone.map of get_archetype_struct should be inited to NULL");
00193 
00194     fail_unless(arch->clone.name == NULL, "arch->clone.name of get_archetype_struct should be inited to NULL");
00195     fail_unless(arch->clone.name_pl == NULL, "arch->clone.name_pl of get_archetype_struct should be inited to NULL");
00196     fail_unless(arch->clone.title == NULL, "arch->clone.title of get_archetype_struct should be inited to NULL");
00197     fail_unless(arch->clone.race == NULL, "arch->clone.race of get_archetype_struct should be inited to NULL");
00198     fail_unless(arch->clone.slaying == NULL, "arch->clone.slaying of get_archetype_struct should be inited to NULL");
00199     fail_unless(arch->clone.msg == NULL, "arch->clone.msg of get_archetype_struct should be inited to NULL");
00200     fail_unless(arch->clone.skill == NULL, "arch->clone.skill of get_archetype_struct should be inited to NULL");
00201     fail_unless(arch->clone.lore == NULL, "arch->clone.lore of get_archetype_struct should be inited to NULL");
00202 
00203     fail_unless(arch->clone.current_weapon == NULL, "arch->clone.current_weapon of get_archetype_struct should be inited to NULL");
00204     fail_unless(arch->clone.enemy == NULL, "arch->clone.enemy of get_archetype_struct should be inited to NULL");
00205     fail_unless(arch->clone.attacked_by == NULL, "arch->clone.attacked_by of get_archetype_struct should be inited to NULL");
00206     fail_unless(arch->clone.randomitems == NULL, "arch->clone.randomitems of get_archetype_struct should be inited to NULL");
00207     fail_unless(arch->clone.chosen_skill == NULL, "arch->clone.chosen_skill of get_archetype_struct should be inited to NULL");
00208     fail_unless(arch->clone.spellitem == NULL, "arch->clone.spellitem of get_archetype_struct should be inited to NULL");
00209     fail_unless(arch->clone.spell == NULL, "arch->clone.spell of get_archetype_struct should be inited to NULL");
00210     fail_unless(arch->clone.spellarg == NULL, "arch->clone.spellarg of get_archetype_struct should be inited to NULL");
00211     fail_unless(arch->clone.arch == arch, "arch->clone.arch of get_archetype_struct should be inited to arch");
00212     fail_unless(arch->clone.other_arch == NULL, "arch->clone.other_arch of get_archetype_struct should be inited to NULL");
00213     fail_unless(arch->clone.custom_name == NULL, "arch->clone.custom_name of get_archetype_struct should be inited to NULL");
00214     fail_unless(arch->clone.key_values == NULL, "arch->clone.key_values of get_archetype_struct should be inited to NULL");
00215 }
00216 END_TEST
00217 
00218 START_TEST(test_arch_to_object) {
00219     archetype *arch;
00220     object *obj;
00221 
00222     arch = find_archetype("empty_archetype");
00223     obj = arch_to_object(arch);
00224     fail_unless(obj != NULL, "instanciating an arch should not return null");
00225 }
00226 END_TEST
00227 
00228 START_TEST(test_create_singularity) {
00229     object *obj;
00230 
00231     obj = create_singularity("XYZABCD");
00232     fail_unless(obj != NULL, "create_singularity should not return null");
00233     fail_unless(strstr(obj->name, "XYZABCD") != NULL, "create_singularity(\"XYZABCD\") should put XYZABCD somewhere in singularity name");
00234 }
00235 END_TEST
00236 
00237 START_TEST(test_create_archetype) {
00238     object *obj;
00239 
00240     obj = create_archetype("empty_archetype");
00241     fail_unless(obj != NULL, "create_archetype(\"empty_archetype\") should not return null");
00242 }
00243 END_TEST
00244 
00245 START_TEST(test_find_archetype) {
00246     archetype *arch;
00247 
00248     arch = find_archetype("empty_archetype");
00249     fail_unless(arch != NULL, "find_archetype(\"empty_archetype\") should not return null");
00250     arch = find_archetype("elvenboots");
00251     fail_unless(arch != NULL, "find_archetype(\"elvenboots\") should not return null");
00252     arch = find_archetype("AA1234567890");
00253     fail_unless(arch == NULL, "find_archetype(\"AA1234567890\") should return null");
00254 }
00255 END_TEST
00256 
00257 START_TEST(test_object_create_arch) {
00258     archetype *arch;
00259     object *obj;
00260 
00261     arch = find_archetype("dark_palace_4");
00262     obj = object_create_arch(arch);
00263     fail_unless(obj != NULL, "Should be able to fully instanciate the dark_palace");
00264     fail_unless(obj->head == NULL, "The object is full, so we should have got it's head. So head should be null but was %p for object %p", obj->head, obj);
00265     fail_unless(obj->more != NULL, "The object is full and multisquare, so more should not return null");
00266 }
00267 END_TEST
00268 
00269 Suite *arch_suite(void) {
00270     Suite *s = suite_create("arch");
00271     TCase *tc_core = tcase_create("Core");
00272 
00273     /*setup and teardown will be called before each test in testcase 'tc_core' */
00274     tcase_add_checked_fixture(tc_core, setup, teardown);
00275 
00276     suite_add_tcase(s, tc_core);
00277     tcase_add_test(tc_core, test_find_archetype_by_object_name);
00278     tcase_add_test(tc_core, test_find_archetype_by_object_type_name);
00279     tcase_add_test(tc_core, test_get_archetype_by_skill_name);
00280     tcase_add_test(tc_core, test_get_archetype_by_type_subtype);
00281     tcase_add_test(tc_core, test_create_archetype_by_object_name);
00282     tcase_add_test(tc_core, test_init_archetypes);
00283     tcase_add_test(tc_core, test_clear_archetable);
00284     tcase_add_test(tc_core, test_free_all_archs);
00285     tcase_add_test(tc_core, test_get_archetype_struct);
00286     tcase_add_test(tc_core, test_arch_to_object);
00287     tcase_add_test(tc_core, test_create_singularity);
00288     tcase_add_test(tc_core, test_create_archetype);
00289     tcase_add_test(tc_core, test_find_archetype);
00290     tcase_add_test(tc_core, test_object_create_arch);
00291 
00292     return s;
00293 }
00294 
00295 int main(void) {
00296     int nf;
00297     Suite *s = arch_suite();
00298     SRunner *sr = srunner_create(s);
00299 
00300     srunner_set_xml(sr, LOGDIR "/unit/common/arch.xml");
00301     srunner_set_log(sr, LOGDIR "/unit/common/arch.out");
00302     srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
00303     nf = srunner_ntests_failed(sr);
00304     srunner_free(sr);
00305     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00306 }