Blender V5.0
BLI_string_utils_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_string_utils.hh"
6
7#include <string>
8
9#include "testing/testing.h"
10
11#include "BLI_vector.hh"
12
13namespace blender {
14
15TEST(BLI_string_utils, BLI_string_replace)
16{
17 {
18 std::string s = "foo bar baz";
19 BLI_string_replace(s, "bar", "hello");
20 EXPECT_EQ(s, "foo hello baz");
21 }
22
23 {
24 std::string s = "foo bar baz world bar";
25 BLI_string_replace(s, "bar", "hello");
26 EXPECT_EQ(s, "foo hello baz world hello");
27 }
28}
29
30TEST(BLI_string_utils, BLI_uniquename_cb)
31{
32 const Vector<std::string> current_names{"Foo", "Bar", "Bar.003", "Baz.001", "Big.999"};
33
34 {
35 auto unique_check_func = [&](const StringRef check_name) {
36 return current_names.contains(check_name);
37 };
38
39 {
40 char name[64] = "";
41 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
42 EXPECT_STREQ(name, "Default Name");
43 }
44
45 {
46 char name[64] = "Baz";
47 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
48 EXPECT_STREQ(name, "Baz");
49 }
50
51 {
52 char name[64] = "Foo";
53 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
54 EXPECT_STREQ(name, "Foo.001");
55 }
56
57 {
58 char name[64] = "Baz.001";
59 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
60 EXPECT_STREQ(name, "Baz.002");
61 }
62
63 {
64 char name[64] = "Bar.003";
65 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
66 EXPECT_STREQ(name, "Bar.004");
67 }
68
69 {
70 char name[64] = "Big.999";
71 BLI_uniquename_cb(unique_check_func, "Default Name", '.', name, sizeof(name));
72 EXPECT_STREQ(name, "Big.1000");
73 }
74 }
75
76 {
77 const auto unique_check = [&](const blender::StringRef name) -> bool {
78 return current_names.contains(name);
79 };
80
81 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', ""), "");
82 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Baz"), "Baz");
83 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Foo"), "Foo.001");
84 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Baz.001"), "Baz.002");
85 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Bar.003"), "Bar.004");
86 EXPECT_EQ(BLI_uniquename_cb(unique_check, '.', "Big.999"), "Big.1000");
87 }
88}
89
90} // namespace blender
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
size_t void BLI_uniquename_cb(blender::FunctionRef< bool(blender::StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
void BLI_string_replace(std::string &haystack, blender::StringRef needle, blender::StringRef other)
bool contains(const T &value) const
TEST(compression, filter_transpose_delta)
const char * name