123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- #include "opentelemetry/nostd/variant.h"
- #include <gtest/gtest.h>
- #include <string>
- namespace nostd = opentelemetry::nostd;
- class DestroyCounter
- {
- public:
- explicit DestroyCounter(int *count) : count_{count} {}
- ~DestroyCounter() { ++*count_; }
- private:
- int *count_;
- };
- TEST(VariantSizeTest, GetVariantSize)
- {
- EXPECT_EQ(nostd::variant_size<nostd::variant<>>::value, 0);
- EXPECT_EQ(nostd::variant_size<nostd::variant<int>>::value, 1);
- EXPECT_EQ((nostd::variant_size<nostd::variant<int, double>>::value), 2);
- }
- #if 0 // Disable this test for now. It does not compile with Visual Studio 2015.
- TEST(VariantAlternativeTest, GetVariantSize)
- {
- EXPECT_TRUE((std::is_same<nostd::variant_alternative_t<0, nostd::variant<int>>, int>::value));
- EXPECT_TRUE(
- (std::is_same<nostd::variant_alternative_t<1, nostd::variant<int, double>>, double>::value));
- EXPECT_TRUE((std::is_same<nostd::variant_alternative_t<1, const nostd::variant<int, double>>,
- const double>::value));
- }
- #endif
- TEST(VariantTest, Get)
- {
- nostd::variant<int, float> v, w;
- v = 12;
- EXPECT_EQ(nostd::get<int>(v), 12);
- EXPECT_EQ(nostd::get<0>(v), 12);
- w = v;
- EXPECT_EQ(nostd::get<int>(w), 12);
- EXPECT_EQ(*nostd::get_if<int>(&v), 12);
- EXPECT_EQ(nostd::get_if<float>(&v), nullptr);
- #if __EXCEPTIONS || (defined(OPENTELEMETRY_STL_VERSION) && (OPENTELEMETRY_STL_VERSION >= 2017))
- EXPECT_THROW(nostd::get<float>(w), nostd::bad_variant_access);
- #else
- EXPECT_DEATH({ nostd::get<float>(w); }, "");
- #endif
- }
- TEST(VariantTest, Comparison)
- {
- nostd::variant<int, float> v, w;
- EXPECT_TRUE(v == w);
- EXPECT_FALSE(v != w);
- v = 3.0f;
- EXPECT_TRUE(v != w);
- EXPECT_FALSE(v == w);
- v = 2;
- w = 3;
- EXPECT_TRUE(v != w);
- EXPECT_FALSE(v == w);
- EXPECT_TRUE(v < w);
- EXPECT_FALSE(v > w);
- }
- TEST(VariantTest, Visit)
- {
- nostd::variant<int, float> v;
- struct
- {
- int operator()(int) { return 0; }
- int operator()(float) { return 1; }
- } a;
- EXPECT_EQ(nostd::visit(a, v), 0);
- v = 2.0f;
- EXPECT_EQ(nostd::visit(a, v), 1);
- }
- TEST(VariantTest, Destructor)
- {
- nostd::variant<int, DestroyCounter> v;
- int destroy_count = 0;
- v = DestroyCounter{&destroy_count};
- destroy_count = 0;
- v = 1;
- EXPECT_EQ(destroy_count, 1);
- {
- nostd::variant<int, DestroyCounter> w;
- w = DestroyCounter{&destroy_count};
- destroy_count = 0;
- }
- EXPECT_EQ(destroy_count, 1);
- }
- TEST(VariantTest, Conversion)
- {
- nostd::variant<std::string> x("abc");
- x = "def";
- EXPECT_EQ(nostd::get<std::string>(x), "def");
- nostd::variant<std::string, void const *> y("abc");
- EXPECT_TRUE(nostd::holds_alternative<void const *>(y));
- y = std::string{"xyz"};
- EXPECT_TRUE(nostd::holds_alternative<std::string>(y));
- }
- TEST(VariantTest, Construction)
- {
- nostd::variant<bool, const char *, std::string> v{"abc"};
- EXPECT_EQ(v.index(), 1);
- }
|