William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame^] | 1 | #include <gtest/gtest.h> |
| 2 | #include <memory> |
| 3 | #include <stdplus/pinned.hpp> |
| 4 | #include <string> |
| 5 | |
| 6 | namespace stdplus |
| 7 | { |
| 8 | |
| 9 | static_assert(noexcept(Pinned<int>(4))); |
| 10 | static_assert(!noexcept(Pinned<std::string>("4"))); |
| 11 | |
| 12 | TEST(Pinned, Basic) |
| 13 | { |
| 14 | EXPECT_EQ("hi", Pinned<std::string>("hi")); |
| 15 | EXPECT_EQ("hi", Pinned<std::string>(std::string("hi"))); |
| 16 | auto s = std::string("hi"); |
| 17 | EXPECT_EQ("hi", Pinned<std::string>(s)); |
| 18 | EXPECT_EQ("hi", Pinned<std::string>(std::move(s))); |
| 19 | 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 | |
| 38 | TEST(Pinned, Fundamental) |
| 39 | { |
| 40 | Pinned<int> pi = 4; |
| 41 | EXPECT_EQ(4, [](int& f) { return f; }(pi)); |
| 42 | EXPECT_EQ(4, [](int f) { return f; }(pi)); |
| 43 | } |
| 44 | |
| 45 | struct NoMove1 |
| 46 | { |
| 47 | NoMove1() = default; |
| 48 | NoMove1(NoMove1&&) = delete; |
| 49 | NoMove1& operator=(NoMove1&&) = default; |
| 50 | }; |
| 51 | |
| 52 | struct NoMove2 |
| 53 | { |
| 54 | NoMove2() = default; |
| 55 | NoMove2(NoMove2&&) = default; |
| 56 | NoMove2& operator=(NoMove2&&) = delete; |
| 57 | }; |
| 58 | |
| 59 | struct NoMove3 |
| 60 | { |
| 61 | NoMove3() = default; |
| 62 | NoMove3(NoMove3&&) = delete; |
| 63 | NoMove3& operator=(NoMove3&&) = delete; |
| 64 | }; |
| 65 | |
| 66 | TEST(PinnedRef, Basic) |
| 67 | { |
| 68 | auto uptr = std::make_unique<std::string>("hi"); |
| 69 | PinnedRef<std::string>(uptr).get()[0] = 'd'; |
| 70 | 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()); |
| 84 | } |
| 85 | |
| 86 | TEST(PinnedRef, Fundamental) |
| 87 | { |
| 88 | auto uptr = std::make_unique<int>(4); |
| 89 | EXPECT_EQ(4, PinnedRef<int>(uptr)); |
| 90 | Pinned<int> pi = 4; |
| 91 | EXPECT_EQ(4, PinnedRef<int>(pi)); |
| 92 | EXPECT_EQ(4, PinnedRef<const int>(pi)); |
| 93 | } |
| 94 | |
| 95 | TEST(PinnedREf, NoMove) |
| 96 | { |
| 97 | // int i; |
| 98 | // PinnedRef<int> ri(i); |
| 99 | // NoMove1 nm1; |
| 100 | // PinnedRef<NoMove1> rnm1(nm1); |
| 101 | // NoMove2 nm2; |
| 102 | // PinnedRef<NoMove2> rnm2(nm2); |
| 103 | NoMove3 nm3; |
| 104 | PinnedRef<NoMove3> rnm3(nm3); |
| 105 | } |
| 106 | |
| 107 | } // namespace stdplus |