| 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 |
#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 |
|
| 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 |
|
| 93 |
StringBuffer *buf; |
| 94 |
object *empty; |
| 95 |
char *result; |
| 96 |
char expect[10000]; |
| 97 |
|
| 98 |
|
| 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 |
|
| 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 |
|
| 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); |
| 150 |
nf = srunner_ntests_failed(sr); |
| 151 |
srunner_free(sr); |
| 152 |
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 153 |
} |