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

source code of libcxx/test/std/numerics/bit/byteswap.pass.cpp