struct_reflection_test.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/struct_reflection.h"
  5. #include <gtest/gtest.h>
  6. namespace Carbon::StructReflection {
  7. namespace {
  8. struct ZeroFields {};
  9. struct OneField {
  10. int x;
  11. };
  12. struct TwoFields {
  13. int x;
  14. int y;
  15. };
  16. struct ReferenceField {
  17. int& ref;
  18. };
  19. struct NoDefaultConstructor {
  20. NoDefaultConstructor(int n) : v(n) {}
  21. int v;
  22. };
  23. struct OneFieldNoDefaultConstructor {
  24. NoDefaultConstructor x;
  25. };
  26. struct TwoFieldsNoDefaultConstructor {
  27. NoDefaultConstructor x;
  28. NoDefaultConstructor y;
  29. };
  30. TEST(StructReflectionTest, CanListInitialize) {
  31. {
  32. using Type = OneField;
  33. using Field = Internal::AnyField<Type>;
  34. static_assert(Internal::CanListInitialize<Type>(0));
  35. static_assert(Internal::CanListInitialize<Type, Field>(0));
  36. static_assert(!Internal::CanListInitialize<Type, Field, Field>(0));
  37. }
  38. {
  39. using Type = OneFieldNoDefaultConstructor;
  40. using Field = Internal::AnyField<Type>;
  41. static_assert(!Internal::CanListInitialize<Type>(0));
  42. static_assert(Internal::CanListInitialize<Type, Field>(0));
  43. static_assert(!Internal::CanListInitialize<Type, Field, Field>(0));
  44. }
  45. }
  46. TEST(StructReflectionTest, CountFields) {
  47. static_assert(Internal::CountFields<ZeroFields>() == 0);
  48. static_assert(Internal::CountFields<OneField>() == 1);
  49. static_assert(Internal::CountFields<TwoFields>() == 2);
  50. static_assert(Internal::CountFields<ReferenceField>() == 1);
  51. static_assert(Internal::CountFields<OneFieldNoDefaultConstructor>() == 1);
  52. }
  53. TEST(StructReflectionTest, EmptyStruct) {
  54. std::tuple<> fields = AsTuple(ZeroFields());
  55. static_cast<void>(fields);
  56. }
  57. TEST(StructReflectionTest, OneField) {
  58. std::tuple<int> fields = AsTuple(OneField{.x = 1});
  59. EXPECT_EQ(std::get<0>(fields), 1);
  60. }
  61. TEST(StructReflectionTest, TwoField) {
  62. std::tuple<int, int> fields = AsTuple(TwoFields{.x = 1, .y = 2});
  63. EXPECT_EQ(std::get<0>(fields), 1);
  64. EXPECT_EQ(std::get<1>(fields), 2);
  65. }
  66. TEST(StructReflectionTest, NoDefaultConstructor) {
  67. std::tuple<NoDefaultConstructor, NoDefaultConstructor> fields =
  68. AsTuple(TwoFieldsNoDefaultConstructor{.x = 1, .y = 2});
  69. EXPECT_EQ(std::get<0>(fields).v, 1);
  70. EXPECT_EQ(std::get<1>(fields).v, 2);
  71. }
  72. TEST(StructReflectionTest, ReferenceField) {
  73. int n = 0;
  74. std::tuple<int&> fields = AsTuple(ReferenceField{.ref = n});
  75. EXPECT_EQ(&std::get<0>(fields), &n);
  76. }
  77. } // namespace
  78. } // namespace Carbon::StructReflection