mem_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. *******************************************************************************
  3. \file mem_test.c
  4. \brief Tests for memory functions
  5. \project bee2/test
  6. \created 2014.02.01
  7. \version 2023.03.06
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/mem.h>
  13. #include <bee2/core/hex.h>
  14. #include <bee2/core/str.h>
  15. #include <bee2/core/util.h>
  16. /*
  17. *******************************************************************************
  18. Тестирование
  19. \remark Если не инициализировать буфер buf, то компилятор GCC при обработке
  20. вызова memIsValid(buf, sizeof(buf)) выдаст предупреждение
  21. -Wmaybe-uninitialized:
  22. > In addition, passing a pointer (or in C++, a reference) to an uninitialized
  23. object to a const-qualified function argument is also diagnosed by this
  24. warning (https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html).
  25. *******************************************************************************
  26. */
  27. bool_t memTest()
  28. {
  29. octet buf[16];
  30. octet buf1[16];
  31. octet buf2[16];
  32. void* p;
  33. void* p1;
  34. size_t i;
  35. // pre
  36. CASSERT(sizeof(buf) == sizeof(buf1));
  37. memSetZero(buf, sizeof(buf));
  38. if (!memIsValid(buf, sizeof(buf)) || !memIsValid(0, 0))
  39. return FALSE;
  40. // alloc (считаем, что памяти хватает)
  41. p = memAlloc(100);
  42. if (!p)
  43. return FALSE;
  44. memSet(p, 7, 100);
  45. if (!(p1 = memRealloc(p, 102)) ||
  46. !memIsRep(p = p1, 100, 7) ||
  47. !(p1 = memRealloc(p, 90)) ||
  48. !memIsRep(p = p1, 90, 7) ||
  49. (p = memRealloc(p, 0)))
  50. {
  51. memFree(p);
  52. return FALSE;
  53. }
  54. memFree(p);
  55. // заполнение / копирование
  56. memSet(buf, 12, sizeof(buf));
  57. memCopy(buf1, buf, sizeof(buf));
  58. for (i = 0; i < sizeof(buf); ++i)
  59. {
  60. if (buf1[i] != buf[i])
  61. return FALSE;
  62. buf[i] = buf1[i] = i % 256u;
  63. }
  64. memMove(buf, buf + 1, sizeof(buf) - 1);
  65. memMove(buf1 + 1, buf1, sizeof(buf1) - 1);
  66. for (i = 0; i < sizeof(buf) - 2; ++i)
  67. {
  68. if (buf1[i + 2] != buf[i])
  69. return FALSE;
  70. }
  71. memCopy(buf1, 0, 0);
  72. memMove(buf1, 0, 0);
  73. // сравнение
  74. hexTo(buf, "000102030405060708090A0B0C0D0E0F");
  75. hexTo(buf1, "F00102030405060708090A0B0C0D0EFF");
  76. if (SAFE(memIsZero)(buf, 15) ||
  77. FAST(memIsZero)(buf, 15) ||
  78. FAST(memIsZero)(buf, 3) ||
  79. SAFE(memEq)(buf + 1, buf1 + 1, 15) ||
  80. FAST(memEq)(buf + 1, buf1 + 1, 15) ||
  81. SAFE(memEq)(buf + 8, buf1 + 8, 8) ||
  82. FAST(memEq)(buf + 8, buf1 + 8, 8) ||
  83. !SAFE(memEq)(buf + 1, buf1 + 1, 8) ||
  84. !FAST(memEq)(buf + 1, buf1 + 1, 8) ||
  85. !SAFE(memEq)(buf + 1, buf1 + 1, 14) ||
  86. !FAST(memEq)(buf + 1, buf1 + 1, 14) ||
  87. SAFE(memCmp)(buf, buf1, 7) != -1 ||
  88. FAST(memCmp)(buf, buf1, 7) != -1 ||
  89. SAFE(memCmpRev)(buf, buf1, 7) != -1 ||
  90. FAST(memCmpRev)(buf, buf1, 7) != -1 ||
  91. SAFE(memCmp)(buf, buf1, 15) != -1 ||
  92. FAST(memCmp)(buf, buf1, 15) != -1 ||
  93. SAFE(memCmpRev)(buf, buf1, 15) != -1 ||
  94. FAST(memCmpRev)(buf, buf1, 15) != -1 ||
  95. SAFE(memCmp)(buf1, buf, 15) != 1 ||
  96. FAST(memCmp)(buf1, buf, 15) != 1 ||
  97. SAFE(memCmpRev)(buf1, buf, 15) != 1 ||
  98. FAST(memCmpRev)(buf1, buf, 15) != 1 ||
  99. SAFE(memCmp)(buf, buf1, 8) != -1 ||
  100. FAST(memCmp)(buf, buf1, 8) != -1 ||
  101. SAFE(memCmpRev)(buf, buf1, 8) != -1 ||
  102. FAST(memCmpRev)(buf, buf1, 8) != -1 ||
  103. SAFE(memCmp)(buf1, buf, 8) != 1 ||
  104. FAST(memCmp)(buf1, buf, 8) != 1 ||
  105. SAFE(memCmpRev)(buf1, buf, 8) != 1 ||
  106. FAST(memCmpRev)(buf1, buf, 8) != 1 ||
  107. SAFE(memCmp)(buf + 1, buf1 + 1, 8) != 0 ||
  108. FAST(memCmp)(buf + 1, buf1 + 1, 8) != 0 ||
  109. SAFE(memCmp)(buf + 1, buf1 + 1, 14) != 0 ||
  110. FAST(memCmp)(buf + 1, buf1 + 1, 14) != 0 ||
  111. SAFE(memCmpRev)(buf + 1, buf1 + 1, 8) != 0 ||
  112. FAST(memCmpRev)(buf + 1, buf1 + 1, 8) != 0 ||
  113. SAFE(memCmpRev)(buf + 1, buf1 + 1, 14) != 0 ||
  114. FAST(memCmpRev)(buf + 1, buf1 + 1, 14) != 0)
  115. return FALSE;
  116. memRev(buf, 15);
  117. if (memNonZeroSize(buf, 15) != 14)
  118. return FALSE;
  119. hexTo(buf, "F001020304050607");
  120. hexTo(buf1, "00010203040506F7");
  121. if (SAFE(memCmp)(buf, buf1, 8) != 1 ||
  122. FAST(memCmp)(buf, buf1, 8) != 1 ||
  123. SAFE(memCmp)(buf1, buf, 8) != -1 ||
  124. FAST(memCmp)(buf1, buf, 8) != -1 ||
  125. SAFE(memCmpRev)(buf, buf1, 8) != -1 ||
  126. FAST(memCmpRev)(buf, buf1, 8) != -1 ||
  127. SAFE(memCmpRev)(buf1, buf, 8) != 1 ||
  128. FAST(memCmpRev)(buf1, buf, 8) != 1)
  129. return FALSE;
  130. hexTo(buf, "01010101010101010102");
  131. if (SAFE(memIsRep)(buf, 7, 0x01) != TRUE ||
  132. FAST(memIsRep)(buf, 7, 0x01) != TRUE ||
  133. SAFE(memIsRep)(buf, 8, 0x01) != TRUE ||
  134. FAST(memIsRep)(buf, 8, 0x01) != TRUE ||
  135. SAFE(memIsRep)(buf, 9, 0x01) != TRUE ||
  136. FAST(memIsRep)(buf, 9, 0x01) != TRUE ||
  137. SAFE(memIsRep)(buf, 10, 0x01) == TRUE ||
  138. FAST(memIsRep)(buf, 10, 0x01) == TRUE)
  139. return FALSE;
  140. // join
  141. hexTo(buf, "0001020304050607");
  142. memJoin(buf, buf + 1, 3, buf + 3, 4);
  143. if (!hexEq(buf, "01020303040506"))
  144. return FALSE;
  145. hexTo(buf, "0001020304050607");
  146. memJoin(buf, buf + 1, 3, buf + 1, 4);
  147. if (!hexEq(buf, "01020301020304"))
  148. return FALSE;
  149. hexTo(buf, "0001020304050607");
  150. memJoin(buf, buf + 3, 4, buf + 2, 2);
  151. if (!hexEq(buf, "030405060203"))
  152. return FALSE;
  153. hexTo(buf, "0001020304050607");
  154. memJoin(buf + 2, buf, 4, buf + 4, 2);
  155. if (!hexEq(buf + 2, "000102030405"))
  156. return FALSE;
  157. // xor
  158. hexTo(buf, "000102030405060708");
  159. hexTo(buf1, "F0F1F2F3F4F5F6F7F8");
  160. memXor(buf2, buf, buf1, 9);
  161. if (!memIsRep(buf2, 9, 0xF0))
  162. return FALSE;
  163. memXor2(buf2, buf1, 9);
  164. memXor2(buf2, buf, 8);
  165. if (!memIsRep(buf2, 8, 0) || buf2[8] != 0x08)
  166. return FALSE;
  167. // все нормально
  168. return TRUE;
  169. }