Skip to content

Commit

Permalink
Use string_view in BoundedReader test
Browse files Browse the repository at this point in the history
  • Loading branch information
gibber9809 committed Dec 13, 2024
1 parent 4dcb7ff commit d261ca2
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions components/core/tests/test-BoundedReader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <array>
#include <cstddef>
#include <string>
#include <string_view>

#include <Catch2/single_include/catch2/catch.hpp>

Expand All @@ -9,38 +10,37 @@
#include "../src/clp/StringReader.hpp"

TEST_CASE("Test Bounded Reader", "[BoundedReader]") {
constexpr char cTestString[]{"0123456789"};
constexpr size_t cTestStringLen{std::char_traits<char>::length(cTestString)};
constexpr std::string_view cTestString{"0123456789"};

SECTION("BoundedReader does not support try_read_to_delimiter") {
clp::StringReader string_reader;
string_reader.open(cTestString);
clp::BoundedReader bounded_reader{&string_reader, cTestStringLen};
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, cTestString.size()};
std::string tmp;
REQUIRE(clp::ErrorCode_Unsupported
== bounded_reader.try_read_to_delimiter('0', false, false, tmp));
}

SECTION("BoundedReader does not allow reads beyond end of underlying stream.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
clp::BoundedReader bounded_reader{&string_reader, cTestStringLen + 1};
std::array<char, cTestStringLen + 1> buf{};
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, cTestString.size() + 1};
std::array<char, cTestString.size() + 1> buf{};
size_t num_bytes_read{};
auto rc = bounded_reader.try_read(buf.data(), cTestStringLen + 1, num_bytes_read);
auto rc = bounded_reader.try_read(buf.data(), cTestString.size() + 1, num_bytes_read);
REQUIRE(clp::ErrorCode_Success == rc);
REQUIRE(num_bytes_read == cTestStringLen);
REQUIRE(cTestStringLen == string_reader.get_pos());
REQUIRE(cTestStringLen == bounded_reader.get_pos());
REQUIRE(num_bytes_read == cTestString.size());
REQUIRE(cTestString.size() == string_reader.get_pos());
REQUIRE(cTestString.size() == bounded_reader.get_pos());
}

SECTION("BoundedReader does not allow reads beyond checkpoint.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, 1};
std::array<char, cTestStringLen> buf{};
std::array<char, cTestString.size()> buf{};
size_t num_bytes_read{};
auto rc = bounded_reader.try_read(buf.data(), cTestStringLen, num_bytes_read);
auto rc = bounded_reader.try_read(buf.data(), cTestString.size(), num_bytes_read);
REQUIRE(clp::ErrorCode_Success == rc);
REQUIRE(1 == num_bytes_read);
REQUIRE(1 == string_reader.get_pos());
Expand All @@ -54,7 +54,7 @@ TEST_CASE("Test Bounded Reader", "[BoundedReader]") {

SECTION("BoundedReader does allow reads before checkpoint.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, 1};
char buf{};
size_t num_bytes_read{};
Expand All @@ -67,28 +67,28 @@ TEST_CASE("Test Bounded Reader", "[BoundedReader]") {

SECTION("BoundedReader does not allow seeks beyond end of underlying stream.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
clp::BoundedReader bounded_reader{&string_reader, cTestStringLen + 1};
auto rc = bounded_reader.try_seek_from_begin(cTestStringLen + 1);
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, cTestString.size() + 1};
auto rc = bounded_reader.try_seek_from_begin(cTestString.size() + 1);
REQUIRE(clp::ErrorCode_EndOfFile == rc);
REQUIRE(cTestStringLen == string_reader.get_pos());
REQUIRE(cTestStringLen == bounded_reader.get_pos());
REQUIRE(cTestString.size() == string_reader.get_pos());
REQUIRE(cTestString.size() == bounded_reader.get_pos());
}

SECTION("BoundedReader does not allow seeks beyond checkpoint.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, 1};
size_t num_bytes_read{};
auto rc = bounded_reader.try_seek_from_begin(cTestStringLen);
auto rc = bounded_reader.try_seek_from_begin(cTestString.size());
REQUIRE(clp::ErrorCode_EndOfFile == rc);
REQUIRE(1 == string_reader.get_pos());
REQUIRE(1 == bounded_reader.get_pos());
}

SECTION("BoundedReader does allow seeks before checkpoint.") {
clp::StringReader string_reader;
string_reader.open(cTestString);
string_reader.open(std::string{cTestString});
clp::BoundedReader bounded_reader{&string_reader, 2};
size_t num_bytes_read{};
auto rc = bounded_reader.try_seek_from_begin(1);
Expand Down

0 comments on commit d261ca2

Please sign in to comment.