[libc-commits] [libc] a25fa92 - [libc][stdbit] Add C tests for stdbit generic macros. (#84670)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 11 12:39:09 PDT 2024


Author: lntue
Date: 2024-03-11T15:39:05-04:00
New Revision: a25fa92d870a5cbb3eeccdc7458d1bc6834b695a

URL: https://github.com/llvm/llvm-project/commit/a25fa92d870a5cbb3eeccdc7458d1bc6834b695a
DIFF: https://github.com/llvm/llvm-project/commit/a25fa92d870a5cbb3eeccdc7458d1bc6834b695a.diff

LOG: [libc][stdbit] Add C tests for stdbit generic macros. (#84670)

Currently there is no tests for generic macros of generated `stdbit.h`
header in C, and it is easy to make typo mistakes as in
https://github.com/llvm/llvm-project/issues/84658. In this patch, we
add a simple test for them in C.

Added: 
    libc/test/include/stdbit_stub.h
    libc/test/include/stdbit_test.c

Modified: 
    libc/test/include/CMakeLists.txt
    libc/test/include/stdbit_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index bf845c94170f97..d76ad442d36ce4 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -22,16 +22,41 @@ if(LLVM_LIBC_FULL_BUILD AND libc.include.stdbit IN_LIST TARGET_PUBLIC_HEADERS)
     stdbit_test
     SUITE
       libc_include_tests
+    HDRS
+      stdbit_stub.h
     SRCS
       stdbit_test.cpp
     DEPENDS
       libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.llvm_libc_common_h
       libc.include.stdbit
       # Intentionally do not depend on libc.src.stdbit.*. The include test is
       # simply testing the macros provided by stdbit.h, not the implementation
       # of the underlying functions which the type generic macros may dispatch
       # to.
   )
+  add_libc_test(
+    stdbit_c_test
+    UNIT_TEST_ONLY
+    SUITE
+      libc_include_tests
+    HDRS
+      stdbit_stub.h
+    SRCS
+      stdbit_test.c
+    COMPILE_OPTIONS
+      -Wall
+      -Werror
+    DEPENDS
+      libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.llvm_libc_common_h
+      libc.include.stdbit
+      libc.src.assert.__assert_fail
+      # Intentionally do not depend on libc.src.stdbit.*. The include test is
+      # simply testing the macros provided by stdbit.h, not the implementation
+      # of the underlying functions which the type generic macros may dispatch
+      # to.
+  )
 endif()
 
 add_libc_test(

diff  --git a/libc/test/include/stdbit_stub.h b/libc/test/include/stdbit_stub.h
new file mode 100644
index 00000000000000..65b1ca3b2c2976
--- /dev/null
+++ b/libc/test/include/stdbit_stub.h
@@ -0,0 +1,73 @@
+//===-- Utilities for testing stdbit --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/*
+ * Declare these BEFORE including stdbit-macros.h so that this test may still be
+ * run even if a given target doesn't yet have these individual entrypoints
+ * enabled.
+ */
+
+#include "include/__llvm-libc-common.h"
+
+#include <stdbool.h> // bool in C
+
+#define STDBIT_STUB_FUNCTION(FUNC_NAME, LEADING_VAL)                           \
+  unsigned FUNC_NAME##_uc(unsigned char x) __NOEXCEPT {                        \
+    return LEADING_VAL##AU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_us(unsigned short x) __NOEXCEPT {                       \
+    return LEADING_VAL##BU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ui(unsigned int x) __NOEXCEPT {                         \
+    return LEADING_VAL##CU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ul(unsigned long x) __NOEXCEPT {                        \
+    return LEADING_VAL##DU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ull(unsigned long long x) __NOEXCEPT {                  \
+    return LEADING_VAL##EU;                                                    \
+  }
+
+__BEGIN_C_DECLS
+
+STDBIT_STUB_FUNCTION(stdc_leading_zeros, 0xA)
+STDBIT_STUB_FUNCTION(stdc_leading_ones, 0xB)
+STDBIT_STUB_FUNCTION(stdc_trailing_zeros, 0xC)
+STDBIT_STUB_FUNCTION(stdc_trailing_ones, 0xD)
+STDBIT_STUB_FUNCTION(stdc_first_leading_zero, 0xE)
+STDBIT_STUB_FUNCTION(stdc_first_leading_one, 0xF)
+STDBIT_STUB_FUNCTION(stdc_first_trailing_zero, 0x0)
+STDBIT_STUB_FUNCTION(stdc_first_trailing_one, 0x1)
+STDBIT_STUB_FUNCTION(stdc_count_zeros, 0x2)
+STDBIT_STUB_FUNCTION(stdc_count_ones, 0x3)
+
+bool stdc_has_single_bit_uc(unsigned char x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_us(unsigned short x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ui(unsigned x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ul(unsigned long x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ull(unsigned long long x) __NOEXCEPT { return false; }
+
+STDBIT_STUB_FUNCTION(stdc_bit_width, 0x4)
+
+unsigned char stdc_bit_floor_uc(unsigned char x) __NOEXCEPT { return 0x5AU; }
+unsigned short stdc_bit_floor_us(unsigned short x) __NOEXCEPT { return 0x5BU; }
+unsigned stdc_bit_floor_ui(unsigned x) __NOEXCEPT { return 0x5CU; }
+unsigned long stdc_bit_floor_ul(unsigned long x) __NOEXCEPT { return 0x5DUL; }
+unsigned long long stdc_bit_floor_ull(unsigned long long x) __NOEXCEPT {
+  return 0x5EULL;
+}
+
+unsigned char stdc_bit_ceil_uc(unsigned char x) __NOEXCEPT { return 0x6AU; }
+unsigned short stdc_bit_ceil_us(unsigned short x) __NOEXCEPT { return 0x6BU; }
+unsigned stdc_bit_ceil_ui(unsigned x) __NOEXCEPT { return 0x6CU; }
+unsigned long stdc_bit_ceil_ul(unsigned long x) __NOEXCEPT { return 0x6DUL; }
+unsigned long long stdc_bit_ceil_ull(unsigned long long x) __NOEXCEPT {
+  return 0x6EULL;
+}
+
+__END_C_DECLS

diff  --git a/libc/test/include/stdbit_test.c b/libc/test/include/stdbit_test.c
new file mode 100644
index 00000000000000..e278e9a7374e08
--- /dev/null
+++ b/libc/test/include/stdbit_test.c
@@ -0,0 +1,61 @@
+//===-- Unittests for stdbit ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/*
+ * The intent of this test is validate that:
+ * 1. We provide the definition of the various type generic macros of stdbit.h
+ * (the macros are transitively included from stdbit-macros.h by stdbit.h).
+ * 2. It dispatches to the correct underlying function.
+ * Because unit tests build without public packaging, the object files produced
+ * do not contain non-namespaced symbols.
+ */
+
+/*
+ * Declare these BEFORE including stdbit-macros.h so that this test may still be
+ * run even if a given target doesn't yet have these individual entrypoints
+ * enabled.
+ */
+#include "stdbit_stub.h"
+
+#include "include/llvm-libc-macros/stdbit-macros.h"
+
+#include <assert.h>
+
+#define CHECK_FUNCTION(FUNC_NAME, VAL)                                         \
+  do {                                                                         \
+    assert(FUNC_NAME((unsigned char)0U) == VAL##AU);                           \
+    assert(FUNC_NAME((unsigned short)0U) == VAL##BU);                          \
+    assert(FUNC_NAME(0U) == VAL##CU);                                          \
+    assert(FUNC_NAME(0UL) == VAL##DU);                                         \
+    assert(FUNC_NAME(0ULL) == VAL##EU);                                        \
+  } while (0)
+
+int main(void) {
+  CHECK_FUNCTION(stdc_leading_zeros, 0xA);
+  CHECK_FUNCTION(stdc_leading_ones, 0xB);
+  CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
+  CHECK_FUNCTION(stdc_trailing_ones, 0xD);
+  CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
+  CHECK_FUNCTION(stdc_first_leading_one, 0xF);
+  CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
+  CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
+  CHECK_FUNCTION(stdc_count_zeros, 0x2);
+  CHECK_FUNCTION(stdc_count_ones, 0x3);
+
+  assert(!stdc_has_single_bit((unsigned char)1U));
+  assert(!stdc_has_single_bit((unsigned short)1U));
+  assert(!stdc_has_single_bit(1U));
+  assert(!stdc_has_single_bit(1UL));
+  assert(!stdc_has_single_bit(1ULL));
+
+  CHECK_FUNCTION(stdc_bit_width, 0x4);
+  CHECK_FUNCTION(stdc_bit_floor, 0x5);
+  CHECK_FUNCTION(stdc_bit_ceil, 0x6);
+
+  return 0;
+}

diff  --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 6c12665c4454d0..f3227eb86959eb 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -22,90 +22,7 @@
  * run even if a given target doesn't yet have these individual entrypoints
  * enabled.
  */
-extern "C" {
-unsigned stdc_leading_zeros_uc(unsigned char) noexcept { return 0xAAU; }
-unsigned stdc_leading_zeros_us(unsigned short) noexcept { return 0xABU; }
-unsigned stdc_leading_zeros_ui(unsigned) noexcept { return 0xACU; }
-unsigned stdc_leading_zeros_ul(unsigned long) noexcept { return 0xADU; }
-unsigned stdc_leading_zeros_ull(unsigned long long) noexcept { return 0xAEU; }
-unsigned stdc_leading_ones_uc(unsigned char) noexcept { return 0xBAU; }
-unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; }
-unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; }
-unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; }
-unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBEU; }
-unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; }
-unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; }
-unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; }
-unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; }
-unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCEU; }
-unsigned stdc_trailing_ones_uc(unsigned char) noexcept { return 0xDAU; }
-unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
-unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
-unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
-unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDEU; }
-unsigned stdc_first_leading_zero_uc(unsigned char) noexcept { return 0xEAU; }
-unsigned stdc_first_leading_zero_us(unsigned short) noexcept { return 0xEBU; }
-unsigned stdc_first_leading_zero_ui(unsigned) noexcept { return 0xECU; }
-unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
-unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
-  return 0xEEU;
-}
-unsigned stdc_first_leading_one_uc(unsigned char) noexcept { return 0xFAU; }
-unsigned stdc_first_leading_one_us(unsigned short) noexcept { return 0xFBU; }
-unsigned stdc_first_leading_one_ui(unsigned) noexcept { return 0xFCU; }
-unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
-unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
-  return 0xFEU;
-}
-unsigned stdc_first_trailing_zero_uc(unsigned char) noexcept { return 0x0AU; }
-unsigned stdc_first_trailing_zero_us(unsigned short) noexcept { return 0x0BU; }
-unsigned stdc_first_trailing_zero_ui(unsigned) noexcept { return 0x0CU; }
-unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
-unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
-  return 0x0EU;
-}
-unsigned stdc_first_trailing_one_uc(unsigned char) noexcept { return 0x1AU; }
-unsigned stdc_first_trailing_one_us(unsigned short) noexcept { return 0x1BU; }
-unsigned stdc_first_trailing_one_ui(unsigned) noexcept { return 0x1CU; }
-unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
-unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
-  return 0x1EU;
-}
-unsigned stdc_count_zeros_uc(unsigned char) noexcept { return 0x2AU; }
-unsigned stdc_count_zeros_us(unsigned short) noexcept { return 0x2BU; }
-unsigned stdc_count_zeros_ui(unsigned) noexcept { return 0x2CU; }
-unsigned stdc_count_zeros_ul(unsigned long) noexcept { return 0x2DU; }
-unsigned stdc_count_zeros_ull(unsigned long long) noexcept { return 0x2EU; }
-unsigned stdc_count_ones_uc(unsigned char) noexcept { return 0x3AU; }
-unsigned stdc_count_ones_us(unsigned short) noexcept { return 0x3BU; }
-unsigned stdc_count_ones_ui(unsigned) noexcept { return 0x3CU; }
-unsigned stdc_count_ones_ul(unsigned long) noexcept { return 0x3DU; }
-unsigned stdc_count_ones_ull(unsigned long long) noexcept { return 0x3EU; }
-bool stdc_has_single_bit_uc(unsigned char) noexcept { return false; }
-bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
-bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
-bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
-bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
-unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
-unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
-unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
-unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
-unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
-unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
-unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
-unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
-unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
-unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
-  return 0x5EU;
-}
-unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
-unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
-unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
-unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
-unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
-  return 0x6EU;
-}
-}
+#include "stdbit_stub.h"
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
 


        


More information about the libc-commits mailing list