//          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/catch/begin.hpp>
#include <fcppt/catch/end.hpp>
#include <fcppt/container/buffer/object.hpp>
#include <fcppt/config/external_begin.hpp>
#include <catch2/catch_test_macros.hpp>
#include <utility>
#include <fcppt/config/external_end.hpp>

namespace
{
using container_type = fcppt::container::buffer::object<int>;

}

template class fcppt::container::buffer::object<int>;

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("container::buffer::object", "[container],[buffer]")
{
  container_type test1{5U};

  REQUIRE(test1.read_size() == 0U);

  REQUIRE(test1.write_size() == 5U);

  *test1.write_data() = 10;

  test1.written(1U);

  REQUIRE(test1.read_size() == 1U);

  REQUIRE(test1.write_size() == 4U);

  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
  CHECK(test1[0] == 10);
}

TEST_CASE("container::buffer move", "[container],[buffer]")
{
  container_type test1{100U};

  *test1.write_data() = 42;

  test1.written(1U);

  container_type test2(std::move(test1));

  REQUIRE(test2.write_size() == 99U);

  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
  CHECK(test2[0] == 42);

  container_type test3{0U};

  test3 = std::move(test2);

  REQUIRE(test3.write_size() == 99U);

  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
  CHECK(test3[0] == 42);
}

TEST_CASE("container::buffer resize_write_area", "[container],[buffer]")
{
  container_type container{2U};

  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
  container.write_data()[0] = 10;

  container.written(1U);

  CHECK(container.read_size() == 1U);

  CHECK(container.write_size() == 1U);

  container.resize_write_area(3U);

  CHECK(container.write_size() == 3U);

  REQUIRE(container.read_size() == 1U);

  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access)
  CHECK(container[0] == 10);
}

// 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
