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