tVectorTest.cpp
Engine/source/core/util/test/tVectorTest.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2014 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifdef TORQUE_TESTS_ENABLED 25#include "testing/unitTesting.h" 26#include "core/util/tVector.h" 27 28// Define some test data used below. 29FIXTURE(Vector) 30{ 31public: 32 struct Dtor 33 { 34 bool* ptr; 35 Dtor() {} // Needed for vector increment. 36 Dtor(bool* ptr): ptr(ptr) {} 37 ~Dtor() 38 { 39 *ptr = true; 40 } 41 }; 42 43 static const S32 ints[]; 44 static const U32 length; 45 static S32 QSORT_CALLBACK sortInts(const S32* a, const S32* b) 46 { 47 S32 av = *a; 48 S32 bv = *b; 49 50 if (av < bv) 51 return -1; 52 else if (av > bv) 53 return 1; 54 else 55 return 0; 56 } 57}; 58 59const S32 VectorFixture::ints[] = {0, 10, 2, 3, 14, 4, 12, 6, 16, 7, 8, 1, 11, 5, 13, 9, 15}; 60const U32 VectorFixture::length = sizeof(VectorFixture::ints) / sizeof(S32); 61 62TEST_FIX(Vector, Allocation) 63{ 64 Vector<S32> *vector = new Vector<S32>; 65 for (S32 i = 0; i < 1000; i++) 66 vector->push_back(10000 + i); 67 68 // Erase the first element, 500 times. 69 for (S32 i = 0; i < 500; i++) 70 vector->erase(U32(0)); 71 72 vector->compact(); 73 74 EXPECT_EQ(vector->size(), 500) << "Vector was unexpectedly short!"; 75 76 delete vector; 77} 78 79TEST_FIX(Vector, Deallocation) 80{ 81 bool dtorVals[10]; 82 Vector<Dtor> v; 83 84 // Only add the first 9 entries; the last is populated below. 85 for (U32 i = 0; i < 9; i++) 86 v.push_back(Dtor(&dtorVals[i])); 87 88 // Fill the values array with false so we can test for destruction. 89 for (U32 i = 0; i < 10; i++) 90 dtorVals[i] = false; 91 92 v.decrement(); 93 EXPECT_TRUE(dtorVals[8]) << "Vector::decrement failed to call destructor"; 94 95 v.decrement(2); 96 EXPECT_TRUE(dtorVals[7]) << "Vector::decrement failed to call destructor"; 97 EXPECT_TRUE(dtorVals[6]) << "Vector::decrement failed to call destructor"; 98 99 v.pop_back(); 100 EXPECT_TRUE(dtorVals[5]) << "Vector::pop_back failed to call destructor"; 101 102 v.increment(); 103 v.last() = Dtor(&dtorVals[9]); 104 v.clear(); 105 106 // All elements should have been destructed. 107 for (U32 i = 0; i < 10; i++) 108 EXPECT_TRUE(dtorVals[i]) 109 << "Element " << i << "'s destructor was not called"; 110} 111 112TEST_FIX(Vector, Sorting) 113{ 114 Vector<S32> v; 115 116 for(U32 i = 0; i < length; i++) 117 v.push_back(ints[i]); 118 119 v.sort(sortInts); 120 121 for(U32 i = 0; i < length - 1; i++) 122 EXPECT_TRUE(v[i] <= v[i + 1]) 123 << "Element " << i << " was not in sorted order"; 124} 125 126#endif 127