| 1 | //===----------------------------------------------------------------------===// |
| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | #include <bit> |
| 12 | #include <cassert> |
| 13 | #include <cstddef> |
| 14 | #include <cstdint> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | template <class T> |
| 20 | concept has_byteswap = requires(T t) { |
| 21 | std::byteswap(t); |
| 22 | }; |
| 23 | |
| 24 | static_assert(!has_byteswap<void*>); |
| 25 | static_assert(!has_byteswap<float>); |
| 26 | static_assert(!has_byteswap<char[2]>); |
| 27 | static_assert(!has_byteswap<std::byte>); |
| 28 | |
| 29 | template <class T> |
| 30 | constexpr void test_num(T in, T expected) { |
| 31 | assert(std::byteswap(in) == expected); |
| 32 | ASSERT_SAME_TYPE(decltype(std::byteswap(in)), decltype(in)); |
| 33 | ASSERT_NOEXCEPT(std::byteswap(in)); |
| 34 | } |
| 35 | |
| 36 | template <class T> |
| 37 | constexpr std::pair<T, T> get_test_data() { |
| 38 | switch (sizeof(T)) { |
| 39 | case 2: |
| 40 | return {static_cast<T>(0x1234), static_cast<T>(0x3412)}; |
| 41 | case 4: |
| 42 | return {static_cast<T>(0x60AF8503), static_cast<T>(0x0385AF60)}; |
| 43 | case 8: |
| 44 | return {static_cast<T>(0xABCDFE9477936406), static_cast<T>(0x0664937794FECDAB)}; |
| 45 | default: |
| 46 | assert(false); |
| 47 | return {}; // for MSVC, whose `assert` is tragically not [[noreturn]] |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | template <class T> |
| 52 | constexpr void test_implementation_defined_size() { |
| 53 | const auto [in, expected] = get_test_data<T>(); |
| 54 | test_num<T>(in, expected); |
| 55 | } |
| 56 | |
| 57 | constexpr bool test() { |
| 58 | test_num<std::uint8_t>(in: 0xAB, expected: 0xAB); |
| 59 | test_num<std::uint16_t>(in: 0xCDEF, expected: 0xEFCD); |
| 60 | test_num<std::uint32_t>(in: 0x01234567, expected: 0x67452301); |
| 61 | test_num<std::uint64_t>(in: 0x0123456789ABCDEF, expected: 0xEFCDAB8967452301); |
| 62 | |
| 63 | test_num<std::int8_t>(in: static_cast<std::int8_t>(0xAB), expected: static_cast<std::int8_t>(0xAB)); |
| 64 | test_num<std::int16_t>(in: static_cast<std::int16_t>(0xCDEF), expected: static_cast<std::int16_t>(0xEFCD)); |
| 65 | test_num<std::int32_t>(in: 0x01234567, expected: 0x67452301); |
| 66 | test_num<std::int64_t>(in: 0x0123456789ABCDEF, expected: 0xEFCDAB8967452301); |
| 67 | |
| 68 | #ifndef TEST_HAS_NO_INT128 |
| 69 | const auto in = static_cast<__uint128_t>(0x0123456789ABCDEF) << 64 | 0x13579BDF02468ACE; |
| 70 | const auto expected = static_cast<__uint128_t>(0xCE8A4602DF9B5713) << 64 | 0xEFCDAB8967452301; |
| 71 | test_num<__uint128_t>(in, expected); |
| 72 | test_num<__int128_t>(in, expected); |
| 73 | #endif |
| 74 | |
| 75 | test_num<bool>(in: true, expected: true); |
| 76 | test_num<bool>(in: false, expected: false); |
| 77 | test_num<char>(in: static_cast<char>(0xCD), expected: static_cast<char>(0xCD)); |
| 78 | test_num<unsigned char>(in: 0xEF, expected: 0xEF); |
| 79 | test_num<signed char>(in: 0x45, expected: 0x45); |
| 80 | test_num<char8_t>(0xAB, 0xAB); |
| 81 | test_num<char16_t>(in: 0xABCD, expected: 0xCDAB); |
| 82 | test_num<char32_t>(in: 0xABCDEF01, expected: 0x01EFCDAB); |
| 83 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 84 | test_implementation_defined_size<wchar_t>(); |
| 85 | #endif |
| 86 | |
| 87 | test_implementation_defined_size<short>(); |
| 88 | test_implementation_defined_size<unsigned short>(); |
| 89 | test_implementation_defined_size<int>(); |
| 90 | test_implementation_defined_size<unsigned int>(); |
| 91 | test_implementation_defined_size<long>(); |
| 92 | test_implementation_defined_size<unsigned long>(); |
| 93 | test_implementation_defined_size<long long>(); |
| 94 | test_implementation_defined_size<unsigned long long>(); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | int main(int, char**) { |
| 99 | test(); |
| 100 | static_assert(test()); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |