platformMemoryTests.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. // We don't want tests in a shipping version.
  23. #ifndef TORQUE_SHIPPING
  24. #ifndef _UNIT_TESTING_H_
  25. #include "testing/unitTesting.h"
  26. #endif
  27. #ifndef _PLATFORM_H_
  28. #include "platform/platform.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. #define PLATFORM_UNITTEST_MEMORY_BUFFERSIZE 16384
  32. //-----------------------------------------------------------------------------
  33. TEST( PlatformMemoryTests, dMallocrAnddFreeTest )
  34. {
  35. // Allocate some memory.
  36. void* pResult = dMalloc_r( PLATFORM_UNITTEST_MEMORY_BUFFERSIZE, __FILE__, __LINE__ );
  37. // Check.
  38. ASSERT_NE( (void*)0, pResult ) << "Memory not allocated.";
  39. // Set memory (ensure no exceptions).
  40. for( U32 index = 0; index < PLATFORM_UNITTEST_MEMORY_BUFFERSIZE; ++index )
  41. {
  42. *((U8*)pResult) = index % 255;
  43. }
  44. // Free memory (ensure no exceptions).
  45. dFree( pResult );
  46. }
  47. //-----------------------------------------------------------------------------
  48. TEST( PlatformMemoryTests, dReallocrAnddFreeTest )
  49. {
  50. // Allocate some memory.
  51. void* pResult = dMalloc_r( PLATFORM_UNITTEST_MEMORY_BUFFERSIZE, __FILE__, __LINE__ );
  52. // Check.
  53. ASSERT_NE( (void*)0, pResult ) << "Memory not allocated.";
  54. // Reallocate some memory.
  55. pResult = dRealloc_r( pResult, PLATFORM_UNITTEST_MEMORY_BUFFERSIZE * 2, __FILE__, __LINE__ );
  56. // Check.
  57. ASSERT_NE( (void*)0, pResult ) << "Memory not allocated.";
  58. // Set memory (ensure no exceptions).
  59. for( U32 index = 0; index < (PLATFORM_UNITTEST_MEMORY_BUFFERSIZE*2); ++index )
  60. {
  61. *((U8*)pResult) = index % 255;
  62. }
  63. // Free memory (ensure no exceptions).
  64. dFree( pResult );
  65. }
  66. //-----------------------------------------------------------------------------
  67. TEST( PlatformMemoryTests, dMemcpyTest )
  68. {
  69. U8 source[] = { 0,1,2,3,4,5,6,7,8,9 };
  70. U8 destination[sizeof(source)];
  71. // Copy.
  72. void* result = dMemcpy( destination, source, sizeof(source) );
  73. // Check.
  74. ASSERT_EQ( result, destination ) << "Destination buffer incorrect.";
  75. // Check,
  76. for( U32 index = 0; index < sizeof(source); ++index )
  77. {
  78. ASSERT_EQ( index, source[index] ) << "Source memory value is incorrect.";
  79. ASSERT_EQ( index, destination[index] ) << "Destination memory value is incorrect.";
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. TEST( PlatformMemoryTests, dMemmoveTest )
  84. {
  85. U8 destination[] = { 0,1,2,3,4,5,6,7,8,9 };
  86. // Move.
  87. void* result = dMemmove( destination, destination+1, sizeof(destination)-1 );
  88. // Check.
  89. ASSERT_EQ( result, destination ) << "Destination buffer incorrect.";
  90. // Check,
  91. for( U32 index = 0; index < (sizeof(destination)-1); ++index )
  92. {
  93. ASSERT_EQ( index+1, destination[index] ) << "Destination memory value is incorrect.";
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. TEST( PlatformMemoryTests, dMemsetTest )
  98. {
  99. U8 destination[] = { 0,1,2,3,4,5,6,7,8,9 };
  100. const U8 magic = 128;
  101. // Set.
  102. void* result = dMemset( destination, magic, sizeof(destination)-1 );
  103. // Check.
  104. ASSERT_EQ( result, destination ) << "Destination buffer incorrect.";
  105. // Check,
  106. for( U32 index = 0; index < (sizeof(destination)-1); ++index )
  107. {
  108. ASSERT_EQ( magic, destination[index] ) << "Destination memory value is incorrect.";
  109. }
  110. // Check.
  111. ASSERT_EQ( 9, destination[sizeof(destination)-1] ) << "Destination memory value is incorrect.";
  112. }
  113. //-----------------------------------------------------------------------------
  114. TEST( PlatformMemoryTests, dMemcmpTest )
  115. {
  116. U8 source1[] = { 0,1,2,3,4,5,6,7,8,10 };
  117. U8 source2[] = { 0,1,2,3,4,5,6,7,8,9 };
  118. U8 source3[] = { 0,1,2,3,4,5,6,7,8,0 };
  119. U8 destination[] = { 0,1,2,3,4,5,6,7,8,9 };
  120. // Compare.
  121. const S32 result1 = dMemcmp( source1, destination, sizeof(destination) );
  122. const S32 result2 = dMemcmp( source2, destination, sizeof(destination) );
  123. const S32 result3 = dMemcmp( source3, destination, sizeof(destination) );
  124. // Check.
  125. ASSERT_LT( 0, result1 ) << "Memory compare is incorrect.";
  126. ASSERT_EQ( 0, result2 ) << "Memory compare is incorrect.";
  127. ASSERT_GT( 0, result3 ) << "Memory compare is incorrect.";
  128. }
  129. #endif // TORQUE_SHIPPING