2
0

platformMemoryTests.cc 5.5 KB

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