test_inventory.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 
00026 #include "CUnit/Basic.h"
00027 #include "test_inventory.h"
00028 #include "test_shared.h"
00029 #include "../common/common.h"
00030 #include "../game/inventory.h"
00031 
00032 static inventoryInterface_t i;
00033 static const int TAG_INVENTORY = 5546;
00034 
00035 static void FreeInventory (void *data)
00036 {
00037     Mem_Free(data);
00038 }
00039 
00040 static void *AllocInventoryMemory (size_t size)
00041 {
00042     return Mem_PoolAlloc(size, com_genericPool, TAG_INVENTORY);
00043 }
00044 
00045 static void FreeAllInventory (void)
00046 {
00047     Mem_FreeTag(com_genericPool, TAG_INVENTORY);
00048 }
00049 
00050 static const inventoryImport_t inventoryImport = { FreeInventory, FreeAllInventory, AllocInventoryMemory };
00051 
00052 static inline void ResetInventoryList (void)
00053 {
00054     INV_DestroyInventory(&i);
00055     INV_InitInventory("test", &i, &csi, &inventoryImport);
00056 }
00057 
00062 static int UFO_InitSuiteInventory (void)
00063 {
00064     TEST_Init();
00065 
00066     Com_ParseScripts(qfalse);
00067 
00068     return 0;
00069 }
00070 
00075 static int UFO_CleanSuiteInventory (void)
00076 {
00077     TEST_Shutdown();
00078     return 0;
00079 }
00080 
00081 static void testItemAdd (void)
00082 {
00083     inventory_t inv;
00084     objDef_t *od;
00085     item_t item;
00086     invDef_t *container;
00087 
00088     ResetInventoryList();
00089 
00090     memset(&inv, 0, sizeof(inv));
00091 
00092     od = INVSH_GetItemByIDSilent("assault");
00093     CU_ASSERT_PTR_NOT_NULL(od);
00094 
00095     container = INVSH_GetInventoryDefinitionByID("right");
00096     CU_ASSERT_PTR_NOT_NULL(container);
00097 
00098     item.t = od;
00099     item.m = NULL;
00100     item.a = 0;
00101 
00102     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qfalse);
00103 
00104     CU_ASSERT_PTR_NOT_NULL(i.AddToInventory(&i, &inv, item, container, NONE, NONE, 1));
00105 
00106     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qtrue);
00107 }
00108 
00109 static void testItemDel (void)
00110 {
00111     inventory_t inv;
00112     objDef_t *od;
00113     item_t item;
00114     invDef_t *container;
00115     invList_t *addedItem;
00116 
00117     ResetInventoryList();
00118 
00119     memset(&inv, 0, sizeof(inv));
00120 
00121     od = INVSH_GetItemByIDSilent("assault");
00122     CU_ASSERT_PTR_NOT_NULL(od);
00123 
00124     container = INVSH_GetInventoryDefinitionByID("right");
00125     CU_ASSERT_PTR_NOT_NULL(container);
00126 
00127     item.t = od;
00128     item.m = NULL;
00129     item.a = 0;
00130 
00131     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qfalse);
00132 
00133     addedItem = i.AddToInventory(&i, &inv, item, container, NONE, NONE, 1);
00134 
00135     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qtrue);
00136 
00137     CU_ASSERT(i.RemoveFromInventory(&i, &inv, container, addedItem));
00138 
00139     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qfalse);
00140 }
00141 
00142 static void testItemMove (void)
00143 {
00144     inventory_t inv;
00145     objDef_t *od;
00146     item_t item;
00147     invDef_t *container, *containerTo;
00148     invList_t *addedItem;
00149 
00150     ResetInventoryList();
00151 
00152     memset(&inv, 0, sizeof(inv));
00153 
00154     od = INVSH_GetItemByIDSilent("assault");
00155     CU_ASSERT_PTR_NOT_NULL(od);
00156 
00157     container = INVSH_GetInventoryDefinitionByID("right");
00158     CU_ASSERT_PTR_NOT_NULL(container);
00159 
00160     item.t = od;
00161     item.m = NULL;
00162     item.a = 0;
00163 
00164     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qfalse);
00165 
00166     addedItem = i.AddToInventory(&i, &inv, item, container, NONE, NONE, 1);
00167     CU_ASSERT_PTR_NOT_NULL(addedItem);
00168 
00169     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qtrue);
00170 
00171     containerTo = INVSH_GetInventoryDefinitionByID("backpack");
00172     CU_ASSERT_PTR_NOT_NULL(containerTo);
00173 
00174     CU_ASSERT_EQUAL(IA_MOVE, i.MoveInInventory(&i, &inv, container, addedItem, containerTo, NONE, NONE, NULL, NULL));
00175 
00176     CU_ASSERT(INVSH_ExistsInInventory(&inv, container, item) == qfalse);
00177     CU_ASSERT(INVSH_ExistsInInventory(&inv, containerTo, item) == qtrue);
00178 }
00179 
00180 static qboolean testAddSingle (inventory_t *inv, objDef_t *od, invDef_t *container)
00181 {
00182     item_t item;
00183 
00184     item.t = od;
00185     item.m = NULL;
00186     item.a = 0;
00187 
00188     return i.TryAddToInventory(&i, inv, item, container);
00189 }
00190 
00191 static void testItemMassActions (void)
00192 {
00193     inventory_t inv;
00194     objDef_t *od;
00195     invDef_t *container;
00196     qboolean addedItem;
00197     int i;
00198 
00199     ResetInventoryList();
00200 
00201     memset(&inv, 0, sizeof(inv));
00202 
00203     od = INVSH_GetItemByIDSilent("assault");
00204     CU_ASSERT_PTR_NOT_NULL_FATAL(od);
00205 
00206     container = INVSH_GetInventoryDefinitionByID("right");
00207     CU_ASSERT_PTR_NOT_NULL_FATAL(container);
00208 
00209     addedItem = testAddSingle(&inv, od, container);
00210     CU_ASSERT(addedItem == qtrue);
00211 
00212     /* second try should fail as the right container is a single container */
00213     addedItem = testAddSingle(&inv, od, container);
00214     CU_ASSERT(addedItem == qfalse);
00215 
00216     container = INVSH_GetInventoryDefinitionByID("left");
00217     CU_ASSERT_PTR_NOT_NULL_FATAL(container);
00218 
00219     od = INVSH_GetItemByIDSilent("fraggrenade");
00220     CU_ASSERT_PTR_NOT_NULL_FATAL(od);
00221 
00222     addedItem = testAddSingle(&inv, od, container);
00223     CU_ASSERT(addedItem == qtrue);
00224 
00225     container = INVSH_GetInventoryDefinitionByID("equip");
00226     CU_ASSERT_PTR_NOT_NULL_FATAL(container);
00227 
00228     for (i = 0; i < csi.numODs; i++) {
00229         int j;
00230         od = INVSH_GetItemByIDX(i);
00231         /* every item should be placable on the ground container and there should really be enough space */
00232         addedItem = testAddSingle(&inv, od, container);
00233         CU_ASSERT(addedItem == qtrue);
00234         addedItem = testAddSingle(&inv, od, container);
00235         CU_ASSERT(addedItem == qtrue);
00236         addedItem = testAddSingle(&inv, od, container);
00237         CU_ASSERT(addedItem == qtrue);
00238         for (j = 0; j < od->numAmmos; j++) {
00239             addedItem = testAddSingle(&inv, od->ammos[j], container);
00240             CU_ASSERT(addedItem == qtrue);
00241             addedItem = testAddSingle(&inv, od->ammos[j], container);
00242             CU_ASSERT(addedItem == qtrue);
00243             addedItem = testAddSingle(&inv, od->ammos[j], container);
00244             CU_ASSERT(addedItem == qtrue);
00245             addedItem = testAddSingle(&inv, od->ammos[j], container);
00246             CU_ASSERT(addedItem == qtrue);
00247             addedItem = testAddSingle(&inv, od->ammos[j], container);
00248             CU_ASSERT(addedItem == qtrue);
00249             addedItem = testAddSingle(&inv, od->ammos[j], container);
00250             CU_ASSERT(addedItem == qtrue);
00251         }
00252     }
00253 }
00254 
00255 int UFO_AddInventoryTests (void)
00256 {
00257     /* add a suite to the registry */
00258     CU_pSuite InventorySuite = CU_add_suite("InventoryTests", UFO_InitSuiteInventory, UFO_CleanSuiteInventory);
00259     if (InventorySuite == NULL)
00260         return CU_get_error();
00261 
00262     /* add the tests to the suite */
00263     if (CU_ADD_TEST(InventorySuite, testItemAdd) == NULL)
00264         return CU_get_error();
00265     if (CU_ADD_TEST(InventorySuite, testItemDel) == NULL)
00266         return CU_get_error();
00267     if (CU_ADD_TEST(InventorySuite, testItemMove) == NULL)
00268         return CU_get_error();
00269     if (CU_ADD_TEST(InventorySuite, testItemMassActions) == NULL)
00270         return CU_get_error();
00271 
00272     return CUE_SUCCESS;
00273 }

Generated by  doxygen 1.6.2