diff options
author | Haavard <havardpe@yahoo-inc.com> | 2017-02-09 11:52:57 +0000 |
---|---|---|
committer | Haavard <havardpe@yahoo-inc.com> | 2017-02-09 11:52:57 +0000 |
commit | e8767798f65a8587f1b7ed253deb3dbfc463ffba (patch) | |
tree | 40e64702c9cfd7a8461e833f8983a89e2693ee52 | |
parent | be6fd7e1440e3dc4fe4c971379c81dbbb7a1de8d (diff) |
split out chunked reader
3 files changed, 39 insertions, 36 deletions
diff --git a/vespalib/src/tests/data/input_reader/input_reader_test.cpp b/vespalib/src/tests/data/input_reader/input_reader_test.cpp index 54c0613b6da..9ef127be364 100644 --- a/vespalib/src/tests/data/input_reader/input_reader_test.cpp +++ b/vespalib/src/tests/data/input_reader/input_reader_test.cpp @@ -2,32 +2,18 @@ #include <vespa/vespalib/testkit/test_kit.h> #include <vespa/vespalib/data/memory_input.h> #include <vespa/vespalib/data/input_reader.h> +#include <vespa/vespalib/test/chunked_input.h> #include <algorithm> using namespace vespalib; - -// make sure input is split into chunks -struct ChunkedInput : Input { - Input &input; - ChunkedInput(Input &input_in) : input(input_in) {} - Memory obtain() override { - Memory memory = input.obtain(); - memory.size = std::min(memory.size, size_t(3)); - return memory; - } - Input &evict(size_t bytes) override { - EXPECT_LESS_EQUAL(bytes, 3u); - input.evict(bytes); - return *this; - } -}; +using vespalib::test::ChunkedInput; TEST("input reader smoke test") { const char *data = "abc\n" "foo bar\n" "2 + 2 = 4\n"; MemoryInput memory_input(data); - ChunkedInput input(memory_input); + ChunkedInput input(memory_input, 3); { InputReader src(input); EXPECT_EQUAL(src.get_offset(), 0u); @@ -91,7 +77,7 @@ TEST("require that input can be explicitly failed with custom message") { TEST("require that reading a byte sequence crossing the end of input fails") { const char *data = "1234567890"; MemoryInput memory_input(data); - ChunkedInput input(memory_input); + ChunkedInput input(memory_input, 3); { InputReader src(input); EXPECT_EQUAL(src.read(15), Memory()); diff --git a/vespalib/src/tests/data/lz4_encode_decode/lz4_encode_decode_test.cpp b/vespalib/src/tests/data/lz4_encode_decode/lz4_encode_decode_test.cpp index 75a9aa6e89d..8593a44d3f6 100644 --- a/vespalib/src/tests/data/lz4_encode_decode/lz4_encode_decode_test.cpp +++ b/vespalib/src/tests/data/lz4_encode_decode/lz4_encode_decode_test.cpp @@ -4,24 +4,10 @@ #include <vespa/vespalib/data/lz4_input_decoder.h> #include <vespa/vespalib/data/simple_buffer.h> #include <vespa/vespalib/data/memory_input.h> +#include <vespa/vespalib/test/chunked_input.h> using namespace vespalib; - -// make sure input is split into chunks -struct ChunkedInput : Input { - Input &input; - ChunkedInput(Input &input_in) : input(input_in) {} - Memory obtain() override { - Memory memory = input.obtain(); - memory.size = std::min(memory.size, size_t(3)); - return memory; - } - Input &evict(size_t bytes) override { - EXPECT_LESS_EQUAL(bytes, 3u); - input.evict(bytes); - return *this; - } -}; +using vespalib::test::ChunkedInput; void transfer(Input &input, Output &output) { for (Memory src = input.obtain(); src.size > 0; src = input.obtain()) { @@ -41,14 +27,14 @@ TEST("require that lz4 encode-decode works") { SimpleBuffer encoded; { MemoryInput memory_input(data.get()); - ChunkedInput chunked_input(memory_input); + ChunkedInput chunked_input(memory_input, 3); Lz4OutputEncoder lz4_encoder(encoded, 10); transfer(chunked_input, lz4_encoder); } SimpleBuffer decoded; { MemoryInput memory_input(encoded.get()); - ChunkedInput chunked_input(memory_input); + ChunkedInput chunked_input(memory_input, 3); Lz4InputDecoder input_decoder(chunked_input, 10); transfer(input_decoder, decoded); EXPECT_TRUE(!input_decoder.failed()); diff --git a/vespalib/src/vespa/vespalib/test/chunked_input.h b/vespalib/src/vespa/vespalib/test/chunked_input.h new file mode 100644 index 00000000000..d73adb8782f --- /dev/null +++ b/vespalib/src/vespa/vespalib/test/chunked_input.h @@ -0,0 +1,31 @@ +// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include <vespa/vespalib/data/input.h> + +namespace vespalib { +namespace test { + +/** + * Input filter making sure the input is split into chunks no larger + * than the maximum chunk size given to the constuctor. + **/ +struct ChunkedInput : Input { + Input &input; + size_t max_chunk_size; + ChunkedInput(Input &input_in, size_t max_chunk_size_in) + : input(input_in), max_chunk_size(max_chunk_size_in) {} + Memory obtain() override { + Memory memory = input.obtain(); + memory.size = std::min(memory.size, max_chunk_size); + return memory; + } + Input &evict(size_t bytes) override { + EXPECT_LESS_EQUAL(bytes, max_chunk_size); + input.evict(bytes); + return *this; + } +}; + +} // namespace test +} // namespace vespalib |