blob: d5e2e5d6d1017b616d2a72909c0393d711b66b8b [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 Schuh8bdfc492023-02-11 12:53:13 -080016constexpr size_t kEncoderBufferSize = 4 * 1024 * 1024;
17
Austin Schuh48d10d62022-10-16 22:19:23 -070018// Tests that buffers are concatenated without being modified.
Brian Silvermanf51499a2020-09-21 12:49:08 -070019TEST_F(DummyEncoderTest, QueuesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080020 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
21 kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -070022 const auto expected = CreateAndEncode(100, &encoder);
Austin Schuh48d10d62022-10-16 22:19:23 -070023 std::vector<uint8_t> data = Flatten(expected);
Brian Silvermanf51499a2020-09-21 12:49:08 -070024
25 auto queue = encoder.queue();
Austin Schuh48d10d62022-10-16 22:19:23 -070026 ASSERT_EQ(queue.size(), 1u);
27 EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
28}
29
30// Tests that buffers are concatenated without being modified.
31TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) {
Austin Schuh8bdfc492023-02-11 12:53:13 -080032 DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize,
33 kEncoderBufferSize);
Austin Schuh48d10d62022-10-16 22:19:23 -070034 const auto expected = CreateAndEncode(100, &encoder);
35 std::vector<uint8_t> data = Flatten(expected);
36
37 auto queue = encoder.queue();
38 ASSERT_EQ(queue.size(), 1u);
39 EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
Brian Silvermanf51499a2020-09-21 12:49:08 -070040}
41
42// Checks that DummyDecoder can read into a buffer.
43TEST(DummyDecoderTest, ReadsIntoExactBuffer) {
44 static const std::string kTestString{"Just some random words."};
45
46 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
47 const std::string file_path = std::string(test_dir) + "/foo";
48 std::ofstream(file_path, std::ios::binary) << kTestString;
49
50 // Read the contents of the file into the buffer.
51 DummyDecoder dummy_decoder(file_path.c_str());
52 std::vector<uint8_t> buffer(kTestString.size());
53 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
54 ASSERT_EQ(std::string(buffer.data(), buffer.data() + count), kTestString);
55
56 for (int i = 0; i < 10; ++i) {
57 // Verify that there is no more data to read from the file.
58 ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0);
59 }
60}
61
62// Checks that DummyDecoder can read into a buffer that can accommodate all the
63// data in the file.
64TEST(DummyDecoderTest, ReadsIntoLargerBuffer) {
65 static const std::string kTestString{"Just some random words."};
66
67 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
68 const std::string file_path = std::string(test_dir) + "/foo";
69 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
86 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
87 const std::string file_path = std::string(test_dir) + "/foo";
88 std::ofstream(file_path, std::ios::binary) << kTestString;
89
90 DummyDecoder dummy_decoder(file_path.c_str());
91 std::vector<uint8_t> buffer((kTestString.size() + 1) / 2);
92
93 {
94 // Read into our buffer once, and verify the contents.
95 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
96 ASSERT_EQ(std::string(buffer.data(), buffer.data() + count),
97 kTestString.substr(0, buffer.size()));
98 }
99
100 {
101 // Read into the same buffer again, and verify the contents.
102 const size_t count = dummy_decoder.Read(&*buffer.begin(), &*buffer.end());
103 ASSERT_EQ(
104 std::string(buffer.data(), buffer.data() + count),
105 kTestString.substr(buffer.size(), kTestString.size() - buffer.size()));
106 }
107
108 // Verify that there is no more data to read from the file.
109 ASSERT_EQ(dummy_decoder.Read(&*buffer.begin(), &*buffer.end()), 0);
110}
111
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -0700112INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113 Dummy, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -0700114 ::testing::Combine(::testing::Values([](size_t max_buffer_size) {
Austin Schuh8bdfc492023-02-11 12:53:13 -0800115 return std::make_unique<DummyEncoder>(
116 max_buffer_size, kEncoderBufferSize);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700117 }),
118 ::testing::Values([](std::string_view filename) {
119 return std::make_unique<DummyDecoder>(filename);
120 }),
121 ::testing::Range(0, 100)));
122
Austin Schuh71a40d42023-02-04 21:22:22 -0800123// Tests that SpanCopier copies as expected.
124TEST(SpanCopierTest, Matches) {
125 std::vector<uint8_t> data;
126 for (int i = 0; i < 32; ++i) {
127 data.push_back(i);
128 }
129
130 CHECK_EQ(data.size(), 32u);
131
132 for (int i = 0; i < 32; i += 8) {
133 for (int j = i; j < 32; j += 8) {
134 std::vector<uint8_t> destination(data.size(), 0);
135 DataEncoder::SpanCopier copier(
136 absl::Span<const uint8_t>(data.data(), data.size()));
137
138 copier.Copy(destination.data(), i, j);
139
140 size_t index = 0;
141 for (int k = i; k < j; ++k) {
142 EXPECT_EQ(destination[index], k);
143 ++index;
144 }
145 for (; index < destination.size(); ++index) {
146 EXPECT_EQ(destination[index], 0u);
147 }
148 }
149 }
150}
151
Brian Silvermanf51499a2020-09-21 12:49:08 -0700152} // namespace aos::logger::testing