mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830 Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase. Test Plan: CI Reviewed By: zertosh Differential Revision: D27979080 fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
84 lines
1.9 KiB
C++
84 lines
1.9 KiB
C++
#include <c10/util/Optional.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
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() {
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
return 42;
|
|
}
|
|
|
|
template <>
|
|
std::string getSampleValue() {
|
|
return "hello";
|
|
}
|
|
|
|
using OptionalTypes = ::testing::Types<
|
|
// 32-bit scalar optimization.
|
|
bool,
|
|
// Trivially destructible but not 32-bit scalar.
|
|
uint64_t,
|
|
// Non-trivial destructor.
|
|
std::string>;
|
|
|
|
TYPED_TEST_CASE(OptionalTest, OptionalTypes);
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
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);
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
|
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);
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
|
|
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);
|
|
}
|
|
}
|
|
|
|
} // namespace
|