1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <c10/util/Optional.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <string>
namespace {
using testing::Eq;
using testing::Ge;
using testing::Gt;
using testing::Le;
using testing::Lt;
using testing::Ne;
using testing::Not;
template <typename T>
class OptionalTest : public ::testing::Test {
public:
using optional = c10::optional<T>;
};
template <typename T>
T getSampleValue();
template <>
bool getSampleValue() {
return true;
}
template <>
uint64_t getSampleValue() {
return 42;
}
template <>
c10::IntArrayRef getSampleValue() {
return {};
}
template <>
std::string getSampleValue() {
return "hello";
}
using OptionalTypes = ::testing::Types<
// 32-bit scalar optimization.
bool,
// Trivially destructible but not 32-bit scalar.
uint64_t,
// ArrayRef optimization.
c10::IntArrayRef,
// Non-trivial destructor.
std::string>;
// This assert is also in Optional.cpp; including here too to make it
// more likely that we'll remember to port this optimization over when
// we move to std::optional.
static_assert(
sizeof(c10::optional<c10::IntArrayRef>) == sizeof(c10::IntArrayRef),
"c10::optional<IntArrayRef> should be size-optimized");
TYPED_TEST_CASE(OptionalTest, OptionalTypes);
TYPED_TEST(OptionalTest, Empty) {
typename TestFixture::optional empty;
EXPECT_FALSE((bool)empty);
EXPECT_FALSE(empty.has_value());
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
EXPECT_THROW(empty.value(), c10::bad_optional_access);
}
TYPED_TEST(OptionalTest, Initialized) {
using optional = typename TestFixture::optional;
const auto val = getSampleValue<TypeParam>();
optional opt((val));
auto copy(opt), moveFrom1(opt), moveFrom2(opt);
optional move(std::move(moveFrom1));
optional copyAssign;
copyAssign = opt;
optional moveAssign;
moveAssign = std::move(moveFrom2);
std::array<typename TestFixture::optional*, 5> opts = {
&opt, ©, ©Assign, &move, &moveAssign};
for (auto* popt : opts) {
auto& opt = *popt;
EXPECT_TRUE((bool)opt);
EXPECT_TRUE(opt.has_value());
EXPECT_EQ(opt.value(), val);
EXPECT_EQ(*opt, val);
}
}
class SelfCompareTest : public testing::TestWithParam<c10::optional<int>> {};
TEST_P(SelfCompareTest, SelfCompare) {
c10::optional<int> x = GetParam();
EXPECT_THAT(x, Eq(x));
EXPECT_THAT(x, Le(x));
EXPECT_THAT(x, Ge(x));
EXPECT_THAT(x, Not(Ne(x)));
EXPECT_THAT(x, Not(Lt(x)));
EXPECT_THAT(x, Not(Gt(x)));
}
INSTANTIATE_TEST_CASE_P(
nullopt,
SelfCompareTest,
testing::Values(c10::nullopt));
INSTANTIATE_TEST_CASE_P(
int,
SelfCompareTest,
testing::Values(c10::make_optional(2)));
TEST(OptionalTest, Nullopt) {
c10::optional<int> x = 2;
EXPECT_THAT(c10::nullopt, Not(Eq(x)));
EXPECT_THAT(x, Not(Eq(c10::nullopt)));
EXPECT_THAT(x, Ne(c10::nullopt));
EXPECT_THAT(c10::nullopt, Ne(x));
EXPECT_THAT(x, Not(Lt(c10::nullopt)));
EXPECT_THAT(c10::nullopt, Lt(x));
EXPECT_THAT(x, Not(Le(c10::nullopt)));
EXPECT_THAT(c10::nullopt, Le(x));
EXPECT_THAT(x, Gt(c10::nullopt));
EXPECT_THAT(c10::nullopt, Not(Gt(x)));
EXPECT_THAT(x, Ge(c10::nullopt));
EXPECT_THAT(c10::nullopt, Not(Ge(x)));
}
// Ensure comparisons work...
using CmpTestTypes = testing::Types<
// between two optionals
std::pair<c10::optional<int>, c10::optional<int>>,
// between an optional and a value
std::pair<c10::optional<int>, int>,
// between a value and an optional
std::pair<int, c10::optional<int>>,
// between an optional and a differently typed value
std::pair<c10::optional<int>, long>,
// between a differently typed value and an optional
std::pair<long, c10::optional<int>>>;
template <typename T>
class CmpTest : public testing::Test {};
TYPED_TEST_CASE(CmpTest, CmpTestTypes);
TYPED_TEST(CmpTest, Cmp) {
TypeParam pair = {2, 3};
auto x = pair.first;
auto y = pair.second;
EXPECT_THAT(x, Not(Eq(y)));
EXPECT_THAT(x, Ne(y));
EXPECT_THAT(x, Lt(y));
EXPECT_THAT(y, Not(Lt(x)));
EXPECT_THAT(x, Le(y));
EXPECT_THAT(y, Not(Le(x)));
EXPECT_THAT(x, Not(Gt(y)));
EXPECT_THAT(y, Gt(x));
EXPECT_THAT(x, Not(Ge(y)));
EXPECT_THAT(y, Ge(x));
}
} // namespace
|