Skip to content

Commit a25fa92

Browse files
authored
[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 #84658. In this patch, we add a simple test for them in C.
1 parent 1def98d commit a25fa92

File tree

4 files changed

+160
-84
lines changed

4 files changed

+160
-84
lines changed

libc/test/include/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,41 @@ if(LLVM_LIBC_FULL_BUILD AND libc.include.stdbit IN_LIST TARGET_PUBLIC_HEADERS)
2222
stdbit_test
2323
SUITE
2424
libc_include_tests
25+
HDRS
26+
stdbit_stub.h
2527
SRCS
2628
stdbit_test.cpp
2729
DEPENDS
2830
libc.include.llvm-libc-macros.stdbit_macros
31+
libc.include.llvm_libc_common_h
2932
libc.include.stdbit
3033
# Intentionally do not depend on libc.src.stdbit.*. The include test is
3134
# simply testing the macros provided by stdbit.h, not the implementation
3235
# of the underlying functions which the type generic macros may dispatch
3336
# to.
3437
)
38+
add_libc_test(
39+
stdbit_c_test
40+
UNIT_TEST_ONLY
41+
SUITE
42+
libc_include_tests
43+
HDRS
44+
stdbit_stub.h
45+
SRCS
46+
stdbit_test.c
47+
COMPILE_OPTIONS
48+
-Wall
49+
-Werror
50+
DEPENDS
51+
libc.include.llvm-libc-macros.stdbit_macros
52+
libc.include.llvm_libc_common_h
53+
libc.include.stdbit
54+
libc.src.assert.__assert_fail
55+
# Intentionally do not depend on libc.src.stdbit.*. The include test is
56+
# simply testing the macros provided by stdbit.h, not the implementation
57+
# of the underlying functions which the type generic macros may dispatch
58+
# to.
59+
)
3560
endif()
3661

3762
add_libc_test(

libc/test/include/stdbit_stub.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===-- Utilities for testing stdbit --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
/*
10+
* Declare these BEFORE including stdbit-macros.h so that this test may still be
11+
* run even if a given target doesn't yet have these individual entrypoints
12+
* enabled.
13+
*/
14+
15+
#include "include/__llvm-libc-common.h"
16+
17+
#include <stdbool.h> // bool in C
18+
19+
#define STDBIT_STUB_FUNCTION(FUNC_NAME, LEADING_VAL) \
20+
unsigned FUNC_NAME##_uc(unsigned char x) __NOEXCEPT { \
21+
return LEADING_VAL##AU; \
22+
} \
23+
unsigned FUNC_NAME##_us(unsigned short x) __NOEXCEPT { \
24+
return LEADING_VAL##BU; \
25+
} \
26+
unsigned FUNC_NAME##_ui(unsigned int x) __NOEXCEPT { \
27+
return LEADING_VAL##CU; \
28+
} \
29+
unsigned FUNC_NAME##_ul(unsigned long x) __NOEXCEPT { \
30+
return LEADING_VAL##DU; \
31+
} \
32+
unsigned FUNC_NAME##_ull(unsigned long long x) __NOEXCEPT { \
33+
return LEADING_VAL##EU; \
34+
}
35+
36+
__BEGIN_C_DECLS
37+
38+
STDBIT_STUB_FUNCTION(stdc_leading_zeros, 0xA)
39+
STDBIT_STUB_FUNCTION(stdc_leading_ones, 0xB)
40+
STDBIT_STUB_FUNCTION(stdc_trailing_zeros, 0xC)
41+
STDBIT_STUB_FUNCTION(stdc_trailing_ones, 0xD)
42+
STDBIT_STUB_FUNCTION(stdc_first_leading_zero, 0xE)
43+
STDBIT_STUB_FUNCTION(stdc_first_leading_one, 0xF)
44+
STDBIT_STUB_FUNCTION(stdc_first_trailing_zero, 0x0)
45+
STDBIT_STUB_FUNCTION(stdc_first_trailing_one, 0x1)
46+
STDBIT_STUB_FUNCTION(stdc_count_zeros, 0x2)
47+
STDBIT_STUB_FUNCTION(stdc_count_ones, 0x3)
48+
49+
bool stdc_has_single_bit_uc(unsigned char x) __NOEXCEPT { return false; }
50+
bool stdc_has_single_bit_us(unsigned short x) __NOEXCEPT { return false; }
51+
bool stdc_has_single_bit_ui(unsigned x) __NOEXCEPT { return false; }
52+
bool stdc_has_single_bit_ul(unsigned long x) __NOEXCEPT { return false; }
53+
bool stdc_has_single_bit_ull(unsigned long long x) __NOEXCEPT { return false; }
54+
55+
STDBIT_STUB_FUNCTION(stdc_bit_width, 0x4)
56+
57+
unsigned char stdc_bit_floor_uc(unsigned char x) __NOEXCEPT { return 0x5AU; }
58+
unsigned short stdc_bit_floor_us(unsigned short x) __NOEXCEPT { return 0x5BU; }
59+
unsigned stdc_bit_floor_ui(unsigned x) __NOEXCEPT { return 0x5CU; }
60+
unsigned long stdc_bit_floor_ul(unsigned long x) __NOEXCEPT { return 0x5DUL; }
61+
unsigned long long stdc_bit_floor_ull(unsigned long long x) __NOEXCEPT {
62+
return 0x5EULL;
63+
}
64+
65+
unsigned char stdc_bit_ceil_uc(unsigned char x) __NOEXCEPT { return 0x6AU; }
66+
unsigned short stdc_bit_ceil_us(unsigned short x) __NOEXCEPT { return 0x6BU; }
67+
unsigned stdc_bit_ceil_ui(unsigned x) __NOEXCEPT { return 0x6CU; }
68+
unsigned long stdc_bit_ceil_ul(unsigned long x) __NOEXCEPT { return 0x6DUL; }
69+
unsigned long long stdc_bit_ceil_ull(unsigned long long x) __NOEXCEPT {
70+
return 0x6EULL;
71+
}
72+
73+
__END_C_DECLS

libc/test/include/stdbit_test.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===-- Unittests for stdbit ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
/*
10+
* The intent of this test is validate that:
11+
* 1. We provide the definition of the various type generic macros of stdbit.h
12+
* (the macros are transitively included from stdbit-macros.h by stdbit.h).
13+
* 2. It dispatches to the correct underlying function.
14+
* Because unit tests build without public packaging, the object files produced
15+
* do not contain non-namespaced symbols.
16+
*/
17+
18+
/*
19+
* Declare these BEFORE including stdbit-macros.h so that this test may still be
20+
* run even if a given target doesn't yet have these individual entrypoints
21+
* enabled.
22+
*/
23+
#include "stdbit_stub.h"
24+
25+
#include "include/llvm-libc-macros/stdbit-macros.h"
26+
27+
#include <assert.h>
28+
29+
#define CHECK_FUNCTION(FUNC_NAME, VAL) \
30+
do { \
31+
assert(FUNC_NAME((unsigned char)0U) == VAL##AU); \
32+
assert(FUNC_NAME((unsigned short)0U) == VAL##BU); \
33+
assert(FUNC_NAME(0U) == VAL##CU); \
34+
assert(FUNC_NAME(0UL) == VAL##DU); \
35+
assert(FUNC_NAME(0ULL) == VAL##EU); \
36+
} while (0)
37+
38+
int main(void) {
39+
CHECK_FUNCTION(stdc_leading_zeros, 0xA);
40+
CHECK_FUNCTION(stdc_leading_ones, 0xB);
41+
CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
42+
CHECK_FUNCTION(stdc_trailing_ones, 0xD);
43+
CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
44+
CHECK_FUNCTION(stdc_first_leading_one, 0xF);
45+
CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
46+
CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
47+
CHECK_FUNCTION(stdc_count_zeros, 0x2);
48+
CHECK_FUNCTION(stdc_count_ones, 0x3);
49+
50+
assert(!stdc_has_single_bit((unsigned char)1U));
51+
assert(!stdc_has_single_bit((unsigned short)1U));
52+
assert(!stdc_has_single_bit(1U));
53+
assert(!stdc_has_single_bit(1UL));
54+
assert(!stdc_has_single_bit(1ULL));
55+
56+
CHECK_FUNCTION(stdc_bit_width, 0x4);
57+
CHECK_FUNCTION(stdc_bit_floor, 0x5);
58+
CHECK_FUNCTION(stdc_bit_ceil, 0x6);
59+
60+
return 0;
61+
}

libc/test/include/stdbit_test.cpp

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -22,90 +22,7 @@
2222
* run even if a given target doesn't yet have these individual entrypoints
2323
* enabled.
2424
*/
25-
extern "C" {
26-
unsigned stdc_leading_zeros_uc(unsigned char) noexcept { return 0xAAU; }
27-
unsigned stdc_leading_zeros_us(unsigned short) noexcept { return 0xABU; }
28-
unsigned stdc_leading_zeros_ui(unsigned) noexcept { return 0xACU; }
29-
unsigned stdc_leading_zeros_ul(unsigned long) noexcept { return 0xADU; }
30-
unsigned stdc_leading_zeros_ull(unsigned long long) noexcept { return 0xAEU; }
31-
unsigned stdc_leading_ones_uc(unsigned char) noexcept { return 0xBAU; }
32-
unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; }
33-
unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; }
34-
unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; }
35-
unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBEU; }
36-
unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; }
37-
unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; }
38-
unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; }
39-
unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; }
40-
unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCEU; }
41-
unsigned stdc_trailing_ones_uc(unsigned char) noexcept { return 0xDAU; }
42-
unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
43-
unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
44-
unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
45-
unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDEU; }
46-
unsigned stdc_first_leading_zero_uc(unsigned char) noexcept { return 0xEAU; }
47-
unsigned stdc_first_leading_zero_us(unsigned short) noexcept { return 0xEBU; }
48-
unsigned stdc_first_leading_zero_ui(unsigned) noexcept { return 0xECU; }
49-
unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
50-
unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
51-
return 0xEEU;
52-
}
53-
unsigned stdc_first_leading_one_uc(unsigned char) noexcept { return 0xFAU; }
54-
unsigned stdc_first_leading_one_us(unsigned short) noexcept { return 0xFBU; }
55-
unsigned stdc_first_leading_one_ui(unsigned) noexcept { return 0xFCU; }
56-
unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
57-
unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
58-
return 0xFEU;
59-
}
60-
unsigned stdc_first_trailing_zero_uc(unsigned char) noexcept { return 0x0AU; }
61-
unsigned stdc_first_trailing_zero_us(unsigned short) noexcept { return 0x0BU; }
62-
unsigned stdc_first_trailing_zero_ui(unsigned) noexcept { return 0x0CU; }
63-
unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
64-
unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
65-
return 0x0EU;
66-
}
67-
unsigned stdc_first_trailing_one_uc(unsigned char) noexcept { return 0x1AU; }
68-
unsigned stdc_first_trailing_one_us(unsigned short) noexcept { return 0x1BU; }
69-
unsigned stdc_first_trailing_one_ui(unsigned) noexcept { return 0x1CU; }
70-
unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
71-
unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
72-
return 0x1EU;
73-
}
74-
unsigned stdc_count_zeros_uc(unsigned char) noexcept { return 0x2AU; }
75-
unsigned stdc_count_zeros_us(unsigned short) noexcept { return 0x2BU; }
76-
unsigned stdc_count_zeros_ui(unsigned) noexcept { return 0x2CU; }
77-
unsigned stdc_count_zeros_ul(unsigned long) noexcept { return 0x2DU; }
78-
unsigned stdc_count_zeros_ull(unsigned long long) noexcept { return 0x2EU; }
79-
unsigned stdc_count_ones_uc(unsigned char) noexcept { return 0x3AU; }
80-
unsigned stdc_count_ones_us(unsigned short) noexcept { return 0x3BU; }
81-
unsigned stdc_count_ones_ui(unsigned) noexcept { return 0x3CU; }
82-
unsigned stdc_count_ones_ul(unsigned long) noexcept { return 0x3DU; }
83-
unsigned stdc_count_ones_ull(unsigned long long) noexcept { return 0x3EU; }
84-
bool stdc_has_single_bit_uc(unsigned char) noexcept { return false; }
85-
bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
86-
bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
87-
bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
88-
bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
89-
unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
90-
unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
91-
unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
92-
unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
93-
unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
94-
unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
95-
unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
96-
unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
97-
unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
98-
unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
99-
return 0x5EU;
100-
}
101-
unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
102-
unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
103-
unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
104-
unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
105-
unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
106-
return 0x6EU;
107-
}
108-
}
25+
#include "stdbit_stub.h"
10926

11027
#include "include/llvm-libc-macros/stdbit-macros.h"
11128

0 commit comments

Comments
 (0)