-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc++][any][NFC] Reformat and refactor any_cast tests #169057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...in preparation for llvm#168826 as requested in the review.
Member
|
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) Changes...in preparation for #168826 as requested in the review. Patch is 40.49 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/169057.diff 6 Files Affected:
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
index 5143befffce73..a5f0e8b80ebec 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_pointer.pass.cpp
@@ -25,156 +25,157 @@
// Test that the operators are properly noexcept.
void test_cast_is_noexcept() {
- std::any a;
- ASSERT_NOEXCEPT(std::any_cast<int>(&a));
+ std::any a;
+ ASSERT_NOEXCEPT(std::any_cast<int>(&a));
- const std::any& ca = a;
- ASSERT_NOEXCEPT(std::any_cast<int>(&ca));
+ const std::any& ca = a;
+ ASSERT_NOEXCEPT(std::any_cast<int>(&ca));
}
// Test that the return type of any_cast is correct.
void test_cast_return_type() {
- std::any a;
- ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&a)), int*);
- ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&a)), int const*);
+ std::any a;
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&a)), int*);
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&a)), int const*);
- const std::any& ca = a;
- ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&ca)), int const*);
- ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&ca)), int const*);
+ const std::any& ca = a;
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int>(&ca)), int const*);
+ ASSERT_SAME_TYPE(decltype(std::any_cast<int const>(&ca)), int const*);
}
// Test that any_cast handles null pointers.
void test_cast_nullptr() {
- std::any *a = nullptr;
- assert(nullptr == std::any_cast<int>(a));
- assert(nullptr == std::any_cast<int const>(a));
+ std::any* a = nullptr;
+ assert(nullptr == std::any_cast<int>(a));
+ assert(nullptr == std::any_cast<int const>(a));
- const std::any *ca = nullptr;
- assert(nullptr == std::any_cast<int>(ca));
- assert(nullptr == std::any_cast<int const>(ca));
+ const std::any* ca = nullptr;
+ assert(nullptr == std::any_cast<int>(ca));
+ assert(nullptr == std::any_cast<int const>(ca));
}
// Test casting an empty object.
void test_cast_empty() {
- {
- std::any a;
- assert(nullptr == std::any_cast<int>(&a));
- assert(nullptr == std::any_cast<int const>(&a));
-
- const std::any& ca = a;
- assert(nullptr == std::any_cast<int>(&ca));
- assert(nullptr == std::any_cast<int const>(&ca));
- }
- // Create as non-empty, then make empty and run test.
- {
- std::any a(42);
- a.reset();
- assert(nullptr == std::any_cast<int>(&a));
- assert(nullptr == std::any_cast<int const>(&a));
-
- const std::any& ca = a;
- assert(nullptr == std::any_cast<int>(&ca));
- assert(nullptr == std::any_cast<int const>(&ca));
- }
+ {
+ std::any a;
+ assert(nullptr == std::any_cast<int>(&a));
+ assert(nullptr == std::any_cast<int const>(&a));
+
+ const std::any& ca = a;
+ assert(nullptr == std::any_cast<int>(&ca));
+ assert(nullptr == std::any_cast<int const>(&ca));
+ }
+ // Create as non-empty, then make empty and run test.
+ {
+ std::any a(42);
+ a.reset();
+ assert(nullptr == std::any_cast<int>(&a));
+ assert(nullptr == std::any_cast<int const>(&a));
+
+ const std::any& ca = a;
+ assert(nullptr == std::any_cast<int>(&ca));
+ assert(nullptr == std::any_cast<int const>(&ca));
+ }
}
template <class Type>
void test_cast() {
- assert(Type::count == 0);
- Type::reset();
- {
- std::any a = Type(42);
- const std::any& ca = a;
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
-
- // Try a cast to a bad type.
- // NOTE: Type cannot be an int.
- assert(std::any_cast<int>(&a) == nullptr);
- assert(std::any_cast<int const>(&a) == nullptr);
- assert(std::any_cast<int const volatile>(&a) == nullptr);
-
- // Try a cast to the right type, but as a pointer.
- assert(std::any_cast<Type*>(&a) == nullptr);
- assert(std::any_cast<Type const*>(&a) == nullptr);
-
- // Check getting a unqualified type from a non-const any.
- Type* v = std::any_cast<Type>(&a);
- assert(v != nullptr);
- assert(v->value == 42);
-
- // change the stored value and later check for the new value.
- v->value = 999;
-
- // Check getting a const qualified type from a non-const any.
- Type const* cv = std::any_cast<Type const>(&a);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check getting a unqualified type from a const any.
- cv = std::any_cast<Type>(&ca);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check getting a const-qualified type from a const any.
- cv = std::any_cast<Type const>(&ca);
- assert(cv != nullptr);
- assert(cv == v);
- assert(cv->value == 999);
-
- // Check that no more objects were created, copied or moved.
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
- }
- assert(Type::count == 0);
+ assert(Type::count == 0);
+ Type::reset();
+ {
+ std::any a = Type(42);
+ const std::any& ca = a;
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+
+ // Try a cast to a bad type.
+ // NOTE: Type cannot be an int.
+ assert(std::any_cast<int>(&a) == nullptr);
+ assert(std::any_cast<int const>(&a) == nullptr);
+ assert(std::any_cast<int const volatile>(&a) == nullptr);
+
+ // Try a cast to the right type, but as a pointer.
+ assert(std::any_cast<Type*>(&a) == nullptr);
+ assert(std::any_cast<Type const*>(&a) == nullptr);
+
+ // Check getting a unqualified type from a non-const any.
+ Type* v = std::any_cast<Type>(&a);
+ assert(v != nullptr);
+ assert(v->value == 42);
+
+ // change the stored value and later check for the new value.
+ v->value = 999;
+
+ // Check getting a const qualified type from a non-const any.
+ Type const* cv = std::any_cast<Type const>(&a);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check getting a unqualified type from a const any.
+ cv = std::any_cast<Type>(&ca);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check getting a const-qualified type from a const any.
+ cv = std::any_cast<Type const>(&ca);
+ assert(cv != nullptr);
+ assert(cv == v);
+ assert(cv->value == 999);
+
+ // Check that no more objects were created, copied or moved.
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+ }
+ assert(Type::count == 0);
}
-void test_cast_non_copyable_type()
-{
- // Even though 'any' never stores non-copyable types
- // we still need to support any_cast<NoCopy>(ptr)
- struct NoCopy { NoCopy(NoCopy const&) = delete; };
- std::any a(42);
- std::any const& ca = a;
- assert(std::any_cast<NoCopy>(&a) == nullptr);
- assert(std::any_cast<NoCopy>(&ca) == nullptr);
+void test_cast_non_copyable_type() {
+ // Even though 'any' never stores non-copyable types
+ // we still need to support any_cast<NoCopy>(ptr)
+ struct NoCopy {
+ NoCopy(NoCopy const&) = delete;
+ };
+ std::any a(42);
+ std::any const& ca = a;
+ assert(std::any_cast<NoCopy>(&a) == nullptr);
+ assert(std::any_cast<NoCopy>(&ca) == nullptr);
}
void test_cast_array() {
- int arr[3];
- std::any a(arr);
- RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed
- // We can't get an array out
- int (*p)[3] = std::any_cast<int[3]>(&a);
- assert(p == nullptr);
+ int arr[3];
+ std::any a(arr);
+ RTTI_ASSERT(a.type() == typeid(int*)); // contained value is decayed
+ // We can't get an array out
+ int (*p)[3] = std::any_cast<int[3]>(&a);
+ assert(p == nullptr);
}
void test_fn() {}
void test_cast_function_pointer() {
- using T = void(*)();
- std::any a(test_fn);
- // An any can never store a function type, but we should at least be able
- // to ask.
- assert(std::any_cast<void()>(&a) == nullptr);
- T fn_ptr = std::any_cast<T>(a);
- assert(fn_ptr == test_fn);
+ using T = void (*)();
+ std::any a(test_fn);
+ // An any can never store a function type, but we should at least be able
+ // to ask.
+ assert(std::any_cast<void()>(&a) == nullptr);
+ T fn_ptr = std::any_cast<T>(a);
+ assert(fn_ptr == test_fn);
}
int main(int, char**) {
- test_cast_is_noexcept();
- test_cast_return_type();
- test_cast_nullptr();
- test_cast_empty();
- test_cast<small>();
- test_cast<large>();
- test_cast_non_copyable_type();
- test_cast_array();
- test_cast_function_pointer();
+ test_cast_is_noexcept();
+ test_cast_return_type();
+ test_cast_nullptr();
+ test_cast_empty();
+ test_cast<small>();
+ test_cast<large>();
+ test_cast_non_copyable_type();
+ test_cast_array();
+ test_cast_function_pointer();
return 0;
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
index 079be66771944..4ba9bf6f30823 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_reference.pass.cpp
@@ -29,278 +29,275 @@
// Test that the operators are NOT marked noexcept.
void test_cast_is_not_noexcept() {
- std::any a;
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&>(a))), "");
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any const&>(a))), "");
- static_assert(!noexcept(std::any_cast<int>(static_cast<std::any &&>(a))), "");
+ std::any a;
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&>(a))), "");
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any const&>(a))), "");
+ static_assert(!noexcept(std::any_cast<int>(static_cast<std::any&&>(a))), "");
}
// Test that the return type of any_cast is correct.
void test_cast_return_type() {
- std::any a;
- static_assert(std::is_same<decltype(std::any_cast<int>(a)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(a)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&>(a)), int&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(a)), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&&>(a)), int&&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(a)), int const&&>::value, "");
-
- static_assert(std::is_same<decltype(std::any_cast<int>(std::move(a))), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(std::move(a))), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&>(std::move(a))), int&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(std::move(a))), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int&&>(std::move(a))), int&&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(std::move(a))), int const&&>::value, "");
-
- const std::any& ca = a;
- static_assert(std::is_same<decltype(std::any_cast<int>(ca)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const>(ca)), int>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&>(ca)), int const&>::value, "");
- static_assert(std::is_same<decltype(std::any_cast<int const&&>(ca)), int const&&>::value, "");
+ std::any a;
+ static_assert(std::is_same<decltype(std::any_cast<int>(a)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(a)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&>(a)), int&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(a)), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&&>(a)), int&&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(a)), int const&&>::value, "");
+
+ static_assert(std::is_same<decltype(std::any_cast<int>(std::move(a))), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(std::move(a))), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&>(std::move(a))), int&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(std::move(a))), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int&&>(std::move(a))), int&&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(std::move(a))), int const&&>::value, "");
+
+ const std::any& ca = a;
+ static_assert(std::is_same<decltype(std::any_cast<int>(ca)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const>(ca)), int>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&>(ca)), int const&>::value, "");
+ static_assert(std::is_same<decltype(std::any_cast<int const&&>(ca)), int const&&>::value, "");
}
template <class Type, class ConstT = Type>
-void checkThrows(std::any& a)
-{
+void checkThrows(std::any& a) {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
- try {
- TEST_IGNORE_NODISCARD std::any_cast<Type>(a);
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
-
- try {
- TEST_IGNORE_NODISCARD std::any_cast<ConstT>(static_cast<const std::any&>(a));
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
-
- try {
- using RefType = typename std::conditional<
- std::is_lvalue_reference<Type>::value,
- typename std::remove_reference<Type>::type&&,
- Type
- >::type;
- TEST_IGNORE_NODISCARD std::any_cast<RefType>(static_cast<std::any&&>(a));
- assert(false);
- } catch (const std::bad_any_cast&) {
- // do nothing
- } catch (...) {
- assert(false);
- }
+ try {
+ TEST_IGNORE_NODISCARD std::any_cast<Type>(a);
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
+
+ try {
+ TEST_IGNORE_NODISCARD std::any_cast<ConstT>(static_cast<const std::any&>(a));
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
+
+ try {
+ using RefType = typename std::
+ conditional< std::is_lvalue_reference<Type>::value, typename std::remove_reference<Type>::type&&, Type >::type;
+ TEST_IGNORE_NODISCARD std::any_cast<RefType>(static_cast<std::any&&>(a));
+ assert(false);
+ } catch (const std::bad_any_cast&) {
+ // do nothing
+ } catch (...) {
+ assert(false);
+ }
#else
- (TEST_IGNORE_NODISCARD a);
+ (TEST_IGNORE_NODISCARD a);
#endif
}
void test_cast_empty() {
- // None of these operations should allocate.
- DisableAllocationGuard g; (TEST_IGNORE_NODISCARD g);
- std::any a;
- checkThrows<int>(a);
+ // None of these operations should allocate.
+ DisableAllocationGuard g;
+ (TEST_IGNORE_NODISCARD g);
+ std::any a;
+ checkThrows<int>(a);
}
template <class Type>
void test_cast_to_reference() {
- assert(Type::count == 0);
- Type::reset();
+ assert(Type::count == 0);
+ Type::reset();
+ {
+ std::any a = Type(42);
+ const std::any& ca = a;
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+
+ // Try a cast to a bad type.
+ // NOTE: Type cannot be an int.
+ checkThrows<int>(a);
+ checkThrows<int&, int const&>(a);
+ checkThrows<Type*, Type const*>(a);
+ checkThrows<Type const*>(a);
+
+ // Check getting a type by reference from a non-const lvalue any.
+ {
+ Type& v = std::any_cast<Type&>(a);
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(a);
+ assert(&cv == &v);
+ }
+ // Check getting a type by reference from a const lvalue any.
{
- std::any a = Type(42);
- const std::any& ca = a;
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
-
- // Try a cast to a bad type.
- // NOTE: Type cannot be an int.
- checkThrows<int>(a);
- checkThrows<int&, int const&>(a);
- checkThrows<Type*, Type const*>(a);
- checkThrows<Type const*>(a);
-
- // Check getting a type by reference from a non-const lvalue any.
- {
- Type& v = std::any_cast<Type&>(a);
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(a);
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const lvalue any.
- {
- Type const& v = std::any_cast<Type const&>(ca);
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(ca);
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type const& v = std::any_cast<Type const&>(std::move(ca));
- assert(v.value == 42);
-
- Type const &cv = std::any_cast<Type const&>(std::move(ca));
- assert(&cv == &v);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type&& v = std::any_cast<Type&&>(std::move(a));
- assert(v.value == 42);
- assert(std::any_cast<Type&>(a).value == 42);
-
- Type&& cv = std::any_cast<Type&&>(std::move(a));
- assert(&cv == &v);
- assert(std::any_cast<Type&>(a).value == 42);
- }
- // Check getting a type by reference from a const rvalue any.
- {
- Type const&& v = std::any_cast<Type const&&>(std::move(a));
- assert(v.value == 42);
- assert(std::any_cast<Type&>(a).value == 42);
-
- Type const&& cv = std::any_cast<Type const&&>(std::move(a));
- assert(&cv == &v);
- assert(std::any_cast<Type&>(a).value == 42);
- }
- // Check that the original object hasn't been changed.
- assertContains<Type>(a, 42);
-
- // Check that no objects have been created/copied/moved.
- assert(Type::count == 1);
- assert(Type::copied == 0);
- assert(Type::moved == 1);
+ Type const& v = std::any_cast<Type const&>(ca);
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(ca);
+ assert(&cv == &v);
}
- assert(Type::count == 0);
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type const& v = std::any_cast<Type const&>(std::move(ca));
+ assert(v.value == 42);
+
+ Type const& cv = std::any_cast<Type const&>(std::move(ca));
+ assert(&cv == &v);
+ }
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type&& v = std::any_cast<Type&&>(std::move(a));
+ assert(v.value == 42);
+ assert(std::any_cast<Type&>(a).value == 42);
+
+ Type&& cv = std::any_cast<Type&&>(std::move(a));
+ assert(&cv == &v);
+ assert(std::any_cast<Type&>(a).value == 42);
+ }
+ // Check getting a type by reference from a const rvalue any.
+ {
+ Type const&& v = std::any_cast<Type const&&>(std::move(a));
+ assert(v.value == 42);
+ assert(std::any_cast<Type&>(a).value == 42);
+
+ Type const&& cv = std::any_cast<Type const&&>(std::move(a));
+ assert(&cv == &v);
+ assert(std::any_cast<Type&>(a).value == 42);
+ }
+ // Check that the original object hasn't been changed.
+ assertContains<Type>(a, 42);
+
+ // Check that no objects have been created/copied/moved.
+ assert(Type::count == 1);
+ assert(Type::copied == 0);
+ assert(Type::moved == 1);
+ }
+ assert(Type::count == 0);
}
template <class Type>
void test_cast_to_value() {
- assert(Type::c...
[truncated]
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
...in preparation for #168826 as requested in the review.