/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include "UserTypes.h" namespace UnitTest { ////////////////////////////////////////////////////////////////////////// // Fixtures // Fixture for non-typed tests template class CompressedPairTest : public LeakDetectionFixture { }; template class CompressedPairSizeTest : public CompressedPairTest {}; namespace CompressedPairInternal { struct EmptyStruct {}; struct EmptyStructNonDerived {}; struct FinalEmptyStruct final {}; struct DerivedFromEmptyStruct : EmptyStruct { }; struct DerivedWithDataFromEmptyStruct : EmptyStruct { uint32_t m_value{}; }; } template struct CompressedPairTestConfig { using first_type = T1; using second_type = T2; static constexpr size_t max_expected_size = MaxExpectedSize; }; using CompressedPairTestConfigs = ::testing::Types< CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig >; TYPED_TEST_SUITE(CompressedPairTest, CompressedPairTestConfigs); using CompressedPairSizeTestConfigs = ::testing::Types< CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig , CompressedPairTestConfig >; TYPED_TEST_SUITE(CompressedPairSizeTest, CompressedPairSizeTestConfigs); TYPED_TEST(CompressedPairTest, CompressedPairDefaultConstructorSucceeds) { AZStd::compressed_pair testPair; (void)testPair; } TYPED_TEST(CompressedPairTest, CompressedPairFirstElementConstructorSucceeds) { AZStd::compressed_pair testPair(typename TypeParam::first_type{}); (void)testPair; } TYPED_TEST(CompressedPairTest, CompressedPairSecondElementConstructorSucceeds) { AZStd::compressed_pair testPair(AZStd::skip_element_tag{}, typename TypeParam::second_type{}); (void)testPair; } TYPED_TEST(CompressedPairTest, CompressedPairPiecewiseElementConstructorSucceeds) { AZStd::compressed_pair testPair(AZStd::piecewise_construct_t{}, std::tuple<>{}, std::tuple<>{}); (void)testPair; } TYPED_TEST(CompressedPairSizeTest, CompressedPairUsesEmptyBaseOptimizationForClassSize) { static_assert(sizeof(TypeParam) <= TypeParam::max_expected_size, "Compressed Pair is not expected size"); } class PairTestFixture : public LeakDetectionFixture {}; TEST_F(PairTestFixture, StructuredBinding_ToConstAutoVar_CompilesSuccessfully) { constexpr AZStd::pair testPair{ 3, 4 }; const auto [firstElement, secondElement] = testPair; static_assert(AZStd::is_same_v); static_assert(AZStd::is_same_v); EXPECT_EQ(3, firstElement); EXPECT_EQ(4, secondElement); } TEST_F(PairTestFixture, CanGetFromAPairRValue) { { int myValue = 42; AZStd::pair myPair(myValue, 0); int& valueRef = AZStd::get<0>(AZStd::move(myPair)); EXPECT_EQ(&myValue, &valueRef); static_assert(AZStd::is_same_v(AZStd::move(myPair))), int&>, "Invoking AZStd::get on an lvalue reference element in a pair should return an lvalue reference"); } { struct MoveOnlyType { MoveOnlyType() = default; MoveOnlyType(const MoveOnlyType&) = delete; MoveOnlyType(MoveOnlyType&&) = default; }; MoveOnlyType myValue; const AZStd::pair myPair(AZStd::move(myValue), 42); MoveOnlyType&& valueRef = AZStd::get<0>(AZStd::move(myPair)); EXPECT_EQ(&myValue, &valueRef); static_assert(AZStd::is_same_v(AZStd::move(myPair))), MoveOnlyType&&>, "Invoking AZStd::get on an rvalue reference element in a pair should return an rvalue reference"); } } }