TypeTraitsTest.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- TypeTraitsTest.cpp -------------------------------------------------===//
  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. #include "llvm/Support/type_traits.h"
  9. #include "gtest/gtest.h"
  10. namespace {
  11. // Compile-time tests using static assert.
  12. namespace triviality {
  13. // Helper for compile time checking trivially copy constructible and trivially
  14. // move constructible type traits.
  15. template <typename T, bool IsTriviallyCopyConstructible,
  16. bool IsTriviallyMoveConstructible>
  17. void TrivialityTester() {
  18. static_assert(llvm::is_trivially_copy_constructible<T>::value ==
  19. IsTriviallyCopyConstructible,
  20. "Mismatch in expected trivial copy construction!");
  21. static_assert(llvm::is_trivially_move_constructible<T>::value ==
  22. IsTriviallyMoveConstructible,
  23. "Mismatch in expected trivial move construction!");
  24. #if defined(_LIBCPP_VERSION) || defined(_MSC_VER)
  25. // On compilers with support for the standard traits, make sure they agree.
  26. static_assert(std::is_trivially_copy_constructible<T>::value ==
  27. IsTriviallyCopyConstructible,
  28. "Mismatch in expected trivial copy construction!");
  29. static_assert(std::is_trivially_move_constructible<T>::value ==
  30. IsTriviallyMoveConstructible,
  31. "Mismatch in expected trivial move construction!");
  32. #endif
  33. }
  34. template void TrivialityTester<int, true, true>();
  35. template void TrivialityTester<void *, true, true>();
  36. template void TrivialityTester<int &, true, true>();
  37. template void TrivialityTester<int &&, false, true>();
  38. struct X {};
  39. struct Y {
  40. Y(const Y &);
  41. };
  42. struct Z {
  43. Z(const Z &);
  44. Z(Z &&);
  45. };
  46. struct A {
  47. A(const A &) = default;
  48. A(A &&);
  49. };
  50. struct B {
  51. B(const B &);
  52. B(B &&) = default;
  53. };
  54. template void TrivialityTester<X, true, true>();
  55. template void TrivialityTester<Y, false, false>();
  56. template void TrivialityTester<Z, false, false>();
  57. template void TrivialityTester<A, true, false>();
  58. template void TrivialityTester<B, false, true>();
  59. template void TrivialityTester<Z &, true, true>();
  60. template void TrivialityTester<A &, true, true>();
  61. template void TrivialityTester<B &, true, true>();
  62. template void TrivialityTester<Z &&, false, true>();
  63. template void TrivialityTester<A &&, false, true>();
  64. template void TrivialityTester<B &&, false, true>();
  65. TEST(Triviality, Tester) {
  66. TrivialityTester<int, true, true>();
  67. TrivialityTester<void *, true, true>();
  68. TrivialityTester<int &, true, true>();
  69. TrivialityTester<int &&, false, true>();
  70. TrivialityTester<X, true, true>();
  71. TrivialityTester<Y, false, false>();
  72. TrivialityTester<Z, false, false>();
  73. TrivialityTester<A, true, false>();
  74. TrivialityTester<B, false, true>();
  75. TrivialityTester<Z &, true, true>();
  76. TrivialityTester<A &, true, true>();
  77. TrivialityTester<B &, true, true>();
  78. TrivialityTester<Z &&, false, true>();
  79. TrivialityTester<A &&, false, true>();
  80. TrivialityTester<B &&, false, true>();
  81. }
  82. } // namespace triviality
  83. } // end anonymous namespace