blob: 7dda30011763c662d989f62216ea79d89e71cf81 [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
7#include "glog/logging.h"
8#include "gmock/gmock.h"
9#include "gtest/gtest.h"
10
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "aos/events/logging/buffer_encoder_param_test.h"
12
Brian Silvermanf51499a2020-09-21 12:49:08 -070013namespace aos::logger::testing {
14
15class DummyEncoderTest : public BufferEncoderBaseTest {};
16
Austin Schuh8bdfc492023-02-11 12:53:13 -080017constexpr size_t kEncoderBufferSize = 4 * 1024 * 1024;
18
Austin Schuh48d10d62022-10-16 22:19:23 -070019// Tests that buffers are concatenated without being modified.
Brian Silvermanf51499a2020-09-21 12:49:08 -070020TEST_F(DummyEncoderTest, QueuesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080021 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
22 kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -070023 const auto expected = CreateAndEncode(100, &encoder);
Austin Schuh48d10d62022-10-16 22:19:23 -070024 std::vector<uint8_t> data = Flatten(expected);
Brian Silvermanf51499a2020-09-21 12:49:08 -070025
26 auto queue = encoder.queue();
Austin Schuh48d10d62022-10-16 22:19:23 -070027 ASSERT_EQ(queue.size(), 1u);
28 EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
29}
30
31// Tests that buffers are concatenated without being modified.
32TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080033 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
34 kEncoderBufferSize);
Austin Schuh48d10d62022-10-16 22:19:23 -070035 const auto expected = CreateAndEncode(100, &encoder);
36 std::vector<uint8_t> data = Flatten(expected);
37
38 auto queue = encoder.queue();
39 ASSERT_EQ(queue.size(), 1u);
40 EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
Brian Silvermanf51499a2020-09-21 12:49:08 -070041}
42
43// Checks that DummyDecoder can read into a buffer.
44TEST(DummyDecoderTest, ReadsIntoExactBuffer) {
45 static const std::string kTestString{"Just some random words."};
46
47 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
48 const std::string file_path = std::string(test_dir) + "/foo";
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.
65TEST(DummyDecoderTest, ReadsIntoLargerBuffer) {
66 static const std::string kTestString{"Just some random words."};
67
68 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
69 const std::string file_path = std::string(test_dir) + "/foo";
70 std::ofstream(file_path, std::ios::binary) << kTestString;
71
72 DummyDecoder dummy_decoder(file_path.c_str());
73 std::vector<uint8_t> buffer(100);
74 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
75 buffer.resize(count);
76 ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString);
77
78 // Verify that there is no more data to read from the file.
79 ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0);
80}
81
82// Checks that DummyDecoder can repeatedly read the contents of the file into a
83// smaller buffer until there is no more to read.
84TEST(DummyDecoderTest, ReadsRepeatedlyIntoSmallerBuffer) {
85 static const std::string kTestString{"Just some random words."};
86
87 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
88 const std::string file_path = std::string(test_dir) + "/foo";
89 std::ofstream(file_path, std::ios::binary) << kTestString;
90
91 DummyDecoder dummy_decoder(file_path.c_str());
92 std::vector<uint8_t> buffer((kTestString.size() + 1) / 2);
93
94 {
95 // Read into our buffer once, and verify the contents.
96 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
97 ASSERT_EQ(std::string(buffer.data(), buffer.data() + count),
98 kTestString.substr(0, buffer.size()));
99 }
100
101 {
102 // Read into the same buffer again, and verify the contents.
103 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
104 ASSERT_EQ(
105 std::string(buffer.data(), buffer.data() + count),
106 kTestString.substr(buffer.size(), kTestString.size() - buffer.size()));
107 }
108
109 // Verify that there is no more data to read from the file.
110 ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0);
111}
112
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -0700113INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf51499a2020-09-21 12:49:08 -0700114 Dummy, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -0700115 ::testing::Combine(::testing::Values([](size_t max_buffer_size) {
Austin Schuh8bdfc492023-02-11 12:53:13 -0800116 return std::make_unique<DummyEncoder>(
117 max_buffer_size, kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700118 }),
119 ::testing::Values([](std::string_view filename) {
120 return std::make_unique<DummyDecoder>(filename);
121 }),
122 ::testing::Range(0, 100)));
123
Austin Schuh71a40d42023-02-04 21:22:22 -0800124// Tests that SpanCopier copies as expected.
125TEST(SpanCopierTest, Matches) {
126 std::vector<uint8_t> data;
127 for (int i = 0; i < 32; ++i) {
128 data.push_back(i);
129 }
130
131 CHECK_EQ(data.size(), 32u);
132
133 for (int i = 0; i < 32; i += 8) {
134 for (int j = i; j < 32; j += 8) {
135 std::vector<uint8_t> destination(data.size(), 0);
136 DataEncoder::SpanCopier copier(
137 absl::Span<const uint8_t>(data.data(), data.size()));
138
139 copier.Copy(destination.data(), i, j);
140
141 size_t index = 0;
142 for (int k = i; k < j; ++k) {
143 EXPECT_EQ(destination[index], k);
144 ++index;
145 }
146 for (; index < destination.size(); ++index) {
147 EXPECT_EQ(destination[index], 0u);
148 }
149 }
150 }
151}
152
Brian Silvermanf51499a2020-09-21 12:49:08 -0700153} // namespace aos::logger::testing