Blender V5.3
BLI_ustring_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include <atomic>
8#include <thread>
9
10#include "BLI_array.hh"
11#include "BLI_map.hh"
12#include "BLI_task.hh"
13#include "BLI_task_c.hh"
14#include "BLI_ustring.hh"
15#include "BLI_vector.hh"
16
17namespace blender::tests {
18
19TEST(ustring, MapUString)
20{
22 map.add("hello"_ustr, 1);
23 map.add_as("world", 2);
24 map.add_as("world", 3);
25 EXPECT_EQ(map.lookup("hello"_ustr), 1);
26 EXPECT_EQ(map.lookup_as("hello"), 1);
27 EXPECT_EQ(map.lookup_as("world"), 2);
28 EXPECT_EQ(map.lookup_as("world"_ustr), 2);
29}
30
31TEST(ustring, MapStringRef)
32{
34 map.add("hello", 1);
35 map.add("world"_ustr.ref(), 2);
36 map.add_as("world"_ustr.ref(), 3);
37 EXPECT_EQ(map.lookup("hello"), 1);
38 EXPECT_EQ(map.lookup("hello"_ustr.ref()), 1);
39 EXPECT_EQ(map.lookup("world"), 2);
40 EXPECT_EQ(map.lookup("world"_ustr.ref()), 2);
41}
42
43TEST(ustring, MapStdString)
44{
46 map.add("hello", 1);
47 map.add_as("world"_ustr.string(), 2);
48 EXPECT_EQ(map.lookup("hello"), 1);
49 EXPECT_EQ(map.lookup_as("hello"_ustr.ref()), 1);
50 EXPECT_EQ(map.lookup("world"), 2);
51 EXPECT_EQ(map.lookup_as("world"_ustr.ref()), 2);
52}
53
54TEST(ustring, Equality)
55{
56 /* This is mostly just checking if all these equality checks compile fine without ambiguities. */
57 EXPECT_EQ("test"_ustr, "test"_ustr);
58 EXPECT_EQ("test"_ustr, "test");
59 EXPECT_EQ("test", "test"_ustr);
60 EXPECT_EQ("test"_ustr, StringRef("test"));
61 EXPECT_EQ(StringRef("test"), "test"_ustr);
62 EXPECT_EQ("test"_ustr, std::string("test"));
63 EXPECT_EQ(std::string("test"), "test"_ustr);
64 EXPECT_EQ("test"_ustr, StringRefNull("test"));
65 EXPECT_EQ(StringRefNull("test"), "test"_ustr);
66 EXPECT_EQ("test"_ustr, std::string_view("test"));
67 EXPECT_EQ(std::string_view("test"), "test"_ustr);
68}
69
70TEST(ustring, UniqueCorrectness)
71{
72 static const char *from_static_literal = "unique_correctness_98765";
73
74 const std::string to_chars_result = std::to_string(98765);
75 const std::string from_to_chars = std::string("unique_correctness_") + to_chars_result;
76
77 const std::string from_concatenation = std::string("unique_correctness_9876") + "5";
78
79 const UString a(from_static_literal);
80 const UString b(from_to_chars);
81 const UString c(from_concatenation);
82
83 /* All three must have found (or created, then found) the exact same table entry. */
84 EXPECT_EQ(a.c_str(), b.c_str());
85 EXPECT_EQ(a.c_str(), c.c_str());
86 EXPECT_EQ(a, b);
87 EXPECT_EQ(a, c);
88}
89
90TEST(ustring, Empty)
91{
92 const UString default_constructed;
93 const UString from_literal("");
94 const UString from_std_string{std::string()};
95 const UString from_ustr_literal = ""_ustr;
96
97 EXPECT_TRUE(default_constructed.is_empty());
98 EXPECT_TRUE(from_literal.is_empty());
99 EXPECT_EQ(from_literal.size(), 0);
100 EXPECT_STREQ(from_literal.c_str(), "");
101 EXPECT_EQ(from_literal[0], '\0');
102 EXPECT_EQ(from_literal.hash(), default_constructed.hash());
103
104 /* All the ways of getting an empty string should end up pointing at the same table entry. */
105 EXPECT_EQ(from_literal.c_str(), from_std_string.c_str());
106 EXPECT_EQ(from_literal.c_str(), from_ustr_literal.c_str());
107 EXPECT_EQ(from_literal, from_std_string);
108 EXPECT_EQ(from_literal, from_ustr_literal);
109 /* The default constructor is documented to avoid touching the shared table, so it uses its own
110 * always-empty entry. Check whether that still compares equal to an interned empty string. */
111 EXPECT_EQ(default_constructed, from_literal);
112}
113
114TEST(ustring, LongString)
115{
116 const std::string long_a(500, 'a');
117 const std::string long_b = std::string(300, 'b') + std::string(300, 'c');
118
119 const UString a(long_a);
120 const UString b(long_b);
121
122 EXPECT_EQ(a.size(), 500);
123 EXPECT_EQ(a.c_str()[a.size()], '\0');
124 EXPECT_EQ(a[0], 'a');
125 EXPECT_EQ(a[499], 'a');
126 EXPECT_EQ(a[500], '\0');
127
128 EXPECT_EQ(b.size(), 600);
129 EXPECT_EQ(b.c_str()[b.size()], '\0');
130 EXPECT_EQ(b[0], 'b');
131 EXPECT_EQ(b[299], 'b');
132 EXPECT_EQ(b[300], 'c');
133 EXPECT_EQ(b[599], 'c');
134}
135
136TEST(ustring, LiteralOperator)
137{
138 const UString from_literal_op = "literal_operator_test_string"_ustr;
139 const UString from_ctor("literal_operator_test_string");
140 const UString from_std_string(std::string("literal_operator_test_string"));
141
142 EXPECT_EQ(from_literal_op.c_str(), from_ctor.c_str());
143 EXPECT_EQ(from_literal_op.c_str(), from_std_string.c_str());
144 EXPECT_EQ(from_literal_op, from_ctor);
145
146 /* Using the literal operator on the exact same literal a second time must hit its own
147 * per-instantiation cache and still agree with the table. */
148 const UString from_literal_op_again = "literal_operator_test_string"_ustr;
149 EXPECT_EQ(from_literal_op.c_str(), from_literal_op_again.c_str());
150}
151
152TEST(ustring, Hashing)
153{
155 map.add("hashing_test_alpha"_ustr, 1);
156 map.add("hashing_test_beta"_ustr, 2);
157
158 EXPECT_EQ(map.size(), 2);
159 EXPECT_EQ(map.lookup("hashing_test_alpha"_ustr), 1);
160 EXPECT_EQ(map.lookup("hashing_test_beta"_ustr), 2);
161 EXPECT_EQ(map.lookup_as("hashing_test_alpha"), 1);
162}
163
164TEST(ustring, Concurrency)
165{
167
168 constexpr int64_t total = 20000;
169 Vector<UString> results(total);
170
171 threading::parallel_for(IndexRange(total), 1, [&](const IndexRange sub_range) {
172 for (const int64_t i : sub_range) {
173 if (i % 2 == 0) {
174 /* Independently allocated on each iteration/thread, but should all intern to the same
175 * entry: this is the actual thing being tested. */
176 const std::string matching = std::string("concurrency_test_shared_string");
177 results[i] = UString(matching);
178 }
179 else {
180 /* A string that is unique to this iteration, to make sure concurrent insertion of
181 * different strings doesn't corrupt the table or cross-contaminate entries. */
182 const std::string number_str = std::to_string(i);
183 results[i] = UString(StringRef(number_str));
184 }
185 }
186 });
187
188 const char *shared_ptr = nullptr;
189 for (const int i : IndexRange(total)) {
190 if (i % 2 == 0) {
191 if (shared_ptr == nullptr) {
192 shared_ptr = results[i].c_str();
193 }
194 else {
195 EXPECT_EQ(results[i].c_str(), shared_ptr);
196 }
197 }
198 else {
199 const std::string number_str = std::to_string(i);
200 EXPECT_EQ(results[i], StringRef(number_str));
201 }
202 }
203}
204
205TEST(ustring, ConcurrentSameNewString)
206{
207 /* Every thread interns the same list of strings, none of which exist yet, so the threads race
208 * to add the *same* new string. */
209 constexpr int THREADS_NUM = 8;
210 constexpr int STRINGS_NUM = 200;
211
212 Vector<std::string> strings;
213 for (const int i : IndexRange(STRINGS_NUM)) {
214 strings.append("concurrent_same_new_string_" + std::to_string(i));
215 }
216
217 Array<Array<UString>> results(THREADS_NUM);
218 for (const int thread_i : IndexRange(THREADS_NUM)) {
219 results[thread_i].reinitialize(STRINGS_NUM);
220 }
221
222 std::atomic<int> ready_num = 0;
223 Vector<std::thread> threads;
224 for (const int thread_i : IndexRange(THREADS_NUM)) {
225 threads.append(std::thread([&, thread_i]() {
226 /* Start interning at the same time in all threads, to maximize contention. */
227 ready_num.fetch_add(1);
228 while (ready_num.load() < THREADS_NUM) {
229 }
230 for (const int i : IndexRange(STRINGS_NUM)) {
231 results[thread_i][i] = UString(strings[i]);
232 }
233 }));
234 }
235 for (std::thread &thread : threads) {
236 thread.join();
237 }
238
239 for (const int i : IndexRange(STRINGS_NUM)) {
240 const UString &expected = results[0][i];
241 EXPECT_EQ(expected, StringRef(strings[i]));
242 /* The UString for each thread should reference the same data. */
243 for (const int thread_i : IndexRange(THREADS_NUM)) {
244 EXPECT_EQ(results[thread_i][i].c_str(), expected.c_str());
245 EXPECT_EQ(results[thread_i][i], expected);
246 }
247 }
248}
249
250} // namespace blender::tests
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr IndexRange()=default
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:312
const Value & lookup_as(const ForwardKey &key) const
Definition BLI_map.hh:553
int64_t size() const
Definition BLI_map.hh:976
constexpr StringRefNull()
constexpr StringRef()
bool is_empty() const
int64_t size() const
const char * c_str() const
uint64_t hash() const
UString()=default
void append(const T &value)
bool join()
Definition thread.cpp:48
#define EXPECT_TRUE(result)
TEST(any_virtual, DefaultConstructor)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
__int64 int64_t
Definition BKE_text.h:90
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BLI_task_scheduler_init(bool use_flush_denormals_to_zero=true)