Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/buffer_encoder.h" |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <fstream> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "glog/logging.h" |
| 8 | #include "gmock/gmock.h" |
| 9 | #include "gtest/gtest.h" |
| 10 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/buffer_encoder_param_test.h" |
| 12 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 13 | namespace aos::logger::testing { |
| 14 | |
| 15 | class DummyEncoderTest : public BufferEncoderBaseTest {}; |
| 16 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 17 | constexpr size_t kEncoderBufferSize = 4 * 1024 * 1024; |
| 18 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 19 | // Tests that buffers are concatenated without being modified. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 20 | TEST_F(DummyEncoderTest, QueuesBuffersAsIs) { |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 21 | DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize, |
| 22 | kEncoderBufferSize); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 23 | const auto expected = CreateAndEncode(100, &encoder); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 24 | std::vector<uint8_t> data = Flatten(expected); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 25 | |
| 26 | auto queue = encoder.queue(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 27 | ASSERT_EQ(queue.size(), 1u); |
| 28 | EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data)); |
| 29 | } |
| 30 | |
| 31 | // Tests that buffers are concatenated without being modified. |
| 32 | TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) { |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 33 | DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize, |
| 34 | kEncoderBufferSize); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 35 | const auto expected = CreateAndEncode(100, &encoder); |
| 36 | std::vector<uint8_t> data = Flatten(expected); |
| 37 | |
| 38 | auto queue = encoder.queue(); |
| 39 | ASSERT_EQ(queue.size(), 1u); |
| 40 | EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data)); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Checks that DummyDecoder can read into a buffer. |
| 44 | TEST(DummyDecoderTest, ReadsIntoExactBuffer) { |
| 45 | static const std::string kTestString{"Just some random words."}; |
| 46 | |
| 47 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 48 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 49 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 50 | |
| 51 | // Read the contents of the file into the buffer. |
| 52 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 53 | std::vector<uint8_t> buffer(kTestString.size()); |
| 54 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 55 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString); |
| 56 | |
| 57 | for (int i = 0; i < 10; ++i) { |
| 58 | // Verify that there is no more data to read from the file. |
| 59 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Checks that DummyDecoder can read into a buffer that can accommodate all the |
| 64 | // data in the file. |
| 65 | TEST(DummyDecoderTest, ReadsIntoLargerBuffer) { |
| 66 | static const std::string kTestString{"Just some random words."}; |
| 67 | |
| 68 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 69 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 70 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 71 | |
| 72 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 73 | std::vector<uint8_t> buffer(100); |
| 74 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 75 | buffer.resize(count); |
| 76 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString); |
| 77 | |
| 78 | // Verify that there is no more data to read from the file. |
| 79 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 80 | } |
| 81 | |
| 82 | // Checks that DummyDecoder can repeatedly read the contents of the file into a |
| 83 | // smaller buffer until there is no more to read. |
| 84 | TEST(DummyDecoderTest, ReadsRepeatedlyIntoSmallerBuffer) { |
| 85 | static const std::string kTestString{"Just some random words."}; |
| 86 | |
| 87 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 88 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 89 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 90 | |
| 91 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 92 | std::vector<uint8_t> buffer((kTestString.size() + 1) / 2); |
| 93 | |
| 94 | { |
| 95 | // Read into our buffer once, and verify the contents. |
| 96 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 97 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), |
| 98 | kTestString.substr(0, buffer.size())); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | // Read into the same buffer again, and verify the contents. |
| 103 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 104 | ASSERT_EQ( |
| 105 | std::string(buffer.data(), buffer.data() + count), |
| 106 | kTestString.substr(buffer.size(), kTestString.size() - buffer.size())); |
| 107 | } |
| 108 | |
| 109 | // Verify that there is no more data to read from the file. |
| 110 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 111 | } |
| 112 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 113 | INSTANTIATE_TEST_SUITE_P( |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 114 | Dummy, BufferEncoderTest, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 115 | ::testing::Combine(::testing::Values([](size_t max_buffer_size) { |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 116 | return std::make_unique<DummyEncoder>( |
| 117 | max_buffer_size, kEncoderBufferSize); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 118 | }), |
| 119 | ::testing::Values([](std::string_view filename) { |
| 120 | return std::make_unique<DummyDecoder>(filename); |
| 121 | }), |
| 122 | ::testing::Range(0, 100))); |
| 123 | |
Austin Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 124 | // Tests that SpanCopier copies as expected. |
| 125 | TEST(SpanCopierTest, Matches) { |
| 126 | std::vector<uint8_t> data; |
| 127 | for (int i = 0; i < 32; ++i) { |
| 128 | data.push_back(i); |
| 129 | } |
| 130 | |
| 131 | CHECK_EQ(data.size(), 32u); |
| 132 | |
| 133 | for (int i = 0; i < 32; i += 8) { |
| 134 | for (int j = i; j < 32; j += 8) { |
| 135 | std::vector<uint8_t> destination(data.size(), 0); |
| 136 | DataEncoder::SpanCopier copier( |
| 137 | absl::Span<const uint8_t>(data.data(), data.size())); |
| 138 | |
| 139 | copier.Copy(destination.data(), i, j); |
| 140 | |
| 141 | size_t index = 0; |
| 142 | for (int k = i; k < j; ++k) { |
| 143 | EXPECT_EQ(destination[index], k); |
| 144 | ++index; |
| 145 | } |
| 146 | for (; index < destination.size(); ++index) { |
| 147 | EXPECT_EQ(destination[index], 0u); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 153 | } // namespace aos::logger::testing |