Crossfire Server, Branch 1.12  R12190
check_loader.c
Go to the documentation of this file.
00001 /*
00002  * static char *rcsid_check_loader_c =
00003  *   "$Id: check_loader.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/loader.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 <object.h>
00039 #include <stringbuffer.h>
00040 
00041 void setup(void) {
00042     cctk_setdatadir(BUILD_ROOT "lib");
00043     cctk_setlog(LOGDIR "/unit/common/loader.out");
00044     cctk_init_std_archetypes();
00045 }
00046 
00047 void teardown(void) {
00048     /* put any cleanup steps here, they will be run after each testcase */
00049 }
00050 
00051 START_TEST(test_get_ob_diff) {
00052     StringBuffer *buf;
00053     object *orc;
00054     archetype *arch;
00055     char *result;
00056 
00057     arch = find_archetype("orc");
00058     fail_unless(arch != NULL, "Can't find 'orc' archetype!");
00059     orc = arch_to_object(arch);
00060     fail_unless(orc != NULL, "Couldn't create first orc!");
00061 
00062     buf = stringbuffer_new();
00063     get_ob_diff(buf, orc, &arch->clone);
00064     result = stringbuffer_finish(buf);
00065     fail_unless(result && result[0] == '\0', "diff obj/clone was %s!", result);
00066     free(result);
00067 
00068     orc->speed = 0.5;
00069     orc->speed_left = arch->clone.speed_left;
00070     FREE_AND_COPY(orc->name, "Orc chief");
00071 
00072     buf = stringbuffer_new();
00073     get_ob_diff(buf, orc, &arch->clone);
00074     result = stringbuffer_finish(buf);
00075     fail_unless(result && strcmp(result, "name Orc chief\nspeed 0.500000\n") == 0, "diff modified obj/clone was %s!", result);
00076     free(result);
00077 
00078     orc->stats.hp = 50;
00079     orc->stats.Wis = 59;
00080     orc->expmul = 8.5;
00081     orc->stats.dam = 168;
00082 
00083     buf = stringbuffer_new();
00084     get_ob_diff(buf, orc, &arch->clone);
00085     result = stringbuffer_finish(buf);
00086     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);
00087     free(result);
00088 }
00089 END_TEST
00090 
00091 START_TEST(test_dump_object) {
00093     StringBuffer *buf;
00094     object *empty;
00095     char *result;
00096     char expect[10000];
00097 
00098     /* Basic */
00099     empty = arch_to_object(empty_archetype);
00100     fail_unless(empty != NULL, "Couldn't create empty archetype!");
00101 
00102     snprintf(expect, sizeof(expect), "arch empty_archetype\nend\n");
00103     buf = stringbuffer_new();
00104     dump_object(empty, buf);
00105     result = stringbuffer_finish(buf);
00106     fail_unless(result && strcmp(result, expect) == 0, "dump_object was \"%s\" instead of \"%s\"!", result, expect);
00107     free(result);
00108 
00109     /* With more things */
00110     empty->head = arch_to_object(empty_archetype);
00111     empty->inv = arch_to_object(empty_archetype);
00112     empty->more = arch_to_object(empty_archetype);
00113     empty->owner = arch_to_object(empty_archetype);
00114     empty->env = arch_to_object(empty_archetype);
00115     fail_unless(empty->head != NULL, "Couldn't create empty archetype as head!");
00116     fail_unless(empty->inv != NULL, "Couldn't create empty archetype as inv!");
00117     fail_unless(empty->more != NULL, "Couldn't create empty archetype as more!");
00118     fail_unless(empty->owner != NULL, "Couldn't create empty archetype as owner!");
00119     fail_unless(empty->env != NULL, "Couldn't create empty archetype as env!");
00120 
00121     snprintf(expect, sizeof(expect), "arch empty_archetype\nmore %d\nhead %d\nenv %d\ninv %d\nowner %d\nend\n", empty->more->count, empty->head->count, empty->env->count, empty->inv->count, empty->owner->count);
00122     buf = stringbuffer_new();
00123     dump_object(empty, buf);
00124     result = stringbuffer_finish(buf);
00125     fail_unless(result && strcmp(result, expect) == 0, "dump_object was \"%s\" instead of \"%s\"!", result, expect);
00126     free(result);
00127 }
00128 END_TEST
00129 
00130 Suite *loader_suite(void) {
00131     Suite *s = suite_create("loader");
00132     TCase *tc_core = tcase_create("Core");
00133 
00134     /*setup and teardown will be called before each test in testcase 'tc_core' */
00135     tcase_add_checked_fixture(tc_core, setup, teardown);
00136 
00137     suite_add_tcase(s, tc_core);
00138     tcase_add_test(tc_core, test_get_ob_diff);
00139     tcase_add_test(tc_core, test_dump_object);
00140 
00141     return s;
00142 }
00143 
00144 int main(void) {
00145     int nf;
00146     Suite *s = loader_suite();
00147     SRunner *sr = srunner_create(s);
00148 
00149     srunner_set_xml(sr, LOGDIR "/unit/common/loader.xml");
00150     srunner_set_log(sr, LOGDIR "/unit/common/loader.out");
00151     srunner_run_all(sr, CK_ENV); /*verbosity from env variable*/
00152     nf = srunner_ntests_failed(sr);
00153     srunner_free(sr);
00154     return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
00155 }