xref: /llvm-project/libcxx/test/std/numerics/bit/bit.endian/endian.pass.cpp (revision da79d6e177c5467e4a2132e017a347ff51f5228e)
130f12a42SMarshall Clow //===----------------------------------------------------------------------===//
230f12a42SMarshall Clow //
330f12a42SMarshall Clow // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
430f12a42SMarshall Clow // See https://llvm.org/LICENSE.txt for license information.
530f12a42SMarshall Clow // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630f12a42SMarshall Clow //
730f12a42SMarshall Clow //===----------------------------------------------------------------------===//
830f12a42SMarshall Clow 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14, c++17
1030f12a42SMarshall Clow 
1130f12a42SMarshall Clow // enum class endian;
1230f12a42SMarshall Clow // <bit>
1330f12a42SMarshall Clow 
1430f12a42SMarshall Clow #include <bit>
1530f12a42SMarshall Clow #include <cassert>
1630f12a42SMarshall Clow #include <cstdint>
1740a20ae6SNikolas Klauser #include <cstring>
184a04f8c2SJoe Loser #include <type_traits>
1930f12a42SMarshall Clow 
2030f12a42SMarshall Clow #include "test_macros.h"
2130f12a42SMarshall Clow 
main(int,char **)2230f12a42SMarshall Clow int main(int, char**) {
2330f12a42SMarshall Clow     static_assert(std::is_enum<std::endian>::value, "");
2430f12a42SMarshall Clow 
2530f12a42SMarshall Clow // Check that E is a scoped enum by checking for conversions.
2630f12a42SMarshall Clow     typedef std::underlying_type<std::endian>::type UT;
2730f12a42SMarshall Clow     static_assert(!std::is_convertible<std::endian, UT>::value, "");
2830f12a42SMarshall Clow 
2930f12a42SMarshall Clow // test that the enumeration values exist
3030f12a42SMarshall Clow     static_assert( std::endian::little == std::endian::little );
3130f12a42SMarshall Clow     static_assert( std::endian::big    == std::endian::big );
3230f12a42SMarshall Clow     static_assert( std::endian::native == std::endian::native );
3330f12a42SMarshall Clow     static_assert( std::endian::little != std::endian::big );
3430f12a42SMarshall Clow 
3530f12a42SMarshall Clow //  Technically not required, but true on all existing machines
3630f12a42SMarshall Clow     static_assert( std::endian::native == std::endian::little ||
3730f12a42SMarshall Clow                    std::endian::native == std::endian::big );
3830f12a42SMarshall Clow 
3930f12a42SMarshall Clow //  Try to check at runtime
4030f12a42SMarshall Clow     {
41*da79d6e1SMark de Wever     std::uint32_t i = 0x01020304;
4230f12a42SMarshall Clow     char c[4];
4330f12a42SMarshall Clow     static_assert(sizeof(i) == sizeof(c));
4430f12a42SMarshall Clow     std::memcpy(c, &i, sizeof(c));
4530f12a42SMarshall Clow 
4630f12a42SMarshall Clow     assert ((c[0] == 1) == (std::endian::native == std::endian::big));
4730f12a42SMarshall Clow     }
4830f12a42SMarshall Clow 
4930f12a42SMarshall Clow   return 0;
5030f12a42SMarshall Clow }
51