Browse Source

Replaced existing Vector tests.

Daniel Buckmaster 11 năm trước cách đây
mục cha
commit
b48050209d

+ 0 - 138
Engine/source/core/util/test/testVector2.cpp

@@ -1,138 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (c) 2012 GarageGames, LLC
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to
-// deal in the Software without restriction, including without limitation the
-// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-// sell copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-// IN THE SOFTWARE.
-//-----------------------------------------------------------------------------
-
-#include "unit/test.h"
-#include "console/console.h"
-#include "core/util/tVector.h"
-
-
-#ifndef TORQUE_SHIPPING
-
-using namespace UnitTesting;
-
-#define TEST( x ) test( ( x ), "FAIL: " #x )
-#define XTEST( t, x ) t->test( ( x ), "FAIL: " #x )
-
-CreateUnitTest( TestVector, "Util/Vector" )
-{
-   bool dtorVals[ 10 ];
-   struct Dtor
-   {
-      bool* ptr;
-      Dtor() {}
-      Dtor( bool* ptr )
-         : ptr( ptr ) { *ptr = false; }
-      ~Dtor()
-      {
-         *ptr = true;
-      }
-   };
-   void testDestruction()
-   {
-      Vector< Dtor > v;
-      
-      for( U32 i = 0; i < 9; ++ i )
-         v.push_back( Dtor( &dtorVals[ i ] ) );
-         
-      v.decrement();
-      v.decrement( 2 );
-      v.pop_back();
-      v.increment();
-      v.last() = Dtor( &dtorVals[ 9 ] );
-      v.clear();
-      
-      TEST( dtorVals[ 0 ] );
-      TEST( dtorVals[ 1 ] );
-      TEST( dtorVals[ 2 ] );
-      TEST( dtorVals[ 3 ] );
-      TEST( dtorVals[ 4 ] );
-      TEST( dtorVals[ 5 ] );
-      TEST( dtorVals[ 6 ] );
-      TEST( dtorVals[ 7 ] );
-      TEST( dtorVals[ 8 ] );
-      TEST( dtorVals[ 9 ] );
-   }
-   
-   static S32 QSORT_CALLBACK sortInts( const S32* a, const S32* b )
-   {
-      S32 av = *a;
-      S32 bv = *b;
-      
-      if( av < bv )
-         return -1;
-      else if( av > bv )
-         return 1;
-      else
-         return 0;
-   }
-   
-   void testSort()
-   {
-      Vector< S32 > v;
-      
-      v.push_back( 0 );
-      v.push_back( 10 );
-      v.push_back( 2 );
-      v.push_back( 3 );
-      v.push_back( 14 );
-      v.push_back( 4 );
-      v.push_back( 12 );
-      v.push_back( 6 );
-      v.push_back( 16 );
-      v.push_back( 7 );
-      v.push_back( 8 );
-      v.push_back( 1 );
-      v.push_back( 11 );
-      v.push_back( 5 );
-      v.push_back( 13 );
-      v.push_back( 9 );
-      v.push_back( 15 );
-      
-      v.sort( sortInts );
-      
-      TEST( v[ 0 ] == 0 );
-      TEST( v[ 1 ] == 1 );
-      TEST( v[ 2 ] == 2 );
-      TEST( v[ 3 ] == 3 );
-      TEST( v[ 4 ] == 4 );
-      TEST( v[ 5 ] == 5 );
-      TEST( v[ 6 ] == 6 );
-      TEST( v[ 7 ] == 7 );
-      TEST( v[ 8 ] == 8 );
-      TEST( v[ 9 ] == 9 );
-      TEST( v[ 10 ] == 10 );
-      TEST( v[ 11 ] == 11 );
-      TEST( v[ 12 ] == 12 );
-      TEST( v[ 13 ] == 13 );
-      TEST( v[ 14 ] == 14 );
-      TEST( v[ 15 ] == 15 );
-      TEST( v[ 16 ] == 16 );
-   }
-   
-   void run()
-   {
-      testSort();
-      testDestruction();
-   }
-};
-
-#endif

+ 118 - 0
Engine/source/core/util/test/vectorTest.cpp

@@ -0,0 +1,118 @@
+//-----------------------------------------------------------------------------
+// Copyright (c) 2014 GarageGames, LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+//-----------------------------------------------------------------------------
+
+#ifdef TORQUE_TESTS_ENABLED
+#include "testing/unitTesting.h"
+#include "core/util/tVector.h"
+
+// Define some test data used below.
+static const S32 ints[] = {0, 10, 2, 3, 14, 4, 12, 6, 16, 7, 8, 1, 11, 5, 13, 9, 15};
+static const U32 length = sizeof(ints) / sizeof(S32);
+static S32 QSORT_CALLBACK sortInts(const S32* a, const S32* b)
+{
+   S32 av = *a;
+   S32 bv = *b;
+      
+   if (av < bv)
+      return -1;
+   else if (av > bv)
+      return 1;
+   else
+      return 0;
+}
+
+TEST(Vector, Allocation)
+{
+   Vector<S32> *vector = new Vector<S32>;
+   for (S32 i = 0; i < 1000; i++)
+      vector->push_back(10000 + i);
+
+   // Erase the first element, 500 times.
+   for (S32 i = 0; i < 500; i++)
+      vector->erase(U32(0));
+
+   vector->compact();
+
+   EXPECT_EQ(vector->size(), 500) << "Vector was unexpectedly short!";
+
+   delete vector;
+}
+
+TEST(Vector, Deallocation)
+{
+   struct Dtor
+   {
+      bool* ptr;
+      Dtor() {} // Needed for vector increment.
+      Dtor(bool* ptr): ptr(ptr) {}
+      ~Dtor()
+      {
+         *ptr = true;
+      }
+   };
+
+   bool dtorVals[10];
+   Vector<Dtor> v;
+
+   // Only add the first 9 entries; the last is populated below.
+   for (U32 i = 0; i < 9; i++)
+      v.push_back(Dtor(&dtorVals[i]));
+
+   // Fill the values array with false so we can test for destruction.
+   for (U32 i = 0; i < 10; i++)
+      dtorVals[i] = false;
+
+   v.decrement();
+   EXPECT_TRUE(dtorVals[8]) << "Vector::decrement failed to call destructor";
+
+   v.decrement(2);
+   EXPECT_TRUE(dtorVals[7]) << "Vector::decrement failed to call destructor";
+   EXPECT_TRUE(dtorVals[6]) << "Vector::decrement failed to call destructor";
+
+   v.pop_back();
+   EXPECT_TRUE(dtorVals[5]) << "Vector::pop_back failed to call destructor";
+
+   v.increment();
+   v.last() = Dtor(&dtorVals[9]);
+   v.clear();
+
+   // All elements should have been destructed.
+   for (U32 i = 0; i < 10; i++)
+      EXPECT_TRUE(dtorVals[i])
+         << "Element " << i << "'s destructor was not called";
+}
+
+TEST(Vector, Sorting)
+{
+   Vector<S32> v;
+
+   for(U32 i = 0; i < length; i++)
+      v.push_back(ints[i]);
+
+   v.sort(sortInts);
+
+   for(U32 i = 0; i < length - 1; i++)
+      EXPECT_TRUE(v[i] <= v[i + 1])
+         << "Element " << i << " was not in sorted order";
+}
+
+#endif

+ 0 - 53
Engine/source/unit/tests/testVector.cpp

@@ -1,53 +0,0 @@
-//-----------------------------------------------------------------------------
-// Copyright (c) 2012 GarageGames, LLC
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to
-// deal in the Software without restriction, including without limitation the
-// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-// sell copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-// IN THE SOFTWARE.
-//-----------------------------------------------------------------------------
-
-#include "platform/platform.h"
-#include "unit/test.h"
-#include "unit/memoryTester.h"
-#include "core/util/tVector.h"
-
-using namespace UnitTesting;
-
-CreateUnitTest(TestVectorAllocate, "Types/Vector")
-{
-   void run()
-   {
-      MemoryTester m;
-      m.mark();
-
-      Vector<S32> *vector = new Vector<S32>;
-      for(S32 i=0; i<1000; i++)
-         vector->push_back(10000 + i);
-
-      // Erase the first element, 500 times.
-      for(S32 i=0; i<500; i++)
-         vector->erase(U32(0));
-
-      vector->compact();
-
-      test(vector->size() == 500, "Vector was unexpectedly short!");
-
-      delete vector;
-
-      test(m.check(), "Vector allocation test leaked memory!");
-   }
-};