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