/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include #include "UserTypes.h" using AZStd::optional; using AZStd::nullopt; using AZStd::in_place; namespace OptionalTestClasses { class NonTriviallyDestructableClass { public: ~NonTriviallyDestructableClass() {} }; class NonTriviallyConstructibleClass { public: NonTriviallyConstructibleClass() {} }; class ConstructibleClass { public: enum Tag { TheTag }; ConstructibleClass(Tag) {} }; class ConstructibleWithInitializerListClass { public: ConstructibleWithInitializerListClass(std::initializer_list il, int theInt) : m_char(*il.begin()) , m_int(theInt) { } const char* m_char; int m_int; }; } // end namespace OptionalTestClasses namespace UnitTest { using namespace OptionalTestClasses; class OptionalFixture : public AllocatorsFixture { }; static_assert(!(optional()), "optional constructed with no args should be false"); static_assert(!(optional(nullopt)), "optional constructed with nullopt should be false"); TEST_F(OptionalFixture, ConstructorNonTrivial) { optional opt; EXPECT_FALSE(bool(opt)) << "optional constructed with no args should be false"; } TEST_F(OptionalFixture, ConstructorInPlace) { const optional opt(in_place, 5); EXPECT_TRUE(bool(opt)) << "optional constructed with args should be true"; EXPECT_EQ(*opt, 5); } TEST_F(OptionalFixture, ConstructorInPlaceNonTriviallyDestructable) { const optional opt(in_place); EXPECT_TRUE(bool(opt)) << "optional constructed with args should be true"; } TEST_F(OptionalFixture, ConstructorInPlaceWithInitializerList) { const optional opt(in_place, {"Lumberyard"}, 4); EXPECT_TRUE(bool(opt)) << "optional constructed with args should be true"; } TEST_F(OptionalFixture, ConstructorCopyTrivial) { const optional opt(in_place, 5); const optional optCopy(opt); EXPECT_EQ(bool(opt), bool(optCopy)) << "Copying an optional should result in the same bool() value"; } TEST_F(OptionalFixture, ConstructorMoveTrivial) { const optional opt(in_place, 5); const optional optMoved(AZStd::move(opt)); EXPECT_EQ(bool(opt), bool(optMoved)) << "Moving an optional should result in the same bool() value"; } TEST_F(OptionalFixture, ConstructorMoveNonTrivial) { AZStd::string s1 {"Hello"}; AZStd::string s2(AZStd::move(s1)); EXPECT_NE(s1, s2); optional opt {"Hello"}; const optional optMoved(AZStd::move(opt)); EXPECT_EQ(bool(opt), bool(optMoved)) << "Moving an optional should result in the same bool() value"; EXPECT_NE(opt.value(), optMoved.value()); } TEST_F(OptionalFixture, CanAssignFromEmptyOptional) { optional opt1; opt1 = {}; EXPECT_FALSE(bool(opt1)) << "Optional should still be empty"; } } // end namespace UnitTest