variant_test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "opentelemetry/nostd/variant.h"
  4. #include <gtest/gtest.h>
  5. #include <string>
  6. namespace nostd = opentelemetry::nostd;
  7. class DestroyCounter
  8. {
  9. public:
  10. explicit DestroyCounter(int *count) : count_{count} {}
  11. ~DestroyCounter() { ++*count_; }
  12. private:
  13. int *count_;
  14. };
  15. TEST(VariantSizeTest, GetVariantSize)
  16. {
  17. EXPECT_EQ(nostd::variant_size<nostd::variant<>>::value, 0);
  18. EXPECT_EQ(nostd::variant_size<nostd::variant<int>>::value, 1);
  19. EXPECT_EQ((nostd::variant_size<nostd::variant<int, double>>::value), 2);
  20. }
  21. #if 0 // Disable this test for now. It does not compile with Visual Studio 2015.
  22. TEST(VariantAlternativeTest, GetVariantSize)
  23. {
  24. EXPECT_TRUE((std::is_same<nostd::variant_alternative_t<0, nostd::variant<int>>, int>::value));
  25. EXPECT_TRUE(
  26. (std::is_same<nostd::variant_alternative_t<1, nostd::variant<int, double>>, double>::value));
  27. EXPECT_TRUE((std::is_same<nostd::variant_alternative_t<1, const nostd::variant<int, double>>,
  28. const double>::value));
  29. }
  30. #endif
  31. TEST(VariantTest, Get)
  32. {
  33. nostd::variant<int, float> v, w;
  34. v = 12;
  35. EXPECT_EQ(nostd::get<int>(v), 12);
  36. EXPECT_EQ(nostd::get<0>(v), 12);
  37. w = v;
  38. EXPECT_EQ(nostd::get<int>(w), 12);
  39. EXPECT_EQ(*nostd::get_if<int>(&v), 12);
  40. EXPECT_EQ(nostd::get_if<float>(&v), nullptr);
  41. #if __EXCEPTIONS || (defined(OPENTELEMETRY_STL_VERSION) && (OPENTELEMETRY_STL_VERSION >= 2017))
  42. EXPECT_THROW(nostd::get<float>(w), nostd::bad_variant_access);
  43. #else
  44. EXPECT_DEATH({ nostd::get<float>(w); }, "");
  45. #endif
  46. }
  47. TEST(VariantTest, Comparison)
  48. {
  49. nostd::variant<int, float> v, w;
  50. EXPECT_TRUE(v == w);
  51. EXPECT_FALSE(v != w);
  52. v = 3.0f;
  53. EXPECT_TRUE(v != w);
  54. EXPECT_FALSE(v == w);
  55. v = 2;
  56. w = 3;
  57. EXPECT_TRUE(v != w);
  58. EXPECT_FALSE(v == w);
  59. EXPECT_TRUE(v < w);
  60. EXPECT_FALSE(v > w);
  61. }
  62. TEST(VariantTest, Visit)
  63. {
  64. nostd::variant<int, float> v;
  65. struct
  66. {
  67. int operator()(int) { return 0; }
  68. int operator()(float) { return 1; }
  69. } a;
  70. EXPECT_EQ(nostd::visit(a, v), 0);
  71. v = 2.0f;
  72. EXPECT_EQ(nostd::visit(a, v), 1);
  73. }
  74. TEST(VariantTest, Destructor)
  75. {
  76. nostd::variant<int, DestroyCounter> v;
  77. int destroy_count = 0;
  78. v = DestroyCounter{&destroy_count};
  79. destroy_count = 0;
  80. v = 1;
  81. EXPECT_EQ(destroy_count, 1);
  82. {
  83. nostd::variant<int, DestroyCounter> w;
  84. w = DestroyCounter{&destroy_count};
  85. destroy_count = 0;
  86. }
  87. EXPECT_EQ(destroy_count, 1);
  88. }
  89. TEST(VariantTest, Conversion)
  90. {
  91. nostd::variant<std::string> x("abc");
  92. x = "def";
  93. EXPECT_EQ(nostd::get<std::string>(x), "def");
  94. nostd::variant<std::string, void const *> y("abc");
  95. EXPECT_TRUE(nostd::holds_alternative<void const *>(y));
  96. y = std::string{"xyz"};
  97. EXPECT_TRUE(nostd::holds_alternative<std::string>(y));
  98. }
  99. TEST(VariantTest, Construction)
  100. {
  101. nostd::variant<bool, const char *, std::string> v{"abc"};
  102. EXPECT_EQ(v.index(), 1);
  103. }