summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/api/test/nostd/variant_test.cc
blob: cea5c4ee7ba83d1c83b4b9f5584894c69c517acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/nostd/variant.h"

#include <string>
#include <type_traits>

#include <gtest/gtest.h>

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
  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);
}