File: variant_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,368 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 38; python: 31
file content (122 lines) | stat: -rw-r--r-- 3,235 bytes parent folder | download
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
120
121
122
// 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_; }

  DestroyCounter(const DestroyCounter &)            = default;
  DestroyCounter &operator=(const DestroyCounter &) = default;
  DestroyCounter(DestroyCounter &&)                 = default;
  DestroyCounter &operator=(DestroyCounter &&)      = default;

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