endian.pass.cpp 1.5 KB

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