summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/testStringAlgorithms.cxx
blob: f73e62ac9363e687ec4daef72db08ef6b4176db4 (plain)
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
/* 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 <iostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include <cm/string_view>

#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 cmTrimWhitespace
  {
    std::string base = "base";
    std::string spaces = "  \f\f\n\n\r\r\t\t\v\v";
    assert_string(cmTrimWhitespace(spaces + base), base,
                  "cmTrimWhitespace front");
    assert_string(cmTrimWhitespace(base + spaces), base,
                  "cmTrimWhitespace back");
    assert_string(cmTrimWhitespace(spaces + base + spaces), base,
                  "cmTrimWhitespace front and back");
  }

  // ----------------------------------------------------------------------
  // Test cmRemoveQuotes
  {
    auto test = [&assert_string](cm::string_view source,
                                 cm::string_view expected,
                                 cm::string_view title) {
      assert_string(cmRemoveQuotes(source), expected, title);
    };

    test("", "", "cmRemoveQuotes empty");
    test("\"", "\"", "cmRemoveQuotes single quote");
    test("\"\"", "", "cmRemoveQuotes double quote");
    test("\"a", "\"a", "cmRemoveQuotes quote char");
    test("\"ab", "\"ab", "cmRemoveQuotes quote char char");
    test("a\"", "a\"", "cmRemoveQuotes char quote");
    test("ab\"", "ab\"", "cmRemoveQuotes char char quote");
    test("a", "a", "cmRemoveQuotes single char");
    test("ab", "ab", "cmRemoveQuotes two chars");
    test("abc", "abc", "cmRemoveQuotes three chars");
    test("\"abc\"", "abc", "cmRemoveQuotes quoted chars");
    test("\"\"abc\"\"", "\"abc\"", "cmRemoveQuotes quoted quoted chars");
  }

  // ----------------------------------------------------------------------
  // Test cmEscapeQuotes
  {
    assert_string(cmEscapeQuotes("plain"), "plain", "cmEscapeQuotes plain");
    std::string base = "\"base\"\"";
    std::string result = "\\\"base\\\"\\\"";
    assert_string(cmEscapeQuotes(base), result, "cmEscapeQuotes escaped");
  }

  // ----------------------------------------------------------------------
  // 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 cmTokenize
  {
    typedef std::vector<std::string> VT;
    assert_ok(cmTokenize("", ";") == VT{ "" }, "cmTokenize empty");
    assert_ok(cmTokenize(";", ";") == VT{ "" }, "cmTokenize sep");
    assert_ok(cmTokenize("abc", ";") == VT{ "abc" }, "cmTokenize item");
    assert_ok(cmTokenize("abc;", ";") == VT{ "abc" }, "cmTokenize item sep");
    assert_ok(cmTokenize(";abc", ";") == VT{ "abc" }, "cmTokenize sep item");
    assert_ok(cmTokenize("abc;;efg", ";") == VT{ "abc", "efg" },
              "cmTokenize item sep sep item");
    assert_ok(cmTokenize("a1;a2;a3;a4", ";") == VT{ "a1", "a2", "a3", "a4" },
              "cmTokenize multiple items");
  }

  // ----------------------------------------------------------------------
  // 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");
  }
  {
    std::string val;
    std::string expect;
    val.reserve(120 * cmStrLen("cmStrCat move"));
    auto data = val.data();
    for (int i = 0; i < 100; i++) {
      val = cmStrCat(std::move(val), "cmStrCat move");
      expect += "cmStrCat move";
    }
    assert_ok((val.data() == data), "cmStrCat move");
    assert_string(val, expect, "cmStrCat move");
  }

  // ----------------------------------------------------------------------
  // 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");
  }

  // ----------------------------------------------------------------------
  // Test cmStrToLong
  {
    long value;
    assert_ok(cmStrToLong("1", &value) && value == 1,
              "cmStrToLong parses a positive decimal integer.");
    assert_ok(cmStrToLong(" 1", &value) && value == 1,
              "cmStrToLong parses a decimal integer after whitespace.");

    assert_ok(cmStrToLong("-1", &value) && value == -1,
              "cmStrToLong parses a negative decimal integer.");
    assert_ok(
      cmStrToLong(" -1", &value) && value == -1,
      "cmStrToLong parses a negative decimal integer after whitespace.");

    assert_ok(!cmStrToLong("1x", &value),
              "cmStrToLong rejects trailing content.");
  }

  // ----------------------------------------------------------------------
  // Test cmStrToULong
  {
    unsigned long value;
    assert_ok(cmStrToULong("1", &value) && value == 1,
              "cmStrToULong parses a decimal integer.");
    assert_ok(cmStrToULong(" 1", &value) && value == 1,
              "cmStrToULong parses a decimal integer after whitespace.");
    assert_ok(!cmStrToULong("-1", &value),
              "cmStrToULong rejects a negative number.");
    assert_ok(!cmStrToULong(" -1", &value),
              "cmStrToULong rejects a negative number after whitespace.");
    assert_ok(!cmStrToULong("1x", &value),
              "cmStrToULong rejects trailing content.");
  }

  // ----------------------------------------------------------------------
  // Test cmStrToLongLong
  {
    long long value;
    assert_ok(cmStrToLongLong("1", &value) && value == 1,
              "cmStrToLongLong parses a positive decimal integer.");
    assert_ok(cmStrToLongLong(" 1", &value) && value == 1,
              "cmStrToLongLong parses a decimal integer after whitespace.");

    assert_ok(cmStrToLongLong("-1", &value) && value == -1,
              "cmStrToLongLong parses a negative decimal integer.");
    assert_ok(
      cmStrToLongLong(" -1", &value) && value == -1,
      "cmStrToLongLong parses a negative decimal integer after whitespace.");

    assert_ok(!cmStrToLongLong("1x", &value),
              "cmStrToLongLong rejects trailing content.");
  }

  // ----------------------------------------------------------------------
  // Test cmStrToULongLong
  {
    unsigned long long value;
    assert_ok(cmStrToULongLong("1", &value) && value == 1,
              "cmStrToULongLong parses a decimal integer.");
    assert_ok(cmStrToULongLong(" 1", &value) && value == 1,
              "cmStrToULongLong parses a decimal integer after whitespace.");
    assert_ok(!cmStrToULongLong("-1", &value),
              "cmStrToULongLong rejects a negative number.");
    assert_ok(!cmStrToULongLong(" -1", &value),
              "cmStrToULongLong rejects a negative number after whitespace.");
    assert_ok(!cmStrToULongLong("1x", &value),
              "cmStrToULongLong rejects trailing content.");
  }

  // ----------------------------------------------------------------------
  // Test cmStrLen
  {
    constexpr auto len = cmStrLen("Hello world!");
    assert_ok(len == 12,
              "cmStrLen returns length of non-empty literal string");
    assert_ok(cmStrLen("") == 0,
              "cmStrLen returns length of empty literal string");
  }

  return failed;
}