xref: /llvm-project/libc/test/src/__support/CPP/cstddef_test.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Unittests for byte ------------------------------------------------===//
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 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/CPP/cstddef.h"
10 #include "src/__support/macros/config.h"
11 #include "test/UnitTest/Test.h"
12 
13 namespace LIBC_NAMESPACE_DECL {
14 namespace cpp {
15 
16 TEST(LlvmLibcByteTest, to_integer) {
17   const char str[] = "abc";
18   const byte *const ptr = reinterpret_cast<const byte *>(str);
19   ASSERT_EQ(to_integer<char>(ptr[0]), 'a');
20   ASSERT_EQ(to_integer<char>(ptr[1]), 'b');
21   ASSERT_EQ(to_integer<char>(ptr[2]), 'c');
22   ASSERT_EQ(to_integer<char>(ptr[3]), '\0');
23 }
24 
25 TEST(LlvmLibcByteTest, bitwise) {
26   byte b{42};
27   ASSERT_EQ(b, byte{0b00101010});
28 
29   b <<= 1;
30   ASSERT_EQ(b, byte{0b01010100});
31   b >>= 1;
32 
33   ASSERT_EQ((b << 1), byte{0b01010100});
34   ASSERT_EQ((b >> 1), byte{0b00010101});
35 
36   b |= byte{0b11110000};
37   ASSERT_EQ(b, byte{0b11111010});
38 
39   b &= byte{0b11110000};
40   ASSERT_EQ(b, byte{0b11110000});
41 
42   b ^= byte{0b11111111};
43   ASSERT_EQ(b, byte{0b00001111});
44 }
45 
46 } // namespace cpp
47 } // namespace LIBC_NAMESPACE_DECL
48