Skip to content

Commit

Permalink
Deploying to gh-pages from @ dcf54d9 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
huixie90 committed Jun 4, 2024
1 parent 0cef11b commit 87b7961
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
4 changes: 3 additions & 1 deletion impl/any_view/test/iterator/exception.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <array>
#include <catch2/catch_test_macros.hpp>

#include "any_view.hpp"

#define TEST_POINT(x) TEST_CASE(x, "[exception]")
using AnyView = std::ranges::any_view<int&, std::ranges::category::forward>;
using Iter = std::ranges::iterator_t<AnyView>;

Expand Down Expand Up @@ -114,7 +116,7 @@ constexpr bool test() {
return true;
}

int main() {
TEST_POINT("exception") {
test();
// static_assert(test());
}
3 changes: 3 additions & 0 deletions impl/any_view/test/iterator/random_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ static_assert(
static_assert(std::same_as<typename std::iterator_traits<Iter>::difference_type,
ptrdiff_t>);

static_assert(std::is_nothrow_move_constructible_v<Iter>);
static_assert(std::is_nothrow_move_assignable_v<Iter>);

constexpr void basic() {
std::array a{1, 2, 3, 4, 5};
AnyView v(std::views::all(a));
Expand Down
114 changes: 114 additions & 0 deletions impl/any_view/test/storage/exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <algorithm>
#include <array>
#include <cassert>
#include <catch2/catch_test_macros.hpp>

#include "storage.hpp"

#define TEST_POINT(x) TEST_CASE(x, "[storage exception]")

template <bool MoveNoexcept, bool isBig>
struct MayThrow {
int i;
bool will_throw = false;

static constexpr bool move_noexcept = MoveNoexcept;
static constexpr std::size_t data_size = isBig ? 100z : 1z;
char data[data_size]{};

MayThrow(int ii, bool thr = false) : i(ii), will_throw(thr) {}
MayThrow(const MayThrow& other) : i(other.i), will_throw(other.will_throw) {
if (will_throw) {
throw 5;
}
}

MayThrow(MayThrow&& other) noexcept(MoveNoexcept)
: i(other.i), will_throw(other.will_throw) {
// modify moved from object
other.i = -1;
other.will_throw = false;
if (!MoveNoexcept && will_throw) {
throw 6;
}
}

MayThrow& operator=(const MayThrow& other) {
i = other.i;
will_throw = other.will_throw;
if (will_throw) {
throw 7;
}
return *this;
}

MayThrow& operator=(MayThrow&& other) noexcept(MoveNoexcept) {
i = other.i;
will_throw = other.will_throw;
other.i = -1;
other.will_throw = false;
if (!MoveNoexcept && will_throw) {
throw 8;
}
return *this;
}
};

using Storage = std::ranges::detail::storage<24, 8, true>;
using std::ranges::detail::type;

template <class T>
int get_i(const Storage& s) {
return s.get_ptr<T>()->i;
}

template <class From, class To, bool Success, bool MoveCalled>
void test() {
// move assign
{
Storage s1{type<From>{}, 5};
Storage s2{type<To>{}, 6};
if constexpr (!From::move_noexcept) {
s1.get_ptr<From>()->will_throw = true;
}
if constexpr (!To::move_noexcept) {
s2.get_ptr<To>()->will_throw = true;
}
try {
s1 = std::move(s2);
if constexpr (!Success) {
REQUIRE(false);
}
} catch (int) {
}

if constexpr (!Success) {
REQUIRE(get_i<From>(s1) == 5);
REQUIRE(get_i<To>(s2) == 6);
} else {
REQUIRE(get_i<To>(s1) == 6);
if constexpr (MoveCalled) {
REQUIRE(get_i<To>(s2) == -1);
} else {
REQUIRE(s2.is_singular());
}
}
}
}

using SmallNoexcept = MayThrow<true, false>;
using SmallThrow = MayThrow<false, false>;
using BigNoexcept = MayThrow<true, true>;
using BigThrow = MayThrow<true, true>;

static_assert(Storage::unittest_is_small<SmallNoexcept>());
static_assert(!Storage::unittest_is_small<SmallThrow>());
static_assert(!Storage::unittest_is_small<BigNoexcept>());
static_assert(!Storage::unittest_is_small<BigThrow>());

TEST_POINT("test noexcept") {
test<SmallNoexcept, SmallNoexcept, true, true>();
test<SmallThrow, SmallThrow, true, false>();
test<BigNoexcept, BigNoexcept, true, false>();
test<BigThrow, BigThrow, true, false>();
}
5 changes: 5 additions & 0 deletions impl/any_view/test/view/random_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static_assert(std::ranges::sized_range<AnyViewFull>);
static_assert(std::ranges::common_range<AnyViewFull>);
static_assert(std::ranges::borrowed_range<AnyViewFull>);

static_assert(std::is_nothrow_move_constructible_v<AnyView>);
static_assert(std::is_nothrow_move_assignable_v<AnyView>);
static_assert(std::is_nothrow_move_constructible_v<AnyViewFull>);
static_assert(std::is_nothrow_move_assignable_v<AnyViewFull>);

template <class V>
constexpr void basic() {
std::array v{1, 2, 3, 4, 5};
Expand Down

0 comments on commit 87b7961

Please sign in to comment.