blob: 127fb4f91e69f2944492365b12029cbb5d41974e [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"
Austin Schuh6bdcc372024-06-27 14:49:11 -070012#include "aos/testing/tmpdir.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070013
Brian Silvermanf51499a2020-09-21 12:49:08 -070014namespace aos::logger::testing {
15
16class DummyEncoderTest : public BufferEncoderBaseTest {};
17
Austin Schuh8bdfc492023-02-11 12:53:13 -080018constexpr size_t kEncoderBufferSize = 4 * 1024 * 1024;
19
Austin Schuh48d10d62022-10-16 22:19:23 -070020// Tests that buffers are concatenated without being modified.
Brian Silvermanf51499a2020-09-21 12:49:08 -070021TEST_F(DummyEncoderTest, QueuesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080022 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
23 kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -070024 const auto expected = CreateAndEncode(100, &encoder);
Austin Schuh48d10d62022-10-16 22:19:23 -070025 std::vector<uint8_t> data = Flatten(expected);
Brian Silvermanf51499a2020-09-21 12:49:08 -070026
27 auto queue = encoder.queue();
Austin Schuh48d10d62022-10-16 22:19:23 -070028 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.
33TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080034 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
35 kEncoderBufferSize);
Austin Schuh48d10d62022-10-16 22:19:23 -070036 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 Silvermanf51499a2020-09-21 12:49:08 -070042}
43
44// Checks that DummyDecoder can read into a buffer.
45TEST(DummyDecoderTest, ReadsIntoExactBuffer) {
46 static const std::string kTestString{"Just some random words."};
47
Austin Schuh6bdcc372024-06-27 14:49:11 -070048 const std::string file_path = aos::testing::TestTmpDir() + "/foo";
Brian Silvermanf51499a2020-09-21 12:49:08 -070049 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
Austin Schuh6bdcc372024-06-27 14:49:11 -070068 const std::string file_path = aos::testing::TestTmpDir() + "/foo";
Brian Silvermanf51499a2020-09-21 12:49:08 -070069 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.
83TEST(DummyDecoderTest, ReadsRepeatedlyIntoSmallerBuffer) {
84 static const std::string kTestString{"Just some random words."};
85
Austin Schuh6bdcc372024-06-27 14:49:11 -070086 const std::string file_path = aos::testing::TestTmpDir() + "/foo";
Brian Silvermanf51499a2020-09-21 12:49:08 -070087 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 Kuszmaulf4bf9fe2021-05-10 22:58:24 -0700111INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 Dummy, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -0700113 ::testing::Combine(::testing::Values([](size_t max_buffer_size) {
Austin Schuh8bdfc492023-02-11 12:53:13 -0800114 return std::make_unique<DummyEncoder>(
115 max_buffer_size, kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700116 }),
117 ::testing::Values([](std::string_view filename) {
118 return std::make_unique<DummyDecoder>(filename);
119 }),
120 ::testing::Range(0, 100)));
121
Austin Schuh71a40d42023-02-04 21:22:22 -0800122// Tests that SpanCopier copies as expected.
123TEST(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 Silvermanf51499a2020-09-21 12:49:08 -0700151} // namespace aos::logger::testing