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