blob: a46d9499c4d40715e9c4bfe0321c78f4c7178bb6 [file] [log] [blame]
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -08001#include <gtest/gtest.h>
2#include <memory>
3#include <stdplus/pinned.hpp>
4#include <string>
5
6namespace stdplus
7{
8
9static_assert(noexcept(Pinned<int>(4)));
10static_assert(!noexcept(Pinned<std::string>("4")));
11
12TEST(Pinned, Basic)
13{
14 EXPECT_EQ("hi", Pinned<std::string>("hi"));
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080015 EXPECT_EQ("hi", Pinned(std::string("hi")));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080016 auto s = std::string("hi");
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080017 EXPECT_EQ("hi", Pinned(s));
18 EXPECT_EQ("hi", Pinned(std::move(s)));
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080019 Pinned<std::string> ps = "hi";
20 EXPECT_EQ("hi", Pinned<std::string>(ps));
21 // EXPECT_EQ("hi", Pinned<std::string>(std::move(ps)));
22 ps = "hi";
23 EXPECT_EQ("hi", ps);
24 s = std::string("hi");
25 ps = s;
26 EXPECT_EQ("hi", ps);
27 ps = std::move(s);
28 EXPECT_EQ("hi", ps);
29 Pinned<std::string> ps2;
30 ps2 = ps;
31 EXPECT_EQ("hi", ps2);
32 // ps2 = std::move(ps);
33 // std::string s2 = std::move(ps2);
34 EXPECT_EQ("hi", [](std::string& f) { return f; }(ps2));
35 EXPECT_EQ("hi", [](std::string f) { return f; }(ps2));
36}
37
38TEST(Pinned, Fundamental)
39{
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080040 Pinned pi = 4;
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080041 EXPECT_EQ(4, [](int& f) { return f; }(pi));
42 EXPECT_EQ(4, [](int f) { return f; }(pi));
43}
44
45struct NoMove1
46{
47 NoMove1() = default;
48 NoMove1(NoMove1&&) = delete;
49 NoMove1& operator=(NoMove1&&) = default;
50};
51
52struct NoMove2
53{
54 NoMove2() = default;
55 NoMove2(NoMove2&&) = default;
56 NoMove2& operator=(NoMove2&&) = delete;
57};
58
59struct NoMove3
60{
61 NoMove3() = default;
62 NoMove3(NoMove3&&) = delete;
63 NoMove3& operator=(NoMove3&&) = delete;
64};
65
66TEST(PinnedRef, Basic)
67{
68 auto uptr = std::make_unique<std::string>("hi");
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080069 PinnedRef(uptr).get()[0] = 'd';
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080070 EXPECT_EQ("di", *uptr);
71 PinnedRef<const std::string> cref(uptr);
72 // cref.get()[0] = 'e';
73 EXPECT_EQ("di", cref.get());
74
75 auto sptr = std::make_shared<std::string>("hi");
76 EXPECT_EQ("hi", PinnedRef<std::string>(sptr).get());
77
78 Pinned<std::string> pstr("hi");
79 EXPECT_EQ("hi", PinnedRef<std::string>(pstr).get());
80 EXPECT_EQ("hi", PinnedRef<const std::string>(pstr).get());
81 const Pinned<std::string> cpstr("hi");
82 // EXPECT_EQ("hi", PinnedRef<std::string>(cpstr).get());
83 EXPECT_EQ("hi", PinnedRef<const std::string>(cpstr).get());
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -080084 EXPECT_EQ("hi", PinnedRef(cpstr).get());
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -080085}
86
87TEST(PinnedRef, Fundamental)
88{
89 auto uptr = std::make_unique<int>(4);
90 EXPECT_EQ(4, PinnedRef<int>(uptr));
91 Pinned<int> pi = 4;
92 EXPECT_EQ(4, PinnedRef<int>(pi));
93 EXPECT_EQ(4, PinnedRef<const int>(pi));
94}
95
96TEST(PinnedREf, NoMove)
97{
98 // int i;
99 // PinnedRef<int> ri(i);
100 // NoMove1 nm1;
101 // PinnedRef<NoMove1> rnm1(nm1);
102 // NoMove2 nm2;
103 // PinnedRef<NoMove2> rnm2(nm2);
104 NoMove3 nm3;
105 PinnedRef<NoMove3> rnm3(nm3);
William A. Kennington IIIc6ca7c22022-12-03 03:50:35 -0800106 PinnedRef rnm3i(nm3);
William A. Kennington IIIcb9ddb62022-11-18 19:50:10 -0800107}
108
109} // namespace stdplus