util_test.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. *******************************************************************************
  3. \file util_test.c
  4. \brief Tests for utilities
  5. \project bee2/test
  6. \author (C) Sergey Agievich [agievich@{bsu.by|gmail.com}]
  7. \created 2017.01.17
  8. \version 2021.05.18
  9. \license This program is released under the GNU General Public License
  10. version 3. See Copyright Notices in bee2/info.h.
  11. *******************************************************************************
  12. */
  13. #include <stdio.h>
  14. #include <bee2/core/util.h>
  15. /*
  16. *******************************************************************************
  17. Информация о runtime-среде и настройках
  18. *******************************************************************************
  19. */
  20. const char* utilInfo()
  21. {
  22. static char descr[128];
  23. sprintf(descr, "%s,B_PER_W=%d,B_PER_S=%d,%s",
  24. (OCTET_ORDER == LITTLE_ENDIAN) ? "LITTLE_ENDIAN" : "BIG_ENDIAN",
  25. B_PER_W, B_PER_S,
  26. #ifdef SAFE_FAST
  27. "FAST"
  28. #else
  29. "SAFE"
  30. #endif
  31. );
  32. return descr;
  33. }
  34. /*
  35. *******************************************************************************
  36. Тестирование
  37. Тест для FNV32: http://isthe.com/chongo/tech/comp/fnv/##zero-hash##67.
  38. *******************************************************************************
  39. */
  40. static size_t _ctr = 5;
  41. static void destroy1()
  42. {
  43. volatile size_t x = (_ctr == 2) ? 1 : 0;
  44. // увы, результат проверки станет известен только on_exit
  45. _ctr = 1 / x;
  46. }
  47. static void destroy2()
  48. {
  49. _ctr--;
  50. }
  51. bool_t utilTest()
  52. {
  53. printf("utilVersion: %s [%s]\n", utilVersion(), utilInfo());
  54. if (utilMin(5, SIZE_1, (size_t)2, (size_t)3, SIZE_1, SIZE_0) != 0 ||
  55. utilMax(5, SIZE_1, (size_t)2, (size_t)3, SIZE_1, SIZE_0) != 3)
  56. return FALSE;
  57. // деструкторы
  58. if (!utilOnExit(destroy1) || !utilOnExit(destroy2) ||
  59. !utilOnExit(destroy2) || !utilOnExit(destroy2))
  60. return FALSE;
  61. // контрольные суммы
  62. if (utilCRC32("123456789", 9, 0) != 0xCBF43926)
  63. return FALSE;
  64. if (utilFNV32("3pjNqM", 6, 0x811C9DC5) != 0)
  65. return FALSE;
  66. // все нормально
  67. return TRUE;
  68. }