Welcome to mirror list, hosted at ThFree Co, Russian Federation.

string_span_tests.cpp « tests - github.com/microsoft/GSL.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 08faaa85e149e5b98b2bd60e1f29bb398c7c6d76 (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/////////////////////////////////////////////////////////////////////////////// 
// 
// Copyright (c) 2015 Microsoft Corporation. All rights reserved. 
// 
// This code is licensed under the MIT License (MIT). 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
// THE SOFTWARE. 
// 
///////////////////////////////////////////////////////////////////////////////

#include <UnitTest++/UnitTest++.h> 
#include <string_span.h>
#include <vector>
#include <cstdlib>
#include <map>
#include <unordered_map>

using namespace std;
using namespace gsl;


SUITE(string_span_tests)
{

    TEST(TestLiteralConstruction)
    {
        cwstring_span<> v = ensure_z(L"Hello");

        CHECK(5 == v.length());

#ifdef CONFIRM_COMPILATION_ERRORS
        wstring_span<> v2 = ensure0(L"Hello");
#endif
    }

    TEST(TestConstructFromStdString)
    {
        std::string s = "Hello there world";
        cstring_span<> v = s;
        CHECK(v.length() == static_cast<cstring_span<>::size_type>(s.length()));
    }

    TEST(TestConstructFromStdVector)
    {
        std::vector<char> vec(5, 'h');
        string_span<> v = vec;
        CHECK(v.length() == static_cast<string_span<>::size_type>(vec.size()));
    }

    TEST(TestStackArrayConstruction)
    {
        wchar_t stack_string[] = L"Hello";

        {
            cwstring_span<> v = ensure_z(stack_string);
            CHECK(v.length() == 5);
        }

        {
            cwstring_span<> v = stack_string;
            CHECK(v.length() == 5);
        }

        {
            wstring_span<> v = ensure_z(stack_string);
            CHECK(v.length() == 5);
        }

        {
            wstring_span<> v = stack_string;
            CHECK(v.length() == 5);
        }
	}

    TEST(TestConstructFromConstCharPointer)
    {
        const char* s = "Hello";
        cstring_span<> v = ensure_z(s);
        CHECK(v.length() == 5);
    }

    TEST(TestConversionToConst)
    {
        char stack_string[] = "Hello";
        string_span<> v = ensure_z(stack_string);
        cstring_span<> v2 = v; 
        CHECK(v.length() == v2.length());
    }

    TEST(TestConversionFromConst)
    {
        char stack_string[] = "Hello";
        cstring_span<> v = ensure_z(stack_string);
        (void)v;
#ifdef CONFIRM_COMPILATION_ERRORS
        string_span<> v2 = v;
        string_span<> v3 = "Hello";
#endif
    }

    TEST(TestToString)
    {
        auto s = gsl::to_string(cstring_span<>{});
        CHECK(s.length() == 0);

        char stack_string[] = "Hello";
        cstring_span<> v = ensure_z(stack_string);
        auto s2 = gsl::to_string(v);
        CHECK(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
        CHECK(s2.length() == 5);
    }

    TEST(ComparisonAndImplicitConstructors)
    {
        {
            cstring_span<> span = "Hello";

            const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
            const char ar1[] = "Hello";
            const char ar2[10] = "Hello";
            const char* ptr = "Hello";
            const std::string str = "Hello";
            const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };

            // comparison to  literal
            CHECK(span == cstring_span<>("Hello"));

            // comparison to static array with no null termination
            CHECK(span == cstring_span<>(ar)); 

            // comparison to static array with null at the end
            CHECK(span == cstring_span<>(ar1));

            // comparison to static array with null in the middle
            CHECK(span == cstring_span<>(ar2)); 

            // comparison to null-terminated c string
            CHECK(span == cstring_span<>(ptr, 5)); 

            // comparison to string
            CHECK(span == cstring_span<>(str)); 

            // comparison to vector of charaters with no null termination
            CHECK(span == cstring_span<>(vec)); 

            // comparison of the original data to string
            CHECK(span.data() == std::string("Hello"));

            CHECK(span == "Hello");
            CHECK(span == ar);
            CHECK(span == ar1);
            CHECK(span == ar2);
            CHECK(span == ptr);
            CHECK(span == str);
            CHECK(span == vec);
            
            char _ar[] = { 'H', 'e', 'l', 'l', 'o' };
            char _ar1[] = "Hello";
            char _ar2[10] = "Hello";
            char* _ptr = _ar1;
            std::string _str = "Hello";
            std::vector<char> _vec = { 'H', 'e', 'l', 'l', 'o' };

            CHECK(span == _ar);
            CHECK(span == _ar1);
            CHECK(span == _ar2);
            CHECK(span == _ptr);
            CHECK(span == _str);
            CHECK(span == _vec);

            string_span<> _span{ _ptr };

            CHECK(_span == _ar);
            CHECK(_span == _ar1);
            CHECK(_span == _ar2);
            CHECK(_span == _ptr);
            CHECK(_span == _str);
            CHECK(_span == _vec);

            CHECK(_span == "Hello");
            CHECK(_span == ar);
            CHECK(_span == ar1);
            CHECK(_span == ar2);
            CHECK(_span == ptr);
            CHECK(_span == str);
            CHECK(_span == vec);
        }

        {
            std::vector<char> str1 = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> span1 = str1;
            std::vector<char> str2 = std::move(str1);
            cstring_span<> span2 = str2;

            // comparison of spans from the same vector before and after move (ok)
            CHECK(span1 == span2);
        }
    }

    TEST(EnzureRemoveZ)
    {
        // remove z from literals
        {
            cstring_span<> sp = "hello";
            CHECK((sp.length() == 5));
        }

        // take the string as is
        {
            auto str = std::string("hello");
            cstring_span<> sp = str;
            CHECK((sp.length() == 5));
        }

        // ensure z on c strings
        {
            char* ptr = new char[3];

            ptr[0] = 'a';
            ptr[1] = 'b';
            ptr[2] = '\0';

            string_span<> span(ptr);
            CHECK(span.length() == 2);

            delete[] ptr;
        }

        // ensuze z on c strings
        {
            char* ptr = new char[2];

            ptr[0] = 'a';
            ptr[1] = 'b';

            // do we want to have a constructor from pointer at all?
            // the behavior is unpredictable if the string is not 0-terminated

            // CHECK_THROW((string_span<>(ptr).length() == 2), fail_fast);

            cstring_span<> sp1{ ptr, 2 }; // good
            cstring_span<> sp2{ ptr, 3 }; // bad... but can't help there

            CHECK(sp1[1] == 'b');
            CHECK_THROW((sp1[2] == 'c'), fail_fast);

            CHECK(sp2[1] == 'b');
            //CHECK_THROW((sp1[2] == 'c'), fail_fast); // buffer overflow

            delete[] ptr;
        }
    }

    TEST(Constructors)
    {
        // from string temporary
#ifdef CONFIRM_COMPILATION_ERRORS
        {
            cstring_span<> span = std::string("Hello");
        }
#endif

        // from string literal
        {
            cstring_span<> span = "Hello";
            CHECK(span.length() == 5);
        }

        // from const static array
        {
            const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> span = ar;
            CHECK(span.length() == 5);
        }

        // from non-const static array
        {
            char ar[] = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> span = ar;
            CHECK(span.length() == 5);
        }

        // from const ptr and length
        {
            const char* ptr = "Hello";
            cstring_span<> span{ ptr, 5 };
            CHECK(span.length() == 5);
        }

        // from non-const ptr and length
        {
            char* ptr = "Hello";
            cstring_span<> span{ ptr, 5 };
            CHECK(span.length() == 5);
        }

        // from const string
        {
            const std::string str = "Hello";
            cstring_span<> span = str;
            CHECK(span.length() == 5);
        }

        // from non-const string
        {
            std::string str = "Hello";
            cstring_span<> span = str;
            CHECK(span.length() == 5);
        }

        // from const vector
        {
            const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> span = vec;
            CHECK(span.length() == 5);
        }

        // from non-const vector
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> span = vec;
            CHECK(span.length() == 5);
        }

        // from const span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            const span<const char> inner = vec;
            cstring_span<> span = inner;
            CHECK(span.length() == 5);
        }

        // from non-const span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            span<char> inner = vec;
            cstring_span<> span = inner;
            CHECK(span.length() == 5);
        }

        // from const string_span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> tmp = vec;
            cstring_span<> span = tmp;
            CHECK(span.length() == 5);
        }

        // from non-const string_span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> tmp = vec;
            cstring_span<> span = tmp;
            CHECK(span.length() == 5);
        }

        ///////////////////////////////////////////////////
        // How string_span should behave with const data

        // from string literal
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            string_span<> span = "Hello";
            CHECK(span.length() == 5);
#endif
        }

        // from const static array
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> span = ar;
            CHECK(span.length() == 5);
#endif
        }

        // from non-const static array
        {
            char ar[] = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> span = ar;
            CHECK(span.length() == 5);
        }

        // from const ptr and length
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const char* ptr = "Hello";
            string_span<> span{ ptr, 5 };
            CHECK(span.length() == 5);
#endif
        }

        // from non-const ptr and length
        {
            char* ptr = "Hello";
            string_span<> span{ ptr, 5 };
            CHECK(span.length() == 5);
        }

        // from const string
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const std::string str = "Hello";
            string_span<> span = str;
            CHECK(span.length() == 5);
#endif
        }

        // from non-const string
        {
            std::string str = "Hello";
            string_span<> span = str;
            CHECK(span.length() == 5);
        }

        // from const vector
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> span = vec;
            CHECK(span.length() == 5);
#endif
        }

        // from non-const vector
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> span = vec;
            CHECK(span.length() == 5);
        }

        // from const span
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            const span<const char> inner = vec;
            string_span<> span = inner;
            CHECK(span.length() == 5);
#endif
        }

        // from non-const span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            span<char> inner = vec;
            string_span<> span = inner;
            CHECK(span.length() == 5);
        }

        // from non-const span of non-const data from const vector (looks like a bug)
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            const span<char> inner = vec; // fix error (happens inside the constructor)
            string_span<> span = inner;
            CHECK(span.length() == 5);
#endif
        }

        // from const string_span
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            cstring_span<> tmp = vec;
            string_span<> span = tmp;
            CHECK(span.length() == 5);
#endif
        }

        // from non-const string_span
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> tmp = vec;
            string_span<> span = tmp;
            CHECK(span.length() == 5);
        }

        // from non-const string_span from const vector
        {
#ifdef CONFIRM_COMPILATION_ERRORS
            const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            string_span<> tmp = vec;
            string_span<> span = tmp;
            CHECK(span.length() == 5);
#endif
        }

        // from const string_span of non-const data
        {
            std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
            const string_span<> tmp = vec; // what does "const span" mean?
            string_span<> span = tmp;
            CHECK(span.length() == 5);
        }
    }

}

int main(int, const char *[])
{
    return UnitTest::RunAllTests();
}