1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <ftl/algorithm.h>
18 #include <ftl/non_null.h>
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <string_view>
25 #include <type_traits>
26 #include <unordered_set>
27 #include <vector>
28 
29 namespace android::test {
30 namespace {
31 
get_length(const ftl::NonNull<std::shared_ptr<std::string>> & string_ptr,ftl::NonNull<std::size_t * > length_ptr)32 void get_length(const ftl::NonNull<std::shared_ptr<std::string>>& string_ptr,
33                 ftl::NonNull<std::size_t*> length_ptr) {
34   // No need for `nullptr` checks.
35   *length_ptr = string_ptr->length();
36 }
37 
38 using Pair = std::pair<ftl::NonNull<std::shared_ptr<int>>, std::shared_ptr<int>>;
39 
dupe_if(ftl::NonNull<std::unique_ptr<int>> non_null_ptr,bool condition)40 Pair dupe_if(ftl::NonNull<std::unique_ptr<int>> non_null_ptr, bool condition) {
41   // Move the underlying pointer out, so `non_null_ptr` must not be accessed after this point.
42   auto unique_ptr = std::move(non_null_ptr).take();
43 
44   auto non_null_shared_ptr = ftl::as_non_null(std::shared_ptr<int>(std::move(unique_ptr)));
45   auto nullable_shared_ptr = condition ? non_null_shared_ptr.get() : nullptr;
46 
47   return {std::move(non_null_shared_ptr), std::move(nullable_shared_ptr)};
48 }
49 
50 }  // namespace
51 
52 // Keep in sync with example usage in header file.
TEST(NonNull,Example)53 TEST(NonNull, Example) {
54   const auto string_ptr = ftl::as_non_null(std::make_shared<std::string>("android"));
55   std::size_t size{};
56   get_length(string_ptr, ftl::as_non_null(&size));
57   EXPECT_EQ(size, 7u);
58 
59   auto ptr = ftl::as_non_null(std::make_unique<int>(42));
60   const auto [ptr1, ptr2] = dupe_if(std::move(ptr), true);
61   EXPECT_EQ(ptr1.get(), ptr2);
62 }
63 
64 namespace {
65 
66 constexpr std::string_view kApple = "apple";
67 constexpr std::string_view kOrange = "orange";
68 
69 using StringViewPtr = ftl::NonNull<const std::string_view*>;
70 constexpr StringViewPtr kApplePtr = ftl::as_non_null(&kApple);
71 constexpr StringViewPtr kOrangePtr = ftl::as_non_null(&kOrange);
72 
longest(StringViewPtr ptr1,StringViewPtr ptr2)73 constexpr StringViewPtr longest(StringViewPtr ptr1, StringViewPtr ptr2) {
74   return ptr1->length() > ptr2->length() ? ptr1 : ptr2;
75 }
76 
77 static_assert(longest(kApplePtr, kOrangePtr) == kOrangePtr);
78 
79 static_assert(static_cast<bool>(kApplePtr));
80 
81 static_assert(std::is_same_v<decltype(ftl::as_non_null(std::declval<const int* const>())),
82                              ftl::NonNull<const int*>>);
83 
84 }  // namespace
85 
TEST(NonNull,SwapRawPtr)86 TEST(NonNull, SwapRawPtr) {
87   int i1 = 123;
88   int i2 = 456;
89   auto ptr1 = ftl::as_non_null(&i1);
90   auto ptr2 = ftl::as_non_null(&i2);
91 
92   std::swap(ptr1, ptr2);
93 
94   EXPECT_EQ(*ptr1, 456);
95   EXPECT_EQ(*ptr2, 123);
96 }
97 
TEST(NonNull,SwapSmartPtr)98 TEST(NonNull, SwapSmartPtr) {
99   auto ptr1 = ftl::as_non_null(std::make_shared<int>(123));
100   auto ptr2 = ftl::as_non_null(std::make_shared<int>(456));
101 
102   std::swap(ptr1, ptr2);
103 
104   EXPECT_EQ(*ptr1, 456);
105   EXPECT_EQ(*ptr2, 123);
106 }
107 
TEST(NonNull,VectorOfRawPtr)108 TEST(NonNull, VectorOfRawPtr) {
109   int i = 1;
110   std::vector<ftl::NonNull<int*>> vpi;
111   vpi.push_back(ftl::as_non_null(&i));
112   EXPECT_FALSE(ftl::contains(vpi, nullptr));
113   EXPECT_TRUE(ftl::contains(vpi, &i));
114   EXPECT_TRUE(ftl::contains(vpi, vpi.front()));
115 }
116 
TEST(NonNull,VectorOfSmartPtr)117 TEST(NonNull, VectorOfSmartPtr) {
118   std::vector<ftl::NonNull<std::shared_ptr<int>>> vpi;
119   vpi.push_back(ftl::as_non_null(std::make_shared<int>(2)));
120   EXPECT_FALSE(ftl::contains(vpi, nullptr));
121   EXPECT_TRUE(ftl::contains(vpi, vpi.front().get()));
122   EXPECT_TRUE(ftl::contains(vpi, vpi.front()));
123 }
124 
TEST(NonNull,SetOfRawPtr)125 TEST(NonNull, SetOfRawPtr) {
126   int i = 1;
127   std::set<ftl::NonNull<int*>> spi;
128   spi.insert(ftl::as_non_null(&i));
129   EXPECT_FALSE(ftl::contains(spi, nullptr));
130   EXPECT_TRUE(ftl::contains(spi, &i));
131   EXPECT_TRUE(ftl::contains(spi, *spi.begin()));
132 }
133 
TEST(NonNull,SetOfSmartPtr)134 TEST(NonNull, SetOfSmartPtr) {
135   std::set<ftl::NonNull<std::shared_ptr<int>>> spi;
136   spi.insert(ftl::as_non_null(std::make_shared<int>(2)));
137   EXPECT_FALSE(ftl::contains(spi, nullptr));
138   EXPECT_TRUE(ftl::contains(spi, spi.begin()->get()));
139   EXPECT_TRUE(ftl::contains(spi, *spi.begin()));
140 }
141 
TEST(NonNull,UnorderedSetOfRawPtr)142 TEST(NonNull, UnorderedSetOfRawPtr) {
143   int i = 1;
144   std::unordered_set<ftl::NonNull<int*>> spi;
145   spi.insert(ftl::as_non_null(&i));
146   EXPECT_FALSE(ftl::contains(spi, nullptr));
147   EXPECT_TRUE(ftl::contains(spi, &i));
148   EXPECT_TRUE(ftl::contains(spi, *spi.begin()));
149 }
150 
TEST(NonNull,UnorderedSetOfSmartPtr)151 TEST(NonNull, UnorderedSetOfSmartPtr) {
152   std::unordered_set<ftl::NonNull<std::shared_ptr<int>>> spi;
153   spi.insert(ftl::as_non_null(std::make_shared<int>(2)));
154   EXPECT_FALSE(ftl::contains(spi, nullptr));
155   EXPECT_TRUE(ftl::contains(spi, spi.begin()->get()));
156   EXPECT_TRUE(ftl::contains(spi, *spi.begin()));
157 }
158 
159 }  // namespace android::test
160