blob: 8e12e37e3a279098ccbd1ffa16e1b2d3671f5872 [file] [log] [blame]
Brian Silvermanf51499a2020-09-21 12:49:08 -07001#include "aos/events/logging/buffer_encoder.h"
2
3#include <algorithm>
4#include <fstream>
5#include <string>
6
Austin Schuh60e77942022-05-16 17:48:24 -07007#include "aos/events/logging/buffer_encoder_param_test.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07008#include "glog/logging.h"
9#include "gmock/gmock.h"
10#include "gtest/gtest.h"
11
Brian Silvermanf51499a2020-09-21 12:49:08 -070012namespace aos::logger::testing {
13
14class DummyEncoderTest : public BufferEncoderBaseTest {};
15
Austin Schuh48d10d62022-10-16 22:19:23 -070016// Tests that buffers are concatenated without being modified.
Brian Silvermanf51499a2020-09-21 12:49:08 -070017TEST_F(DummyEncoderTest, QueuesBuffersAsIs) {
Austin Schuh48d10d62022-10-16 22:19:23 -070018 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -070019 const auto expected = CreateAndEncode(100, &encoder);
Austin Schuh48d10d62022-10-16 22:19:23 -070020 std::vector<uint8_t> data = Flatten(expected);
Brian Silvermanf51499a2020-09-21 12:49:08 -070021
22 auto queue = encoder.queue();
Austin Schuh48d10d62022-10-16 22:19:23 -070023 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.
28TEST_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 Silvermanf51499a2020-09-21 12:49:08 -070036}
37
38// Checks that DummyDecoder can read into a buffer.
39TEST(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.
60TEST(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.
79TEST(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 Kuszmaulf4bf9fe2021-05-10 22:58:24 -0700108INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf51499a2020-09-21 12:49:08 -0700109 Dummy, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -0700110 ::testing::Combine(::testing::Values([](size_t max_buffer_size) {
111 return std::make_unique<DummyEncoder>(max_buffer_size);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 }),
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