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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include <cmConfigure.h> // IWYU pragma: keep
#include "cm_string_view.hxx"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "cmStringAlgorithms.h"
int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
{
int failed = 0;
auto assert_ok = [&failed](bool test, cm::string_view title) {
if (test) {
std::cout << "Passed: " << title << "\n";
} else {
std::cout << "Failed: " << title << "\n";
++failed;
}
};
auto assert_string = [&failed](cm::string_view generated,
cm::string_view expected,
cm::string_view title) {
if (generated == expected) {
std::cout << "Passed: " << title << "\n";
} else {
std::cout << "Failed: " << title << "\n";
std::cout << "Expected: " << expected << "\n";
std::cout << "Got: " << generated << "\n";
++failed;
}
};
// ----------------------------------------------------------------------
// Test cmJoin
{
typedef std::string ST;
typedef std::vector<std::string> VT;
assert_string(cmJoin(ST("abc"), ";"), "a;b;c", "cmJoin std::string");
assert_string(cmJoin(VT{}, ";"), "", "cmJoin std::vector empty");
assert_string(cmJoin(VT{ "a" }, ";"), "a", "cmJoin std::vector single");
assert_string(cmJoin(VT{ "a", "b", "c" }, ";"), "a;b;c",
"cmJoin std::vector multiple");
assert_string(cmJoin(VT{ "a", "b", "c" }, "<=>"), "a<=>b<=>c",
"cmJoin std::vector long sep");
}
// ----------------------------------------------------------------------
// Test cmStrCat
{
int ni = -1100;
unsigned int nui = 1100u;
long int nli = -12000l;
unsigned long int nuli = 12000ul;
long long int nlli = -130000ll;
unsigned long long int nulli = 130000ull;
std::string val =
cmStrCat("<test>", ni, ',', nui, ',', nli, ",", nuli, ", ", nlli,
std::string(", "), nulli, cm::string_view("</test>"));
std::string expect =
"<test>-1100,1100,-12000,12000, -130000, 130000</test>";
assert_string(val, expect, "cmStrCat strings and integers");
}
{
float const val = 1.5f;
float const div = 0.00001f;
float f = 0.0f;
std::istringstream(cmStrCat("", val)) >> f;
f -= val;
assert_ok((f < div) && (f > -div), "cmStrCat float");
}
{
double const val = 1.5;
double const div = 0.00001;
double d = 0.0;
std::istringstream(cmStrCat("", val)) >> d;
d -= val;
assert_ok((d < div) && (d > -div), "cmStrCat double");
}
// ----------------------------------------------------------------------
// Test cmWrap
{
typedef std::vector<std::string> VT;
assert_string(cmWrap("<", VT{}, ">", "; "), //
"", //
"cmWrap empty, string prefix and suffix");
assert_string(cmWrap("<", VT{ "abc" }, ">", "; "), //
"<abc>", //
"cmWrap single, string prefix and suffix");
assert_string(cmWrap("<", VT{ "a1", "a2", "a3" }, ">", "; "), //
"<a1>; <a2>; <a3>", //
"cmWrap multiple, string prefix and suffix");
assert_string(cmWrap('<', VT{}, '>', "; "), //
"", //
"cmWrap empty, char prefix and suffix");
assert_string(cmWrap('<', VT{ "abc" }, '>', "; "), //
"<abc>", //
"cmWrap single, char prefix and suffix");
assert_string(cmWrap('<', VT{ "a1", "a2", "a3" }, '>', "; "), //
"<a1>; <a2>; <a3>", //
"cmWrap multiple, char prefix and suffix");
}
// ----------------------------------------------------------------------
// Test cmHas(Literal)Prefix and cmHas(Literal)Suffix
{
std::string str("abc");
assert_ok(cmHasPrefix(str, 'a'), "cmHasPrefix char");
assert_ok(!cmHasPrefix(str, 'c'), "cmHasPrefix char not");
assert_ok(cmHasPrefix(str, "ab"), "cmHasPrefix string");
assert_ok(!cmHasPrefix(str, "bc"), "cmHasPrefix string not");
assert_ok(cmHasPrefix(str, str), "cmHasPrefix complete string");
assert_ok(cmHasLiteralPrefix(str, "ab"), "cmHasLiteralPrefix string");
assert_ok(!cmHasLiteralPrefix(str, "bc"), "cmHasLiteralPrefix string not");
assert_ok(cmHasSuffix(str, 'c'), "cmHasSuffix char");
assert_ok(!cmHasSuffix(str, 'a'), "cmHasSuffix char not");
assert_ok(cmHasSuffix(str, "bc"), "cmHasSuffix string");
assert_ok(!cmHasSuffix(str, "ab"), "cmHasSuffix string not");
assert_ok(cmHasSuffix(str, str), "cmHasSuffix complete string");
assert_ok(cmHasLiteralSuffix(str, "bc"), "cmHasLiteralSuffix string");
assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not");
}
return failed;
}
|