File: string_test.cpp

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (327 lines) | stat: -rw-r--r-- 10,908 bytes parent folder | download | duplicates (3)
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
/* Copyright 2016-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0. */

#include "watchman.h"
#include "watchman_string.h"
#include <string>
#include "thirdparty/tap.h"

void test_integrals() {
  ok(w_string::build(int8_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(int16_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(int32_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(int64_t(1)) == w_string("1"), "made 1");

  ok(w_string::build(int8_t(-1)) == w_string("-1"), "made -1");
  ok(w_string::build(int16_t(-1)) == w_string("-1"), "made -1");
  ok(w_string::build(int32_t(-1)) == w_string("-1"), "made -1");
  ok(w_string::build(int64_t(-1)) == w_string("-1"), "made -1");

  ok(w_string::build(uint8_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(uint16_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(uint32_t(1)) == w_string("1"), "made 1");
  ok(w_string::build(uint64_t(1)) == w_string("1"), "made 1");

  ok(w_string::build(uint8_t(255)) == w_string("255"), "made 255");
  ok(w_string::build(uint16_t(255)) == w_string("255"), "made 255");
  ok(w_string::build(uint32_t(255)) == w_string("255"), "made 255");
  ok(w_string::build(uint64_t(255)) == w_string("255"), "made 255");

  ok(w_string::build(int8_t(-127)) == w_string("-127"), "made -127");

  ok(w_string::build(bool(true)) == w_string("1"), "true -> 1");
  ok(w_string::build(bool(false)) == w_string("0"), "false -> 0");
}

void test_strings() {
  {
    auto hello = w_string::build("hello");
    ok(hello == w_string("hello"), "hello");
    ok(hello.size() == 5, "there are 5 chars in hello");
    ok(!strcmp("hello", hello.c_str()),
       "looks nul terminated `%s` %" PRIu32,
       hello.c_str(),
       strlen_uint32(hello.c_str()));
  }

  {
    w_string_piece piece("hello");
    ok(piece.size() == 5, "piece has 5 char size");
    auto hello = w_string::build(piece);
    ok(hello.size() == 5, "hello has 5 char size");
    ok(!strcmp("hello", hello.c_str()), "looks nul terminated");
  }

  {
    char foo[] = "foo";
    auto str = w_string::build(foo);
    ok(str.size() == 3, "foo has 3 char size");
    ok(!str.empty(), "foo is not empty");
    ok(!strcmp("foo", foo), "foo matches");
  }

  {
    w_string defaultStr;
    ok(defaultStr.empty(), "default constructed string should be empty");

    w_string nullStr(nullptr);
    ok(nullStr.empty(), "nullptr string should be empty");

    ok(w_string_piece().empty(),
       "default constructed string piece shouldbe empty");

    ok(w_string_piece(nullptr).empty(),
       "nullptr string piece shouldbe empty");

    ok(w_string::build("").empty(), "empty string is empty");
  }
}

void test_pointers() {
  bool foo = true;
  char lowerBuf[20];

  auto str = w_string::build(&foo);
  snprintf(
      lowerBuf, sizeof(lowerBuf), "0x%" PRIx64, (uint64_t)(uintptr_t)(&foo));
  ok(str.size() == strlen_uint32(lowerBuf),
     "reasonable seeming bool pointer len, got %" PRIu32
     " vs expected %" PRIu32,
     str.size(),
     strlen_uint32(lowerBuf));
  ok(str.size() == strlen_uint32(str.c_str()),
     "string is really nul terminated, size %" PRIu32
     " strlen of c_str %" PRIu32,
     str.size(),
     strlen_uint32(str.c_str()));
  ok(!strcmp(lowerBuf, str.c_str()),
     "bool pointer rendered right hex value sprintf->%s, str->%s",
     lowerBuf,
     str.c_str());

  str = w_string::build(nullptr);
  ok(str.size() > 0, "nullptr has reasonable size: %" PRIsize_t, str.size());
  ok(str == w_string("0x0"), "nullptr looks right %s", str.c_str());

  void* zero = 0;
  ok(w_string::build(zero) == "0x0", "zero pointer looks right");
}

void test_double() {
  auto str = w_string::build(5.5);
  char buf[16];
  snprintf(buf, sizeof(buf), "%f", 5.5);
  ok(str.size() == 8, "size is %" PRIsize_t, str.size());
  ok(!strcmp(str.c_str(), buf), "str.c_str=%s, buf=%s", str.c_str(), buf);
  ok(str == w_string("5.500000"), "double looks good '%s'", str.c_str());
}

void test_concat() {
  auto str = w_string::build("one", 2, "three", 1.2, false, w_string(nullptr));
  ok(str == w_string("one2three1.2000000"), "concatenated to %s", str.c_str());
}

void test_suffix() {
  ok(!w_string("").suffix(), "empty string suffix");
  ok(w_string(".").suffix() == w_string(""), "only one dot suffix");
  ok(w_string("endwithdot.").suffix() == w_string(""), "end with dot");
  ok(!w_string("nosuffix").suffix(), "no suffix");
  ok(w_string(".beginwithdot").suffix() == w_string("beginwithdot"),
     "begin with dot");
  ok(w_string("MainActivity.java").suffix() == w_string("java"), "java suffix");

  std::string longName(128, 'a');
  auto str = w_string::build(".", longName.c_str());
  ok(!str.suffix(), "too long suffix");

  std::string nearlongName(127, 'a');
  str = w_string::build("I am not long enough.", nearlongName.c_str());
  ok(str.suffix().size() == 127, "nearly too long suffix");

  // 255 is the longest suffix among some systems
  std::string toolongName(255, 'a');
  str = w_string::build(".", toolongName.c_str());
  ok(!str.suffix(), "too long suffix");
}

void test_to() {
  auto str = watchman::to<std::string>("foo", 123);
  ok(str == "foo123", "concatenated to foo123: %s", str.c_str());
  ok(str.size() == 6, "got size %d", int(str.size()));
}

void test_path_cat() {
  auto str = w_string::pathCat({"foo", ""});
  ok(str == "foo", "concat yields %s", str.c_str());

  str = w_string::pathCat({"", "foo"});
  ok(str == "foo", "concat yields %s", str.c_str());

  str = w_string::pathCat({"foo", "bar"});
  ok(str == "foo/bar", "concat yields %s", str.c_str());

  str = w_string::pathCat({"foo", "bar", ""});
  ok(str == "foo/bar", "concat yields %s", str.c_str());

  str = w_string::pathCat({"foo", "", "bar"});
  ok(str == "foo/bar", "concat yields %s", str.c_str());
}

void test_basename_dirname() {
  auto str = w_string_piece("foo/bar").baseName().asWString();
  ok(str == "bar", "basename of foo/bar is bar: %s", str.c_str());

  str = w_string_piece("foo/bar").dirName().asWString();
  ok(str == "foo", "dirname of foo/bar is foo: %s", str.c_str());

  str = w_string_piece("").baseName().asWString();
  ok(str == "", "basename of empty string is empty: %s", str.c_str());

  str = w_string_piece("").dirName().asWString();
  ok(str == "", "dirname of empty string is empty: %s", str.c_str());

  str = w_string_piece("foo").dirName().asWString();
  ok(str == "", "dirname of foo is nothing: %s", str.c_str());

  str = w_string("f/b/z");
  auto piece = str.piece().dirName();
  auto str2 = piece.baseName().asWString();
  ok(str2 == "b", "basename of dirname of f/b/z is b: %s", str.c_str());

  str = w_string_piece("foo/bar/baz").dirName().dirName().asWString();
  ok(str == "foo", "dirname of dirname of foo/bar/baz is foo: %s", str.c_str());

  str = w_string_piece("foo").baseName().asWString();
  ok(str == "foo", "basename of foo is foo: %s", str.c_str());

  str = w_string_piece("foo\\bar").baseName().asWString();
#ifdef _WIN32
  ok(str == "bar", "basename of foo\\bar is bar: %s", str.c_str());
#else
  ok(str == "foo\\bar", "basename of foo\\bar is foo\\bar: %s", str.c_str());
#endif

  str = w_string_piece("foo\\bar").dirName().asWString();
#ifdef _WIN32
  ok(str == "foo", "dirname of foo\\bar is foo: %s", str.c_str());
#else
  ok(str == "", "dirname of foo\\bar is nothing: %s", str.c_str());
#endif

#ifdef _WIN32
  w_string_piece winFoo("C:\\foo");

  str = winFoo.baseName().asWString();
  ok(str == "foo", "basename of winfoo is %s", str.c_str());

  str = winFoo.dirName().asWString();
  ok(str == "C:\\", "dirname of winfoo is %s", str.c_str());

  str = winFoo.dirName().dirName().asWString();
  ok(str == "C:\\", "dirname of dirname winfoo is %s", str.c_str());
#endif

  // This is testing that we don't walk off the end of the string.
  // We had a bug where if the buffer had a slash as the character
  // after the end of the string, baseName and dirName could incorrectly
  // match that position and trigger a string range check.
  // The endSlash string below has 7 characters, with the 8th byte
  // as a slash to trigger this condition.
  w_string_piece endSlash("dir/foo/", 7);
  str = endSlash.baseName().asWString();
  ok(str == "foo", "basename is %s", str.c_str());
  str = endSlash.dirName().asWString();
  ok(str == "dir", "dirname is %s", str.c_str());
}

void test_operator() {
  ok(w_string_piece("a") < w_string_piece("b"), "a < b");
  ok(w_string_piece("a") < w_string_piece("ba"), "a < ba");
  ok(w_string_piece("aa") < w_string_piece("b"), "aa < b");
  ok(!(w_string_piece("b") < w_string_piece("a")), "b not < a");
  ok(!(w_string_piece("a") < w_string_piece("a")), "a not < a");
  ok(w_string_piece("A") < w_string_piece("a"), "A < a");
}

void test_split() {
  {
    std::vector<std::string> expected{"a", "b", "c"};
    std::vector<std::string> result;
    w_string_piece("a:b:c").split(result, ':');

    ok(expected == result, "split ok");
  }

  {
    std::vector<w_string> expected{"a", "b", "c"};
    std::vector<w_string> result;
    w_string_piece("a:b:c").split(result, ':');

    ok(expected == result, "split ok (w_string)");
  }

  {
    std::vector<std::string> expected{"a", "b", "c"};
    std::vector<std::string> result;
    w_string_piece("a:b:c:").split(result, ':');

    ok(expected == result, "split doesn't create empty last element");
  }

  {
    std::vector<std::string> expected{"a", "b", "", "c"};
    std::vector<std::string> result;
    w_string_piece("a:b::c:").split(result, ':');

    ok(expected == result, "split does create empty element");
  }

  {
    std::vector<std::string> result;
    w_string_piece().split(result, ':');
    ok(result.size() == 0, "split as 0 elements, got %d", int(result.size()));

    w_string_piece(w_string()).split(result, ':');
    ok(result.size() == 0, "split as 0 elements, got %d", int(result.size()));

    w_string_piece(w_string(nullptr)).split(result, ':');
    ok(result.size() == 0, "split as 0 elements, got %d", int(result.size()));
  }
}

void test_path_equal() {
  ok(w_string_piece("/foo/bar").pathIsEqual("/foo/bar"), "/foo/bar");
  ok(!w_string_piece("/foo/bar").pathIsEqual("/Foo/bar"), "/foo/bar");
#ifdef _WIN32
  ok(w_string_piece("c:/foo/bar").pathIsEqual("C:/foo/bar"),
     "allow different case for drive letter only c:/foo/bar");
  ok(w_string_piece("c:/foo\\bar").pathIsEqual("C:/foo/bar"),
     "allow different slashes c:/foo\\bar");
  ok(!w_string_piece("c:/Foo/bar").pathIsEqual("C:/foo/bar"),
     "strict case in the other positions c:/Foo/bar");
#endif
}

int main(int, char**) {
  plan_tests(
      86
#ifdef _WIN32
      // extra basename tests
      + 6
#endif
      );
  test_integrals();
  test_strings();
  test_pointers();
  test_double();
  test_concat();
  test_suffix();
  test_to();
  test_path_cat();
  test_basename_dirname();
  test_operator();
  test_split();
  test_path_equal();

  return exit_status();
}