brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 9d4e328 Raw
104 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11#include <bit>12#include <cassert>13#include <cstddef>14#include <cstdint>15#include <utility>16 17#include "test_macros.h"18 19template <class T>20concept has_byteswap = requires(T t) {21  std::byteswap(t);22};23 24static_assert(!has_byteswap<void*>);25static_assert(!has_byteswap<float>);26static_assert(!has_byteswap<char[2]>);27static_assert(!has_byteswap<std::byte>);28 29template <class T>30constexpr 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 36template <class T>37constexpr 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 51template <class T>52constexpr void test_implementation_defined_size() {53  const auto [in, expected] = get_test_data<T>();54  test_num<T>(in, expected);55}56 57constexpr bool test() {58  test_num<std::uint8_t>(0xAB, 0xAB);59  test_num<std::uint16_t>(0xCDEF, 0xEFCD);60  test_num<std::uint32_t>(0x01234567, 0x67452301);61  test_num<std::uint64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301);62 63  test_num<std::int8_t>(static_cast<std::int8_t>(0xAB), static_cast<std::int8_t>(0xAB));64  test_num<std::int16_t>(static_cast<std::int16_t>(0xCDEF), static_cast<std::int16_t>(0xEFCD));65  test_num<std::int32_t>(0x01234567, 0x67452301);66  test_num<std::int64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301);67 68#ifndef TEST_HAS_NO_INT12869  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#endif74 75  test_num<bool>(true, true);76  test_num<bool>(false, false);77  test_num<char>(static_cast<char>(0xCD), static_cast<char>(0xCD));78  test_num<unsigned char>(0xEF, 0xEF);79  test_num<signed char>(0x45, 0x45);80  test_num<char8_t>(0xAB, 0xAB);81  test_num<char16_t>(0xABCD, 0xCDAB);82  test_num<char32_t>(0xABCDEF01, 0x01EFCDAB);83#ifndef TEST_HAS_NO_WIDE_CHARACTERS84  test_implementation_defined_size<wchar_t>();85#endif86 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 98int main(int, char**) {99  test();100  static_assert(test());101 102  return 0;103}104