|
| 1 | +/* |
| 2 | + * Copyright (c), 2025, HighFive Developers |
| 3 | + * |
| 4 | + * Distributed under the Boost Software License, Version 1.0. |
| 5 | + * (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | + * http://www.boost.org/LICENSE_1_0.txt) |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <catch2/catch_template_test_macros.hpp> |
| 11 | + |
| 12 | +#include <highfive/highfive.hpp> |
| 13 | + |
| 14 | +using namespace HighFive; |
| 15 | + |
| 16 | +template<typename T, bool T1, bool T2, bool T3, bool T4> |
| 17 | +constexpr void check_constructible_assignable() { |
| 18 | + static_assert(std::is_copy_constructible_v<T> == T1); |
| 19 | + static_assert(std::is_copy_assignable_v<T> == T2); |
| 20 | + static_assert(std::is_move_constructible_v<T> == T3); |
| 21 | + static_assert(std::is_move_assignable_v<T> == T4); |
| 22 | +} |
| 23 | + |
| 24 | +TEST_CASE("Enshrine constructible/assignable status", "[core]") { |
| 25 | + // Object isn't constructible at all, because its dtor has |
| 26 | + // been deleted. |
| 27 | + check_constructible_assignable<Object, false, false, false, false>(); |
| 28 | + check_constructible_assignable<File, true, true, true, true>(); |
| 29 | + check_constructible_assignable<CompoundType, true, true, true, true>(); |
| 30 | +} |
| 31 | + |
| 32 | +template<class T> |
| 33 | +constexpr void |
| 34 | +check_nothrow_movable() { |
| 35 | + // Check that if it's moveable, it's nothrow movable. |
| 36 | + static_assert(std::is_move_constructible_v<T> == std::is_nothrow_move_constructible_v<T>); |
| 37 | + static_assert(std::is_move_assignable_v<T> == std::is_nothrow_move_assignable_v<T>); |
| 38 | +} |
| 39 | + |
| 40 | +TEST_CASE("Nothrow movable exceptions", "[core]") { |
| 41 | + check_nothrow_movable<Exception>(); |
| 42 | + check_nothrow_movable<FileException>(); |
| 43 | + check_nothrow_movable<ObjectException>(); |
| 44 | + check_nothrow_movable<AttributeException>(); |
| 45 | + check_nothrow_movable<DataSpaceException>(); |
| 46 | + check_nothrow_movable<DataSetException>(); |
| 47 | + check_nothrow_movable<GroupException>(); |
| 48 | + check_nothrow_movable<PropertyException>(); |
| 49 | + check_nothrow_movable<ReferenceException>(); |
| 50 | + check_nothrow_movable<DataTypeException>(); |
| 51 | +} |
| 52 | + |
| 53 | +template<class T> |
| 54 | +constexpr void |
| 55 | +check_nothrow_object() { |
| 56 | + // HighFive objects are reference counted. During move assignment, the |
| 57 | + // reference count can drop to zero, triggering freeing of the object, which |
| 58 | + // can fail. Hence, move assignment can file, but move construction shouldn't. |
| 59 | + static_assert(std::is_move_constructible_v<T> == std::is_nothrow_move_constructible_v<T>); |
| 60 | + static_assert(!std::is_nothrow_move_assignable_v<T>); |
| 61 | + static_assert(!std::is_nothrow_copy_constructible_v<T>); |
| 62 | + static_assert(!std::is_nothrow_copy_assignable_v<T>); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_CASE("HighFive::Objects are nothrow move constructible", "[core]") { |
| 66 | + check_nothrow_object<Object>(); |
| 67 | + check_nothrow_object<File>(); |
| 68 | + check_nothrow_object<Attribute>(); |
| 69 | + check_nothrow_object<DataSpace>(); |
| 70 | + check_nothrow_object<DataSet>(); |
| 71 | + check_nothrow_object<Group>(); |
| 72 | + check_nothrow_object<DataType>(); |
| 73 | + check_nothrow_object<Selection>(); |
| 74 | +} |
| 75 | + |
| 76 | +TEST_CASE("Regular HighFive objects are nothrow movable", "[core]") { |
| 77 | + check_nothrow_movable<RegularHyperSlab>(); |
| 78 | + check_nothrow_movable<HyperSlab>(); |
| 79 | + check_nothrow_movable<ElementSet>(); |
| 80 | + check_nothrow_movable<ProductSet>(); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_CASE("Nothrow movable data types", "[core]") { |
| 84 | + check_nothrow_object<AtomicType<double>>(); |
| 85 | + check_nothrow_object<CompoundType>(); |
| 86 | + check_nothrow_object<StringType>(); |
| 87 | +} |
0 commit comments