//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 #include #include #include #include #include "test_macros.h" template concept has_byteswap = requires(T t) { std::byteswap(t); }; static_assert(!has_byteswap); static_assert(!has_byteswap); static_assert(!has_byteswap); static_assert(!has_byteswap); template constexpr void test_num(T in, T expected) { assert(std::byteswap(in) == expected); ASSERT_SAME_TYPE(decltype(std::byteswap(in)), decltype(in)); ASSERT_NOEXCEPT(std::byteswap(in)); } template constexpr std::pair get_test_data() { switch (sizeof(T)) { case 2: return {static_cast(0x1234), static_cast(0x3412)}; case 4: return {static_cast(0x60AF8503), static_cast(0x0385AF60)}; case 8: return {static_cast(0xABCDFE9477936406), static_cast(0x0664937794FECDAB)}; default: assert(false); return {}; // for MSVC, whose `assert` is tragically not [[noreturn]] } } template constexpr void test_implementation_defined_size() { const auto [in, expected] = get_test_data(); test_num(in, expected); } constexpr bool test() { test_num(0xAB, 0xAB); test_num(0xCDEF, 0xEFCD); test_num(0x01234567, 0x67452301); test_num(0x0123456789ABCDEF, 0xEFCDAB8967452301); test_num(static_cast(0xAB), static_cast(0xAB)); test_num(static_cast(0xCDEF), static_cast(0xEFCD)); test_num(0x01234567, 0x67452301); test_num(0x0123456789ABCDEF, 0xEFCDAB8967452301); #ifndef TEST_HAS_NO_INT128 const auto in = static_cast<__uint128_t>(0x0123456789ABCDEF) << 64 | 0x13579BDF02468ACE; const auto expected = static_cast<__uint128_t>(0xCE8A4602DF9B5713) << 64 | 0xEFCDAB8967452301; test_num<__uint128_t>(in, expected); test_num<__int128_t>(in, expected); #endif test_num(true, true); test_num(false, false); test_num(static_cast(0xCD), static_cast(0xCD)); test_num(0xEF, 0xEF); test_num(0x45, 0x45); test_num(0xAB, 0xAB); test_num(0xABCD, 0xCDAB); test_num(0xABCDEF01, 0x01EFCDAB); #ifndef TEST_HAS_NO_WIDE_CHARACTERS test_implementation_defined_size(); #endif test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); test_implementation_defined_size(); return true; } int main(int, char**) { test(); static_assert(test()); return 0; }