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 | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 16 | // Tests that buffers are concatenated without being modified. |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 17 | TEST_F(DummyEncoderTest, QueuesBuffersAsIs) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 18 | DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 19 | const auto expected = CreateAndEncode(100, &encoder); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 20 | std::vector<uint8_t> data = Flatten(expected); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 21 | |
| 22 | auto queue = encoder.queue(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 23 | ASSERT_EQ(queue.size(), 1u); |
| 24 | EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data)); |
| 25 | } |
| 26 | |
| 27 | // Tests that buffers are concatenated without being modified. |
| 28 | TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) { |
| 29 | DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize); |
| 30 | const auto expected = CreateAndEncode(100, &encoder); |
| 31 | std::vector<uint8_t> data = Flatten(expected); |
| 32 | |
| 33 | auto queue = encoder.queue(); |
| 34 | ASSERT_EQ(queue.size(), 1u); |
| 35 | EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data)); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Checks that DummyDecoder can read into a buffer. |
| 39 | TEST(DummyDecoderTest, ReadsIntoExactBuffer) { |
| 40 | static const std::string kTestString{"Just some random words."}; |
| 41 | |
| 42 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 43 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 44 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 45 | |
| 46 | // Read the contents of the file into the buffer. |
| 47 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 48 | std::vector<uint8_t> buffer(kTestString.size()); |
| 49 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 50 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString); |
| 51 | |
| 52 | for (int i = 0; i < 10; ++i) { |
| 53 | // Verify that there is no more data to read from the file. |
| 54 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Checks that DummyDecoder can read into a buffer that can accommodate all the |
| 59 | // data in the file. |
| 60 | TEST(DummyDecoderTest, ReadsIntoLargerBuffer) { |
| 61 | static const std::string kTestString{"Just some random words."}; |
| 62 | |
| 63 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 64 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 65 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 66 | |
| 67 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 68 | std::vector<uint8_t> buffer(100); |
| 69 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 70 | buffer.resize(count); |
| 71 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString); |
| 72 | |
| 73 | // Verify that there is no more data to read from the file. |
| 74 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 75 | } |
| 76 | |
| 77 | // Checks that DummyDecoder can repeatedly read the contents of the file into a |
| 78 | // smaller buffer until there is no more to read. |
| 79 | TEST(DummyDecoderTest, ReadsRepeatedlyIntoSmallerBuffer) { |
| 80 | static const std::string kTestString{"Just some random words."}; |
| 81 | |
| 82 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 83 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 84 | std::ofstream(file_path, std::ios::binary) << kTestString; |
| 85 | |
| 86 | DummyDecoder dummy_decoder(file_path.c_str()); |
| 87 | std::vector<uint8_t> buffer((kTestString.size() + 1) / 2); |
| 88 | |
| 89 | { |
| 90 | // Read into our buffer once, and verify the contents. |
| 91 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 92 | ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), |
| 93 | kTestString.substr(0, buffer.size())); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | // Read into the same buffer again, and verify the contents. |
| 98 | const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end()); |
| 99 | ASSERT_EQ( |
| 100 | std::string(buffer.data(), buffer.data() + count), |
| 101 | kTestString.substr(buffer.size(), kTestString.size() - buffer.size())); |
| 102 | } |
| 103 | |
| 104 | // Verify that there is no more data to read from the file. |
| 105 | ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0); |
| 106 | } |
| 107 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 108 | INSTANTIATE_TEST_SUITE_P( |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 109 | Dummy, BufferEncoderTest, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 110 | ::testing::Combine(::testing::Values([](size_t max_buffer_size) { |
| 111 | return std::make_unique<DummyEncoder>(max_buffer_size); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | }), |
| 113 | ::testing::Values([](std::string_view filename) { |
| 114 | return std::make_unique<DummyDecoder>(filename); |
| 115 | }), |
| 116 | ::testing::Range(0, 100))); |
| 117 | |
| 118 | } // namespace aos::logger::testing |