Torque3D Documentation / _generateds / swizzleTest.cpp

swizzleTest.cpp

Engine/source/core/util/test/swizzleTest.cpp

More...

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 "platform/platform.h"
 26#include "testing/unitTesting.h"
 27#include "core/util/swizzle.h"
 28#include "math/mRandom.h"
 29
 30class TestStruct
 31{
 32private:
 33   static U32 smIdx;
 34   U32 mIdx;
 35   U32 mData;
 36   
 37public:
 38   TestStruct( const S32 data = -1 ) : mData( data ), mIdx( smIdx++ ) {};
 39
 40   dsize_t Idx() const { return mIdx; }
 41
 42   U32 Data() const { return mData; }
 43   void Data(U32 val) { mData = val; }
 44};
 45
 46U32 TestStruct::smIdx = 0;
 47
 48TEST(Swizzle, Swizzle)
 49{
 50   //------------------------------------------------------------------------
 51   // Debugger-Observable Functionality Tests
 52   //------------------------------------------------------------------------
 53   U8 simpleBuffer[] = { 0, 1, 2, 3 };
 54   U8 simpleTest[] = { 0, 1, 2, 3 };
 55
 56#define RESET_SIMPLE() dMemcpy( simpleTest, simpleBuffer, sizeof( simpleBuffer ) )
 57
 58   //------------------------------------------------------------------------
 59   // No-switch test
 60   dsize_t noSwzl4[] = { 0, 1, 2, 3 };
 61   Swizzle<U8,4> noSwizzle4( noSwzl4 );
 62
 63   noSwizzle4.InPlace( simpleTest, sizeof( simpleTest ) );
 64   EXPECT_EQ( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ), 0 )
 65      << "No-switch test failed!";
 66   RESET_SIMPLE();
 67
 68   //------------------------------------------------------------------------
 69   // No-brainer RGBA->BGRA test
 70   dsize_t bgraSwzl[] = { 2, 1, 0, 3 };
 71   Swizzle<U8,4> bgraSwizzle( bgraSwzl );
 72
 73   U8 bgraTest[] = { 2, 1, 0, 3 };
 74   bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
 75   EXPECT_EQ( dMemcmp( simpleTest, bgraTest, sizeof( bgraTest ) ), 0 )
 76      << "U8 RGBA->BGRA test failed";
 77      
 78   //------------------------------------------------------------------------
 79   // Reverse test
 80   bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
 81   EXPECT_EQ( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ), 0 )
 82      << "U8 RGBA->BGRA reverse test failed";
 83
 84   RESET_SIMPLE();
 85
 86   //------------------------------------------------------------------------
 87   // Object support test
 88   Swizzle<TestStruct,4> bgraObjSwizzle( bgraSwzl );
 89   {
 90      U32 objIdx[] = { 0, 1, 2, 3 };
 91
 92      FrameTemp<TestStruct> objTest( sizeof( objIdx ) / sizeof( U32 ) );
 93      FrameTemp<U32> randData( sizeof( objIdx ) / sizeof( U32 ) );
 94
 95      bool same = true;
 96
 97      for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
 98      {
 99         // Make random data and assign it
100         randData[i] = gRandGen.randI();
101         objTest[i].Data( randData[i] );
102
103         // Continue object sanity check
104         same &= ( objTest[i].Idx() == objIdx[i] );
105      }
106
107      EXPECT_TRUE( same )
108         << "Test object failed to be competent";
109
110      bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
111      same = true;
112
113      for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
114         same &= ( objTest[i].Idx() == bgraTest[i] ) && ( objTest[i].Data() == randData[ (U32)bgraTest[ i ] ] );
115
116      EXPECT_TRUE( same )
117         << "Object RGBA->BGRA test failed.";
118
119      bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
120      same = true;
121
122      for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
123         same &= ( objTest[i].Idx() == objIdx[i] ) && ( objTest[i].Data() == randData[i] );
124
125      EXPECT_TRUE( same )
126         << "Object RGBA->BGRA reverse test failed.";
127   }
128};
129
130#endif
131