-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add type_traits tests #65956
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
[libc] Add type_traits tests #65956
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
@llvm/pr-subscribers-libc ChangesNoneFull diff: https://github.com/llvm/llvm-project/pull/65956.diff 3 Files Affected:
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt index be2bc20d780471a..5772a83a65cad13 100644 --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -106,3 +106,13 @@ add_libc_test( libc.src.__support.CPP.string libc.src.__support.CPP.string_view ) + +add_libc_test( + type_traits_test + SUITE + libc-cpp-utils-tests + SRCS + type_traits_test.cpp + DEPENDS + libc.src.__support.CPP.type_traits +) diff --git a/libc/test/src/__support/CPP/type_traits_test.cpp b/libc/test/src/__support/CPP/type_traits_test.cpp new file mode 100644 index 000000000000000..69b7009e636b3a8 --- /dev/null +++ b/libc/test/src/__support/CPP/type_traits_test.cpp @@ -0,0 +1,268 @@ +//===-- Unittests for type_traits -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/CPP/type_traits.h" +#include "test/UnitTest/Test.h" + +// TODO: Split this file if it becomes too big. + +namespace __llvm_libc::cpp { + +class Class {}; +union Union {}; +struct Struct {}; + +using UnqualObjectTypes = testing::TypeList; + +TYPED_TEST(LlvmLibcTypeTraitsTest, add_lvalue_reference, UnqualObjectTypes) { + + // non-ref cv, adds ref + EXPECT_TRUE((is_same_v, T &>)); + EXPECT_TRUE((is_same_v, const T &>)); + EXPECT_TRUE((is_same_v, volatile T &>)); + EXPECT_TRUE(( + is_same_v, const volatile T &>)); + + // ref cv, returns same type + EXPECT_TRUE((is_same_v, T &>)); + EXPECT_TRUE((is_same_v, const T &>)); + EXPECT_TRUE((is_same_v, volatile T &>)); + EXPECT_TRUE((is_same_v, + const volatile T &>)); +} + +TEST(LlvmLibcTypeTraitsTest, add_lvalue_reference_void) { + // void cannot be referenced + EXPECT_TRUE((is_same_v, void>)); + EXPECT_TRUE((is_same_v, const void>)); + EXPECT_TRUE( + (is_same_v, volatile void>)); + EXPECT_TRUE((is_same_v, + const volatile void>)); +} + +TYPED_TEST(LlvmLibcTypeTraitsTest, add_pointer, UnqualObjectTypes) { + // object types -> pointer type + EXPECT_TRUE((is_same_v, T *>)); + EXPECT_TRUE((is_same_v, const T *>)); + EXPECT_TRUE((is_same_v, volatile T *>)); + EXPECT_TRUE((is_same_v, const volatile T *>)); + + // reference type -> pointer type + EXPECT_TRUE((is_same_v, T *>)); + EXPECT_TRUE((is_same_v, const T *>)); + EXPECT_TRUE((is_same_v, volatile T *>)); + EXPECT_TRUE( + (is_same_v, const volatile T *>)); +} + +TEST(LlvmLibcTypeTraitsTest, add_pointer_void) { + // void -> pointer type + EXPECT_TRUE((is_same_v, void *>)); + EXPECT_TRUE((is_same_v, const void *>)); + EXPECT_TRUE((is_same_v, volatile void *>)); + EXPECT_TRUE( + (is_same_v, const volatile void *>)); +} + +TYPED_TEST(LlvmLibcTypeTraitsTest, add_rvalue_reference, UnqualObjectTypes) { + + // non-ref cv, adds ref + EXPECT_TRUE((is_same_v, T &&>)); + EXPECT_TRUE((is_same_v, const T &&>)); + EXPECT_TRUE((is_same_v, volatile T &&>)); + EXPECT_TRUE((is_same_v, + const volatile T &&>)); + + // ref cv, returns same type + EXPECT_TRUE((is_same_v, T &>)); + EXPECT_TRUE((is_same_v, const T &>)); + EXPECT_TRUE((is_same_v, volatile T &>)); + EXPECT_TRUE((is_same_v, + const volatile T &>)); +} + +TEST(LlvmLibcTypeTraitsTest, add_rvalue_reference_void) { + // void cannot be referenced + EXPECT_TRUE((is_same_v, void>)); + EXPECT_TRUE((is_same_v, const void>)); + EXPECT_TRUE( + (is_same_v, volatile void>)); + EXPECT_TRUE((is_same_v, + const volatile void>)); +} + +TEST(LlvmLibcTypeTraitsTest, bool_constant) { + EXPECT_TRUE((bool_constant::value)); + EXPECT_FALSE((bool_constant::value)); +} + +TEST(LlvmLibcTypeTraitsTest, conditional_t) { + EXPECT_TRUE((is_same_v, int>)); + EXPECT_TRUE((is_same_v, float>)); +} + +TEST(LlvmLibcTypeTraitsTest, decay) { + EXPECT_TRUE((is_same_v, int>)); + + // array decay + EXPECT_TRUE((is_same_v, int *>)); + EXPECT_TRUE((is_same_v, int *>)); + EXPECT_TRUE((is_same_v, int(*)[4]>)); + + // cv ref decay + EXPECT_TRUE((is_same_v, int>)); + EXPECT_TRUE((is_same_v, int>)); + EXPECT_TRUE((is_same_v, int>)); + EXPECT_TRUE((is_same_v, int>)); +} + +// TODO enable_if + +TEST(LlvmLibcTypeTraitsTest, false_type) { EXPECT_FALSE((false_type::value)); } + +TEST(LlvmLibcTypeTraitsTest, integral_constant) { + EXPECT_EQ((integral_constant::value), 4); +} + +using IntegralTypes = + testing::TypeList; + +TYPED_TEST(LlvmLibcTypeTraitsTest, is_arithmetic, IntegralTypes) { + EXPECT_TRUE((is_arithmetic_v)); + EXPECT_TRUE((is_arithmetic_v)); + EXPECT_TRUE((is_arithmetic_v)); + EXPECT_TRUE((is_arithmetic_v)); + + EXPECT_FALSE((is_arithmetic_v)); + EXPECT_FALSE((is_arithmetic_v)); +} + +TEST(LlvmLibcTypeTraitsTest, is_arithmetic_non_integral) { + EXPECT_FALSE((is_arithmetic_v)); + EXPECT_FALSE((is_arithmetic_v)); + EXPECT_FALSE((is_arithmetic_v)); +} + +TEST(LlvmLibcTypeTraitsTest, is_array) { + EXPECT_FALSE((is_array_v)); + EXPECT_FALSE((is_array_v)); + EXPECT_FALSE((is_array_v)); + + EXPECT_TRUE((is_array_v)); + EXPECT_TRUE((is_array_v)); +} + +TEST(LlvmLibcTypeTraitsTest, is_base_of) { + struct A {}; + EXPECT_TRUE((is_base_of_v)); + + struct B : public A {}; + EXPECT_TRUE((is_base_of_v)); + EXPECT_FALSE((is_base_of_v)); + + struct C : protected A {}; + EXPECT_TRUE((is_base_of_v)); + EXPECT_FALSE((is_base_of_v)); + + struct D : private A {}; + EXPECT_TRUE((is_base_of_v)); + EXPECT_FALSE((is_base_of_v)); +} + +TEST(LlvmLibcTypeTraitsTest, is_class) { + EXPECT_TRUE((is_class_v)); + EXPECT_TRUE((is_class_v)); + EXPECT_FALSE((is_class_v)); + EXPECT_FALSE((is_class_v)); +} + +TYPED_TEST(LlvmLibcTypeTraitsTest, is_const, UnqualObjectTypes) { + EXPECT_FALSE((is_const_v)); + EXPECT_TRUE((is_const_v)); +} + +// TODO is_convertible + +TYPED_TEST(LlvmLibcTypeTraitsTest, is_destructible, UnqualObjectTypes) { + EXPECT_TRUE((is_destructible_v)); +} +TEST(LlvmLibcTypeTraitsTest, is_destructible_no_destructor) { + struct S { + ~S() = delete; + }; + EXPECT_FALSE((is_destructible_v |
The buildkite failure is unrelated to this patch. |
legrosbuffle
approved these changes
Sep 11, 2023
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
This is not exhaustive for now but it provides a placeholder for `invoke_result` test mentioned in llvm#65750.
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.
This is not exhaustive for now but it provides a placeholder for
invoke_result
test mentioned in #65750.