parseInteger.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <stdint.h>
  8. #include <ArduinoJson/Polyfills/parseInteger.hpp>
  9. #include <catch.hpp>
  10. using namespace ArduinoJson::Polyfills;
  11. template <typename T>
  12. void check(const char* input, T expected) {
  13. CAPTURE(input);
  14. T actual = parseInteger<T>(input);
  15. REQUIRE(expected == actual);
  16. }
  17. TEST_CASE("parseInteger<int8_t>()") {
  18. check<int8_t>("-128", -128);
  19. check<int8_t>("127", 127);
  20. check<int8_t>("+127", 127);
  21. check<int8_t>("3.14", 3);
  22. // check<int8_t>(" 42", 0);
  23. check<int8_t>("x42", 0);
  24. check<int8_t>("128", -128);
  25. check<int8_t>("-129", 127);
  26. check<int8_t>(NULL, 0);
  27. }
  28. TEST_CASE("parseInteger<int16_t>()") {
  29. check<int16_t>("-32768", -32768);
  30. check<int16_t>("32767", 32767);
  31. check<int16_t>("+32767", 32767);
  32. check<int16_t>("3.14", 3);
  33. // check<int16_t>(" 42", 0);
  34. check<int16_t>("x42", 0);
  35. check<int16_t>("-32769", 32767);
  36. check<int16_t>("32768", -32768);
  37. check<int16_t>(NULL, 0);
  38. }
  39. TEST_CASE("parseInteger<int32_t>()") {
  40. check<int32_t>("-2147483648", (-2147483647 - 1));
  41. check<int32_t>("2147483647", 2147483647);
  42. check<int32_t>("+2147483647", 2147483647);
  43. check<int32_t>("3.14", 3);
  44. // check<int32_t>(" 42", 0);
  45. check<int32_t>("x42", 0);
  46. check<int32_t>("-2147483649", 2147483647);
  47. check<int32_t>("2147483648", (-2147483647 - 1));
  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>(" 42", 0);
  55. check<uint8_t>("x42", 0);
  56. check<uint8_t>("-1", 255);
  57. check<uint8_t>("256", 0);
  58. }
  59. TEST_CASE("parseInteger<uint16_t>()") {
  60. check<uint16_t>("0", 0);
  61. check<uint16_t>("65535", 65535);
  62. check<uint16_t>("+65535", 65535);
  63. check<uint16_t>("3.14", 3);
  64. // check<uint16_t>(" 42", 0);
  65. check<uint16_t>("x42", 0);
  66. check<uint16_t>("-1", 65535);
  67. check<uint16_t>("65536", 0);
  68. }