//          Copyright Carl Philipp Reh 2009 - 2021.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <fcppt/declare_strong_typedef.hpp>
#include <fcppt/strong_typedef_impl.hpp> // IWYU pragma: keep
#include <fcppt/strong_typedef_operators.hpp>
#include <fcppt/strong_typedef_output.hpp> // IWYU pragma: keep
#include <fcppt/config/compiler.hpp> // IWYU pragma: keep
#include <fcppt/catch/begin.hpp>
#include <fcppt/catch/end.hpp>
#include <fcppt/preprocessor/disable_gcc_warning.hpp>
#include <fcppt/preprocessor/pop_warning.hpp>
#include <fcppt/preprocessor/push_warning.hpp>
#include <fcppt/config/external_begin.hpp>
#include <catch2/catch_test_macros.hpp>
#include <fcppt/config/external_end.hpp>

namespace
{
FCPPT_DECLARE_STRONG_TYPEDEF(int, strong_int);

FCPPT_DECLARE_STRONG_TYPEDEF(unsigned, strong_uint);

}

FCPPT_CATCH_BEGIN
// NOLINTBEGIN(bugprone-throwing-static-initialization,clang-analyzer-optin.core.EnumCastOutOfRange,misc-const-correctness,cert-err58-cpp,fuchsia-statically-constructed-objects,misc-use-anonymous-namespace,cppcoreguidelines-avoid-do-while)

TEST_CASE("strong_typedef arithmetic", "[strongtypedef]")
{
  strong_int test1(2);

  strong_int const test2(4);

  CHECK(test1 + test2 == strong_int(6));

  CHECK(test2 - test1 == strong_int(2));

  CHECK(test1 * test2 == strong_int(8));

  CHECK(-test1 == strong_int(-2));

  CHECK(test1++ == strong_int(2));

  REQUIRE(test1 == strong_int(3));

  REQUIRE(test1-- == strong_int(3));

  REQUIRE(test1 == strong_int(2));

  REQUIRE(++test1 == strong_int(3));

  REQUIRE(--test1 == strong_int(2));
}

TEST_CASE("strong_typedef assignment arithmetic", "[strongtypedef]")
{
  strong_int test(1);

  test += strong_int(2);

  REQUIRE(test == strong_int(3));

  test -= strong_int(1);

  REQUIRE(test == strong_int(2));

  test *= strong_int(2);

  REQUIRE(test == strong_int(4));
}

TEST_CASE("strong_typedef assignment bitwise", "[strongtypedef]")
{
  strong_uint test1{3U};

  test1 &= strong_uint{2U};

  REQUIRE(test1 == strong_uint{2U});

  test1 |= strong_uint{3U};

  REQUIRE(test1 == strong_uint{3U});

  test1 ^= strong_uint{2U};

  REQUIRE(test1 == strong_uint{1U});
}

TEST_CASE("strong_typedef bitwise", "[strongtypedef]")
{
  strong_uint const test1(2U);

  strong_uint const test2(3U);

  CHECK((test1 & test2) == strong_uint{2U});

  CHECK((test1 | test2) == strong_uint{3U});

  CHECK((test1 ^ test2) == strong_uint{1U});

  CHECK(~test1 == strong_uint{~2U});
}

TEST_CASE("strong_typedef comparison", "[strongtypedef]")
{
  strong_int const test1(1);

  strong_int const test2(1);

  strong_int const test3(2);

  CHECK(test1 == test2);

  CHECK(test1 != test3);

  CHECK(test1 <= test2);

  CHECK(test1 >= test2);

  CHECK(test1 < test3);

  CHECK(test3 > test1);

#ifndef FCPPT_CONFIG_MSVC_COMPILER
FCPPT_PP_PUSH_WARNING
FCPPT_PP_DISABLE_GCC_WARNING(-Wzero-as-null-pointer-constant)
  CHECK((test1 <=> test2) == 0);

  CHECK((test1 <=> test3) < 0);

  CHECK((test3 <=> test1) > 0);
FCPPT_PP_POP_WARNING
#endif
}

// NOLINTEND(bugprone-throwing-static-initialization,clang-analyzer-optin.core.EnumCastOutOfRange,misc-const-correctness,cert-err58-cpp,fuchsia-statically-constructed-objects,misc-use-anonymous-namespace,cppcoreguidelines-avoid-do-while)
FCPPT_CATCH_END
