ww_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *******************************************************************************
  3. \file u64_test.c
  4. \brief Tests for operations on arbitrary length words
  5. \project bee2/test
  6. \created 2023.03.31
  7. \version 2023.02.31
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/util.h>
  13. #include <bee2/math/ww.h>
  14. /*
  15. *******************************************************************************
  16. Тестирование
  17. *******************************************************************************
  18. */
  19. bool_t wwTest()
  20. {
  21. word a[8];
  22. word b[8];
  23. word c[8];
  24. // заполнение и сравнение
  25. if ((wwSetW(a, COUNT_OF(a), 0), FALSE) ||
  26. !SAFE(wwIsZero)(a, 8) || !FAST(wwIsZero)(a, 8) ||
  27. (wwSetZero(a, COUNT_OF(a)), FALSE) ||
  28. !SAFE(wwIsZero)(a, 8) || !FAST(wwIsZero)(a, 8) ||
  29. (wwSetW(a, COUNT_OF(a), 0x36), FALSE) ||
  30. SAFE(wwIsZero)(a, 8) || FAST(wwIsZero)(a, 8) ||
  31. !SAFE(wwIsW)(a, 8, 0x36) || !FAST(wwIsW)(a, 8, 0x36) ||
  32. SAFE(wwIsW)(a, 8, 0x5C) || FAST(wwIsW)(a, 8, 0x5C) ||
  33. SAFE(wwCmpW)(a, 8, 0x36) != 0 || FAST(wwCmpW)(a, 8, 0x36) != 0 ||
  34. SAFE(wwCmpW)(a, 8, 0x5C) >=0 || FAST(wwCmpW)(a, 8, 0x5C) >= 0 ||
  35. (wwRepW(a, COUNT_OF(a), 0x36), FALSE) ||
  36. SAFE(wwIsZero)(a, 8) || FAST(wwIsZero)(a, 8) ||
  37. !SAFE(wwIsRepW)(a, 8, 0x36) || !FAST(wwIsRepW)(a, 8, 0x36) ||
  38. SAFE(wwIsRepW)(a, 8, 0x5C) || FAST(wwIsRepW)(a, 8, 0x5C))
  39. return FALSE;
  40. // копирование и сравнение
  41. if ((wwCopy(a, a, 8), FALSE) ||
  42. !SAFE(wwEq)(a, a, 8) || !FAST(wwEq)(a, a, 8) ||
  43. (wwRepW(b, 8, 0x5C), FALSE) ||
  44. SAFE(wwEq)(a, b, 8) || FAST(wwEq)(a, b, 8) ||
  45. SAFE(wwCmp)(a, b, 8) >= 0 || FAST(wwCmp)(a, b, 8) >= 0 ||
  46. SAFE(wwCmp2)(a, 8, b, 7) <= 0 || FAST(wwCmp2)(a, 8, b, 7) <= 0 ||
  47. (wwSwap(a, b, 8), FALSE) ||
  48. SAFE(wwCmp)(a, b, 8) <= 0 || FAST(wwCmp)(a, b, 8) <= 0 ||
  49. SAFE(wwCmp2)(a, 7, b, 8) >= 0 || FAST(wwCmp2)(a, 7, b, 8) >= 0)
  50. return FALSE;
  51. // операции с битами
  52. ASSERT(wwIsRepW(b, 8, 0x36));
  53. if (wwTestBit(b, 0) || !wwTestBit(b, 1) ||
  54. wwTestBit(b, 0 + B_PER_W) || !wwTestBit(b, 1 + B_PER_W) ||
  55. wwTestBit(b, B_PER_W - 1) || wwTestBit(b, 2 * B_PER_W - 1) ||
  56. wwGetBits(b, B_PER_W, 6) != 0x36 ||
  57. wwGetBits(b, B_PER_W + 1, 5) != (0x36 >> 1) ||
  58. (wwSetBit(b, B_PER_W - 1, TRUE), FALSE) ||
  59. !wwTestBit(b, B_PER_W - 1) ||
  60. (wwFlipBit(b, B_PER_W - 1), FALSE) ||
  61. wwTestBit(b, B_PER_W - 1) ||
  62. (wwSetBit(b, B_PER_W - 1, FALSE), FALSE) ||
  63. wwTestBit(b, B_PER_W - 1) ||
  64. (wwSetBits(b, B_PER_W - 2, 7, 0x36), FALSE) ||
  65. wwGetBits(b, B_PER_W - 2, 7) != 0x36)
  66. return FALSE;
  67. // XOR
  68. if ((wwXor(c, a, b, 8), wwXor2(c, a, 8), wwXor2(c, b, 8), FALSE) ||
  69. !wwIsZero(c, 8) ||
  70. (wwCopy(c, b, 8), wwXor(c, c, c, 8), FALSE) ||
  71. !wwIsZero(c, 8) ||
  72. (wwCopy(c, a, 8), wwXor2(c, c, 8), FALSE) ||
  73. !wwIsZero(c, 8))
  74. return FALSE;
  75. // нулевые разряды
  76. ASSERT(wwIsRepW(a, 8, 0x5C) && wwIsZero(c, 8));
  77. if (wwLoZeroBits(a, 8) != 2 ||
  78. wwHiZeroBits(a, 8) != B_PER_W - 7 ||
  79. wwLoZeroBits(c, 8) != B_PER_W * 8 ||
  80. wwLoZeroBits(c, 8) != B_PER_W * 8 ||
  81. (wwCopy(b, a, 8), wwTrimHi(b, 8, 6 * B_PER_W + 3), FALSE) ||
  82. wwHiZeroBits(b, 8) != 2 * B_PER_W - 3 ||
  83. (wwTrimLo(b, 8, B_PER_W + 5), FALSE) ||
  84. wwLoZeroBits(b, 8) != B_PER_W + 6)
  85. return FALSE;
  86. // сдвиги
  87. ASSERT(wwIsRepW(a, 8, 0x5C));
  88. if (wwShHiCarry(a, 8, B_PER_W, 0x5C) != 0x5C ||
  89. !wwIsRepW(a, 8, 0x5C) ||
  90. wwShLoCarry(a, 8, B_PER_W, 0x5C) != 0x5C ||
  91. !wwIsRepW(a, 8, 0x5C) ||
  92. wwShHiCarry(a, 8, B_PER_W - 1, 0x5C) != (0x5C >> 1) ||
  93. !wwIsRepW(a, 8, 0x5C >> 1) ||
  94. wwShLoCarry(a, 8, B_PER_W - 1, 0x5C >> 1) != 0x5C ||
  95. !wwIsRepW(a, 8, 0x5C) ||
  96. (wwShHi(a, 8, B_PER_W), FALSE) ||
  97. !wwIsRepW(a + 1, 7, 0x5C) || a[0] != 0 ||
  98. (wwShLo(a, 8, 2 * B_PER_W), FALSE) ||
  99. !wwIsRepW(a, 6, 0x5C) || a[6] != 0 || a[7] != 0 ||
  100. (wwShHi(a, 6, 2 * B_PER_W - 1), FALSE) ||
  101. !wwIsRepW(a + 2, 4, 0x5C >> 1) ||
  102. (wwShLo(a + 2, 4, 2 * B_PER_W - 1), FALSE) ||
  103. !wwIsRepW(a + 2, 2, 0x5C))
  104. return FALSE;
  105. // все нормально
  106. return TRUE;
  107. }