| 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 |
| 10 |
|
| 11 |
// enum class endian; |
| 12 |
// <bit> |
| 13 |
|
| 14 |
#include <bit> |
| 15 |
#include <cassert> |
| 16 |
#include <cstdint> |
| 17 |
#include <cstring> |
| 18 |
#include <type_traits> |
| 19 |
|
| 20 |
#include "test_macros.h" |
| 21 |
|
| 22 |
int main(int, char**) { |
| 23 |
static_assert(std::is_enum<std::endian>::value, ""); |
| 24 |
|
| 25 |
// Check that E is a scoped enum by checking for conversions. |
| 26 |
typedef std::underlying_type<std::endian>::type UT; |
| 27 |
static_assert(!std::is_convertible<std::endian, UT>::value, ""); |
| 28 |
|
| 29 |
// test that the enumeration values exist |
| 30 |
static_assert( std::endian::little == std::endian::little ); |
| 31 |
static_assert( std::endian::big == std::endian::big ); |
| 32 |
static_assert( std::endian::native == std::endian::native ); |
| 33 |
static_assert( std::endian::little != std::endian::big ); |
| 34 |
|
| 35 |
// Technically not required, but true on all existing machines |
| 36 |
static_assert( std::endian::native == std::endian::little || |
| 37 |
std::endian::native == std::endian::big ); |
| 38 |
|
| 39 |
// Try to check at runtime |
| 40 |
{ |
| 41 |
std::uint32_t i = 0x01020304; |
| 42 |
char c[4]; |
| 43 |
static_assert(sizeof(i) == sizeof(c)); |
| 44 |
std::memcpy(c, &i, sizeof(c)); |
| 45 |
|
| 46 |
assert ((c[0] == 1) == (std::endian::native == std::endian::big)); |
| 47 |
} |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |