forked from AcademySoftwareFoundation/OpenImageIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathustring_test.cpp
More file actions
353 lines (285 loc) · 11.3 KB
/
ustring_test.cpp
File metadata and controls
353 lines (285 loc) · 11.3 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include <cstdio>
#include <functional>
#include <iostream>
#include <OpenImageIO/argparse.h>
#include <OpenImageIO/benchmark.h>
#include <OpenImageIO/parallel.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/thread.h>
#include <OpenImageIO/timer.h>
#include <OpenImageIO/unittest.h>
#include <OpenImageIO/ustring.h>
#include <OpenImageIO/detail/fmt/std.h>
using namespace OIIO;
// Test ustring's internal locks by creating a bunch of strings in many
// threads simultaneously. Hopefully something will crash if the
// internal table is not being locked properly.
static int iterations = 1000000;
static int numthreads = 16;
static int ntrials = 1;
static bool verbose = false;
static bool wedge = false;
static int collide = 1; // Millions
static std::vector<std::array<char, 16>> strings;
static void
getargs(int argc, char* argv[])
{
// clang-format off
ArgParse ap;
ap.intro("ustring_test\n" OIIO_INTRO_STRING)
.usage("ustring_test [options]");
ap.arg("-v", &verbose)
.help("Verbose mode");
ap.arg("--threads %d", &numthreads)
.help(Strutil::fmt::format("Number of threads (default: {})", numthreads));
ap.arg("--iters %d", &iterations)
.help(Strutil::fmt::format("Number of iterations (default: {})", iterations));
ap.arg("--trials %d", &ntrials)
.help("Number of trials");
ap.arg("--wedge", &wedge)
.help("Do a wedge test");
ap.arg("--collide %d", &collide)
.help("Strings (x 1M) to create to make hash collisions");
// clang-format on
ap.parse(argc, (const char**)argv);
const int nhw_threads = Sysutil::hardware_concurrency();
std::cout << "hw threads = " << nhw_threads << "\n";
// user wants to max out the number of threads
if (numthreads <= 0)
numthreads = nhw_threads;
}
void
test_ustring()
{
ustring foo("foo"), bar("bar"), empty(""), uninit;
ustring foobarbaz("foobarbaz");
OIIO_CHECK_ASSERT(std::is_default_constructible<OIIO::ustring>());
OIIO_CHECK_ASSERT(std::is_trivially_copyable<OIIO::ustring>());
OIIO_CHECK_ASSERT(std::is_trivially_destructible<OIIO::ustring>());
OIIO_CHECK_ASSERT(std::is_trivially_move_constructible<OIIO::ustring>());
OIIO_CHECK_ASSERT(std::is_trivially_copy_constructible<OIIO::ustring>());
OIIO_CHECK_ASSERT(std::is_trivially_move_assignable<OIIO::ustring>());
// Size of a ustring is just a pointer
OIIO_CHECK_EQUAL(sizeof(ustring), sizeof(const char*));
// Test constructors
OIIO_CHECK_ASSERT(uninit.c_str() == nullptr);
OIIO_CHECK_EQUAL(foo, ustring(string_view("foo")));
OIIO_CHECK_EQUAL(foo, ustring(std::string("foo")));
OIIO_CHECK_EQUAL(ustring("hoobarfoo123", 6, 3), ustring("foo"));
OIIO_CHECK_EQUAL(ustring("hoobarfoo123", 3), ustring("hoo"));
OIIO_CHECK_EQUAL(ustring(3, 'x'), ustring("xxx"));
OIIO_CHECK_EQUAL(ustring(foo), foo);
OIIO_CHECK_EQUAL(ustring(foo, 2, 1), ustring("o"));
// Conversion to char*, string_view, string
OIIO_CHECK_ASSERT(!strcmp(foo.c_str(), "foo"));
OIIO_CHECK_EQUAL(foo.string(), "foo");
OIIO_CHECK_EQUAL(string_view(foo), "foo");
OIIO_CHECK_EQUAL(std::string(foo), "foo");
// assignment, clear
ustring foo2;
foo2 = foo;
OIIO_CHECK_EQUAL(foo2, foo);
foo2.clear();
OIIO_CHECK_EQUAL(foo2, uninit);
// length/size, empty
OIIO_CHECK_EQUAL(foo.length(), 3);
OIIO_CHECK_EQUAL(foo.size(), 3);
OIIO_CHECK_EQUAL(empty.size(), 0);
OIIO_CHECK_EQUAL(uninit.size(), 0);
OIIO_CHECK_ASSERT(empty.empty());
OIIO_CHECK_ASSERT(uninit.empty());
OIIO_CHECK_ASSERT(!foo.empty());
// Characters
OIIO_CHECK_EQUAL(foo[0], 'f');
OIIO_CHECK_EQUAL(bar[1], 'a');
// copy
char buf[10];
foo.copy(buf, sizeof(buf) - 1);
OIIO_CHECK_ASSERT(!strcmp(buf, "foo"));
ustring("foobarbaz").copy(buf, 4, 3);
OIIO_CHECK_ASSERT(!strcmp(buf, "barb"));
// substr
OIIO_CHECK_ASSERT(foobarbaz.substr(3, 4) == ustring("barb"));
// find
OIIO_CHECK_ASSERT(foobarbaz.find("ba") == 3);
OIIO_CHECK_ASSERT(foobarbaz.find("ba", 4) == 6);
OIIO_CHECK_ASSERT(foobarbaz.rfind("ba") == 6);
OIIO_CHECK_ASSERT(foobarbaz.rfind("ba") == 6);
// FIXME: there are lots of find_* permutations to test, but I'm lazy
// concat
OIIO_CHECK_EQUAL(ustring::concat(foo, bar), "foobar");
OIIO_CHECK_EQUAL(ustring::concat(foo, "bar"), "foobar");
OIIO_CHECK_EQUAL(ustring::concat(foo, ""), "foo");
OIIO_CHECK_EQUAL(ustring::concat("", foo), "foo");
ustring longstring(Strutil::repeat("01234567890", 100));
OIIO_CHECK_EQUAL(ustring::concat(longstring, longstring),
ustring::fmtformat("{}{}", longstring, longstring));
// from_hash
OIIO_CHECK_EQUAL(ustring::from_hash(foo.hash()), foo);
OIIO_CHECK_EQUAL(empty.hash(), 0);
OIIO_CHECK_EQUAL(ustring().hash(), 0);
// make_unique, is_unique, from_unique
const char* foostr = foo.c_str();
OIIO_CHECK_EQUAL(ustring::make_unique("foo"), foostr);
OIIO_CHECK_EQUAL(ustring::make_unique(string_view()), ustring());
OIIO_CHECK_EQUAL(ustring::is_unique(foostr), true);
OIIO_CHECK_EQUAL(ustring::is_unique("foo"), false);
OIIO_CHECK_EQUAL(ustring::from_unique(foostr), foo);
// std::hash
OIIO_CHECK_EQUAL(std::hash<ustring> {}(foo), foo.hash());
// string literals
auto whichtype = "foo"_us;
OIIO_CHECK_EQUAL(whichtype, ustring("foo"));
OIIO_CHECK_ASSERT((std::is_same<decltype(whichtype), ustring>::value));
OIIO_CHECK_ASSERT(!(std::is_same<decltype(whichtype), const char*>::value));
// Test some edge cases for fmt formatting
Strutil::print("Test print empty ustring: '{}'\n", empty);
Strutil::print("Test print default initialized ustring: '{}'\n", uninit);
OIIO_CHECK_EQUAL(Strutil::format("{}", empty),
Strutil::format("{}", uninit));
}
void
test_ustringhash()
{
// Two ustrings
ustring foo("foo"), bar("bar");
OIIO_CHECK_ASSERT(std::is_default_constructible<OIIO::ustringhash>());
OIIO_CHECK_ASSERT(std::is_trivially_copyable<OIIO::ustringhash>());
OIIO_CHECK_ASSERT(std::is_trivially_destructible<OIIO::ustringhash>());
OIIO_CHECK_ASSERT(
std::is_trivially_move_constructible<OIIO::ustringhash>());
OIIO_CHECK_ASSERT(
std::is_trivially_copy_constructible<OIIO::ustringhash>());
OIIO_CHECK_ASSERT(std::is_trivially_move_assignable<OIIO::ustringhash>());
OIIO_CHECK_EQUAL(sizeof(ustringhash), sizeof(size_t));
// Make two ustringhash's from strings
ustringhash hfoo("foo"), hbar("bar");
OIIO_CHECK_EQUAL(hfoo.hash(), foo.hash());
OIIO_CHECK_EQUAL(hbar.hash(), bar.hash());
OIIO_CHECK_NE(hfoo.hash(), hbar.hash());
// Check copy construction, assignment, ==, !=
ustringhash hfoo_copy(hfoo);
OIIO_CHECK_EQUAL(hfoo_copy, hfoo);
OIIO_CHECK_NE(hfoo, hbar);
ustringhash hfoo_copy2;
hfoo_copy2 = hfoo;
OIIO_CHECK_EQUAL(hfoo_copy2, hfoo);
// Assignment from a ustring
ustringhash hfoo_from_foo = foo;
OIIO_CHECK_EQUAL(hfoo_from_foo, hfoo);
// Ask a ustring for its ustringhash
OIIO_CHECK_EQUAL(hfoo, foo.uhash());
// ustring constructed from a ustringhash
OIIO_CHECK_EQUAL(hfoo_from_foo, foo);
// string_view and string from ustringhash
string_view foo_sv = hfoo;
OIIO_CHECK_EQUAL(foo_sv, "foo");
OIIO_CHECK_EQUAL(std::string(foo_sv), "foo");
// clear and empty()
OIIO_CHECK_ASSERT(!hfoo_copy2.empty());
hfoo_copy2.clear();
OIIO_CHECK_ASSERT(hfoo_copy2.empty());
// Can we get to the characters?
OIIO_CHECK_EQUAL(hfoo.c_str(), foo.c_str());
OIIO_CHECK_EQUAL(hfoo.length(), foo.length());
OIIO_CHECK_EQUAL(hfoo.size(), foo.size());
// Check ==, != with strings, and with ustring's
OIIO_CHECK_EQUAL(hfoo, "foo");
OIIO_CHECK_NE(hbar, "foo");
// OIIO_CHECK_EQUAL(hfoo, std::string("foo"));
// OIIO_CHECK_NE(hbar, std::string("foo"));
OIIO_CHECK_EQUAL(hfoo, foo);
OIIO_CHECK_NE(hbar, foo);
// Conversion to string
OIIO_CHECK_EQUAL(Strutil::to_string(hfoo), "foo");
// from_hash
OIIO_CHECK_EQUAL(ustringhash::from_hash(hfoo.hash()), hfoo);
OIIO_CHECK_EQUAL(ustringhash("").hash(), 0);
OIIO_CHECK_EQUAL(ustringhash().hash(), 0);
// std::hash
OIIO_CHECK_EQUAL(std::hash<ustringhash> {}(hfoo), hfoo.hash());
// formatting string
OIIO_CHECK_EQUAL(Strutil::fmt::format("{}", hfoo), "foo");
// string literals
auto whichtype = "foo"_ush;
OIIO_CHECK_EQUAL(whichtype, ustringhash("foo"));
OIIO_CHECK_ASSERT((std::is_same<decltype(whichtype), ustringhash>::value));
OIIO_CHECK_ASSERT(!(std::is_same<decltype(whichtype), const char*>::value));
}
static void
create_lotso_ustrings(int iterations)
{
OIIO_DASSERT(size_t(iterations) <= strings.size());
if (verbose)
Strutil::print("thread {}\n", std::this_thread::get_id());
size_t h = 0;
for (int i = 0; i < iterations; ++i) {
ustring s(strings[i].data());
h += s.hash();
}
if (verbose)
Strutil::printf("checksum %08x\n", unsigned(h));
}
void
benchmark_threaded_ustring_creation()
{
// prepare the strings we will turn into ustrings to avoid
// including snprintf in the benchmark
strings.resize(wedge ? iterations : iterations / numthreads);
int i = 0;
for (auto& s : strings)
snprintf(s.data(), s.size(), "%d", i++);
if (wedge) {
timed_thread_wedge(create_lotso_ustrings, numthreads, iterations,
ntrials);
} else {
timed_thread_wedge(create_lotso_ustrings, numthreads, iterations,
ntrials,
numthreads /* just this one thread count */);
}
OIIO_CHECK_ASSERT(true); // If we make it here without crashing, pass
}
void
verify_no_collisions()
{
// Try to force a hash collision
parallel_for(int64_t(0), int64_t(1000000LL * int64_t(collide)),
[](int64_t i) { (void)ustring::fmtformat("{:x}", i); });
std::vector<ustring> collisions;
size_t ncollisions = ustring::hash_collisions(&collisions);
OIIO_CHECK_ASSERT(ncollisions == 0);
if (ncollisions) {
Strutil::print(" Hash collisions: {}\n", ncollisions);
for (auto c : collisions)
Strutil::print(" \"{}\" (orig {:08x} rehashed {:08x})\n", c,
Strutil::strhash(c), c.hash());
}
}
int
main(int argc, char* argv[])
{
#if !defined(NDEBUG) || defined(OIIO_CI) || defined(OIIO_CODE_COVERAGE)
// For the sake of test time, reduce the default number of benchmark
// trials for DEBUG, CI, and code coverage builds. Explicit use of
// --trials or --iterations will override this, since it comes before the
// getargs() call.
ntrials = 1;
#endif
#if !defined(NDEBUG)
// For debug+CI combination runs, reduce to truly one iteration.
if (Strutil::stoi(Sysutil::getenv("OpenImageIO_CI")) != 0)
iterations = 1;
#endif
getargs(argc, argv);
test_ustring();
test_ustringhash();
verify_no_collisions();
benchmark_threaded_ustring_creation();
verify_no_collisions();
std::cout << "\n" << ustring::getstats(true) << "\n";
return unit_test_failures;
}