u32_test.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 2019.07.08
  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. // weight / parity
  38. if (u32Weight(0) != 0 || u32Parity(0) || !u32Parity(1) ||
  39. u32Weight(0xA001) != 3 || !u32Parity(0xA001) ||
  40. u32Weight(0xFFFF) != 16 || u32Parity(0xFFFF) ||
  41. u32Weight(0xF000A001) != 7 || !u32Parity(0xF000A001) ||
  42. u32Weight(0x0E00A001) != 6 || u32Parity(0x0E00A001) ||
  43. u32Weight(0xFFFFFFFF) != 32 || u32Parity(0xFFFFFFFF))
  44. return FALSE;
  45. // CTZ / CLZ
  46. if (SAFE(u32CTZ)(0) != 32 || FAST(u32CTZ)(0) != 32 ||
  47. SAFE(u32CLZ)(0) != 32 || FAST(u32CLZ)(0) != 32 ||
  48. SAFE(u32CTZ)(1) != 0 || FAST(u32CTZ)(1) != 0 ||
  49. SAFE(u32CLZ)(1) != 31 || FAST(u32CLZ)(1) != 31 ||
  50. SAFE(u32CTZ)(0xFFF8) != 3 || FAST(u32CTZ)(0xFFF8) != 3 ||
  51. SAFE(u32CLZ)(0xFFF8) != 16 || FAST(u32CLZ)(0xFFF8) != 16 ||
  52. SAFE(u32CTZ)(0x7FFFE000) != 13 || FAST(u32CTZ)(0x7FFFE000) != 13 ||
  53. SAFE(u32CLZ)(0x7FFFE000) != 1 || FAST(u32CLZ)(0x7FFFE000) != 1)
  54. return FALSE;
  55. // shuffle
  56. if (u32Deshuffle(0) != 0 || u32Deshuffle(1) != 1 ||
  57. u32Deshuffle(2) != 0x00010000 ||
  58. u32Deshuffle(0xAAAAAAAA) != 0xFFFF0000 ||
  59. u32Shuffle(u32Deshuffle(0x76543210)) != 0x76543210 ||
  60. u32Deshuffle(u32Shuffle(0x10FEDCBA)) != 0x10FEDCBA)
  61. return FALSE;
  62. // negInv
  63. if (u32NegInv(1) != U32_MAX ||
  64. u32NegInv(5) != 858993459 || u32NegInv(858993459) != 5)
  65. return FALSE;
  66. // from / to
  67. u32To(b, 7, a), u32From(a, b, 7);
  68. if (a[0] != w || a[1] != 0x00030201)
  69. return FALSE;
  70. // все нормально
  71. return TRUE;
  72. }