u16_test.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. *******************************************************************************
  3. \file u16_test.c
  4. \brief Tests for operations on 16-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/u16.h>
  13. /*
  14. *******************************************************************************
  15. Тестирование
  16. *******************************************************************************
  17. */
  18. bool_t u16Test()
  19. {
  20. u16 w = 0x0102;
  21. u16 a[2] = {0x0102, 0x0201};
  22. octet b[4];
  23. // rot
  24. if (u16RotHi(w, 1) != 0x0204 ||
  25. u16RotHi(w, 4) != 0x1020 ||
  26. u16RotHi(w, 8) != 0x0201 ||
  27. u16RotLo(u16RotHi(w, 7), 7) != w ||
  28. u16RotLo(u16RotHi(w, 9), 9) != w ||
  29. u16RotLo(u16RotHi(w, 13), 13) != w)
  30. return FALSE;
  31. // reverse
  32. if (u16Rev(w) != a[1] || u16Rev(a[1]) != w)
  33. return FALSE;
  34. u16Rev2(a, 2), u16Rev2(a, 2);
  35. if (a[0] != w || a[1] != u16Rev(w))
  36. return FALSE;
  37. // bit reverse
  38. if (u16Bitrev(w) != 0x4080|| u16Bitrev(u16Bitrev(w)) != w)
  39. return FALSE;
  40. // weight / parity
  41. if (u16Weight(0) != 0 || u16Parity(0) || !u16Parity(1) ||
  42. u16Weight(0xA001) != 3 || !u16Parity(0xA001) ||
  43. u16Weight(0xFFFF) != 16 || u16Parity(0xFFFF) ||
  44. u16Weight(0xFFF8) != 13)
  45. return FALSE;
  46. // CTZ / CLZ
  47. if (SAFE(u16CTZ)(0) != 16 || FAST(u16CTZ)(0) != 16 ||
  48. SAFE(u16CLZ)(0) != 16 || FAST(u16CLZ)(0) != 16 ||
  49. SAFE(u16CTZ)(1) != 0 || FAST(u16CTZ)(1) != 0 ||
  50. SAFE(u16CLZ)(1) != 15 || FAST(u16CLZ)(1) != 15 ||
  51. SAFE(u16CTZ)(0xFFF8) != 3 || FAST(u16CTZ)(0xFFF8) != 3 ||
  52. SAFE(u16CLZ)(0xFFF8) != 0 || FAST(u16CLZ)(0xFFF8) != 0
  53. )
  54. return FALSE;
  55. // shuffle
  56. if (u16Deshuffle(0) != 0 || u16Deshuffle(1) != 1 ||
  57. u16Deshuffle(2) != 0x0100 ||
  58. u16Deshuffle(0xAAAA) != 0xFF00 ||
  59. u16Shuffle(u16Deshuffle(0x3210)) != 0x3210 ||
  60. u16Deshuffle(u16Shuffle(0xDCBA)) != 0xDCBA)
  61. return FALSE;
  62. // negInv
  63. if (u16NegInv(1) != U16_MAX ||
  64. u16NegInv(5) != 13107 || u16NegInv(13107) != 5)
  65. return FALSE;
  66. // from / to
  67. u16To(b, 3, a), u16From(a, b, 3);
  68. if (a[0] != w || a[1] != 0x0001)
  69. return FALSE;
  70. // все нормально
  71. return TRUE;
  72. }