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