-
Notifications
You must be signed in to change notification settings - Fork 44
/
span.t.cpp
2251 lines (1846 loc) · 64.2 KB
/
span.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
//
// span for C++98 and later.
// Based on http://wg21.link/p0122r7
// For more information see https://github.com/martinmoene/span-lite
//
// Copyright 2015, 2018-2020 Martin Moene
// Copyright 2015 Microsoft Corporation. All rights reserved.
//
// 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 <ciso646>
#include <memory> // std::unique_ptr
#include "span-main.t.hpp"
#define DIMENSION_OF( a ) ( sizeof(a) / sizeof(0[a]) )
#define span_STD_OR( expr ) ( span_USES_STD_SPAN) || ( expr )
#define span_NONSTD_AND( expr ) (!span_USES_STD_SPAN) && ( expr )
using namespace nonstd;
typedef span<int>::size_type size_type;
typedef std::ptrdiff_t ssize_type;
CASE( "span<>: Terminates construction from a nullptr and a non-zero size (C++11)" )
{
#if span_NONSTD_AND( span_HAVE( NULLPTR ) )
struct F { static void blow() { span<int> v( nullptr, 42 ); } };
EXPECT_THROWS( F::blow() );
#else
EXPECT( !!"nullptr is not available (no C++11), or no exception, using std::span)" );
#endif
}
CASE( "span<>: Terminates construction from two pointers in the wrong order" )
{
#if !span_USES_STD_SPAN
struct F { static void blow() { int a[2]; span<int> v( &a[1], &a[0] ); } };
EXPECT_THROWS( F::blow() );
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Terminates construction from a null pointer and a non-zero size" )
{
#if !span_USES_STD_SPAN
struct F { static void blow() { int * p = span_nullptr; span<int> v( p, 42 ); } };
EXPECT_THROWS( F::blow() );
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Terminates creation of a sub span of the first n elements for n exceeding the span" )
{
#if !span_USES_STD_SPAN
struct F {
static void blow()
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
(void) v.first( 4 );
}};
EXPECT_THROWS( F::blow() );
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Terminates creation of a sub span of the last n elements for n exceeding the span" )
{
#if !span_USES_STD_SPAN
struct F {
static void blow()
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
(void) v.last( 4 );
}};
EXPECT_THROWS( F::blow() );
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Terminates creation of a sub span outside the span" )
{
#if !span_USES_STD_SPAN
struct F {
static void blow_offset() { int arr[] = { 1, 2, 3, }; span<int> v( arr ); (void) v.subspan( 4 ); }
static void blow_count() { int arr[] = { 1, 2, 3, }; span<int> v( arr ); (void) v.subspan( 1, 3 ); }
};
EXPECT_THROWS( F::blow_offset() );
EXPECT_THROWS( F::blow_count() );
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Terminates access outside the span" )
{
#if !span_USES_STD_SPAN
struct F {
static void blow_ix(size_type i) { int arr[] = { 1, 2, 3, }; span<int> v( arr ); (void) v[i]; }
#if span_NONSTD_AND( span_FEATURE( MEMBER_CALL_OPERATOR ) )
static void blow_iv(size_type i) { int arr[] = { 1, 2, 3, }; span<int> v( arr ); (void) v(i); }
#endif
#if span_NONSTD_AND( span_FEATURE( MEMBER_AT ) )
static void blow_at(size_type i) { int arr[] = { 1, 2, 3, }; span<int> v( arr ); (void) v.at(i); }
#endif
};
EXPECT_NO_THROW( F::blow_ix(2) );
EXPECT_THROWS( F::blow_ix(3) );
#if span_NONSTD_AND( span_FEATURE( MEMBER_CALL_OPERATOR ) )
EXPECT_NO_THROW( F::blow_iv(2) );
EXPECT_THROWS( F::blow_iv(3) );
#endif
#if span_NONSTD_AND( span_FEATURE( MEMBER_AT ) )
EXPECT_NO_THROW( F::blow_at(2) );
EXPECT_THROWS( F::blow_at(3) );
#endif
#else
EXPECT( !!"No exception, using std::span" );
#endif
}
CASE( "span<>: Throws on access outside the span via at(): std::out_of_range [span_FEATURE_MEMBER_AT>0][span_CONFIG_NO_EXCEPTIONS=0]" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_AT ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
EXPECT_THROWS_AS( v.at(42), std::out_of_range );
EXPECT_THROWS_AS( w.at(42), std::out_of_range );
struct F {
static void fail(lest::env & lest_env) {
int arr[] = { 1, 2, 3, }; span<int> v( arr ); EXPECT( (v.at(42), true) );
}};
lest::test fail[] = { lest::test( "F", F::fail ) };
std::ostringstream os;
EXPECT( 1 == run( fail, os ) );
#if span_FEATURE( MEMBER_AT ) > 1
EXPECT( std::string::npos != os.str().find("42") );
EXPECT( std::string::npos != os.str().find("3)") );
#else
EXPECT( std::string::npos != os.str().find("index outside span") );
#endif
#else
EXPECT( !!"member at() is not available (span_FEATURE_MEMBER_AT=0, or using std::span)" );
#endif
}
CASE( "span<>: Termination throws std::logic_error-derived exception [span_CONFIG_CONTRACT_VIOLATION_THROWS=1]" )
{
#if span_NONSTD_AND( span_CONFIG( CONTRACT_VIOLATION_THROWS_V ) )
struct F {
static void blow() { int arr[] = { 1, }; span<int> v( arr ); (void) v[1]; }
};
EXPECT_THROWS_AS( F::blow(), nonstd::span_lite::detail::contract_violation );
#else
EXPECT( !!"exception contract_violation is not available (non-throwing contract violation, or using std::span)" );
#endif
}
CASE( "span<>: Allows to default-construct" )
{
span<int> v;
span<int, 0> w;
EXPECT( v.size() == size_type( 0 ) );
EXPECT( w.size() == size_type( 0 ) );
}
CASE( "span<>: Allows to construct from a nullptr and a zero size (C++11)" )
{
#if span_NONSTD_AND( span_HAVE( NULLPTR ) )
span< int> v( nullptr, size_type(0) );
span<const int> w( nullptr, size_type(0) );
EXPECT( v.size() == size_type( 0 ) );
EXPECT( w.size() == size_type( 0 ) );
#else
EXPECT( !!"nullptr is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from two pointers" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( &arr[0], &arr[0] + DIMENSION_OF( arr ) );
span<const int> w( &arr[0], &arr[0] + DIMENSION_OF( arr ) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from two iterators" )
{
#if span_HAVE_ITERATOR_CTOR
std::vector<int> v( 5, 123 );
span<int> s( v.begin(), v.end() );
EXPECT( std::equal( s.begin(), s.end(), v.begin() ) );
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from two iterators - empty range" )
{
#if span_HAVE_ITERATOR_CTOR
#if ! (span_COMPILER_MSVC_VER && defined(_DEBUG) )
std::vector<int> v;
span<int> s( v.begin(), v.end() );
EXPECT( std::equal( s.begin(), s.end(), v.begin() ) );
#else
EXPECT( !!"construction from empty range not available (MSVC Debug)" );
#endif
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from two iterators - move-only element" )
{
#if span_HAVE_ITERATOR_CTOR
std::vector<std::unique_ptr<int> > v(5);
nonstd::span<std::unique_ptr<int> > s{ v.begin(), v.end() };
EXPECT( s.size() == 5 );
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from an iterator and a size" )
{
#if span_HAVE_ITERATOR_CTOR
std::vector<int> v( 5, 123 );
span<int> s( v.begin(), v.size() );
EXPECT( std::equal( s.begin(), s.end(), v.begin() ) );
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from an iterator and a size - empty range" )
{
#if span_HAVE_ITERATOR_CTOR
#if ! (span_COMPILER_MSVC_VER && defined(_DEBUG) )
std::vector<int> v;
span<int> s( v.begin(), v.size() );
EXPECT( std::equal( s.begin(), s.end(), v.begin() ) );
#else
EXPECT( !!"construction from empty range not available (MSVC Debug)" );
#endif
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from an iterator and a size - move-only element" )
{
#if span_HAVE_ITERATOR_CTOR
std::vector<std::unique_ptr<int> > v(5);
nonstd::span<std::unique_ptr<int> > s{ v.begin(), v.size() };
EXPECT( s.size() == v.size() );
#else
EXPECT( !!"construction from iterator is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from two pointers to const" )
{
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span<const int> v( &arr[0], &arr[0] + DIMENSION_OF( arr ) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from a non-null pointer and a size" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( &arr[0], DIMENSION_OF( arr ) );
span<const int> w( &arr[0], DIMENSION_OF( arr ) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from a non-null pointer to const and a size" )
{
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span<const int> v( &arr[0], DIMENSION_OF( arr ) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from a temporary pointer and a size" )
{
int x = 42;
span< int> v( &x, 1 );
span<const int> w( &x, 1 );
EXPECT( std::equal( v.begin(), v.end(), &x ) );
EXPECT( std::equal( w.begin(), w.end(), &x ) );
}
CASE( "span<>: Allows to construct from a temporary pointer to const and a size" )
{
const int x = 42;
span<const int> v( &x, 1 );
EXPECT( std::equal( v.begin(), v.end(), &x ) );
}
CASE( "span<>: Allows to construct from any pointer and a zero size (C++98)" )
{
#if !span_HAVE_ITERATOR_CTOR
struct F {
static void null() {
int * p = span_nullptr; span<int> v( p, size_type( 0 ) );
}
static void nonnull() {
int i = 7; int * p = &i; span<int> v( p, size_type( 0 ) );
}
};
EXPECT_NO_THROW( F::null() );
EXPECT_NO_THROW( F::nonnull() );
#else
EXPECT( !!"Only with C++98" );
#endif
}
CASE( "span<>: Allows to construct from a pointer and a size via a deduction guide (C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) )
char const * argv[] = { "prog", "arg1", "arg2" };
int const argc = DIMENSION_OF( argv );
span args( &argv[0], argc );
EXPECT( args.size() == DIMENSION_OF( argv ) );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to construct from an iterator and a size via a deduction guide (C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) )
std::vector<int> v( 5, 123 );
span spn( v.begin(), v.size() );
EXPECT( spn.size() == v.size() );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to construct from two iterators via a deduction guide (C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) )
std::vector<int> v( 5, 123 );
span spn( v.begin(), v.end() );
EXPECT( spn.size() == v.size() );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to construct from a C-array" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( arr );
span<const int> w( arr );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from a C-array via a deduction guide (C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) ) && !span_BETWEEN( _MSC_VER, 1, 1920 )
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span v( arr );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to construct from a const C-array" )
{
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span<const int> v( arr );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
}
CASE( "span<>: Allows to construct from a C-array with size via decay to pointer (potentially dangerous)" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
{
span< int> v( &arr[0], DIMENSION_OF(arr) );
span<const int> w( &arr[0], DIMENSION_OF(arr) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0] ) );
}
#if span_CPP14_OR_GREATER
{
span< int> v( &arr[0], 3 );
span<const int> w( &arr[0], 3 );
EXPECT( std::equal( v.begin(), v.end(), &arr[0], &arr[0] + 3 ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0], &arr[0] + 3 ) );
}
#endif
}
CASE( "span<>: Allows to construct from a const C-array with size via decay to pointer (potentially dangerous)" )
{
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
{
span<const int> v( &arr[0], DIMENSION_OF(arr) );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
}
#if span_CPP14_OR_GREATER
{
span<const int> w( &arr[0], 3 );
EXPECT( std::equal( w.begin(), w.end(), &arr[0], &arr[0] + 3 ) );
}
#endif
}
CASE( "span<>: Allows to construct from a std::initializer_list<> (C++11)" )
{
#if span_STD_OR( span_HAVE( INITIALIZER_LIST ) )
#if span_STD_OR( span_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) )
SETUP("") {
SECTION("empty initializer list") {
std::initializer_list<int const> il = {};
span<int const> v( il );
#if span_BETWEEN( span_COMPILER_MSVC_VERSION, 120, 130 )
EXPECT( v.size() == 0u );
#else
EXPECT( std::equal( v.begin(), v.end(), il.begin() ) );
#endif
}
SECTION("non-empty initializer list") {
auto il = { 1, 2, 3, 4, 5, };
span<int const> v( il );
EXPECT( std::equal( v.begin(), v.end(), il.begin() ) );
}}
#else
EXPECT( !!"constrained construction from container is not available" );
#endif
#else
EXPECT( !!"std::initializer_list<> is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from a std::initializer_list<> as a constant set of values (C++11, p2447)" )
{
#if span_HAVE( INITIALIZER_LIST )
#if span_NONSTD_AND( span_FEATURE( WITH_INITIALIZER_LIST_P2447 ) )
SETUP("") {
SECTION("empty initializer list") {
std::initializer_list<int const> il = {};
auto f = [&]( span<const int> spn ) {
#if span_BETWEEN( span_COMPILER_MSVC_VERSION, 120, 130 )
EXPECT( spn.size() == 0u );
#else
EXPECT( std::equal( spn.begin(), spn.end(), il.begin() ) );
#endif
};
f({});
}
SECTION("non-empty initializer list") {
auto il = { 1, 2, 3, 4, 5, };
auto f = [&]( span<const int> spn ) {
EXPECT( std::equal( spn.begin(), spn.end(), il.begin() ) );
};
f({ 1, 2, 3, 4, 5, });
}}
#else
EXPECT( !!"construction from std::initializer_list is not available (span_FEATURE_WITH_INITIALIZER_LIST_P2447, or using std::span)" );
#endif
#else
EXPECT( !!"std::initializer_list<> is not available, or std::span<> (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from a std::array<> (C++11)" )
{
#if span_STD_OR( span_HAVE( ARRAY ) )
std::array<int,9> arr = {{ 1, 2, 3, 4, 5, 6, 7, 8, 9, }};
span< int> v( arr );
span<const int> w( arr );
EXPECT( std::equal( v.begin(), v.end(), arr.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), arr.begin() ) );
#else
EXPECT( !!"std::array<> is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from a std::array via a deduction guide (C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) ) && !span_BETWEEN( _MSC_VER, 1, 1920 )
std::array<int,9> arr = {{ 1, 2, 3, 4, 5, 6, 7, 8, 9, }};
span v( arr );
EXPECT( std::equal( v.begin(), v.end(), arr.begin() ) );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to construct from a std::array<> with const data (C++11, span_FEATURE_CONSTR..._ELEMENT_TYPE=1)" )
{
#if span_FEATURE( CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE )
# if span_HAVE( ARRAY )
std::array<const int,9> arr = {{ 1, 2, 3, 4, 5, 6, 7, 8, 9, }};
span<const int> v( arr );
span<const int> const w( arr );
EXPECT( std::equal( v.begin(), v.end(), arr.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), arr.begin() ) );
# else
EXPECT( !!"std::array<> is not available (no C++11)" );
# endif
#else
EXPECT( !!"construction is not available (span_FEATURE_CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE=0)" );
#endif
}
CASE( "span<>: Allows to construct from an empty std::array<> (C++11)" )
{
#if span_STD_OR( span_HAVE( ARRAY ) )
std::array<int,0> arr;
span< int> v( arr );
span<const int> w( arr );
EXPECT( std::equal( v.begin(), v.end(), arr.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), arr.begin() ) );
#else
EXPECT( !!"std::array<> is not available (no C++11)" );
#endif
}
CASE( "span<>: Allows to construct from a container (std::vector<>)" )
{
#if span_STD_OR( span_HAVE( INITIALIZER_LIST ) )
std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
#else
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
std::vector<int> vec( arr, &arr[0] + DIMENSION_OF(arr) );
#endif
#if span_STD_OR( span_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) )
span< int> v( vec );
span<const int> w( vec );
EXPECT( std::equal( v.begin(), v.end(), vec.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), vec.begin() ) );
#else
EXPECT( !!"constrained construction from container is not available" );
#endif
}
CASE( "span<>: Allows to construct from a container via a deduction guide (std::vector<>, C++17)" )
{
#if span_STD_OR( span_HAVE( DEDUCTION_GUIDES ) ) && !span_BETWEEN( _MSC_VER, 1, 1920 )
std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span v( vec );
EXPECT( std::equal( v.begin(), v.end(), vec.begin() ) );
#else
EXPECT( !!"deduction guide is not available (no C++17)" );
#endif
}
CASE( "span<>: Allows to tag-construct from a container (std::vector<>)" )
{
#if span_NONSTD_AND( span_FEATURE_TO_STD( WITH_CONTAINER ) )
# if span_HAVE( INITIALIZER_LIST )
std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
# else
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
std::vector<int> vec( arr, &arr[0] + DIMENSION_OF(arr) );
# endif
span< int> v( with_container, vec );
span<const int> w( with_container, vec );
EXPECT( std::equal( v.begin(), v.end(), vec.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), vec.begin() ) );
#else
EXPECT( !!"with_container is not available (span_FEATURE_WITH_CONTAINER_TO_STD, or using std::span)" );
#endif
}
CASE( "span<>: Allows to tag-construct from a const container (std::vector<>)" )
{
#if span_NONSTD_AND( span_FEATURE_TO_STD( WITH_CONTAINER ) )
# if span_HAVE( INITIALIZER_LIST )
const std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
# else
const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
const std::vector<int> vec( arr, &arr[0] + DIMENSION_OF(arr) );
# endif
span< int> v( with_container, vec );
span<const int> w( with_container, vec );
EXPECT( std::equal( v.begin(), v.end(), vec.begin() ) );
EXPECT( std::equal( w.begin(), w.end(), vec.begin() ) );
#else
EXPECT( !!"with_container is not available (span_FEATURE_WITH_CONTAINER_TO_STD, or using std::span)" );
#endif
}
CASE( "span<>: Allows to copy-construct from another span of the same type" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( arr );
span<const int> w( arr );
span< int> x( v );
span<const int> y( w );
EXPECT( std::equal( x.begin(), x.end(), &arr[0] ) );
EXPECT( std::equal( y.begin(), y.end(), &arr[0] ) );
}
CASE( "span<>: Allows to copy-construct from another span of a compatible type" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( arr );
span<const int> w( arr );
span<const volatile int> x( v );
span<const volatile int> y( v );
#ifndef _LIBCPP_VERSION
EXPECT( std::equal( x.begin(), x.end(), &arr[0] ) );
EXPECT( std::equal( y.begin(), y.end(), &arr[0] ) );
#else
for(size_t i = 0; i < x.size(); ++i)
EXPECT(x[i] == arr[i]);
for(size_t i = 0; i < y.size(); ++i)
EXPECT(y[i] == arr[i]);
#endif
}
CASE( "span<>: Allows to copy-construct from a temporary span of the same type (C++11)" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v(( span< int>( arr ) ));
// span< int> w(( span<const int>( arr ) ));
span<const int> x(( span< int>( arr ) ));
span<const int> y(( span<const int>( arr ) ));
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( x.begin(), x.end(), &arr[0] ) );
EXPECT( std::equal( y.begin(), y.end(), &arr[0] ) );
}
CASE( "span<>: Allows to copy-assign from another span of the same type" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v( arr );
span< int> s;
span<const int> t;
s = v;
t = v;
EXPECT( std::equal( s.begin(), s.end(), &arr[0] ) );
EXPECT( std::equal( t.begin(), t.end(), &arr[0] ) );
}
CASE( "span<>: Allows to copy-assign from a temporary span of the same type (C++11)" )
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };
span< int> v;
span<const int> w;
v = span<int>( arr );
w = span<int>( arr );
EXPECT( std::equal( v.begin(), v.end(), &arr[0] ) );
EXPECT( std::equal( w.begin(), w.end(), &arr[0] ) );
}
CASE( "span<>: Allows to create a sub span of the first n elements" )
{
int arr[] = { 1, 2, 3, 4, 5, };
span<int> v( arr );
size_type count = 3;
span< int> s = v.first( count );
span<const int> t = v.first( count );
EXPECT( s.size() == count );
EXPECT( t.size() == count );
EXPECT( std::equal( s.begin(), s.end(), &arr[0] ) );
EXPECT( std::equal( t.begin(), t.end(), &arr[0] ) );
}
CASE( "span<>: Allows to create a sub span of the last n elements" )
{
int arr[] = { 1, 2, 3, 4, 5, };
span<int> v( arr );
size_type count = 3;
span< int> s = v.last( count );
span<const int> t = v.last( count );
EXPECT( s.size() == count );
EXPECT( t.size() == count );
EXPECT( std::equal( s.begin(), s.end(), &arr[0] + v.size() - count ) );
EXPECT( std::equal( t.begin(), t.end(), &arr[0] + v.size() - count ) );
}
CASE( "span<>: Allows to create a sub span starting at a given offset" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
size_type offset = 1;
span< int> s = v.subspan( offset );
span<const int> t = v.subspan( offset );
EXPECT( s.size() == v.size() - offset );
EXPECT( t.size() == v.size() - offset );
EXPECT( std::equal( s.begin(), s.end(), &arr[0] + offset ) );
EXPECT( std::equal( t.begin(), t.end(), &arr[0] + offset ) );
}
CASE( "span<>: Allows to create a sub span starting at a given offset with a given length" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
size_type offset = 1;
size_type length = 1;
span< int> s = v.subspan( offset, length );
span<const int> t = v.subspan( offset, length );
EXPECT( s.size() == length );
EXPECT( t.size() == length );
EXPECT( std::equal( s.begin(), s.end(), &arr[0] + offset ) );
EXPECT( std::equal( t.begin(), t.end(), &arr[0] + offset ) );
}
//CASE( "span<>: Allows to create an empty sub span at full offset" )
//{
// int arr[] = { 1, 2, 3, };
// span<int> v( arr );
// size_type offset = v.size() - 1;
//
// span< int> s = v.subspan( offset );
// span<const int> t = v.subspan( offset );
//
// EXPECT( s.empty() );
// EXPECT( t.empty() );
//}
//CASE( "span<>: Allows to create an empty sub span at full offset with zero length" )
//{
// int arr[] = { 1, 2, 3, };
// span<int> v( arr );
// size_type offset = v.size();
// size_type length = 0;
//
// span< int> s = v.subspan( offset, length );
// span<const int> t = v.subspan( offset, length );
//
// EXPECT( s.empty() );
// EXPECT( t.empty() );
//}
CASE( "span<>: Allows to observe an element via array indexing" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
for ( size_type i = 0; i < v.size(); ++i )
{
EXPECT( v[i] == arr[i] );
EXPECT( w[i] == arr[i] );
}
}
CASE( "span<>: Allows to observe an element via call indexing" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_CALL_OPERATOR ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
for ( size_type i = 0; i < v.size(); ++i )
{
EXPECT( v(i) == arr[i] );
EXPECT( w(i) == arr[i] );
}
#else
EXPECT( !!"member () is not available (span_FEATURE_MEMBER_CALL_OPERATOR=0, or using std::span)" );
#endif
}
CASE( "span<>: Allows to observe an element via at() [span_FEATURE_MEMBER_AT>0]" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_AT ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
for ( size_type i = 0; i < v.size(); ++i )
{
EXPECT( v.at(i) == arr[i] );
EXPECT( w.at(i) == arr[i] );
}
#else
EXPECT( !!"member at() is not available (span_FEATURE_MEMBER_AT=0, or using std::span)" );
#endif
}
CASE( "span<>: Allows to observe an element via data()" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
EXPECT( *v.data() == *v.begin() );
EXPECT( *w.data() == *v.begin() );
for ( size_type i = 0; i < v.size(); ++i )
{
EXPECT( v.data()[i] == arr[i] );
EXPECT( w.data()[i] == arr[i] );
}
}
CASE( "span<>: Allows to observe the first element via front() [span_FEATURE_MEMBER_BACK_FRONT=1]" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_BACK_FRONT ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
EXPECT( v.front() == 1 );
#else
EXPECT( !!"front() is not available (span_FEATURE_MEMBER_BACK_FRONT undefined or 0)" );
#endif
}
CASE( "span<>: Allows to observe the last element via back() [span_FEATURE_MEMBER_BACK_FRONT=1]" )
{
#if span_FEATURE( MEMBER_BACK_FRONT )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
EXPECT( v.back() == 3 );
#else
EXPECT( !!"back()is not available (span_FEATURE_MEMBER_BACK_FRONT undefined or 0)" );
#endif
}
CASE( "span<>: Allows to change an element via array indexing" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
v[1] = 22;
w[2] = 33;
EXPECT( 22 == arr[1] );
EXPECT( 33 == arr[2] );
}
CASE( "span<>: Allows to change an element via call indexing" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_CALL_OPERATOR ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
v(1) = 22;
w(2) = 33;
EXPECT( 22 == arr[1] );
EXPECT( 33 == arr[2] );
#else
EXPECT( !!"member () is not available (span_FEATURE_MEMBER_CALL_OPERATOR=0, or using std::span)" );
#endif
}
CASE( "span<>: Allows to change an element via at() [span_FEATURE_MEMBER_AT>0]" )
{
#if span_NONSTD_AND( span_FEATURE( MEMBER_AT ) )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
v.at(1) = 22;
w.at(2) = 33;
EXPECT( 22 == arr[1] );
EXPECT( 33 == arr[2] );
#else
EXPECT( !!"member at() is not available (span_FEATURE_MEMBER_AT=0, or using std::span)" );
#endif
}
CASE( "span<>: Allows to change an element via data()" )
{
int arr[] = { 1, 2, 3, };
span<int> v( arr );
span<int> const w( arr );
*v.data() = 22;
EXPECT( 22 == *v.data() );
*w.data() = 33;
EXPECT( 33 == *w.data() );
}
CASE( "span<>: Allows to change the first element via front() [span_FEATURE_MEMBER_BACK_FRONT=1]" )
{
#if span_FEATURE( MEMBER_BACK_FRONT )
int arr[] = { 1, 2, 3, };
span<int> v( arr );
v.front() = 42;