blob: 88c9bef8e2da6e33760a203c98e74b8f6b6679bc [file] [log] [blame]
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08001#include <stdplus/pinned.hpp>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08003#include <string>
4
Patrick Williamsd1984dd2023-05-10 16:12:44 -05005#include <gtest/gtest.h>
6
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08007namespace stdplus
8{
9
10static_assert(noexcept(Pinned<int>(4)));
11static_assert(!noexcept(Pinned<std::string>("4")));
12
13TEST(Pinned, Basic)
14{
15 EXPECT_EQ("hi", Pinned<std::string>("hi"));
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080016 EXPECT_EQ("hi", Pinned(std::string("hi")));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080017 auto s = std::string("hi");
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080018 EXPECT_EQ("hi", Pinned(s));
19 EXPECT_EQ("hi", Pinned(std::move(s)));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080020 Pinned<std::string> ps = "hi";
21 EXPECT_EQ("hi", Pinned<std::string>(ps));
22 // EXPECT_EQ("hi", Pinned<std::string>(std::move(ps)));
23 ps = "hi";
24 EXPECT_EQ("hi", ps);
25 s = std::string("hi");
26 ps = s;
27 EXPECT_EQ("hi", ps);
28 ps = std::move(s);
29 EXPECT_EQ("hi", ps);
30 Pinned<std::string> ps2;
31 ps2 = ps;
32 EXPECT_EQ("hi", ps2);
33 // ps2 = std::move(ps);
34 // std::string s2 = std::move(ps2);
35 EXPECT_EQ("hi", [](std::string& f) { return f; }(ps2));
36 EXPECT_EQ("hi", [](std::string f) { return f; }(ps2));
37}
38
39TEST(Pinned, Fundamental)
40{
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080041 Pinned pi = 4;
William A. Kennington III49d92692023-06-06 14:02:01 -070042 // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080043 EXPECT_EQ(4, [](int& f) { return f; }(pi));
44 EXPECT_EQ(4, [](int f) { return f; }(pi));
45}
46
47struct NoMove1
48{
49 NoMove1() = default;
50 NoMove1(NoMove1&&) = delete;
51 NoMove1& operator=(NoMove1&&) = default;
52};
53
54struct NoMove2
55{
56 NoMove2() = default;
57 NoMove2(NoMove2&&) = default;
58 NoMove2& operator=(NoMove2&&) = delete;
59};
60
61struct NoMove3
62{
63 NoMove3() = default;
64 NoMove3(NoMove3&&) = delete;
65 NoMove3& operator=(NoMove3&&) = delete;
66};
67
68TEST(PinnedRef, Basic)
69{
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080070 Pinned<std::string> pstr("hi");
71 EXPECT_EQ("hi", PinnedRef<std::string>(pstr).get());
72 EXPECT_EQ("hi", PinnedRef<const std::string>(pstr).get());
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080073 EXPECT_EQ("hi", PinnedRef(pstr).get());
74
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080075 const Pinned<std::string> cpstr("hi");
76 // EXPECT_EQ("hi", PinnedRef<std::string>(cpstr).get());
77 EXPECT_EQ("hi", PinnedRef<const std::string>(cpstr).get());
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080078 EXPECT_EQ("hi", PinnedRef(cpstr).get());
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080079}
80
81TEST(PinnedRef, Fundamental)
82{
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080083 Pinned<int> pi = 4;
84 EXPECT_EQ(4, PinnedRef<int>(pi));
85 EXPECT_EQ(4, PinnedRef<const int>(pi));
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080086 EXPECT_EQ(4, PinnedRef(pi));
87
88 const Pinned<int> cpi = 4;
89 // EXPECT_EQ(4, PinnedRef<int>(cpi));
90 EXPECT_EQ(4, PinnedRef<const int>(cpi));
91 EXPECT_EQ(4, PinnedRef(cpi));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080092}
93
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080094TEST(PinnedRef, NoMove)
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080095{
96 // int i;
97 // PinnedRef<int> ri(i);
98 // NoMove1 nm1;
99 // PinnedRef<NoMove1> rnm1(nm1);
100 // NoMove2 nm2;
101 // PinnedRef<NoMove2> rnm2(nm2);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -0800102
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -0800103 NoMove3 nm3;
104 PinnedRef<NoMove3> rnm3(nm3);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -0800105 PinnedRef<const NoMove3> rnm3c(nm3);
106 PinnedRef<const NoMove3> rnm3cr(rnm3);
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -0800107 PinnedRef rnm3i(nm3);
William A. Kennington III1cad7d72023-06-06 13:50:01 -0700108 [[maybe_unused]] PinnedRef rnm3ir(rnm3);
109 [[maybe_unused]] PinnedRef rnm3ic(rnm3c);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -0800110
111 const NoMove3 cnm3;
112 // PinnedRef<NoMove3> crnm3(cnm3);
113 PinnedRef<const NoMove3> crnm3c(cnm3);
114 PinnedRef crnm3i(cnm3);
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -0800115}
116
117} // namespace stdplus