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