blob: 183688e088edf4d4043ee88c784bdcbc426f43ab [file] [log] [blame]
James Kuszmaul731a05d2022-01-07 17:59:26 -08001#include "aos/util/scoped_pipe.h"
2
3#include <array>
4#include <string>
5
6#include "gtest/gtest.h"
7
8namespace aos {
9namespace util {
10namespace testing {
11
12// Tests using uint32_t read/write methods on the ScopedPipe objects.
13TEST(ScopedPipeTest, IntegerPipe) {
14 std::tuple<ScopedPipe::ScopedReadPipe, ScopedPipe::ScopedWritePipe>
15 pipe = ScopedPipe::MakePipe();
16 ASSERT_FALSE(std::get<0>(pipe).Read().has_value())
17 << "Shouldn't get anything on empty read.";
18 std::get<1>(pipe).Write(971);
19 ASSERT_EQ(971, std::get<0>(pipe).Read().value());
20}
21
22// Tests using string read/write methods on the ScopedPipe objects.
23TEST(ScopedPipeTest, StringPipe) {
24 std::tuple<ScopedPipe::ScopedReadPipe, ScopedPipe::ScopedWritePipe>
25 pipe = ScopedPipe::MakePipe();
26 std::string buffer;
27 ASSERT_EQ(0u, std::get<0>(pipe).Read(&buffer))
28 << "Shouldn't get anything on empty read.";
29 ASSERT_TRUE(buffer.empty());
30
31 const char *const kAbc = "abcdef";
32 std::get<1>(pipe).Write(
33 absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(kAbc), 6));
34 ASSERT_EQ(6u, std::get<0>(pipe).Read(&buffer));
35 ASSERT_EQ("abcdef", buffer);
36
37 std::array<uint8_t, 10000> large_buffer;
38 large_buffer.fill(99);
39 std::get<1>(pipe).Write(
40 absl::Span<const uint8_t>(large_buffer.data(), large_buffer.size()));
41 ASSERT_EQ(large_buffer.size(), std::get<0>(pipe).Read(&buffer));
42 for (size_t ii = 0; ii < large_buffer.size(); ++ii) {
43 ASSERT_EQ(large_buffer[ii], buffer[ii + 6]);
44 }
45}
46
47} // namespace testing
48} // namespace util
49} // namespace aos