/* * 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 "UserTypes.h" #include #include namespace UnitTest { class FunctionalBasicTest : public ScopedAllocatorSetupFixture { }; namespace Internal { struct FunctionalOperatorConfig { template static void PerformOperation(T&& lhs, U&& rhs, const TupleType& expectedValues) { // Arithmetic EXPECT_EQ(AZStd::get<0>(expectedValues), AZStd::plus{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<1>(expectedValues), AZStd::minus{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<2>(expectedValues), AZStd::multiplies{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<3>(expectedValues), AZStd::divides{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<4>(expectedValues), AZStd::modulus{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<5>(expectedValues), AZStd::negate{}(AZStd::forward(lhs))); // Comparison EXPECT_EQ(AZStd::get<6>(expectedValues), AZStd::equal_to{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<7>(expectedValues), AZStd::not_equal_to{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<8>(expectedValues), AZStd::greater{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<9>(expectedValues), AZStd::less{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<10>(expectedValues), AZStd::greater_equal{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<11>(expectedValues), AZStd::less_equal{}(AZStd::forward(lhs), AZStd::forward(rhs))); // Logical EXPECT_EQ(AZStd::get<12>(expectedValues), AZStd::logical_and{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<13>(expectedValues), AZStd::logical_or{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<14>(expectedValues), AZStd::logical_not{}(AZStd::forward(lhs))); // Bitwise EXPECT_EQ(AZStd::get<15>(expectedValues), AZStd::bit_and{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<16>(expectedValues), AZStd::bit_or{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<17>(expectedValues), AZStd::bit_xor{}(AZStd::forward(lhs), AZStd::forward(rhs))); EXPECT_EQ(AZStd::get<18>(expectedValues), AZStd::bit_not{}(AZStd::forward(lhs))); } }; struct IntWrapper { int32_t m_value; }; // arithmetic operators int32_t operator+(IntWrapper lhs, int32_t rhs) { return lhs.m_value + rhs; } int32_t operator+(int32_t lhs, IntWrapper rhs) { return lhs + rhs.m_value; } int32_t operator-(IntWrapper lhs, int32_t rhs) { return lhs.m_value - rhs; } int32_t operator-(int32_t lhs, IntWrapper rhs) { return lhs - rhs.m_value; } int32_t operator*(IntWrapper lhs, int32_t rhs) { return lhs.m_value * rhs; } int32_t operator*(int32_t lhs, IntWrapper rhs) { return lhs * rhs.m_value; } int32_t operator/(IntWrapper lhs, int32_t rhs) { return lhs.m_value / rhs; } int32_t operator/(int32_t lhs, IntWrapper rhs) { return lhs / rhs.m_value; } int32_t operator%(IntWrapper lhs, int32_t rhs) { return lhs.m_value % rhs; } int32_t operator%(int32_t lhs, IntWrapper rhs) { return lhs % rhs.m_value; } int32_t operator-(IntWrapper lhs) { return -lhs.m_value; } // comparison operators bool operator==(IntWrapper lhs, int32_t rhs) { return lhs.m_value == rhs; } bool operator==(int32_t lhs, IntWrapper rhs) { return lhs == rhs.m_value; } bool operator!=(IntWrapper lhs, int32_t rhs) { return lhs.m_value != rhs; } bool operator!=(int32_t lhs, IntWrapper rhs) { return lhs != rhs.m_value; } bool operator>(IntWrapper lhs, int32_t rhs) { return lhs.m_value > rhs; } bool operator>(int lhs, IntWrapper rhs) { return lhs > rhs.m_value; } bool operator<(IntWrapper lhs, int32_t rhs) { return lhs.m_value < rhs; } bool operator<(int lhs, IntWrapper rhs) { return lhs < rhs.m_value; } bool operator>=(IntWrapper lhs, int32_t rhs) { return lhs.m_value >= rhs; } bool operator>=(int lhs, IntWrapper rhs) { return lhs >= rhs.m_value; } bool operator<=(IntWrapper lhs, int32_t rhs) { return lhs.m_value <= rhs; } bool operator<=(int lhs, IntWrapper rhs) { return lhs <= rhs.m_value; } // logical operators bool operator&&(IntWrapper lhs, int32_t rhs) { return lhs.m_value && rhs; } bool operator&&(int lhs, IntWrapper rhs) { return lhs && rhs.m_value; } bool operator||(IntWrapper lhs, int32_t rhs) { return lhs.m_value || rhs; } bool operator||(int lhs, IntWrapper rhs) { return lhs || rhs.m_value; } bool operator!(IntWrapper lhs) { return !lhs.m_value; } // bitwise operators int32_t operator&(IntWrapper lhs, int32_t rhs) { return lhs.m_value & rhs; } int32_t operator&(int lhs, IntWrapper rhs) { return lhs & rhs.m_value; } int32_t operator|(IntWrapper lhs, int32_t rhs) { return lhs.m_value | rhs; } int32_t operator|(int lhs, IntWrapper rhs) { return lhs | rhs.m_value; } int32_t operator^(IntWrapper lhs, int32_t rhs) { return lhs.m_value ^ rhs; } int32_t operator^(int lhs, IntWrapper rhs) { return lhs ^ rhs.m_value; } int32_t operator~(IntWrapper lhs) { return ~lhs.m_value; } } TEST_F(FunctionalBasicTest, FunctionalOperators_ReturnsExpectedValue) { Internal::FunctionalOperatorConfig::PerformOperation(7, 11, AZStd::make_tuple(18, -4, 77, 0, 7, -7, false, true, false, true, false, true, true, true, false, 3, 15, 12, ~7)); Internal::FunctionalOperatorConfig::PerformOperation(45, 34, AZStd::make_tuple(79, 11, 1530, 1, 11, -45, false, true, true, false, true, false, true, true, false, 32, 47, 15, ~45)); Internal::FunctionalOperatorConfig::PerformOperation(24, 24, AZStd::make_tuple(48, 0, 576, 1, 0, -24, true, false, false, false, true, true, true, true, false, 24, 24, 0, ~24)); } TEST_F(FunctionalBasicTest, FunctionalOperators_TransparentOperands) { Internal::FunctionalOperatorConfig::PerformOperation(7, Internal::IntWrapper{ 11 }, AZStd::make_tuple(18, -4, 77, 0, 7, -7, false, true, false, true, false, true, true, true, false, 3, 15, 12, ~7)); Internal::FunctionalOperatorConfig::PerformOperation(Internal::IntWrapper{ 45 }, 34, AZStd::make_tuple(79, 11, 1530, 1, 11, -45, false, true, true, false, true, false, true, true, false, 32, 47, 15, ~45)); Internal::FunctionalOperatorConfig::PerformOperation(24, Internal::IntWrapper{ 24 }, AZStd::make_tuple(48, 0, 576, 1, 0, -24, true, false, false, false, true, true, true, true, false, 24, 24, 0, ~24)); } }