[libc-commits] [libc] [libc] Add type_traits tests (PR #65956)
Clement Courbet via libc-commits
libc-commits at lists.llvm.org
Mon Sep 11 07:13:57 PDT 2023
================
@@ -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<int, float, Class, Union, Struct>;
+
+TYPED_TEST(LlvmLibcTypeTraitsTest, add_lvalue_reference, UnqualObjectTypes) {
+
+ // non-ref cv, adds ref
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<T>, T &>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<const T>, const T &>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<volatile T>, volatile T &>));
+ EXPECT_TRUE((
+ is_same_v<add_lvalue_reference_t<const volatile T>, const volatile T &>));
+
+ // ref cv, returns same type
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<T &>, T &>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<const T &>, const T &>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<volatile T &>, volatile T &>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<const volatile T &>,
+ const volatile T &>));
+}
+
+TEST(LlvmLibcTypeTraitsTest, add_lvalue_reference_void) {
+ // void cannot be referenced
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<void>, void>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<const void>, const void>));
+ EXPECT_TRUE(
+ (is_same_v<add_lvalue_reference_t<volatile void>, volatile void>));
+ EXPECT_TRUE((is_same_v<add_lvalue_reference_t<const volatile void>,
+ const volatile void>));
+}
+
+TYPED_TEST(LlvmLibcTypeTraitsTest, add_pointer, UnqualObjectTypes) {
+ // object types -> pointer type
+ EXPECT_TRUE((is_same_v<add_pointer_t<T>, T *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<const T>, const T *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<volatile T>, volatile T *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<const volatile T>, const volatile T *>));
+
+ // reference type -> pointer type
+ EXPECT_TRUE((is_same_v<add_pointer_t<T &>, T *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<const T &>, const T *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<volatile T &>, volatile T *>));
+ EXPECT_TRUE(
+ (is_same_v<add_pointer_t<const volatile T &>, const volatile T *>));
+}
+
+TEST(LlvmLibcTypeTraitsTest, add_pointer_void) {
+ // void -> pointer type
+ EXPECT_TRUE((is_same_v<add_pointer_t<void>, void *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<const void>, const void *>));
+ EXPECT_TRUE((is_same_v<add_pointer_t<volatile void>, volatile void *>));
+ EXPECT_TRUE(
+ (is_same_v<add_pointer_t<const volatile void>, const volatile void *>));
+}
+
+TYPED_TEST(LlvmLibcTypeTraitsTest, add_rvalue_reference, UnqualObjectTypes) {
+
+ // non-ref cv, adds ref
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<T>, T &&>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const T>, const T &&>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<volatile T>, volatile T &&>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const volatile T>,
+ const volatile T &&>));
+
+ // ref cv, returns same type
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<T &>, T &>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const T &>, const T &>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<volatile T &>, volatile T &>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const volatile T &>,
+ const volatile T &>));
+}
+
+TEST(LlvmLibcTypeTraitsTest, add_rvalue_reference_void) {
+ // void cannot be referenced
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<void>, void>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const void>, const void>));
+ EXPECT_TRUE(
+ (is_same_v<add_rvalue_reference_t<volatile void>, volatile void>));
+ EXPECT_TRUE((is_same_v<add_rvalue_reference_t<const volatile void>,
+ const volatile void>));
+}
+
+TEST(LlvmLibcTypeTraitsTest, bool_constant) {
+ EXPECT_TRUE((bool_constant<true>::value));
----------------
legrosbuffle wrote:
Interesting...
https://github.com/llvm/llvm-project/pull/65956
More information about the libc-commits
mailing list