struct_reflection_test.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. explicit 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>(nullptr));
  35. static_assert(Internal::CanListInitialize<Type, Field>(nullptr));
  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>(nullptr));
  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 = NoDefaultConstructor(1),
  69. .y = NoDefaultConstructor(2)});
  70. EXPECT_EQ(std::get<0>(fields).v, 1);
  71. EXPECT_EQ(std::get<1>(fields).v, 2);
  72. }
  73. TEST(StructReflectionTest, ReferenceField) {
  74. int n = 0;
  75. std::tuple<int&> fields = AsTuple(ReferenceField{.ref = n});
  76. EXPECT_EQ(&std::get<0>(fields), &n);
  77. }
  78. } // namespace
  79. } // namespace Carbon::StructReflection