// ArduinoJson - https://arduinojson.org // Copyright © 2014-2024, Benoit BLANCHON // MIT License #include #include #include #include using namespace ArduinoJson::detail; TEST_CASE("Reader") { SECTION("read()") { std::istringstream src("\x01\xFF"); Reader reader(src); REQUIRE(reader.read() == 0x01); REQUIRE(reader.read() == 0xFF); REQUIRE(reader.read() == -1); } SECTION("readBytes() all at once") { std::istringstream src("ABC"); Reader reader(src); char buffer[8] = "abcd"; REQUIRE(reader.readBytes(buffer, 4) == 3); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'd'); } SECTION("readBytes() in two parts") { std::istringstream src("ABCDEF"); Reader reader(src); char buffer[12] = "abcdefg"; REQUIRE(reader.readBytes(buffer, 4) == 4); REQUIRE(reader.readBytes(buffer + 4, 4) == 2); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'D'); REQUIRE(buffer[4] == 'E'); REQUIRE(buffer[5] == 'F'); REQUIRE(buffer[6] == 'g'); } } TEST_CASE("BoundedReader") { SECTION("read") { BoundedReader reader("\x01\xFF", 2); REQUIRE(reader.read() == 0x01); REQUIRE(reader.read() == 0xFF); REQUIRE(reader.read() == -1); REQUIRE(reader.read() == -1); } SECTION("readBytes() all at once") { BoundedReader reader("ABCD", 3); char buffer[8] = "abcd"; REQUIRE(reader.readBytes(buffer, 4) == 3); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'd'); } SECTION("readBytes() in two parts") { BoundedReader reader("ABCDEF", 6); char buffer[8] = "abcdefg"; REQUIRE(reader.readBytes(buffer, 4) == 4); REQUIRE(reader.readBytes(buffer + 4, 4) == 2); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'D'); REQUIRE(buffer[4] == 'E'); REQUIRE(buffer[5] == 'F'); REQUIRE(buffer[6] == 'g'); } } TEST_CASE("Reader") { SECTION("read()") { Reader reader("\x01\xFF\x00\x12"); REQUIRE(reader.read() == 0x01); REQUIRE(reader.read() == 0xFF); REQUIRE(reader.read() == 0); REQUIRE(reader.read() == 0x12); } SECTION("readBytes() all at once") { Reader reader("ABCD"); char buffer[8] = "abcd"; REQUIRE(reader.readBytes(buffer, 3) == 3); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'd'); } SECTION("readBytes() in two parts") { Reader reader("ABCDEF"); char buffer[8] = "abcdefg"; REQUIRE(reader.readBytes(buffer, 4) == 4); REQUIRE(reader.readBytes(buffer + 4, 2) == 2); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'D'); REQUIRE(buffer[4] == 'E'); REQUIRE(buffer[5] == 'F'); REQUIRE(buffer[6] == 'g'); } } TEST_CASE("IteratorReader") { SECTION("read()") { std::string src("\x01\xFF"); IteratorReader reader(src.begin(), src.end()); REQUIRE(reader.read() == 0x01); REQUIRE(reader.read() == 0xFF); REQUIRE(reader.read() == -1); } SECTION("readBytes() all at once") { std::string src("ABC"); IteratorReader reader(src.begin(), src.end()); char buffer[8] = "abcd"; REQUIRE(reader.readBytes(buffer, 4) == 3); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'd'); } SECTION("readBytes() in two parts") { std::string src("ABCDEF"); IteratorReader reader(src.begin(), src.end()); char buffer[12] = "abcdefg"; REQUIRE(reader.readBytes(buffer, 4) == 4); REQUIRE(reader.readBytes(buffer + 4, 4) == 2); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'D'); REQUIRE(buffer[4] == 'E'); REQUIRE(buffer[5] == 'F'); REQUIRE(buffer[6] == 'g'); } } class StreamStub : public Stream { public: StreamStub(const char* s) : stream_(s) {} int read() { return stream_.get(); } size_t readBytes(char* buffer, size_t length) { stream_.read(buffer, static_cast(length)); return static_cast(stream_.gcount()); } private: std::istringstream stream_; }; TEST_CASE("Reader") { SECTION("read()") { StreamStub src("\x01\xFF"); Reader reader(src); REQUIRE(reader.read() == 0x01); REQUIRE(reader.read() == 0xFF); REQUIRE(reader.read() == -1); } SECTION("readBytes() all at once") { StreamStub src("ABC"); Reader reader(src); char buffer[8] = "abcd"; REQUIRE(reader.readBytes(buffer, 4) == 3); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'd'); } SECTION("readBytes() in two parts") { StreamStub src("ABCDEF"); Reader reader(src); char buffer[12] = "abcdefg"; REQUIRE(reader.readBytes(buffer, 4) == 4); REQUIRE(reader.readBytes(buffer + 4, 4) == 2); REQUIRE(buffer[0] == 'A'); REQUIRE(buffer[1] == 'B'); REQUIRE(buffer[2] == 'C'); REQUIRE(buffer[3] == 'D'); REQUIRE(buffer[4] == 'E'); REQUIRE(buffer[5] == 'F'); REQUIRE(buffer[6] == 'g'); } }