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#include "test/UnitTest/Test.h"
10
11/*
12 * The intent of this test is validate that:
13 * 1. We provide the definition of the various type generic macros of stdbit.h
14 * (the macros are transitively included from stdbit-macros.h by stdbit.h).
15 * 2. It dispatches to the correct underlying function.
16 * Because unit tests build without public packaging, the object files produced
17 * do not contain non-namespaced symbols.
18 */
19
20/*
21 * Declare these BEFORE including stdbit-macros.h so that this test may still be
22 * run even if a given target doesn't yet have these individual entrypoints
23 * enabled.
24 */
25#include "stdbit_stub.h"
26
27#include "include/llvm-libc-macros/stdbit-macros.h"
28
29TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingZeros) {
30 EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned char>(0U)), 0xAAU);
31 EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned short>(0U)), 0xABU);
32 EXPECT_EQ(stdc_leading_zeros(0U), 0xACU);
33 EXPECT_EQ(stdc_leading_zeros(0UL), 0xADU);
34 EXPECT_EQ(stdc_leading_zeros(0ULL), 0xAEU);
35}
36
37TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
38 EXPECT_EQ(stdc_leading_ones(static_cast<unsigned char>(0U)), 0xBAU);
39 EXPECT_EQ(stdc_leading_ones(static_cast<unsigned short>(0U)), 0xBBU);
40 EXPECT_EQ(stdc_leading_ones(0U), 0xBCU);
41 EXPECT_EQ(stdc_leading_ones(0UL), 0xBDU);
42 EXPECT_EQ(stdc_leading_ones(0ULL), 0xBEU);
43}
44
45TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
46 EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned char>(0U)), 0xCAU);
47 EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned short>(0U)), 0xCBU);
48 EXPECT_EQ(stdc_trailing_zeros(0U), 0xCCU);
49 EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
50 EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCEU);
51}
52
53TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
54 EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned char>(0U)), 0xDAU);
55 EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned short>(0U)), 0xDBU);
56 EXPECT_EQ(stdc_trailing_ones(0U), 0xDCU);
57 EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
58 EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDEU);
59}
60
61TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
62 EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned char>(0U)), 0xEAU);
63 EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned short>(0U)), 0xEBU);
64 EXPECT_EQ(stdc_first_leading_zero(0U), 0xECU);
65 EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
66 EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEEU);
67}
68
69TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
70 EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned char>(0U)), 0xFAU);
71 EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned short>(0U)), 0xFBU);
72 EXPECT_EQ(stdc_first_leading_one(0U), 0xFCU);
73 EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
74 EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFEU);
75}
76
77TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
78 EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned char>(0U)), 0x0AU);
79 EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned short>(0U)), 0x0BU);
80 EXPECT_EQ(stdc_first_trailing_zero(0U), 0x0CU);
81 EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
82 EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0EU);
83}
84
85TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingOne) {
86 EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned char>(0U)), 0x1AU);
87 EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned short>(0U)), 0x1BU);
88 EXPECT_EQ(stdc_first_trailing_one(0U), 0x1CU);
89 EXPECT_EQ(stdc_first_trailing_one(0UL), 0x1DU);
90 EXPECT_EQ(stdc_first_trailing_one(0ULL), 0x1EU);
91}
92
93TEST(LlvmLibcStdbitTest, TypeGenericMacroCountZeros) {
94 EXPECT_EQ(stdc_count_zeros(static_cast<unsigned char>(0U)), 0x2AU);
95 EXPECT_EQ(stdc_count_zeros(static_cast<unsigned short>(0U)), 0x2BU);
96 EXPECT_EQ(stdc_count_zeros(0U), 0x2CU);
97 EXPECT_EQ(stdc_count_zeros(0UL), 0x2DU);
98 EXPECT_EQ(stdc_count_zeros(0ULL), 0x2EU);
99}
100
101TEST(LlvmLibcStdbitTest, TypeGenericMacroCountOnes) {
102 EXPECT_EQ(stdc_count_ones(static_cast<unsigned char>(0U)), 0x3AU);
103 EXPECT_EQ(stdc_count_ones(static_cast<unsigned short>(0U)), 0x3BU);
104 EXPECT_EQ(stdc_count_ones(0U), 0x3CU);
105 EXPECT_EQ(stdc_count_ones(0UL), 0x3DU);
106 EXPECT_EQ(stdc_count_ones(0ULL), 0x3EU);
107}
108
109TEST(LlvmLibcStdbitTest, TypeGenericMacroHasSingleBit) {
110 EXPECT_EQ(stdc_has_single_bit(static_cast<unsigned char>(1U)), false);
111 EXPECT_EQ(stdc_has_single_bit(static_cast<unsigned short>(1U)), false);
112 EXPECT_EQ(stdc_has_single_bit(1U), false);
113 EXPECT_EQ(stdc_has_single_bit(1UL), false);
114 EXPECT_EQ(stdc_has_single_bit(1ULL), false);
115}
116
117TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
118 EXPECT_EQ(stdc_bit_width(static_cast<unsigned char>(1U)), 0x4AU);
119 EXPECT_EQ(stdc_bit_width(static_cast<unsigned short>(1U)), 0x4BU);
120 EXPECT_EQ(stdc_bit_width(1U), 0x4CU);
121 EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
122 EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
123}
124
125TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
126 EXPECT_EQ(stdc_bit_floor(static_cast<unsigned char>(0U)),
127 static_cast<unsigned char>(0x5AU));
128 EXPECT_EQ(stdc_bit_floor(static_cast<unsigned short>(0U)),
129 static_cast<unsigned short>(0x5BU));
130 EXPECT_EQ(stdc_bit_floor(0U), 0x5CU);
131 EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
132 EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
133}
134
135TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
136 EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned char>(0U)),
137 static_cast<unsigned char>(0x6AU));
138 EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned short>(0U)),
139 static_cast<unsigned short>(0x6BU));
140 EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU);
141 EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
142 EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
143}
144
145TEST(LlvmLibcStdbitTest, VersionMacro) {
146 // 7.18.1p2 an integer constant expression with a value equivalent to 202311L.
147 EXPECT_EQ(__STDC_VERSION_STDBIT_H__, 202311L);
148}
149
150TEST(LlvmLibcStdbitTest, EndianMacros) {
151 // 7.18.2p3 The values of the integer constant expressions for
152 // __STDC_ENDIAN_LITTLE__ and __STDC_ENDIAN_BIG__ are not equal.
153 EXPECT_NE(__STDC_ENDIAN_LITTLE__, __STDC_ENDIAN_BIG__);
154 // The standard does allow for __STDC_ENDIAN_NATIVE__ to be an integer
155 // constant expression with an implementation defined value for non-big or
156 // little endianness environments. I assert such machines are no longer
157 // relevant.
158 EXPECT_TRUE(__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__ ||
159 __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__);
160}
161

source code of libc/test/include/stdbit_test.cpp