-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathstring-view.t.cpp
1277 lines (1028 loc) · 42.3 KB
/
string-view.t.cpp
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017-2020 Martin Moene
//
// https://github.com/martinmoene/string-view-lite
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "string-view-main.t.hpp"
#include <vector>
#ifndef nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS
# define nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS 0
#endif
namespace {
using namespace nonstd;
template< class T >
T * data( std::vector<T> & v )
{
#if nssv_CPP11_OR_GREATER
return v.data();
#else
return &v[0];
#endif
}
typedef string_view::size_type size_type;
// 24.4.2.1 Construction and assignment:
CASE( "string_view: Allows to default construct an empty string_view" )
{
string_view sv;
// use parenthesis with data() to prevent lest from using sv.data() as C-string:
EXPECT( sv.size() == size_type( 0 ) );
EXPECT( (sv.data() == nssv_nullptr) );
}
CASE( "string_view: Allows to construct from pointer and size" )
{
string_view sv( "hello world", 5 );
EXPECT( sv.size() == size_type( 5 ) );
EXPECT( *(sv.data() + 0) == 'h' );
EXPECT( *(sv.data() + 4) == 'o' );
}
CASE( "string_view: Allows to construct from C-string" )
{
string_view sv( "hello world" );
EXPECT( sv.size() == size_type( 11 ) );
EXPECT( *(sv.data() + 0) == 'h' );
EXPECT( *(sv.data() + 10) == 'd' );
}
CASE( "string_view: Allows to copy-construct from empty string_view" )
{
string_view sv1;
string_view sv2( sv1 );
// use parenthesis with data() to prevent lest from using sv.data() as C-string:
EXPECT( sv2.size() == size_type( 0 ) );
EXPECT( (sv2.data() == nssv_nullptr) );
}
CASE( "string_view: Allows to copy-construct from non-empty string_view" )
{
string_view sv1( "hello world", 5 );
string_view sv2( sv1 );
EXPECT( sv2.size() == sv1.size() );
EXPECT( (sv2.data() == sv1.data()) );
EXPECT( *(sv2.data() + 0) == 'h' );
EXPECT( *(sv2.data() + 4) == 'o' );
}
CASE( "string_view: Disallows to copy-construct from nullptr (C++11)" )
{
#if nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS
#if nssv_HAVE_NULLPTR
string_view sv( nullptr );
EXPECT( true );
#else
EXPECT( !!"nullptr not available (no C++11)." );
#endif
#else
EXPECT( !!"Compile-time verification not enabled (nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS: 0)." );
#endif
}
// Assignment:
CASE( "string_view: Disallows to copy-assign from nullptr (C++11)" )
{
#if nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS
#if nssv_HAVE_NULLPTR
string_view sv;
sv = nullptr;
EXPECT( true );
#else
EXPECT( !!"nullptr not available (no C++11)." );
#endif
#else
EXPECT( !!"Compile-time verification not enabled (nssv_CONFIG_CONFIRMS_COMPILATION_ERRORS: 0)." );
#endif
}
CASE( "string_view: Allows to copy-assign from empty string_view" )
{
string_view sv1;
string_view sv2;
sv2 = sv1;
// use parenthesis with data() to prevent lest from using sv.data() as C-string:
EXPECT( sv2.size() == size_type( 0 ) );
EXPECT( (sv2.data() == nssv_nullptr) );
}
CASE( "string_view: Allows to copy-assign from non-empty string_view" )
{
string_view sv1( "hello world", 5 );
string_view sv2;
sv2 = sv1;
// use parenthesis with data() to prevent lest from using sv.data() as C-string:
EXPECT( sv2.size() == sv1.size() );
EXPECT( (sv2.data() == sv1.data()) );
EXPECT( *(sv2.data() + 0) == 'h' );
EXPECT( *(sv2.data() + 4) == 'o' );
}
// 24.4.2.2 Iterator support:
CASE( "string_view: Allows forward iteration" )
{
char hello[] = "hello";
string_view sv( hello );
for ( string_view::iterator pos = sv.begin(); pos != sv.end(); ++pos )
{
typedef std::iterator_traits< string_view::iterator >::difference_type difference_type;
difference_type i = std::distance( sv.begin(), pos );
EXPECT( *pos == *(sv.data() + i) );
}
}
CASE( "string_view: Allows const forward iteration" )
{
char hello[] = "hello";
string_view sv( hello );
for ( string_view::const_iterator pos = sv.begin(); pos != sv.end(); ++pos )
{
typedef std::iterator_traits< string_view::const_iterator >::difference_type difference_type;
difference_type i = std::distance( sv.cbegin(), pos );
EXPECT( *pos == *(sv.data() + i) );
}
}
CASE( "string_view: Allows reverse iteration" )
{
char hello[] = "hello";
string_view sv( hello );
for ( string_view::reverse_iterator pos = sv.rbegin(); pos != sv.rend(); ++pos )
{
typedef std::iterator_traits< string_view::reverse_iterator >::difference_type difference_type;
difference_type dist = std::distance( sv.rbegin(), pos );
EXPECT( *pos == *(sv.data() + sv.size() - 1 - dist) );
}
}
CASE( "string_view: Allows const reverse iteration" )
{
char hello[] = "hello";
string_view sv( hello );
for ( string_view::const_reverse_iterator pos = sv.crbegin(); pos != sv.crend(); ++pos )
{
typedef std::iterator_traits< string_view::const_reverse_iterator >::difference_type difference_type;
difference_type dist = std::distance( sv.crbegin(), pos );
EXPECT( *pos == *(sv.data() + sv.size() - 1 - dist) );
}
}
// 24.4.2.3 Capacity:
CASE( "string_view: Allows to obtain the size of the view via size()" )
{
char hello[] = "hello";
string_view sv( hello );
EXPECT( sv.size() == std::char_traits<char>::length(hello) );
}
CASE( "string_view: Allows to obtain the size of the view via length()" )
{
char hello[] = "hello";
string_view sv( hello );
EXPECT( sv.length() == std::char_traits<char>::length(hello) );
}
CASE( "string_view: Allows to obtain the maximum size a view can be via max_size()" )
{
// "large"
EXPECT( string_view().max_size() >= (std::numeric_limits< string_view::size_type >::max)() / 10 );
}
CASE( "string_view: Allows to check for an empty string_view via empty()" )
{
string_view sve;
string_view svne("hello");
EXPECT( sve.size() == size_type( 0 ) );
EXPECT( sve.empty() );
EXPECT_NOT( svne.empty() );
}
// 24.4.2.4 Element access:
CASE( "string_view: Allows to observe an element via array indexing" )
{
// Requires: index < sv.size()
char hello[] = "hello";
string_view sv( hello );
for ( size_type i = 0; i < sv.size(); ++i )
{
EXPECT( sv[i] == hello[i] );
}
}
CASE( "string_view: Allows to observe an element via at()" )
{
char hello[] = "hello";
string_view sv( hello );
for ( size_type i = 0; i < sv.size(); ++i )
{
EXPECT( sv.at(i) == hello[i] );
}
}
CASE( "string_view: Throws at observing an element via at() with an index of size() or larger" )
{
string_view sv("hello");
EXPECT_THROWS( sv.at( sv.size() ) );
EXPECT_NO_THROW( sv.at( sv.size() - 1 ) );
}
CASE( "string_view: Allows to observe elements via data()" )
{
char hello[] = "hello";
string_view sv( hello );
EXPECT( *sv.data() == *sv.begin() );
for ( size_type i = 0; i < sv.size(); ++i )
{
EXPECT( sv.data()[i] == hello[i] );
}
}
CASE( "string_view: Yields nullptr (or NULL) with data() for an empty string_view" )
{
string_view sv;
// use parenthesis with data() to prevent lest from using sv.data() as C-string:
EXPECT( (sv.data() == nssv_nullptr) );
}
// 24.4.2.5 Modifiers:
CASE( "string_view: Allows to remove a prefix of n elements" )
{
char hello[] = "hello world";
string_view sv( hello );
sv.remove_prefix( 6 );
EXPECT( sv.size() == size_type( 5 ) );
EXPECT( std::equal( sv.begin(), sv.end(), hello + 6) );
}
CASE( "string_view: Allows to remove a suffix of n elements" )
{
char hello[] = "hello world";
string_view sv( hello );
sv.remove_suffix( 6 );
EXPECT( sv.size() == size_type( 5 ) );
EXPECT( std::equal( sv.begin(), sv.end(), hello ) );
}
CASE( "string_view: Allows to swap with other string_view" )
{
char hello[] = "hello";
char world[] = "world";
string_view sv1( hello );
string_view sv2( world );
sv1.swap( sv2 );
EXPECT( std::equal( sv1.begin(), sv1.end(), world ) );
EXPECT( std::equal( sv2.begin(), sv2.end(), hello ) );
}
// 24.4.2.6 String operations:
CASE( "string_view: Allows to copy a substring of length n, starting at position pos (default: 0) via copy()" )
{
char hello[] = "hello world";
string_view sv( hello );
{
std::vector<string_view::value_type> vec( sv.size() );
sv.copy( data(vec), vec.size() );
EXPECT( std::equal( vec.begin(), vec.end(), hello ) );
}{
std::size_t offset = 3u; std::size_t length = 4u;
std::vector<string_view::value_type> vec( length );
sv.copy( data(vec), length, offset );
EXPECT( std::equal( vec.begin(), vec.end(), hello + offset ) );
}
}
CASE( "string_view: Throws if requested position of copy() exceeds string_view's size()" )
{
string_view sv("hello world");
std::vector<string_view::value_type> vec( sv.size() );
EXPECT_THROWS( sv.copy( data(vec), sv.size() - 4, sv.size() + 1 ) );
EXPECT_NO_THROW( sv.copy( data(vec), sv.size() - 4, sv.size() + 0 ) );
}
CASE( "string_view: Allow to obtain a sub string, starting at position pos (default: 0) and of length n (default full), via substr()" )
{
char hello[] = "hello world";
string_view sv( hello );
{
#if nssv_USES_STD_STRING_VIEW && defined(__GNUC__)
EXPECT( !!"std::string_view::substr(), with default pos, count is not available with GNU C" );
#else
EXPECT( std::equal( sv.begin(), sv.end(), sv.substr().begin() ) );
#endif
}{
string_view subv = sv.substr( 6 );
EXPECT( std::equal( subv.begin(), subv.end(), hello + 6 ) );
}{
string_view subv = sv.substr( 3, 4 );
EXPECT( std::equal( subv.begin(), subv.end(), hello + 3 ) );
}
}
CASE( "string_view: Throws if requested position of substr() exceeds string_view's size()" )
{
string_view sv("hello world");
EXPECT_THROWS( sv.substr( sv.size() + 1 ) );
EXPECT_NO_THROW( sv.substr( sv.size() + 0 ) );
}
CASE( "string_view: Allows to lexically compare to another string_view via compare(), (1)" )
{
char hello[] = "hello";
char world[] = "world";
EXPECT( string_view( hello ).compare( string_view( hello ) ) == 0 );
EXPECT( string_view( hello ).compare( string_view( world ) ) < 0 );
EXPECT( string_view( world ).compare( string_view( hello ) ) > 0 );
char hello_sp[] = "hello ";
EXPECT( string_view( hello ).compare( string_view( hello_sp ) ) < 0 );
EXPECT( string_view( hello_sp ).compare( string_view( hello ) ) > 0 );
}
CASE( "string_view: Allows to compare empty string_views as equal via compare(), (1)" )
{
EXPECT( string_view().compare( string_view() ) == 0 );
}
CASE( "string_view: Allows to constexpr-compare string_views via compare(), (1) (C++14)" )
{
#if nssv_HAVE_CONSTEXPR_14
static_assert( string_view( "hello" ).compare( string_view( "hello" ) ) == 0, "" );
static_assert( string_view( "hello" ).compare( string_view( "world" ) ) < 0, "" );
static_assert( string_view( "world" ).compare( string_view( "hello" ) ) > 0, "" );
#else
EXPECT( !!"C++14 constexpr is not available (no C++14)" );
#endif
}
CASE( "string_view: Allows to compare a sub string to another string_view via compare(), (2)" )
{
string_view sv1("hello world");
string_view sv2("world");
EXPECT( sv1.compare ( 0, sv1.length(), sv1 ) == 0 );
EXPECT( sv1.compare ( 6, 5, sv2 ) == 0 );
EXPECT( sv1.compare ( 0, 5, sv2 ) < 0 );
EXPECT( sv2.compare ( 0, 5, sv1 ) > 0 );
}
CASE( "string_view: Allows to compare a sub string to another string_view sub string via compare(), (3)" )
{
string_view sv1("hello world");
EXPECT( sv1.compare ( 0, sv1.length(), sv1 ) == 0 );
EXPECT( sv1.compare ( 6, 5, sv1, 6, 5 ) == 0 );
EXPECT( sv1.compare ( 0, 5, sv1, 6, 5 ) < 0 );
EXPECT( sv1.compare ( 6, 5, sv1, 0, 5 ) > 0 );
}
CASE( "string_view: Allows to compare to a C-string via compare(), (4)" )
{
char hello[] = "hello";
char world[] = "world";
EXPECT( string_view( hello ).compare( hello ) == 0 );
EXPECT( string_view( hello ).compare( world ) < 0 );
EXPECT( string_view( world ).compare( hello ) > 0 );
}
CASE( "string_view: Allows to compare a sub string to a C-string via compare(), (5)" )
{
char hello[] = "hello world";
char world[] = "world";
EXPECT( string_view( hello ).compare( 6, 5, world ) == 0 );
EXPECT( string_view( hello ).compare( world ) < 0 );
EXPECT( string_view( world ).compare( hello ) > 0 );
}
CASE( "string_view: Allows to compare a sub string to a C-string prefix via compare(), (6)" )
{
char hello[] = "hello world";
char world[] = "world";
EXPECT( string_view( hello ).compare( 6, 5, world, 5 ) == 0 );
EXPECT( string_view( hello ).compare( 0, 5, world, 5 ) < 0 );
EXPECT( string_view( hello ).compare( 6, 5, hello, 5 ) > 0 );
}
// 24.4.2.7 Searching:
#if nssv_HAVE_STARTS_WITH
CASE( "string_view: Allows to check for a prefix string_view via starts_with(), (1)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).starts_with( string_view( hello ) ) );
EXPECT( string_view( hello ).starts_with( string_view("hello") ) );
EXPECT_NOT( string_view( hello ).starts_with( string_view("world") ) );
}
CASE( "string_view: Allows to check for a prefix character via starts_with(), (2)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).starts_with( 'h' ) );
EXPECT_NOT( string_view( hello ).starts_with( 'e' ) );
}
CASE( "string_view: Allows to check for a prefix C-string via starts_with(), (3)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).starts_with( hello ) );
EXPECT( string_view( hello ).starts_with("hello") );
EXPECT_NOT( string_view( hello ).starts_with("world") );
}
#endif // nssv_HAVE_STARTS_WITH
#if nssv_HAVE_ENDS_WITH
CASE( "string_view: Allows to check for a suffix string_view via ends_with(), (1)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).ends_with( string_view( hello ) ) );
EXPECT( string_view( hello ).ends_with( string_view("world") ) );
EXPECT_NOT( string_view( hello ).ends_with( string_view("hello") ) );
}
CASE( "string_view: Allows to check for a suffix character via ends_with(), (2)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).ends_with( 'd' ) );
EXPECT_NOT( string_view( hello ).ends_with( 'l' ) );
}
CASE( "string_view: Allows to check for a suffix C-string via ends_with(), (3)" )
{
char hello[] = "hello world";
EXPECT( string_view( hello ).ends_with( hello ) );
EXPECT( string_view( hello ).ends_with("world") );
EXPECT_NOT( string_view( hello ).ends_with("hello") );
}
#endif // nssv_HAVE_ENDS_WITH
CASE( "string_view: Allows to search for a string_view substring, starting at position pos (default: 0) via find(), (1)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find( sv ) == size_type( 0 ) );
EXPECT( sv.find( sv, 1 ) == string_view::npos );
EXPECT( sv.find( string_view("world" ) ) == size_type( 6 ) );
EXPECT( sv.find( string_view("world" ), 6 ) == size_type( 6 ) );
EXPECT( sv.find( string_view("world" ), 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search for a character, starting at position pos (default: 0) via find(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find('h' ) == size_type( 0 ) );
EXPECT( sv.find('h', 1 ) == string_view::npos );
EXPECT( sv.find('w' ) == size_type( 6 ) );
EXPECT( sv.find('w', 6 ) == size_type( 6 ) );
EXPECT( sv.find('w', 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search for a C-string substring, starting at position pos and of length n via find(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find( hello , 0, sv.size() ) == size_type( 0 ) );
EXPECT( sv.find( hello , 1, sv.size() ) == string_view::npos );
EXPECT( sv.find("world", 0, 5 ) == size_type( 6 ) );
EXPECT( sv.find("world", 6, 5 ) == size_type( 6 ) );
EXPECT( sv.find("world", 7, 4 ) == string_view::npos );
EXPECT( sv.find("world", 3, 0 ) == size_type( 3 ) );
}
CASE( "string_view: Allows to search for a C-string substring, starting at position pos (default: 0) via find(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find( hello ) == size_type( 0 ) );
EXPECT( sv.find( hello , 1 ) == string_view::npos );
EXPECT( sv.find("world" ) == size_type( 6 ) );
EXPECT( sv.find("world", 6 ) == size_type( 6 ) );
EXPECT( sv.find("world", 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for a string_view substring, starting at position pos (default: npos) via rfind(), (1)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.rfind( sv ) == size_type( 0 ) );
EXPECT( sv.rfind( sv, 3 ) == size_type( 0 ) );
EXPECT( sv.rfind( string_view( ) ) == size_type(11 ) );
EXPECT( sv.rfind( string_view("world" ) ) == size_type( 6 ) );
EXPECT( sv.rfind( string_view("world" ), 6 ) == size_type( 6 ) );
EXPECT( sv.rfind( string_view("world" ), 5 ) == string_view::npos );
EXPECT( sv.rfind( string_view("hello world, a longer text" ) ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for a character, starting at position pos (default: npos) via rfind(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.rfind('h' ) == size_type( 0 ) );
EXPECT( sv.rfind('e' ) == size_type( 1 ) );
EXPECT( sv.rfind('e', 0 ) == string_view::npos );
EXPECT( sv.rfind('w' ) == size_type( 6 ) );
EXPECT( sv.rfind('w', 6 ) == size_type( 6 ) );
EXPECT( sv.rfind('w', 5 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for a C-string substring, starting at position pos and of length n via rfind(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.rfind( hello ) == size_type( 0 ) );
EXPECT( sv.rfind( hello , 0, 5 ) == size_type( 0 ) );
EXPECT( sv.rfind( hello , 1, 5 ) == size_type( 0 ) );
EXPECT( sv.rfind("world", 10, 5 ) == size_type( 6 ) );
EXPECT( sv.rfind("world", 6, 5 ) == size_type( 6 ) );
EXPECT( sv.rfind("world", 5, 5 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for a C-string substring, starting at position pos (default: 0) via rfind(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.rfind( hello ) == size_type( 0 ) );
EXPECT( sv.rfind( hello , 3 ) == size_type( 0 ) );
EXPECT( sv.rfind("world" ) == size_type( 6 ) );
EXPECT( sv.rfind("world", 6 ) == size_type( 6 ) );
EXPECT( sv.rfind("world", 5 ) == string_view::npos );
}
CASE( "string_view: Allows to search for the first occurrence of any of the characters specified in a string view, starting at position pos (default: 0) via find_first_of(), (1)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_of( sv ) == size_type( 0 ) );
EXPECT( sv.find_first_of( sv, 3 ) == size_type( 3 ) );
EXPECT( sv.find_first_of( string_view("xwo" ) ) == size_type( 4 ) );
EXPECT( sv.find_first_of( string_view("wdx" ), 6 ) == size_type( 6 ) );
EXPECT( sv.find_first_of( string_view("wxy" ), 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search for a character, starting at position pos (default: 0) via find_first_of(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_of('h' ) == size_type( 0 ) );
EXPECT( sv.find_first_of('h', 1 ) == string_view::npos );
EXPECT( sv.find_first_of('w' ) == size_type( 6 ) );
EXPECT( sv.find_first_of('w', 6 ) == size_type( 6 ) );
EXPECT( sv.find_first_of('w', 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search for the first occurrence of any of the characters specified in a C-string, starting at position pos and of length n via find_first_of(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_of( hello , 0, sv.size() ) == size_type( 0 ) );
EXPECT( sv.find_first_of( hello , 1, sv.size() ) == size_type( 1 ) );
EXPECT( sv.find_first_of( "xwy", 0, 3 ) == size_type( 6 ) );
EXPECT( sv.find_first_of( "xwy", 6, 3 ) == size_type( 6 ) );
EXPECT( sv.find_first_of( "xwy", 7, 3 ) == string_view::npos );
EXPECT( sv.find_first_of( "xyw", 0, 2 ) == string_view::npos );
}
CASE( "string_view: Allows to search for the first occurrence of any of the characters specified in a C-string, starting at position pos via find_first_of(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_of( hello , 0 ) == size_type( 0 ) );
EXPECT( sv.find_first_of( hello , 1 ) == size_type( 1 ) );
EXPECT( sv.find_first_of( "xwy", 0 ) == size_type( 6 ) );
EXPECT( sv.find_first_of( "xwy", 6 ) == size_type( 6 ) );
EXPECT( sv.find_first_of( "xwy", 7 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for the last occurrence of any of the characters specified in a string view, starting at position pos (default: npos) via find_last_of(), (1)" )
{
char hello[] = "hello world";
char empty[] = "";
string_view sv( hello );
string_view sve( empty );
EXPECT( sv.find_last_of( sv ) == size_type( 10 ) );
EXPECT( sv.find_last_of( sv, 3 ) == size_type( 3 ) );
EXPECT( sv.find_last_of( string_view("xwo" ) ) == size_type( 7 ) );
EXPECT( sv.find_last_of( string_view("wdx" ), 6 ) == size_type( 6 ) );
EXPECT( sv.find_last_of( string_view("wxy" ), 7 ) == size_type( 6 ) );
EXPECT( sve.find_last_of( string_view("x") ) == string_view::npos ); // issue 20 (endless loop)
}
CASE( "string_view: Allows to search backwards for a character, starting at position pos (default: 0) via find_last_of(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_of('h' ) == size_type( 0 ) );
EXPECT( sv.find_last_of('l', 1 ) == string_view::npos );
EXPECT( sv.find_last_of('w' ) == size_type( 6 ) );
EXPECT( sv.find_last_of('w', 6 ) == size_type( 6 ) );
EXPECT( sv.find_last_of('w', 5 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for the first occurrence of any of the characters specified in a C-string, starting at position pos and of length n via find_last_of(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_of( hello , 0, sv.size() ) == size_type( 0 ) );
EXPECT( sv.find_last_of( hello , 1, sv.size() ) == size_type( 1 ) );
EXPECT( sv.find_last_of("xwy", 10, 3 ) == size_type( 6 ) );
EXPECT( sv.find_last_of("xwy", 6, 3 ) == size_type( 6 ) );
EXPECT( sv.find_last_of("xwy", 5, 3 ) == string_view::npos );
EXPECT( sv.find_last_of("xyw", 10, 2 ) == string_view::npos );
}
CASE( "string_view: Allows to search backwards for the first occurrence of any of the characters specified in a C-string, starting at position pos via find_last_of(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_of( hello , 0 ) == size_type( 0 ) );
EXPECT( sv.find_last_of( hello , 1 ) == size_type( 1 ) );
EXPECT( sv.find_last_of( "xwy", 10 ) == size_type( 6 ) );
EXPECT( sv.find_last_of( "xwy", 6 ) == size_type( 6 ) );
EXPECT( sv.find_last_of( "xwy", 5 ) == string_view::npos );
}
CASE( "string_view: Allows to search for the first character not specified in a string view, starting at position pos (default: 0) via find_first_not_of(), (1)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_not_of( sv ) == string_view::npos );
EXPECT( sv.find_first_not_of( sv, 3 ) == string_view::npos );
EXPECT( sv.find_first_not_of( string_view("helo " ) ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( string_view("helo " ), 6 ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( string_view("helo " ), 7 ) == size_type( 8 ) );
EXPECT( sv.find_first_not_of( string_view("helo wr" ) ) == size_type( 10 ) );
}
CASE( "string_view: Allows to search for the first character not equal to the specified character, starting at position pos (default: 0) via find_first_not_of(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_not_of('h' ) == size_type( 1 ) );
EXPECT( sv.find_first_not_of('h', 1 ) == size_type( 1 ) );
EXPECT( sv.find_first_not_of('w' ) == size_type( 0 ) );
EXPECT( sv.find_first_not_of('w', 6 ) == size_type( 7 ) );
EXPECT( sv.find_first_not_of('d', 10 ) == string_view::npos );
}
CASE( "string_view: Allows to search for the first character not equal to any of the characters specified in a C-string, starting at position pos and of length n via find_first_not_of(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_not_of( hello, 0, sv.size() ) == string_view::npos );
EXPECT( sv.find_first_not_of( hello, 3, sv.size() ) == string_view::npos );
EXPECT( sv.find_first_not_of( "helo " , 0, 5 ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( "helo " , 6, 5 ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( "helo " , 7, 5 ) == size_type( 8 ) );
EXPECT( sv.find_first_not_of( "helo wr", 0, 7 ) == size_type( 10 ) );
EXPECT( sv.find_first_not_of( "he" , 0, 1 ) == size_type( 1 ) );
}
CASE( "string_view: Allows to search for the first character not equal to any of the characters specified in a C-string, starting at position pos via find_first_not_of(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_first_not_of( hello , 0 ) == string_view::npos );
EXPECT( sv.find_first_not_of( hello , 3 ) == string_view::npos );
EXPECT( sv.find_first_not_of( "helo " , 0 ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( "helo " , 6 ) == size_type( 6 ) );
EXPECT( sv.find_first_not_of( "helo " , 7 ) == size_type( 8 ) );
EXPECT( sv.find_first_not_of( "helo wr", 0 ) == size_type( 10 ) );
}
CASE( "string_view: Allows to search backwards for the first character not specified in a string view, starting at position pos (default: npos) via find_last_not_of(), (1)" )
{
char hello[] = "hello world";
char empty[] = "";
string_view sv( hello );
string_view sve( empty );
EXPECT( sv.find_last_not_of( sv ) == string_view::npos );
EXPECT( sv.find_last_not_of( sv, 3 ) == string_view::npos );
EXPECT( sv.find_last_not_of( string_view("world " ) ) == size_type( 1 ) );
EXPECT( sv.find_last_not_of( string_view("heo " ), 4 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( string_view("heo " ), 3 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( string_view("heo " ), 2 ) == size_type( 2 ) );
EXPECT( sv.find_last_not_of( string_view("x" ) ) == size_type( 10 ) );
EXPECT( sve.find_last_not_of( string_view("x") ) == string_view::npos ); // issue 20 (endless loop)
}
CASE( "string_view: Allows to search backwards for the first character not equal to the specified character, starting at position pos (default: npos) via find_last_not_of(), (2)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_not_of('d' ) == size_type( 9 ) );
EXPECT( sv.find_last_not_of('d', 10 ) == size_type( 9 ) );
EXPECT( sv.find_last_not_of('d', 9 ) == size_type( 9 ) );
EXPECT( sv.find_last_not_of('d', 8 ) == size_type( 8 ) );
EXPECT( sv.find_last_not_of('d', 0 ) == size_type( 0 ) );
}
CASE( "string_view: Allows to search backwards for the first character not equal to any of the characters specified in a C-string, starting at position pos and of length n via find_last_not_of(), (3)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_not_of( hello, 0, sv.size() ) == string_view::npos );
EXPECT( sv.find_last_not_of( hello, 3, sv.size() ) == string_view::npos );
EXPECT( sv.find_last_not_of( "world ", 10, 6 ) == size_type( 1 ) );
EXPECT( sv.find_last_not_of( "heo " , 4, 4 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( "heo " , 3, 4 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( "heo " , 2, 4 ) == size_type( 2 ) );
EXPECT( sv.find_last_not_of( "x" ) == size_type( 10 ) );
}
CASE( "string_view: Allows to search backwards for the first character not equal to any of the characters specified in a C-string, starting at position pos via find_last_not_of(), (4)" )
{
char hello[] = "hello world";
string_view sv( hello );
EXPECT( sv.find_last_not_of( hello , 0 ) == string_view::npos );
EXPECT( sv.find_last_not_of( hello , 3 ) == string_view::npos );
EXPECT( sv.find_last_not_of( "world ", 10 ) == size_type( 1 ) );
EXPECT( sv.find_last_not_of( "heo " , 4 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( "heo " , 3 ) == size_type( 3 ) );
EXPECT( sv.find_last_not_of( "heo " , 2 ) == size_type( 2 ) );
EXPECT( sv.find_last_not_of( "x" ) == size_type( 10 ) );
}
CASE( "string_view: Allows to create a string_view, wstring_view, u16string_view, u32string_view via literal \"sv\"" )
{
#if nssv_CONFIG_STD_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_STD_DEFINED_LITERALS )
using namespace nonstd::literals::string_view_literals;
string_view sv1 = "abc"sv;
wstring_view sv2 = L"abc"sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 3 ) );
#if nssv_HAVE_WCHAR16_T
u16string_view sv3 = u"abc"sv;
EXPECT( sv3.size() == size_type( 3 ) );
#endif
#if nssv_HAVE_WCHAR32_T
u32string_view sv4 = U"abc"sv;
EXPECT( sv4.size() == size_type( 3 ) );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (nssv_CONFIG_STD_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view via literal \"sv\", using namespace nonstd::literals::string_view_literals" )
{
#if nssv_CONFIG_STD_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_STD_DEFINED_LITERALS )
using namespace nonstd::literals::string_view_literals;
string_view sv1 = "abc\0\0def";
string_view sv2 = "abc\0\0def"sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 8 ) );
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (nssv_CONFIG_STD_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view via literal \"sv\", using namespace nonstd::string_view_literals" )
{
#if nssv_CONFIG_STD_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_STD_DEFINED_LITERALS )
#if nssv_STD_SV_OR( nssv_HAVE_INLINE_NAMESPACE )
using namespace nonstd::string_view_literals;
string_view sv1 = "abc\0\0def";
string_view sv2 = "abc\0\0def"sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 8 ) );
#else
EXPECT( !!"Inline namespaces for literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (nssv_CONFIG_STD_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view via literal \"sv\", using namespace nonstd::literals" )
{
#if nssv_CONFIG_STD_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_STD_DEFINED_LITERALS )
#if nssv_STD_SV_OR( nssv_HAVE_INLINE_NAMESPACE )
using namespace nonstd::literals;
string_view sv1 = "abc\0\0def";
string_view sv2 = "abc\0\0def"sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 8 ) );
#else
EXPECT( !!"Inline namespaces for literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator 'sv' for string_view not available (nssv_CONFIG_STD_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view, wstring_view, u16string_view, u32string_view via literal \"_sv\"" )
{
#if nssv_CONFIG_USR_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_USER_DEFINED_LITERALS )
using namespace nonstd::literals::string_view_literals;
string_view sv1 = "abc"_sv;
wstring_view sv2 = L"abc"_sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 3 ) );
#if nssv_HAVE_WCHAR16_T
u16string_view sv3 = u"abc"_sv;
EXPECT( sv3.size() == size_type( 3 ) );
#endif
#if nssv_HAVE_WCHAR32_T
u32string_view sv4 = U"abc"_sv;
EXPECT( sv4.size() == size_type( 3 ) );
#endif
#else
EXPECT( !!"Literal operator '_sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator '_sv' for string_view not available (nssv_CONFIG_USR_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view via literal \"_sv\", using namespace nonstd::literals::string_view_literals" )
{
#if nssv_CONFIG_USR_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_USER_DEFINED_LITERALS )
using namespace nonstd::literals::string_view_literals;
string_view sv1 = "abc\0\0def";
string_view sv2 = "abc\0\0def"_sv;
EXPECT( sv1.size() == size_type( 3 ) );
EXPECT( sv2.size() == size_type( 8 ) );
#else
EXPECT( !!"Literal operator '_sv' for string_view not available (no C++11)." );
#endif
#else
EXPECT( !!"Literal operator '_sv' for string_view not available (nssv_CONFIG_USR_SV_OPERATOR=0)." );
#endif
}
CASE( "string_view: Allows to create a string_view via literal \"_sv\", using namespace nonstd::string_view_literals" )
{
#if nssv_CONFIG_USR_SV_OPERATOR
#if nssv_STD_SV_OR( nssv_HAVE_USER_DEFINED_LITERALS )
#if nssv_STD_SV_OR( nssv_HAVE_INLINE_NAMESPACE )
using namespace nonstd::string_view_literals;
string_view sv1 = "abc\0\0def";
string_view sv2 = "abc\0\0def"_sv;