blob: f33f41aecddab8c1fa87862ddf7a7c66cb17461d [file] [log] [blame]
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08001#include <gtest/gtest.h>
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08002#include <stdplus/pinned.hpp>
3#include <string>
4
5namespace stdplus
6{
7
8static_assert(noexcept(Pinned<int>(4)));
9static_assert(!noexcept(Pinned<std::string>("4")));
10
11TEST(Pinned, Basic)
12{
13 EXPECT_EQ("hi", Pinned<std::string>("hi"));
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080014 EXPECT_EQ("hi", Pinned(std::string("hi")));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080015 auto s = std::string("hi");
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080016 EXPECT_EQ("hi", Pinned(s));
17 EXPECT_EQ("hi", Pinned(std::move(s)));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080018 Pinned<std::string> ps = "hi";
19 EXPECT_EQ("hi", Pinned<std::string>(ps));
20 // EXPECT_EQ("hi", Pinned<std::string>(std::move(ps)));
21 ps = "hi";
22 EXPECT_EQ("hi", ps);
23 s = std::string("hi");
24 ps = s;
25 EXPECT_EQ("hi", ps);
26 ps = std::move(s);
27 EXPECT_EQ("hi", ps);
28 Pinned<std::string> ps2;
29 ps2 = ps;
30 EXPECT_EQ("hi", ps2);
31 // ps2 = std::move(ps);
32 // std::string s2 = std::move(ps2);
33 EXPECT_EQ("hi", [](std::string& f) { return f; }(ps2));
34 EXPECT_EQ("hi", [](std::string f) { return f; }(ps2));
35}
36
37TEST(Pinned, Fundamental)
38{
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080039 Pinned pi = 4;
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080040 EXPECT_EQ(4, [](int& f) { return f; }(pi));
41 EXPECT_EQ(4, [](int f) { return f; }(pi));
42}
43
44struct NoMove1
45{
46 NoMove1() = default;
47 NoMove1(NoMove1&&) = delete;
48 NoMove1& operator=(NoMove1&&) = default;
49};
50
51struct NoMove2
52{
53 NoMove2() = default;
54 NoMove2(NoMove2&&) = default;
55 NoMove2& operator=(NoMove2&&) = delete;
56};
57
58struct NoMove3
59{
60 NoMove3() = default;
61 NoMove3(NoMove3&&) = delete;
62 NoMove3& operator=(NoMove3&&) = delete;
63};
64
65TEST(PinnedRef, Basic)
66{
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080067 Pinned<std::string> pstr("hi");
68 EXPECT_EQ("hi", PinnedRef<std::string>(pstr).get());
69 EXPECT_EQ("hi", PinnedRef<const std::string>(pstr).get());
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080070 EXPECT_EQ("hi", PinnedRef(pstr).get());
71
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080072 const Pinned<std::string> cpstr("hi");
73 // EXPECT_EQ("hi", PinnedRef<std::string>(cpstr).get());
74 EXPECT_EQ("hi", PinnedRef<const std::string>(cpstr).get());
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080075 EXPECT_EQ("hi", PinnedRef(cpstr).get());
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080076}
77
78TEST(PinnedRef, Fundamental)
79{
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080080 Pinned<int> pi = 4;
81 EXPECT_EQ(4, PinnedRef<int>(pi));
82 EXPECT_EQ(4, PinnedRef<const int>(pi));
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080083 EXPECT_EQ(4, PinnedRef(pi));
84
85 const Pinned<int> cpi = 4;
86 // EXPECT_EQ(4, PinnedRef<int>(cpi));
87 EXPECT_EQ(4, PinnedRef<const int>(cpi));
88 EXPECT_EQ(4, PinnedRef(cpi));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080089}
90
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080091TEST(PinnedRef, NoMove)
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080092{
93 // int i;
94 // PinnedRef<int> ri(i);
95 // NoMove1 nm1;
96 // PinnedRef<NoMove1> rnm1(nm1);
97 // NoMove2 nm2;
98 // PinnedRef<NoMove2> rnm2(nm2);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -080099
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -0800100 NoMove3 nm3;
101 PinnedRef<NoMove3> rnm3(nm3);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -0800102 PinnedRef<const NoMove3> rnm3c(nm3);
103 PinnedRef<const NoMove3> rnm3cr(rnm3);
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -0800104 PinnedRef rnm3i(nm3);
William A. Kennington IIIb6be0432022-12-05 18:48:05 -0800105 PinnedRef rnm3ir(rnm3);
106 PinnedRef rnm3ic(rnm3c);
107
108 const NoMove3 cnm3;
109 // PinnedRef<NoMove3> crnm3(cnm3);
110 PinnedRef<const NoMove3> crnm3c(cnm3);
111 PinnedRef crnm3i(cnm3);
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -0800112}
113
114} // namespace stdplus