1 /* object/util */
2
3 #include "unit-test.h"
4 #include "unit-test-data.h"
5
6 #include "object.h"
7 #include "obj-make.h"
8 #include "obj-pile.h"
9 #include "obj-util.h"
10
setup_tests(void ** state)11 int setup_tests(void **state) {
12 player = &test_player;
13 player->body = test_player_body;
14 player->body.slots = &test_slot_light;
15 z_info = mem_zalloc(sizeof(struct angband_constants));
16 z_info->fuel_torch = 5000;
17 z_info->fuel_lamp = 15000;
18 z_info->default_lamp = 7500;
19
20 quarks_init();
21 return 0;
22 }
23
teardown_tests(void * state)24 int teardown_tests(void *state) {
25 quarks_free();
26 mem_free(z_info);
27 return 0;
28 }
29
30 /* Regression test for #1661 */
test_obj_can_refill(void * state)31 int test_obj_can_refill(void *state) {
32 struct object obj_torch, obj_lantern, obj_candidate;
33
34 /* Torches cannot be refilled */
35 object_prep(&obj_torch, &test_torch, 1, AVERAGE);
36 player->gear = &obj_torch;
37 player->body.slots->obj = &obj_torch;
38 eq(obj_can_refill(&obj_torch), false);
39
40 /* Lanterns can be refilled */
41 object_prep(&obj_lantern, &test_lantern, 1, AVERAGE);
42 player->gear = &obj_lantern;
43 player->body.slots->obj = &obj_lantern;
44
45 /* Not by torches */
46 eq(obj_can_refill(&obj_torch), false);
47
48 /* Lanterns can be refilled by other lanterns */
49 eq(obj_can_refill(&obj_lantern), true);
50
51 /* ...but not by empty lanterns */
52 obj_lantern.timeout = 0;
53 eq(obj_can_refill(&obj_lantern), false);
54
55 /* Lanterns can be refilled by flasks of oil */
56 object_prep(&obj_candidate, &test_flask, 1, AVERAGE);
57 eq(obj_can_refill(&obj_candidate), true);
58
59 /* Lanterns cannot be refilled by charging rods of treasure detection */
60 object_prep(&obj_candidate, &test_rod_treasure_location, 1, AVERAGE);
61 obj_candidate.timeout = 50;
62 eq(obj_can_refill(&obj_candidate), false);
63 ok;
64 }
65
66 /* Test basic functionality of check_for_inscrip_with_int(). */
test_basic_check_for_inscrip_with_int(void * state)67 int test_basic_check_for_inscrip_with_int(void *state) {
68 struct object obj;
69 int dummy = 8974;
70 int inarg;
71
72 /* No inscription should return zero and leave inarg unchanged. */
73 memset(&obj, 0, sizeof(obj));
74 inarg = dummy;
75 eq(check_for_inscrip_with_int(&obj, "=g", &inarg), 0);
76 eq(inarg, dummy);
77
78 /* An inscription not containing the search string should return zero and
79 * leave inarg unchanged. */
80 obj.note = quark_add("@m1@b1@G1");
81 inarg = dummy;
82 eq(check_for_inscrip_with_int(&obj, "=g", &inarg), 0);
83 eq(inarg, dummy);
84
85 /* An inscription containing the search string but not followed by an
86 * integer should return zero and leave inarg unchanged. */
87 obj.note = quark_add("=g@m1@b1@G1");
88 inarg = dummy;
89 eq(check_for_inscrip_with_int(&obj, "=g", &inarg), 0);
90 eq(inarg, dummy);
91
92 /* An inscription containing one instance of the search string followed
93 * by a nonnegative integer should return one and set inarg to the value
94 * of the integer. */
95 obj.note = quark_add("=g5@m1@b1@G1");
96 inarg = dummy;
97 eq(check_for_inscrip_with_int(&obj, "=g", &inarg), 1);
98 eq(inarg, 5);
99
100 /* An inscription containing two instances of the search string followed
101 * by a nonnegative integer should return two and set inarg to the value
102 * of the integer following the first instance. */
103 obj.note = quark_add("@m1@b1=g8@G1=g5");
104 inarg = dummy;
105 eq(check_for_inscrip_with_int(&obj, "=g", &inarg), 2);
106 eq(inarg, 8);
107
108 ok;
109 }
110
111 const char *suite_name = "object/util";
112 struct test tests[] = {
113 { "obj_can_refill", test_obj_can_refill },
114 { "basic_check_for_inscrip_with_uint", test_basic_check_for_inscrip_with_int },
115 { NULL, NULL }
116 };
117