toml_test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *******************************************************************************
  3. \file toml_test.c
  4. \brief Tests for TOML
  5. \project bee2/test
  6. \created 2023.08.22
  7. \version 2024.03.01
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include "bee2/core/toml.h"
  13. #include "bee2/core/err.h"
  14. #include "bee2/core/hex.h"
  15. #include "bee2/core/mem.h"
  16. #include "bee2/core/str.h"
  17. #include "bee2/core/util.h"
  18. /*
  19. *******************************************************************************
  20. Кодирование
  21. *******************************************************************************
  22. */
  23. bool_t tomlTestEnc()
  24. {
  25. char toml[1024];
  26. size_t count;
  27. octet octs[16];
  28. size_t sizes[8];
  29. // имя
  30. if (tomlNameIsValid("bare@name") ||
  31. !tomlNameIsValid("bare_name") ||
  32. !tomlNameIsValid("bare-name") ||
  33. !tomlNameIsValid("\'quoted_name\'") ||
  34. !tomlNameIsValid("\"quoted@name\"") ||
  35. tomlNameIsValid("\'quoted_name\"") ||
  36. !tomlNameIsValid("\"\"") ||
  37. !tomlNameIsValid("\'\'") ||
  38. !tomlNameIsValid("dotted.name") ||
  39. !tomlNameIsValid("dotted . name") ||
  40. tomlNameIsValid("dotted..name") ||
  41. !tomlNameIsValid("dotted.\"\".name") ||
  42. !tomlNameIsValid("dotted.\' name \'") ||
  43. !tomlNameIsValid("3.14159265") ||
  44. !tomlNameIsValid("192.168.208.1") ||
  45. tomlNameIsValid("192.168.208.1 "))
  46. return FALSE;
  47. // строка октетов
  48. if (tomlOctsDec(0, 0, "0x") != 2 ||
  49. tomlOctsDec(0, &count, "0x1") != 2 || count != 0 ||
  50. tomlOctsDec(0, &count, "0x123") != 4 || count != 1 ||
  51. tomlOctsDec(0, 0, "0x12") != 4 ||
  52. tomlOctsDec(octs, 0, " 0x1234") != 7 ||
  53. tomlOctsDec(octs, 0, "0x1234 ,") != 7 ||
  54. tomlOctsDec(0, &count, " 0x1234 ") != 8 || count != 2 ||
  55. tomlOctsDec(0, &count, " 0x12\\") != 6 || count != 1 ||
  56. tomlOctsDec(0, &count, " 0x1\\2") != 3 || count != 0 ||
  57. tomlOctsDec(0, &count, "0x12\\\r\n34\n") != 9 || count != 2 ||
  58. tomlOctsDec(0, &count, "0x12 \\\r\n34\n") != 5 || count != 1 ||
  59. tomlOctsDec(0, &count, "0x12\\\r34\n") != SIZE_MAX ||
  60. tomlOctsDec(0, &count, "0x12\\\n\\\n34") != 6 || count != 1 ||
  61. tomlOctsDec(octs, &count, "0x12\\\n34\\\n56") != 12 || count != 3 ||
  62. tomlOctsEnc(0, octs, count) != 8 ||
  63. tomlOctsEnc(toml, octs, count) != 8 ||
  64. !strEq(toml, "0x123456"))
  65. return FALSE;
  66. // неотрицательное целое
  67. if (tomlSizeDec(0, "]") != SIZE_MAX ||
  68. tomlSizeDec(0, "00") != SIZE_MAX ||
  69. tomlSizeDec(0, "01") != SIZE_MAX ||
  70. tomlSizeDec(sizes, "0") != 1 || sizes[0] != 0 ||
  71. tomlSizeDec(sizes, "123") != 3 || sizes[0] != 123 ||
  72. tomlSizeDec(sizes, " 123") != 4 || sizes[0] != 123 ||
  73. tomlSizeDec(sizes, "123 ") != 4 || sizes[0] != 123 ||
  74. tomlSizeDec(sizes, " 123 ") != 5 || sizes[0] != 123 ||
  75. tomlSizeEnc(toml, 0) != 1 || !strEq(toml, "0") ||
  76. tomlSizeEnc(toml, SIZE_MAX) == SIZE_MAX ||
  77. tomlSizeDec(sizes, toml) == SIZE_MAX ||
  78. sizes[0] != SIZE_MAX ||
  79. (toml[strLen(toml) - 1]++, tomlSizeDec(sizes, toml)) != SIZE_MAX)
  80. return FALSE;
  81. // список неотрицательных целых
  82. if (tomlSizesDec(0, 0, "[]") == SIZE_MAX ||
  83. tomlSizesDec(0, &count, "[,]") != 3 || count != 0 ||
  84. tomlSizesDec(0, 0, "[01,2]") != SIZE_MAX ||
  85. tomlSizesDec(0, 0, "[1 [ 2]") != SIZE_MAX ||
  86. tomlSizesDec(0, 0, "[1,,2]") != SIZE_MAX ||
  87. tomlSizesDec(0, 0, "[1,2,]") == SIZE_MAX ||
  88. tomlSizesDec(0, 0, "[1,2,,]") != SIZE_MAX ||
  89. tomlSizesDec(sizes, &count, " [1 , 2] ") != 9 ||
  90. count != 2 || sizes[0] != 1 || sizes[1] != 2 ||
  91. tomlSizesEnc(toml, sizes, count) == SIZE_MAX ||
  92. tomlSizesDec(sizes, &count, " [1 , 2] ") != 9 ||
  93. count != 2 || sizes[0] != 1 || sizes[1] != 2)
  94. return FALSE;
  95. // все хорошо
  96. return TRUE;
  97. }
  98. /*
  99. *******************************************************************************
  100. Интеграция тестов
  101. *******************************************************************************
  102. */
  103. bool_t tomlTest()
  104. {
  105. return tomlTestEnc();
  106. }