William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 1 | #include <stdplus/pinned.hpp> |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 2 | |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 3 | #include <string> |
| 4 | |
Patrick Williams | d1984dd | 2023-05-10 16:12:44 -0500 | [diff] [blame] | 5 | #include <gtest/gtest.h> |
| 6 | |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 7 | namespace stdplus |
| 8 | { |
| 9 | |
| 10 | static_assert(noexcept(Pinned<int>(4))); |
| 11 | static_assert(!noexcept(Pinned<std::string>("4"))); |
| 12 | |
| 13 | TEST(Pinned, Basic) |
| 14 | { |
| 15 | EXPECT_EQ("hi", Pinned<std::string>("hi")); |
William A. Kennington III | c6ca7c2 | 2022-12-03 03:50:35 -0800 | [diff] [blame] | 16 | EXPECT_EQ("hi", Pinned(std::string("hi"))); |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 17 | auto s = std::string("hi"); |
William A. Kennington III | c6ca7c2 | 2022-12-03 03:50:35 -0800 | [diff] [blame] | 18 | EXPECT_EQ("hi", Pinned(s)); |
| 19 | EXPECT_EQ("hi", Pinned(std::move(s))); |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 20 | 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 | |
| 39 | TEST(Pinned, Fundamental) |
| 40 | { |
William A. Kennington III | c6ca7c2 | 2022-12-03 03:50:35 -0800 | [diff] [blame] | 41 | Pinned pi = 4; |
William A. Kennington III | 49d9269 | 2023-06-06 14:02:01 -0700 | [diff] [blame] | 42 | // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 43 | EXPECT_EQ(4, [](int& f) { return f; }(pi)); |
| 44 | EXPECT_EQ(4, [](int f) { return f; }(pi)); |
| 45 | } |
| 46 | |
| 47 | struct NoMove1 |
| 48 | { |
| 49 | NoMove1() = default; |
| 50 | NoMove1(NoMove1&&) = delete; |
| 51 | NoMove1& operator=(NoMove1&&) = default; |
| 52 | }; |
| 53 | |
| 54 | struct NoMove2 |
| 55 | { |
| 56 | NoMove2() = default; |
| 57 | NoMove2(NoMove2&&) = default; |
| 58 | NoMove2& operator=(NoMove2&&) = delete; |
| 59 | }; |
| 60 | |
| 61 | struct NoMove3 |
| 62 | { |
| 63 | NoMove3() = default; |
| 64 | NoMove3(NoMove3&&) = delete; |
| 65 | NoMove3& operator=(NoMove3&&) = delete; |
| 66 | }; |
| 67 | |
| 68 | TEST(PinnedRef, Basic) |
| 69 | { |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 70 | 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 III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 73 | EXPECT_EQ("hi", PinnedRef(pstr).get()); |
| 74 | |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 75 | 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 III | c6ca7c2 | 2022-12-03 03:50:35 -0800 | [diff] [blame] | 78 | EXPECT_EQ("hi", PinnedRef(cpstr).get()); |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(PinnedRef, Fundamental) |
| 82 | { |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 83 | Pinned<int> pi = 4; |
| 84 | EXPECT_EQ(4, PinnedRef<int>(pi)); |
| 85 | EXPECT_EQ(4, PinnedRef<const int>(pi)); |
William A. Kennington III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 86 | 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 III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 92 | } |
| 93 | |
William A. Kennington III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 94 | TEST(PinnedRef, NoMove) |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 95 | { |
| 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 III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 102 | |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 103 | NoMove3 nm3; |
| 104 | PinnedRef<NoMove3> rnm3(nm3); |
William A. Kennington III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 105 | PinnedRef<const NoMove3> rnm3c(nm3); |
| 106 | PinnedRef<const NoMove3> rnm3cr(rnm3); |
William A. Kennington III | c6ca7c2 | 2022-12-03 03:50:35 -0800 | [diff] [blame] | 107 | PinnedRef rnm3i(nm3); |
William A. Kennington III | 1cad7d7 | 2023-06-06 13:50:01 -0700 | [diff] [blame] | 108 | [[maybe_unused]] PinnedRef rnm3ir(rnm3); |
| 109 | [[maybe_unused]] PinnedRef rnm3ic(rnm3c); |
William A. Kennington III | b6be043 | 2022-12-05 18:48:05 -0800 | [diff] [blame] | 110 | |
| 111 | const NoMove3 cnm3; |
| 112 | // PinnedRef<NoMove3> crnm3(cnm3); |
| 113 | PinnedRef<const NoMove3> crnm3c(cnm3); |
| 114 | PinnedRef crnm3i(cnm3); |
William A. Kennington III | cb9ddb6 | 2022-11-18 19:50:10 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | } // namespace stdplus |