writeInteger.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <catch.hpp>
  5. #include <limits>
  6. #include <string>
  7. #include <ArduinoJson/Json/TextFormatter.hpp>
  8. #include <ArduinoJson/Serialization/Writer.hpp>
  9. using namespace ARDUINOJSON_NAMESPACE;
  10. template <typename T>
  11. void checkWriteInteger(T value, std::string expected) {
  12. char output[64] = {0};
  13. StaticStringWriter sb(output, sizeof(output));
  14. TextFormatter<StaticStringWriter> writer(sb);
  15. writer.writeInteger<T>(value);
  16. REQUIRE(expected == output);
  17. REQUIRE(writer.bytesWritten() == expected.size());
  18. }
  19. TEST_CASE("int8_t") {
  20. checkWriteInteger<int8_t>(0, "0");
  21. checkWriteInteger<int8_t>(-128, "-128");
  22. checkWriteInteger<int8_t>(127, "127");
  23. }
  24. TEST_CASE("uint8_t") {
  25. checkWriteInteger<uint8_t>(0, "0");
  26. checkWriteInteger<uint8_t>(255, "255");
  27. }
  28. TEST_CASE("int16_t") {
  29. checkWriteInteger<int16_t>(0, "0");
  30. checkWriteInteger<int16_t>(-32768, "-32768");
  31. checkWriteInteger<int16_t>(32767, "32767");
  32. }
  33. TEST_CASE("uint16_t") {
  34. checkWriteInteger<uint16_t>(0, "0");
  35. checkWriteInteger<uint16_t>(65535, "65535");
  36. }
  37. TEST_CASE("int32_t") {
  38. checkWriteInteger<int32_t>(0, "0");
  39. checkWriteInteger<int32_t>(-2147483647 - 1, "-2147483648");
  40. checkWriteInteger<int32_t>(2147483647, "2147483647");
  41. }
  42. TEST_CASE("uint32_t") {
  43. checkWriteInteger<uint32_t>(0, "0");
  44. checkWriteInteger<uint32_t>(4294967295U, "4294967295");
  45. }