File: Test_zstring_view.cpp

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (274 lines) | stat: -rw-r--r-- 6,634 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "Test.h"

#include "pymol/zstring_view.h"

#include <map>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>

using namespace pymol::test;

TEST_CASE("special member functions", "[zstring_view]")
{
  std::string s1 = "foobar";
  std::string s2 = s1; // same value, different data() pointer
  std::string s3 = "other value";

  // view from std::string
  pymol::zstring_view v1(s1);
  REQUIRE(v1 == s1);
  REQUIRE(s1 == v1); // swap lhs rhs
  REQUIRE(v1 == s2);
  REQUIRE(v1 != s3);
  REQUIRE(v1 == "foobar");
  REQUIRE(v1 != "abc");

  // view from view
  pymol::zstring_view v2(v1);
  REQUIRE(v2 == v1);
  REQUIRE(v2 == s1);
  REQUIRE(v2 == s2);
  REQUIRE(v2 != s3);

  // view assigment
  v1 = v2;
  REQUIRE(v1 == v2);
  REQUIRE(v1 == "foobar");

  // string assignment
  v1 = s3;
  REQUIRE(v1 != v2);
  REQUIRE(v1 != s1);
  REQUIRE(v1 != s2);
  REQUIRE(v1 == s3);

  // static string constant assingment
  v1 = "constant";
  REQUIRE(v1 == "constant");
  REQUIRE(v1 == std::string("constant"));

  // swap
  std::swap(v1, v2);
  REQUIRE(v1 == "foobar");
  REQUIRE(v2 == "constant");

  // view to string
  auto s4 = std::string(v1.c_str());
  REQUIRE(s4 == "foobar");
}

TEST_CASE("nullsafe", "[zstring_view]")
{
  std::string s3 = "other value";
  const char* c1 = nullptr;

  auto nullsafe1 = pymol::null_safe_zstring_view(c1);
  auto nullsafe2 = pymol::null_safe_zstring_view(nullsafe1);
  auto nullsafe3 = pymol::null_safe_zstring_view(s3);
  REQUIRE(nullsafe1.c_str() != nullptr);
  REQUIRE(nullsafe1.c_str() == std::string(""));
  REQUIRE(nullsafe1 == "");
  REQUIRE("" == nullsafe1); // swap lhs rhs
  REQUIRE(nullsafe1 == std::string(""));
  REQUIRE(nullsafe1 == nullsafe2);
  REQUIRE(nullsafe3 == s3);
  REQUIRE(nullsafe3 == std::string(s3).c_str());
  REQUIRE(nullsafe3 != nullsafe1);

  // assign view from null-safe
  pymol::zstring_view v1 = nullsafe1;
  v1 = nullsafe1;

  // construct view from null-safe
  auto v4 = pymol::zstring_view(nullsafe3);
  REQUIRE(v4 == s3);
}

TEST_CASE("substr", "[zstring_view]")
{
  pymol::zstring_view v1 = "foobar";
  pymol::zstring_view v2 = v1;
  v1.remove_prefix(3);
  REQUIRE(v1 == "bar");
  REQUIRE(v2.substr(3) == "bar");
  REQUIRE(v2 == "foobar");
}

TEST_CASE("size", "[zstring_view]")
{
  auto v1 = pymol::zstring_view("foo");
  REQUIRE(v1.size() == 3);
  REQUIRE(!v1.empty());
  v1 = "";
  REQUIRE(v1.size() == 0);
  REQUIRE(v1.empty());
}

TEST_CASE("sets", "[zstring_view]")
{
  std::set<pymol::zstring_view> set1;
  std::unordered_set<pymol::zstring_view> set2;

  pymol::zstring_view v1 =
      "some very long string which can't be small string optimized";
  std::string s1 = v1.c_str(); // copy
  pymol::null_safe_zstring_view nullsafe1 = s1;

  REQUIRE(v1 == s1);
  REQUIRE(v1 == nullsafe1);
  REQUIRE(s1 == nullsafe1);

  set1.insert(v1);
  set1.insert(s1);
  set1.insert(nullsafe1);

  set2.insert(v1);
  set2.insert(s1);
  set2.insert(nullsafe1);

  set1.insert("foo");
  set2.insert("foo");

  std::string s2("foo");
  set1.insert(s2);
  set2.insert(s2);

  REQUIRE(set1.size() == 2);
  REQUIRE(set2.size() == 2);

  REQUIRE(set1.count("foo") == 1);
  REQUIRE(set2.count("foo") == 1);

  REQUIRE(set1.count("bar") == 0);
  REQUIRE(set2.count("bar") == 0);

  REQUIRE(set1.count(s1) == 1);
  REQUIRE(set2.count(s1) == 1);
}

TEST_CASE("maps", "[zstring_view]")
{
  std::map<pymol::zstring_view, int> map1;
  std::unordered_map<pymol::zstring_view, int> map2;

  map1["foo"] = 12;
  map2["foo"] = 12;
  REQUIRE(map1["foo"] == 12);
  REQUIRE(map2["foo"] == 12);
  REQUIRE(map1.size() == 1);
  REQUIRE(map2.size() == 1);

  auto s1 = std::string("foo");

  REQUIRE(map1[s1] == 12);
  REQUIRE(map2[s1] == 12);
  map1[s1] = 34;
  map2[s1] = 34;
  REQUIRE(map1.size() == 1);
  REQUIRE(map2.size() == 1);
  REQUIRE(map1["foo"] == 34);
  REQUIRE(map2["foo"] == 34);

  auto s2 = std::string("bar");

  REQUIRE(map1[s2] == 0);
  REQUIRE(map2[s2] == 0);
  map1[s2] = 56;
  map2[s2] = 56;
  REQUIRE(map1.size() == 2);
  REQUIRE(map2.size() == 2);
  REQUIRE(map1[s2] == 56);
  REQUIRE(map2[s2] == 56);
}

TEST_CASE("ostream", "[zstring_view]")
{
  pymol::zstring_view v1("foobar");
  std::ostringstream out;
  out << v1;
  REQUIRE(out.str() == "foobar");
}

TEST_CASE("copy", "[zstring_view]")
{
  pymol::zstring_view v1 = "foobar";
  char buffer[16];
  buffer[v1.copy(buffer, sizeof(buffer) - 1)] = 0;
  REQUIRE(v1 == buffer);
  buffer[v1.copy(buffer, 3)] = 0;
  REQUIRE(std::string(v1.c_str(), 3) == buffer);
  buffer[v1.copy(buffer, sizeof(buffer) - 1, 3)] = 0;
  REQUIRE(std::string(v1.c_str() + 3) == buffer);
}

TEST_CASE("starts or ends with", "[zstring_view]")
{
  pymol::zstring_view v1 = "foobar";
  REQUIRE(v1.starts_with("foo"));
  REQUIRE(!v1.starts_with("bar"));
  REQUIRE(!v1.ends_with("foo"));
  REQUIRE(v1.ends_with("bar"));
}

TEST_CASE("find", "[zstring_view]")
{
  pymol::zstring_view v1 = "foobar";
  REQUIRE(v1.find('a') == 4);
  REQUIRE(v1.find("ob") == 2);
  REQUIRE(v1.find("obo") == pymol::zstring_view::npos);
  REQUIRE(v1.find_first_of('b') == 3);
  REQUIRE(v1.find_first_of("abc") == 3);
  REQUIRE(v1.find_first_not_of("abc") == 0);
  REQUIRE(v1.find_first_not_of("abfor") == pymol::zstring_view::npos);
}

TEST_CASE("explicit bool cast", "[zstring_view]")
{
  // default constructed is false
  pymol::zstring_view v1;
  REQUIRE(!v1);
  // nullsafe always true
  pymol::null_safe_zstring_view v2;
  REQUIRE(v2);

  // empty string is true
  v1 = "";
  REQUIRE(v1);
  v2 = "";
  REQUIRE(v2);

  // null assignment is legal
  v1 = nullptr;
  REQUIRE(!v1);
  v2 = nullptr;
  REQUIRE(v2);

  v1 = "foo";
  REQUIRE(v1);
  v2 = "foo";
  REQUIRE(v2);
}

TEST_CASE("implicit cast", "[zstring_view]")
{
  static_assert(std::is_convertible<const char*, pymol::zstring_view>::value,
      "from const char*");
  static_assert(!std::is_convertible<pymol::zstring_view, const char*>::value,
      "to const char*");
  static_assert(std::is_convertible<pymol::null_safe_zstring_view,
                    pymol::zstring_view>::value,
      "from null_safe_zstring_view");
  static_assert(std::is_convertible<pymol::zstring_view,
                    pymol::null_safe_zstring_view>::value,
      "to null_safe_zstring_view");
  static_assert(std::is_convertible<std::string, pymol::zstring_view>::value,
      "from std::string");
  static_assert(!std::is_convertible<pymol::zstring_view, std::string>::value,
      "to std::string");
  static_assert(
      !std::is_convertible<bool, pymol::zstring_view>::value, "from bool");
  static_assert(
      !std::is_convertible<pymol::zstring_view, bool>::value, "to bool");
}