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
123
124
125
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/nostd/string_view.h"
#include <gtest/gtest.h>
#include <cstring>
#include <map>
using opentelemetry::nostd::string_view;
TEST(StringViewTest, DefaultConstruction)
{
string_view ref;
EXPECT_EQ(ref.data(), nullptr);
EXPECT_EQ(ref.length(), 0);
}
TEST(StringViewTest, CStringInitialization)
{
const char *val = "hello world";
string_view ref(val);
EXPECT_EQ(ref.data(), val);
EXPECT_EQ(ref.length(), std::strlen(val));
}
TEST(StringViewTest, StdStringInitialization)
{
const std::string val = "hello world";
string_view ref(val);
EXPECT_EQ(ref.data(), val.data());
EXPECT_EQ(ref.length(), val.size());
}
TEST(StringViewTest, Copy)
{
const std::string val = "hello world";
string_view ref(val);
string_view cpy(ref);
EXPECT_EQ(cpy.data(), val);
EXPECT_EQ(cpy.length(), val.length());
EXPECT_EQ(cpy, val);
}
TEST(StringViewTest, Accessor)
{
string_view s = "abc123";
EXPECT_EQ(s.data(), &s[0]);
EXPECT_EQ(s.data() + 1, &s[1]);
}
TEST(StringViewTest, ExplicitStdStringConversion)
{
std::string s = static_cast<std::string>(string_view{"abc"});
EXPECT_EQ(s, "abc");
}
TEST(StringViewTest, SubstrPortion)
{
string_view s = "abc123";
EXPECT_EQ("123", s.substr(3));
EXPECT_EQ("12", s.substr(3, 2));
}
TEST(StringViewTest, SubstrOutOfRange)
{
string_view s = "abc123";
#if __EXCEPTIONS || defined(HAVE_CPP_STDLIB)
EXPECT_THROW(s.substr(10), std::out_of_range);
#else
EXPECT_DEATH({ s.substr(10); }, "");
#endif
}
TEST(StringViewTest, FindSingleCharacter)
{
string_view s = "abc";
// starting from 0-th position (default)
EXPECT_EQ(s.find('a'), 0);
EXPECT_EQ(s.find('b'), 1);
EXPECT_EQ(s.find('c'), 2);
EXPECT_EQ(s.find('d'), -1); // FIXME: string_view:npos - problem with linker
// starting from given index
EXPECT_EQ(s.find('a', 1), -1);
EXPECT_EQ(s.find('b', 1), 1);
// out of index
EXPECT_EQ(s.find('a', 10), -1);
}
TEST(StringViewTest, Compare)
{
string_view s1 = "aaa";
string_view s2 = "bbb";
string_view s3 = "aaa";
// Equals
EXPECT_EQ(s1, s3);
EXPECT_EQ(s1, s1);
// Less then
EXPECT_LT(s1, s2);
// Greater then
EXPECT_GT(s2, s1);
}
TEST(StringViewTest, MapKeyOrdering)
{
std::map<string_view, size_t> m = {{"bbb", 2}, {"aaa", 1}, {"ccc", 3}};
size_t i = 1;
for (const auto &kv : m)
{
EXPECT_EQ(kv.second, i);
i++;
}
}
|