Skip to content

Commit

Permalink
Add missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Apr 13, 2021
1 parent 29b43e4 commit 133aed2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions extras/tests/TextFormatter/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@

add_executable(TextFormatterTests
writeFloat.cpp
writeInteger.cpp
writeString.cpp
)

Expand Down
55 changes: 55 additions & 0 deletions extras/tests/TextFormatter/writeInteger.cpp
@@ -0,0 +1,55 @@
// ArduinoJson - https://arduinojson.org
// Copyright Benoit Blanchon 2014-2021
// MIT License

#include <catch.hpp>
#include <limits>
#include <string>

#include <ArduinoJson/Json/TextFormatter.hpp>
#include <ArduinoJson/Serialization/Writer.hpp>

using namespace ARDUINOJSON_NAMESPACE;

template <typename T>
void checkWriteInteger(T value, std::string expected) {
char output[1024];
StaticStringWriter sb(output, sizeof(output));
TextFormatter<StaticStringWriter> writer(sb);
writer.writeInteger<T>(value);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());
}

TEST_CASE("int8_t") {
checkWriteInteger<int8_t>(0, "0");
checkWriteInteger<int8_t>(-128, "-128");
checkWriteInteger<int8_t>(127, "127");
}

TEST_CASE("uint8_t") {
checkWriteInteger<uint8_t>(0, "0");
checkWriteInteger<uint8_t>(255, "255");
}

TEST_CASE("int16_t") {
checkWriteInteger<int16_t>(0, "0");
checkWriteInteger<int16_t>(-32768, "-32768");
checkWriteInteger<int16_t>(32767, "32767");
}

TEST_CASE("uint16_t") {
checkWriteInteger<uint16_t>(0, "0");
checkWriteInteger<uint16_t>(65535, "65535");
}

TEST_CASE("int32_t") {
checkWriteInteger<int32_t>(0, "0");
checkWriteInteger<int32_t>(-2147483647 - 1, "-2147483648");
checkWriteInteger<int32_t>(2147483647, "2147483647");
}

TEST_CASE("uint32_t") {
checkWriteInteger<uint32_t>(0, "0");
checkWriteInteger<uint32_t>(4294967295U, "4294967295");
}
1 change: 0 additions & 1 deletion src/ArduinoJson/Json/TextFormatter.hpp
Expand Up @@ -92,7 +92,6 @@ class TextFormatter {
unsigned_type unsigned_value;
if (value < 0) {
writeRaw('-');
// TODO: check -128
unsigned_value = static_cast<unsigned_type>(-value);
} else {
unsigned_value = static_cast<unsigned_type>(value);
Expand Down

0 comments on commit 133aed2

Please sign in to comment.