parseInteger.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <stdint.h>
  5. #include <ArduinoJson/Polyfills/parseInteger.hpp>
  6. #include <catch.hpp>
  7. using namespace ArduinoJson::Internals;
  8. template <typename T>
  9. void check(const char* input, T expected) {
  10. CAPTURE(input);
  11. T actual = parseInteger<T>(input);
  12. REQUIRE(expected == actual);
  13. }
  14. TEST_CASE("parseInteger<int8_t>()") {
  15. check<int8_t>("-128", -128);
  16. check<int8_t>("127", 127);
  17. check<int8_t>("+127", 127);
  18. check<int8_t>("3.14", 3);
  19. check<int8_t>("x42", 0);
  20. check<int8_t>("128", -128);
  21. check<int8_t>("-129", 127);
  22. check<int8_t>(NULL, 0);
  23. check<int8_t>("true", 1);
  24. check<int8_t>("false", 0);
  25. }
  26. TEST_CASE("parseInteger<int16_t>()") {
  27. check<int16_t>("-32768", -32768);
  28. check<int16_t>("32767", 32767);
  29. check<int16_t>("+32767", 32767);
  30. check<int16_t>("3.14", 3);
  31. check<int16_t>("x42", 0);
  32. check<int16_t>("-32769", 32767);
  33. check<int16_t>("32768", -32768);
  34. check<int16_t>(NULL, 0);
  35. check<int16_t>("true", 1);
  36. check<int16_t>("false", 0);
  37. }
  38. TEST_CASE("parseInteger<int32_t>()") {
  39. check<int32_t>("-2147483648", (-2147483647 - 1));
  40. check<int32_t>("2147483647", 2147483647);
  41. check<int32_t>("+2147483647", 2147483647);
  42. check<int32_t>("3.14", 3);
  43. check<int32_t>("x42", 0);
  44. check<int32_t>("-2147483649", 2147483647);
  45. check<int32_t>("2147483648", (-2147483647 - 1));
  46. check<int32_t>("true", 1);
  47. check<int32_t>("false", 0);
  48. }
  49. TEST_CASE("parseInteger<uint8_t>()") {
  50. check<uint8_t>("0", 0);
  51. check<uint8_t>("255", 255);
  52. check<uint8_t>("+255", 255);
  53. check<uint8_t>("3.14", 3);
  54. check<uint8_t>("x42", 0);
  55. check<uint8_t>("-1", 255);
  56. check<uint8_t>("256", 0);
  57. check<uint8_t>("true", 1);
  58. check<uint8_t>("false", 0);
  59. }
  60. TEST_CASE("parseInteger<uint16_t>()") {
  61. check<uint16_t>("0", 0);
  62. check<uint16_t>("65535", 65535);
  63. check<uint16_t>("+65535", 65535);
  64. check<uint16_t>("3.14", 3);
  65. // check<uint16_t>(" 42", 0);
  66. check<uint16_t>("x42", 0);
  67. check<uint16_t>("-1", 65535);
  68. check<uint16_t>("65536", 0);
  69. check<uint16_t>("true", 1);
  70. check<uint16_t>("false", 0);
  71. }