UFO: Alien Invasion
test_inventory.cpp
Go to the documentation of this file.
1
5/*
6Copyright (C) 2002-2022 UFO: Alien Invasion.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25
26#include "test_shared.h"
27#include "../common/common.h"
28#include "../game/inventory.h"
29
31static const int TAG_INVENTORY = 5546;
32
33static void FreeInventory (void* data)
34{
36}
37
38static void* AllocInventoryMemory (size_t size)
39{
41}
42
43static void FreeAllInventory (void)
44{
46}
47
49
50static inline void ResetInventoryList (void)
51{
54}
55
56class InventoryTest: public ::testing::Test {
57protected:
58 static void SetUpTestCase() {
59 TEST_Init();
60 Com_ParseScripts(true);
61 }
62
63 static void TearDownTestCase() {
66 }
67
68 void SetUp() {
70 }
71};
72
74{
75 Inventory inv;
76 const objDef_t* od;
77 const invDef_t* container;
78
79 od = INVSH_GetItemByIDSilent("assault");
80 ASSERT_TRUE(nullptr != od);
81
82 container = INVSH_GetInventoryDefinitionByID("right");
83 ASSERT_TRUE(nullptr != container);
84
85 Item item(od);
86
87 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
88
89 ASSERT_TRUE(nullptr != i.addToInventory(&inv, &item, container, NONE, NONE, 1));
90
91 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
92}
93
95{
96 Inventory inv;
97 const objDef_t* od;
98 const invDef_t* container;
99 Item* addedItem;
100
101 od = INVSH_GetItemByIDSilent("assault");
102 ASSERT_TRUE(nullptr != od);
103
104 container = INVSH_GetInventoryDefinitionByID("right");
105 ASSERT_TRUE(nullptr != container);
106
107 Item item(od);
108
109 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
110
111 addedItem = i.addToInventory(&inv, &item, container, NONE, NONE, 1);
112
113 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
114
115 ASSERT_TRUE(i.removeFromInventory(&inv, container, addedItem));
116
117 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
118}
119
121{
122 Inventory inv;
123 const objDef_t* od;
124 const invDef_t* container, *containerTo;
125 Item* addedItem;
126
127 od = INVSH_GetItemByIDSilent("assault");
128 ASSERT_TRUE(nullptr != od);
129
130 container = INVSH_GetInventoryDefinitionByID("right");
131 ASSERT_TRUE(nullptr != container);
132
133 Item item(od);
134
135 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
136
137 addedItem = i.addToInventory(&inv, &item, container, NONE, NONE, 1);
138 ASSERT_TRUE(nullptr != addedItem);
139
140 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
141
142 containerTo = INVSH_GetInventoryDefinitionByID("backpack");
143 ASSERT_TRUE(nullptr != containerTo);
144
145 ASSERT_EQ(IA_MOVE, i.moveInInventory(&inv, container, addedItem, containerTo, NONE, NONE, nullptr, nullptr));
146
147 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
148 ASSERT_TRUE(inv.containsItem(containerTo->id, &item) == true);
149}
150
152{
153 Inventory inv;
154 const objDef_t* od, *ad;
155 const invDef_t* container, *containerFrom;
156 Item* addedItem;
157
158 od = INVSH_GetItemByIDSilent("rpg");
159 ASSERT_TRUE(nullptr != od);
160
161 container = INVSH_GetInventoryDefinitionByID("right");
162 ASSERT_TRUE(nullptr != container);
163
164 Item item(od);
165
166 ASSERT_TRUE(inv.containsItem(container->id, &item) == false);
167
168 addedItem = i.addToInventory(&inv, &item, container, NONE, NONE, 1);
169 ASSERT_TRUE(nullptr != addedItem);
170
171 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
172
173 ad = INVSH_GetItemByIDSilent("rpg_ammo");
174 ASSERT_TRUE(nullptr != ad);
175
176 Item ammo(ad);
177
178 containerFrom = INVSH_GetInventoryDefinitionByID("backpack");
179 ASSERT_TRUE(nullptr != containerFrom);
180
181 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammo) == false);
182
183 addedItem = i.addToInventory(&inv, &ammo, containerFrom, NONE, NONE, 1);
184 ASSERT_TRUE(nullptr != addedItem);
185
186 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammo) == true);
187
188 ASSERT_EQ(IA_RELOAD, i.moveInInventory(&inv, containerFrom, addedItem, container, NONE, NONE, nullptr, nullptr));
189
190 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammo) == false);
191
192 item.setAmmoDef(ad);
193 item.setAmmoLeft(1);
194
195 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
196
197 ad = INVSH_GetItemByIDSilent("rpg_incendiary_ammo");
198 ASSERT_TRUE(nullptr != ad);
199
200 Item ammoFrom(ad);
201
202 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammoFrom) == false);
203
204 addedItem = i.addToInventory(&inv, &ammoFrom, containerFrom, NONE, NONE, 1);
205 ASSERT_TRUE(nullptr != addedItem);
206
207 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammoFrom) == true);
208
209 ASSERT_EQ(IA_RELOAD_SWAP, i.moveInInventory(&inv, containerFrom, addedItem, container, NONE, NONE, nullptr, nullptr));
210
211 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammoFrom) == false);
212 ASSERT_TRUE(inv.containsItem(containerFrom->id, &ammo) == true);
213
214 item.setAmmoDef(ad);
215
216 ASSERT_TRUE(inv.containsItem(container->id, &item) == true);
217}
218
219static bool testAddSingle (Inventory* inv, const objDef_t* od, const invDef_t* container)
220{
221 Item item(od);
222
223 return i.tryAddToInventory(inv, &item, container);
224}
225
226TEST_F(InventoryTest, ItemMassActions)
227{
228 const objDef_t* od = INVSH_GetItemByIDSilent("assault");
229 ASSERT_TRUE(nullptr != od);
230
231 const invDef_t* container = INVSH_GetInventoryDefinitionByID("right");
232 ASSERT_TRUE(nullptr != container);
233
234 Inventory inv;
235 bool addedItem = testAddSingle(&inv, od, container);
236 ASSERT_TRUE(addedItem == true);
237
238 /* second try should fail as the right container is a single container */
239 addedItem = testAddSingle(&inv, od, container);
240 ASSERT_TRUE(addedItem == false);
241
242 container = INVSH_GetInventoryDefinitionByID("left");
243 ASSERT_TRUE(nullptr != container);
244
245 od = INVSH_GetItemByIDSilent("fraggrenade");
246 ASSERT_TRUE(nullptr != od);
247
248 addedItem = testAddSingle(&inv, od, container);
249 ASSERT_TRUE(addedItem == true);
250
251 container = INVSH_GetInventoryDefinitionByID("equip");
252 ASSERT_TRUE(nullptr != container);
253
254 for (int i = 0; i < csi.numODs; i++) {
255 od = INVSH_GetItemByIDX(i);
256 /* every item should be placable on the ground container and there should really be enough space */
257 addedItem = testAddSingle(&inv, od, container);
258 ASSERT_TRUE(addedItem == true);
259 addedItem = testAddSingle(&inv, od, container);
260 ASSERT_TRUE(addedItem == true);
261 addedItem = testAddSingle(&inv, od, container);
262 ASSERT_TRUE(addedItem == true);
263 for (int j = 0; j < od->numAmmos; j++) {
264 addedItem = testAddSingle(&inv, od->ammos[j], container);
265 ASSERT_TRUE(addedItem == true);
266 addedItem = testAddSingle(&inv, od->ammos[j], container);
267 ASSERT_TRUE(addedItem == true);
268 addedItem = testAddSingle(&inv, od->ammos[j], container);
269 ASSERT_TRUE(addedItem == true);
270 addedItem = testAddSingle(&inv, od->ammos[j], container);
271 ASSERT_TRUE(addedItem == true);
272 addedItem = testAddSingle(&inv, od->ammos[j], container);
273 ASSERT_TRUE(addedItem == true);
274 addedItem = testAddSingle(&inv, od->ammos[j], container);
275 ASSERT_TRUE(addedItem == true);
276 }
277 }
278}
279
280TEST_F(InventoryTest, ItemToHeadgear)
281{
282 Inventory inv;
283 const objDef_t* od;
284 const invDef_t* container;
285
286 od = INVSH_GetItemByIDSilent("irgoggles");
287 ASSERT_TRUE(nullptr != od);
288
289 container = INVSH_GetInventoryDefinitionByID("headgear");
290 ASSERT_TRUE(nullptr != container);
291
292 Item item(od);
293
294 ASSERT_FALSE(inv.containsItem(container->id, &item));
295
296 ASSERT_TRUE(nullptr != i.addToInventory(&inv, &item, container, NONE, NONE, 1));
297
298 ASSERT_TRUE(inv.containsItem(container->id, &item));
299
300 ASSERT_TRUE(nullptr == i.addToInventory(&inv, &item, container, NONE, NONE, 1));
301}
inventory definition with all its containers
Definition: inv_shared.h:525
bool containsItem(const containerIndex_t contId, const Item *const item) const
Searches if there is a specific item already in the inventory&container.
Definition: inv_shared.h:572
bool tryAddToInventory(Inventory *const inv, const Item *const item, const invDef_t *container)
Tries to add an item to a container (in the inventory inv).
Definition: inventory.cpp:470
inventory_action_t moveInInventory(Inventory *const inv, const invDef_t *from, Item *item, const invDef_t *to, int tx, int ty, int *TU, Item **icp)
Conditions for moving items between containers.
Definition: inventory.cpp:239
void initInventory(const char *name, const csi_t *csi, const inventoryImport_t *import)
Initializes the inventory definition by linking the ->next pointers properly.
Definition: inventory.cpp:986
bool removeFromInventory(Inventory *const inv, const invDef_t *container, Item *fItem) __attribute__((warn_unused_result))
Definition: inventory.cpp:152
void destroyInventoryInterface(void)
Definition: inventory.cpp:997
Item * addToInventory(Inventory *const inv, const Item *const item, const invDef_t *container, int x, int y, int amount) __attribute__((warn_unused_result))
Add an item to a specified container in a given inventory.
Definition: inventory.cpp:91
static void TearDownTestCase()
static void SetUpTestCase()
item instance data, with linked list capability
Definition: inv_shared.h:402
void setAmmoDef(const objDef_t *od)
Definition: inv_shared.h:435
void setAmmoLeft(int value)
Definition: inv_shared.h:441
csi_t csi
Definition: common.cpp:39
memPool_t * com_genericPool
Definition: common.cpp:73
#define NONE
Definition: defines.h:68
const objDef_t * INVSH_GetItemByIDX(int index)
Returns the item that belongs to the given index or nullptr if the index is invalid.
Definition: inv_shared.cpp:266
const invDef_t * INVSH_GetInventoryDefinitionByID(const char *id)
Definition: inv_shared.cpp:340
const objDef_t * INVSH_GetItemByIDSilent(const char *id)
Returns the item that belongs to the given id or nullptr if it wasn't found.
Definition: inv_shared.cpp:249
@ IA_MOVE
Definition: inv_shared.h:68
@ IA_RELOAD
Definition: inv_shared.h:70
@ IA_RELOAD_SWAP
Definition: inv_shared.h:71
voidpf void uLong size
Definition: ioapi.h:42
#define Mem_Free(ptr)
Definition: mem.h:35
#define Mem_FreeTag(pool, tagNum)
Definition: mem.h:36
#define Mem_PoolAlloc(size, pool, tagNum)
Definition: mem.h:41
void NET_Shutdown(void)
Definition: net.cpp:337
QGL_EXTERN GLsizei const GLvoid * data
Definition: r_gl.h:89
void Com_ParseScripts(bool onlyServer)
Definition: scripts.cpp:3619
int numODs
Definition: q_shared.h:518
inventory definition for our menus
Definition: inv_shared.h:371
containerIndex_t id
Definition: inv_shared.h:373
Defines all attributes of objects used in the inventory.
Definition: inv_shared.h:264
const struct objDef_s * ammos[MAX_AMMOS_PER_OBJDEF]
Definition: inv_shared.h:307
int numAmmos
Definition: inv_shared.h:308
static void ResetInventoryList(void)
static const int TAG_INVENTORY
static void * AllocInventoryMemory(size_t size)
static void FreeAllInventory(void)
static InventoryInterface i
TEST_F(InventoryTest, ItemAdd)
static bool testAddSingle(Inventory *inv, const objDef_t *od, const invDef_t *container)
static void FreeInventory(void *data)
static const inventoryImport_t inventoryImport
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
void TEST_Init(void)
Definition: test_shared.cpp:72