testSwizzle.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "unit/test.h"
  24. #include "unit/memoryTester.h"
  25. #include "core/util/swizzle.h"
  26. #include "math/mRandom.h"
  27. using namespace UnitTesting;
  28. class TestStruct
  29. {
  30. private:
  31. static U32 smIdx;
  32. U32 mIdx;
  33. U32 mData;
  34. public:
  35. TestStruct( const S32 data = -1 ) : mData( data ), mIdx( smIdx++ ) {};
  36. dsize_t Idx() const { return mIdx; }
  37. U32 Data() const { return mData; }
  38. void Data(U32 val) { mData = val; }
  39. };
  40. U32 TestStruct::smIdx = 0;
  41. CreateUnitTest(TestSwizzle, "Utils/Swizzle")
  42. {
  43. void run()
  44. {
  45. //------------------------------------------------------------------------
  46. // Debugger-Observable Functionality Tests
  47. //------------------------------------------------------------------------
  48. U8 simpleBuffer[] = { 0, 1, 2, 3 };
  49. U8 simpleTest[] = { 0, 1, 2, 3 };
  50. #define RESET_SIMPLE() dMemcpy( simpleTest, simpleBuffer, sizeof( simpleBuffer ) )
  51. //------------------------------------------------------------------------
  52. // No-switch test
  53. dsize_t noSwzl4[] = { 0, 1, 2, 3 };
  54. Swizzle<U8,4> noSwizzle4( noSwzl4 );
  55. noSwizzle4.InPlace( simpleTest, sizeof( simpleTest ) );
  56. test( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ) == 0, "No-switch test failed!" );
  57. RESET_SIMPLE();
  58. //------------------------------------------------------------------------
  59. // No-brainer RGBA->BGRA test
  60. dsize_t bgraSwzl[] = { 2, 1, 0, 3 };
  61. Swizzle<U8,4> bgraSwizzle( bgraSwzl );
  62. U8 bgraTest[] = { 2, 1, 0, 3 };
  63. bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
  64. test( dMemcmp( simpleTest, bgraTest, sizeof( bgraTest ) ) == 0, "U8 RGBA->BGRA test failed" );
  65. //------------------------------------------------------------------------
  66. // Reverse test
  67. bgraSwizzle.InPlace( simpleTest, sizeof( simpleTest ) );
  68. test( dMemcmp( simpleTest, simpleBuffer, sizeof( simpleBuffer ) ) == 0, "U8 RGBA->BGRA reverse test failed" );
  69. RESET_SIMPLE();
  70. //------------------------------------------------------------------------
  71. // Object support test
  72. Swizzle<TestStruct,4> bgraObjSwizzle( bgraSwzl );
  73. {
  74. U32 objIdx[] = { 0, 1, 2, 3 };
  75. FrameTemp<TestStruct> objTest( sizeof( objIdx ) / sizeof( U32 ) );
  76. FrameTemp<U32> randData( sizeof( objIdx ) / sizeof( U32 ) );
  77. bool same = true;
  78. for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
  79. {
  80. // Make random data and assign it
  81. randData[i] = gRandGen.randI();
  82. objTest[i].Data( randData[i] );
  83. // Continue object sanity check
  84. same &= ( objTest[i].Idx() == objIdx[i] );
  85. }
  86. test( same, "Test object failed to be competent" );
  87. bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
  88. same = true;
  89. for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
  90. same &= ( objTest[i].Idx() == bgraTest[i] ) && ( objTest[i].Data() == randData[ (U32)bgraTest[ i ] ] );
  91. test( same, "Object RGBA->BGRA test failed." );
  92. bgraObjSwizzle.InPlace( ~objTest, sizeof( TestStruct ) * ( sizeof( objIdx ) / sizeof( U32 ) ) );
  93. same = true;
  94. for( U32 i = 0; i < sizeof( objIdx ) / sizeof( U32 ); i++ )
  95. same &= ( objTest[i].Idx() == objIdx[i] ) && ( objTest[i].Data() == randData[i] );
  96. test( same, "Object RGBA->BGRA reverse test failed." );
  97. }
  98. }
  99. };