u32_test.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. *******************************************************************************
  3. \file u32_test.c
  4. \brief Tests for operations on 32-bit words
  5. \project bee2/test
  6. \created 2017.01.11
  7. \version 2024.11.18
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include <bee2/core/u32.h>
  13. /*
  14. *******************************************************************************
  15. Тестирование
  16. *******************************************************************************
  17. */
  18. bool_t u32Test()
  19. {
  20. u32 w = 0x01020304;
  21. u32 a[2] = {0x01020304, 0x04030201};
  22. octet b[8];
  23. // rot
  24. if (u32RotHi(w, 1) != 0x02040608 ||
  25. u32RotHi(w, 4) != 0x10203040 ||
  26. u32RotHi(w, 8) != 0x02030401 ||
  27. u32RotLo(u32RotHi(w, 7), 7) != w ||
  28. u32RotLo(u32RotHi(w, 19), 19) != w ||
  29. u32RotLo(u32RotHi(w, 23), 23) != w)
  30. return FALSE;
  31. // reverse
  32. if (u32Rev(w) != a[1] || u32Rev(a[1]) != w)
  33. return FALSE;
  34. u32Rev2(a, 2), u32Rev2(a, 2);
  35. if (a[0] != w || a[1] != u32Rev(w))
  36. return FALSE;
  37. // bit reverse
  38. if (u32Bitrev(w) != 0x20C04080 || u32Bitrev(u32Bitrev(w)) != w)
  39. return FALSE;
  40. // weight / parity
  41. if (u32Weight(0) != 0 || u32Parity(0) || !u32Parity(1) ||
  42. u32Weight(0xA001) != 3 || !u32Parity(0xA001) ||
  43. u32Weight(0xFFFF) != 16 || u32Parity(0xFFFF) ||
  44. u32Weight(0xF000A001) != 7 || !u32Parity(0xF000A001) ||
  45. u32Weight(0x0E00A001) != 6 || u32Parity(0x0E00A001) ||
  46. u32Weight(0xFFFFFFFF) != 32 || u32Parity(0xFFFFFFFF))
  47. return FALSE;
  48. // CTZ / CLZ
  49. if (SAFE(u32CTZ)(0) != 32 || FAST(u32CTZ)(0) != 32 ||
  50. SAFE(u32CLZ)(0) != 32 || FAST(u32CLZ)(0) != 32 ||
  51. SAFE(u32CTZ)(1) != 0 || FAST(u32CTZ)(1) != 0 ||
  52. SAFE(u32CLZ)(1) != 31 || FAST(u32CLZ)(1) != 31 ||
  53. SAFE(u32CTZ)(0xFFF8) != 3 || FAST(u32CTZ)(0xFFF8) != 3 ||
  54. SAFE(u32CLZ)(0xFFF8) != 16 || FAST(u32CLZ)(0xFFF8) != 16 ||
  55. SAFE(u32CTZ)(0x7FFFE000) != 13 || FAST(u32CTZ)(0x7FFFE000) != 13 ||
  56. SAFE(u32CLZ)(0x7FFFE000) != 1 || FAST(u32CLZ)(0x7FFFE000) != 1)
  57. return FALSE;
  58. // shuffle
  59. if (u32Deshuffle(0) != 0 || u32Deshuffle(1) != 1 ||
  60. u32Deshuffle(2) != 0x00010000 ||
  61. u32Deshuffle(0xAAAAAAAA) != 0xFFFF0000 ||
  62. u32Shuffle(u32Deshuffle(0x76543210)) != 0x76543210 ||
  63. u32Deshuffle(u32Shuffle(0x10FEDCBA)) != 0x10FEDCBA)
  64. return FALSE;
  65. // negInv
  66. if (u32NegInv(1) != U32_MAX ||
  67. u32NegInv(5) != 858993459 || u32NegInv(858993459) != 5)
  68. return FALSE;
  69. // from / to
  70. u32To(b, 7, a), u32From(a, b, 7);
  71. if (a[0] != w || a[1] != 0x00030201)
  72. return FALSE;
  73. // все нормально
  74. return TRUE;
  75. }