-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_algorithm_test.cpp
More file actions
971 lines (834 loc) · 27.2 KB
/
Copy pathdaw_algorithm_test.cpp
File metadata and controls
971 lines (834 loc) · 27.2 KB
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
// Copyright (c) Darrell Wright
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/beached/header_libraries
//
#include "daw/algorithms/daw_algorithm_find_transform.h"
#include "daw/daw_algorithm.h"
#include "daw/daw_algorithm_cx.h"
#include "daw/daw_benchmark.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <functional>
#include <iterator>
#include <unordered_map>
constexpr bool daw_safe_advance_test_001( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, 5 );
bool const ans = it != ::std::begin( a );
daw::expecting( ans );
auto it2 = ::std::next( ::std::begin( a ), 5 );
bool const ans2 = it == it2;
daw::expecting( ans2 );
return true;
}
static_assert( daw_safe_advance_test_001( ) );
constexpr bool binary_search_001( ) noexcept {
int arry[10] = { 1, 5, 10, 15, 16, 17, 18, 19, 20, 21 };
auto pos = daw::algorithm::binary_search( arry, arry + 10, 17 );
return *pos == 17;
}
static_assert( binary_search_001( ) );
constexpr bool lower_bound_001( ) noexcept {
std::array<int, 10> arry = { 1, 5, 10, 15, 16, 17, 18, 19, 20, 21 };
auto pos =
daw::algorithm::lower_bound( ::std::begin( arry ), ::std::end( arry ), 17 );
return *pos == 17;
}
static_assert( lower_bound_001( ) );
constexpr bool daw_safe_advance_test_002( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, static_cast<ptrdiff_t>( a.size( ) + 5u ) );
bool const ans = ( it == ::std::end( a ) );
daw::expecting( ans );
return true;
}
static_assert( daw_safe_advance_test_002( ) );
constexpr bool daw_safe_advance_test_003( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, -5 );
bool const ans = ( it == ::std::begin( a ) );
daw::expecting( ans );
return true;
}
static_assert( daw_safe_advance_test_003( ) );
constexpr bool daw_safe_advance_test_004( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, 5 );
bool const ans = ( it != ::std::begin( a ) );
daw::expecting( ans );
auto it2 = ::std::next( ::std::begin( a ), 5 );
bool const ans2 = it == it2;
daw::expecting( ans2 );
return true;
}
static_assert( daw_safe_advance_test_004( ) );
constexpr bool daw_safe_advance_test_005( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, static_cast<ptrdiff_t>( a.size( ) + 5u ) );
bool const ans = ( it == ::std::end( a ) );
daw::expecting( ans );
return true;
}
static_assert( daw_safe_advance_test_005( ) );
constexpr bool daw_safe_advance_test_006( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = ::std::begin( a );
daw::safe_advance( a, it, -5 );
bool const ans = ( it == ::std::begin( a ) );
daw::expecting( ans );
return true;
}
static_assert( daw_safe_advance_test_006( ) );
constexpr bool daw_safe_next_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_next( ::std::begin( a ), ::std::end( a ), 5 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in safe_next" );
daw::expecting( it, ::std::next( ::std::begin( a ), 5 ) );
return true;
}
static_assert( daw_safe_next_test_001( ) );
constexpr bool daw_safe_next_test_002( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_next( ::std::begin( a ), ::std::end( a ), a.size( ) + 5 );
daw::expecting( it, ::std::end( a ) );
return true;
}
static_assert( daw_safe_next_test_002( ) );
constexpr bool daw_safe_next_test_003( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_next( ::std::begin( a ), ::std::end( a ), 5 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in safe_next" );
daw::expecting( it, ::std::next( ::std::begin( a ), 5 ) );
return true;
}
static_assert( daw_safe_next_test_003( ) );
constexpr bool daw_safe_next_test_004( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_next( ::std::begin( a ), ::std::end( a ), a.size( ) + 5 );
daw::expecting( it, ::std::end( a ) );
return true;
}
static_assert( daw_safe_next_test_004( ) );
constexpr bool daw_safe_prev_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_prev( ::std::end( a ), ::std::begin( a ), 5 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in safe_prev" );
daw::expecting( it, ::std::prev( ::std::end( a ), 5 ) );
return true;
}
static_assert( daw_safe_prev_test_001( ) );
constexpr bool daw_safe_prev_test_002( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_prev( a.data( ) + a.size( ), a.data( ), a.size( ) + 5 );
daw::expecting( it, a.data( ) );
return true;
}
static_assert( daw_safe_prev_test_002( ) );
constexpr bool daw_safe_prev_test_003( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_prev( ::std::end( a ), ::std::begin( a ), 5 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in safe_prev" );
daw::expecting( it, ::std::prev( ::std::end( a ), 5 ) );
return true;
}
static_assert( daw_safe_prev_test_003( ) );
constexpr bool daw_safe_prev_test_004( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::safe_prev( ::std::end( a ), ::std::begin( a ), a.size( ) + 5 );
daw::expecting( it, ::std::begin( a ) );
return true;
}
static_assert( daw_safe_prev_test_004( ) );
constexpr bool daw_begin_at_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::begin_at( a, 0 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in begin_at" );
daw::expecting( it, ::std::begin( a ) );
return true;
}
static_assert( daw_begin_at_test_001( ) );
constexpr bool daw_begin_at_test_002( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::begin_at( a, 5 );
daw::expecting( it, ::std::next( ::std::begin( a ), 5 ) );
return true;
}
static_assert( daw_begin_at_test_002( ) );
constexpr bool daw_begin_at_test_003( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::begin_at( a, 0 );
static_assert(
::std::is_same_v<decltype( it ), decltype( ::std::begin( a ) )>,
"Iterator type is changing in begin_at" );
daw::expecting( it, ::std::begin( a ) );
return true;
}
static_assert( daw_begin_at_test_003( ) );
constexpr bool daw_begin_at_test_004( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto it = daw::begin_at( a, 5 );
daw::expecting( it, ::std::next( ::std::begin( a ), 5 ) );
return true;
}
static_assert( daw_begin_at_test_004( ) );
/*
constexpr bool daw_transform_many( ) {
std::array<uint32_t, 5> in1 = {1, 3, 5, 7, 9};
std::array<uint32_t, 5> in2 = {0, 2, 4, 6, 8};
std::array<uint32_t, 5> out{};
daw::algorithm::transform_many(
begin( in1 ), end( in1 ), begin( in2 ), out.data( ),
[]( auto const &v1, auto const &v2 ) { return v1 + v2; } );
daw::expecting_message( out.size( ) == in1.size( ),
"1. Incorrect size on output" );
return true;
}
static_assert( daw_transform_many( ) );
*/
constexpr bool daw_map_test_001( ) {
std::array<int, 6> blah = { 23, 5, 2, -1, 100, -1000 };
daw::algorithm::map(
blah.cbegin( ), blah.cend( ), blah.begin( ), []( auto val ) {
return val % 2 == 0 ? 0 : 1;
} );
int sum = 0;
for( auto v : blah ) {
sum += v;
}
daw::expecting( 3, sum );
return true;
}
static_assert( daw_map_test_001( ) );
constexpr bool daw_map_test_002( ) {
std::array<int, 6> blah = { 23, 5, 2, -1, 100, -1000 };
struct {
constexpr int operator( )( int val ) const noexcept {
return val % 2 == 0 ? 0 : 1;
}
} unary_op{ };
daw::algorithm::map( blah.begin( ), blah.end( ), blah.begin( ), unary_op );
int result = 0;
for( size_t n = 0; n < 6; ++n ) {
result += blah[n];
}
daw::expecting( 3, result );
return true;
}
static_assert( daw_map_test_002( ) );
constexpr bool daw_reduce_test_001( ) {
int blah[6] = { 1, 0, 1, 0, 1, 0 };
auto const tst = daw::algorithm::reduce(
blah, daw::next( blah, 6 ), 0, []( auto lhs, auto rhs ) noexcept {
return lhs + rhs;
} );
daw::expecting( 3, tst );
return true;
}
static_assert( daw_reduce_test_001( ) );
constexpr bool daw_minmax_element_test_001( ) {
std::array<int, 9> const tst = { 1, 3, 2, -1, 100, -50, 1, 5, 2442 };
auto result = daw::algorithm::minmax_element( tst.cbegin( ), tst.cend( ) );
daw::expecting( -50, *( result.min_element ) );
daw::expecting( 2442, *( result.max_element ) );
return true;
}
static_assert( daw_minmax_element_test_001( ) );
constexpr bool daw_satisfies_one_test_001( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_one(
tst,
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 11 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 12 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 13 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 5 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 15 ) != v.cend( );
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( ans );
return true;
}
static_assert( daw_satisfies_one_test_001( ) );
constexpr bool daw_satisfies_one_test_002( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_one(
tst,
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 11 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 12 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 13 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 5 ) != v.cend( );
},
[]( auto const &v ) {
return daw::algorithm::find( v.cbegin( ), v.cend( ), 15 ) != v.cend( );
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( !ans );
return true;
}
static_assert( daw_satisfies_one_test_002( ) );
constexpr bool daw_satisfies_one_test_003( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_one(
tst.begin( ),
tst.end( ),
[]( auto v ) {
return v == 11;
},
[]( auto v ) {
return v == 12;
},
[]( auto v ) {
return v == 13;
},
[]( auto v ) {
return v == 14;
},
[]( auto v ) {
return v == 15;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( !ans );
return true;
}
static_assert( daw_satisfies_one_test_003( ) );
constexpr bool daw_satisfies_one_test_004( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_one(
tst.begin( ),
tst.end( ),
[]( auto v ) {
return v == 11;
},
[]( auto v ) {
return v == 12;
},
[]( auto v ) {
return v == 13;
},
[]( auto v ) {
return v == 5;
},
[]( auto v ) {
return v == 15;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( ans );
return true;
}
static_assert( daw_satisfies_one_test_004( ) );
constexpr bool daw_satisfies_all_test_001( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_all(
tst,
[]( auto const &v ) {
return v.size( ) == 11;
},
[]( auto const &v ) {
return v.front( ) == 0;
},
[]( auto const &v ) {
return v.back( ) == 10;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( ans );
return true;
}
static_assert( daw_satisfies_all_test_001( ) );
constexpr bool daw_satisfies_all_test_002( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_all(
tst,
[]( auto const &v ) {
return v.size( ) == 11;
},
[]( auto const &v ) {
return v.front( ) == 0;
},
[]( auto const &v ) {
return v.back( ) == 11;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( !ans );
return true;
}
static_assert( daw_satisfies_all_test_002( ) );
constexpr bool daw_satisfies_all_test_003( ) {
std::array<int, 6> const tst = { 0, 2, 4, 6, 8, 10 };
auto const ans = daw::algorithm::satisfies_all(
tst.begin( ),
tst.end( ),
[]( auto v ) {
return v < 11;
},
[]( auto v ) {
return v >= 0;
},
[]( auto v ) {
return v % 2 == 0;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( ans );
return true;
}
static_assert( daw_satisfies_all_test_003( ) );
constexpr bool daw_satisfies_all_test_004( ) {
std::array<int, 11> const tst = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const ans = daw::algorithm::satisfies_all(
tst.begin( ),
tst.end( ),
[]( auto v ) {
return v < 11;
},
[]( auto v ) {
return v >= 0;
},
[]( auto v ) {
return v % 2 == 0;
} );
static_assert( ::std::is_same_v<bool, daw::remove_cvref_t<decltype( ans )>> );
daw::expecting( !ans );
return true;
}
static_assert( daw_satisfies_all_test_004( ) );
constexpr bool daw_in_range_test_001( ) {
auto tst = daw::algorithm::in_range( 1, 5 );
daw::expecting( tst( 4 ) );
daw::expecting( tst( 1 ) );
daw::expecting( tst( 5 ) );
daw::expecting( !tst( 0 ) );
daw::expecting( !tst( 6 ) );
daw::expecting( !tst( 14 ) );
return true;
}
static_assert( daw_in_range_test_001( ) );
constexpr bool daw_equal_to_test_001( ) {
auto tst = daw::algorithm::equal_to( 5 );
daw::expecting( tst( 5 ) );
daw::expecting( !tst( -1 ) );
return true;
}
static_assert( daw_equal_to_test_001( ) );
constexpr bool daw_greater_than_test_001( ) {
auto tst = daw::algorithm::greater_than( 5 );
daw::expecting( tst( 6 ) );
daw::expecting( !tst( 5 ) );
daw::expecting( !tst( -1 ) );
return true;
}
static_assert( daw_greater_than_test_001( ) );
constexpr bool daw_greater_than_or_equal_to_test_001( ) {
auto tst = daw::algorithm::greater_than_or_equal_to( 5 );
daw::expecting( tst( 6 ) );
daw::expecting( tst( 5 ) );
daw::expecting( !tst( -1 ) );
return true;
}
static_assert( daw_greater_than_or_equal_to_test_001( ) );
constexpr bool daw_less_than_test_001( ) {
auto tst = daw::algorithm::less_than( 5 );
daw::expecting( !tst( 6 ) );
daw::expecting( !tst( 5 ) );
daw::expecting( tst( -1 ) );
return true;
}
static_assert( daw_less_than_test_001( ) );
constexpr bool daw_less_than_or_equal_to_test_001( ) {
auto tst = daw::algorithm::less_than_or_equal_to( 5 );
daw::expecting( !tst( 6 ) );
daw::expecting( tst( 5 ) );
daw::expecting( tst( -1 ) );
return true;
}
static_assert( daw_less_than_or_equal_to_test_001( ) );
constexpr bool daw_lexigraphical_compare_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 10> const b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto ans = daw::algorithm::lexicographical_compare(
a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) );
daw::expecting( ans );
return true;
}
static_assert( daw_lexigraphical_compare_test_001( ) );
constexpr bool daw_lexigraphical_compare_test_002( ) {
std::array<int, 10> const a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> const b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto ans = daw::algorithm::lexicographical_compare(
a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) );
daw::expecting( !ans );
return true;
}
static_assert( daw_lexigraphical_compare_test_002( ) );
constexpr bool daw_transform_if_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 6> b{ };
std::array<int, 6> const expected_b = { -1, 1, 3, 5, 7, 9 };
daw::algorithm::transform_if(
a.cbegin( ),
a.cend( ),
b.data( ),
[]( auto const &v ) {
return v % 2 == 0;
},
[]( auto const &v ) {
return v - 1;
} );
daw::expecting( daw::algorithm::equal(
b.cbegin( ), b.cend( ), expected_b.cbegin( ), expected_b.cend( ) ) );
return true;
}
static_assert( daw_transform_if_test_001( ) );
constexpr bool daw_transform_n_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 4> b{ -1, -1, -1, -1 };
std::array<int, 4> const expected_b = { 0, 2, 4, 6 };
daw::algorithm::transform_n( a.cbegin( ), b.data( ), 4, []( auto const &v ) {
return v * 2;
} );
daw::expecting( daw::algorithm::equal(
b.cbegin( ), b.cend( ), expected_b.cbegin( ), expected_b.cend( ) ) );
return true;
}
static_assert( daw_transform_n_test_001( ) );
constexpr bool daw_transform_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> b{ };
std::array<int, 11> const expected_b = {
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
daw::algorithm::transform(
a.cbegin( ), a.cend( ), b.data( ), []( auto const &v ) {
return v * 2;
} );
daw::expecting( daw::algorithm::equal(
b.cbegin( ), b.cend( ), expected_b.cbegin( ), expected_b.cend( ) ) );
return true;
}
static_assert( daw_transform_test_001( ) );
constexpr bool daw_transform_it_test_001( ) {
std::array<int, 6> a = { 1, 2, 3, 4, 5, 6 };
std::array<int, 21> b{ };
std::array<int, 21> const expected_b = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 5, 5, 6, 6, 6, 6, 6, 6 };
daw::algorithm::transform_it(
a.cbegin( ), a.cend( ), b.data( ), []( int a_val, auto out_it ) {
for( size_t n = 0; static_cast<int>( n ) < a_val; ++n ) {
*out_it++ = a_val;
}
return out_it;
} );
daw::expecting( daw::algorithm::equal(
expected_b.begin( ), expected_b.end( ), b.begin( ) ) );
return true;
}
static_assert( daw_transform_it_test_001( ) );
constexpr bool daw_copy_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> b{ };
daw::algorithm::copy( a.cbegin( ), a.cend( ), b.data( ) );
daw::expecting(
daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) ) );
return true;
}
static_assert( daw_copy_test_001( ) );
constexpr bool daw_copy_n_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 6> b{ };
std::array<int, 6> const expected_b = { 0, 1, 2, 3, 4, 5 };
daw::algorithm::copy_n( a.cbegin( ), b.data( ), 6 );
daw::expecting( daw::algorithm::equal(
b.cbegin( ), b.cend( ), expected_b.cbegin( ), expected_b.cend( ) ) );
return true;
}
static_assert( daw_copy_n_test_001( ) );
constexpr bool daw_move_test_001( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto expected_b = a;
std::array<int, 11> b{ };
daw::algorithm::move( a.begin( ), a.end( ), b.data( ) );
daw::expecting( daw::algorithm::equal(
expected_b.cbegin( ), expected_b.cend( ), b.cbegin( ), b.cend( ) ) );
return true;
}
static_assert( daw_move_test_001( ) );
constexpr bool daw_move_n_test_001( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 6> b{ };
std::array<int, 6> const expected_b = { 0, 1, 2, 3, 4, 5 };
daw::algorithm::move_n( a.begin( ), b.data( ), 6 );
daw::expecting( daw::algorithm::equal(
b.cbegin( ), b.cend( ), expected_b.cbegin( ), expected_b.cend( ) ) );
return true;
}
static_assert( daw_move_n_test_001( ) );
constexpr bool daw_equal_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const b = a;
daw::expecting(
daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ) ) );
return true;
}
static_assert( daw_equal_test_001( ) );
constexpr bool daw_equal_test_002( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> const b = { 0, 1, 2, 3, 4, 1, 6, 7, 8, 9, 10 };
daw::expecting(
!daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ) ) );
return true;
}
static_assert( daw_equal_test_002( ) );
constexpr bool daw_equal_test_003( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto const b = a;
daw::expecting(
daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) ) );
return true;
}
static_assert( daw_equal_test_003( ) );
constexpr bool daw_equal_test_004( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> const b = { 0, 1, 2, 3, 4, 1, 6, 7, 8, 9, 10 };
daw::expecting(
!daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) ) );
return true;
}
static_assert( daw_equal_test_004( ) );
constexpr bool daw_equal_test_005( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 5> const b = { 0, 1, 2, 3, 4 };
daw::expecting(
!daw::algorithm::equal( a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ) ) );
return true;
}
static_assert( daw_equal_test_005( ) );
constexpr bool daw_equal_test_006( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> const b = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
daw::expecting( daw::algorithm::equal(
a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ), []( auto lhs, auto rhs ) {
return lhs == rhs;
} ) );
return true;
}
static_assert( daw_equal_test_006( ) );
constexpr bool daw_equal_test_007( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 11> const b = { 0, 1, 2, 5, 4, 5, 6, 7, 8, 9, 10 };
daw::expecting( !daw::algorithm::equal(
a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ), []( auto lhs, auto rhs ) {
return lhs == rhs;
} ) );
return true;
}
static_assert( daw_equal_test_007( ) );
constexpr bool daw_equal_test_008( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::array<int, 10> const b = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10 };
daw::expecting( !daw::algorithm::equal(
a.cbegin( ), a.cend( ), b.cbegin( ), b.cend( ), []( auto lhs, auto rhs ) {
return lhs == rhs;
} ) );
return true;
}
static_assert( daw_equal_test_008( ) );
constexpr bool daw_rotate_test_001( ) {
std::array<int, 11> a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
daw::algorithm::rotate( a.begin( ), ::std::next( a.begin( ), 5 ), a.end( ) );
daw::expecting( 5, a.front( ) );
daw::expecting( 4, a.back( ) );
daw::expecting( 10, a[5] );
return true;
}
static_assert( daw_rotate_test_001( ) );
constexpr bool daw_upper_bound_test_001( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto tst = daw::algorithm::upper_bound( a.cbegin( ), a.cend( ), 5 );
daw::expecting( tst, ::std::next( a.cbegin( ), 6 ) );
return true;
}
static_assert( daw_upper_bound_test_001( ) );
constexpr bool daw_upper_bound_test_002( ) {
std::array<int, 11> const a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto tst = daw::algorithm::upper_bound( a.cbegin( ), a.cend( ), 20 );
daw::expecting( tst, a.cend( ) );
return true;
}
static_assert( daw_upper_bound_test_002( ) );
constexpr bool daw_minmax_item_test_001( ) {
int a = 5;
int b = -100;
auto ans = daw::algorithm::minmax_item( a, b );
daw::expecting( -100, ans.first );
daw::expecting( 5, ans.second );
return true;
}
static_assert( daw_minmax_item_test_001( ) );
constexpr bool daw_minmax_item_test_002( ) {
struct A {
int v;
};
A a{ 5 };
A b{ -100 };
auto ans =
daw::algorithm::minmax_item( a, b, []( auto const &lhs, auto const &rhs ) {
return lhs.v < rhs.v;
} );
daw::expecting( -100, ans.first.v );
daw::expecting( 5, ans.second.v );
return true;
}
static_assert( daw_minmax_item_test_002( ) );
constexpr bool cartesian_product_test_001( ) {
std::array<int, 5> a = { 1, 2, 3, 4, 5 };
std::array<int, 5> b = { 9, 8, 7, 6, 5 };
std::array<int, 5> c = { 10, 20, 30, 40, 50 };
// Sum of all is 200
int sum = 0;
daw::algorithm::cartesian_product(
[&sum]( auto... vals ) {
sum += ( vals + ... );
},
begin( a ),
end( a ),
begin( b ),
begin( c ) );
daw::expecting( 200, sum );
return true;
}
static_assert( cartesian_product_test_001( ) );
void daw_extract_to_001( ) {
#if defined( __cpp_lib_node_extract )
std::unordered_map<int, int> a = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
std::unordered_map<int, int> b{ };
daw::expecting( a.size( ) == 3 );
daw::expecting( b.empty( ) );
daw::algorithm::extract_to( a, b, 3 );
daw::expecting( a.size( ) == 2 );
daw::expecting( b.size( ) == 1 );
std::pair<int const, int> c( 3, 4 );
daw::expecting( !b.empty( ) );
daw::expecting( *std::begin( b ) == c );
daw::algorithm::extract_all( a, b );
daw::expecting( a.empty( ) );
daw::expecting( b.size( ) == 3 );
#endif
}
constexpr bool do_n_1_test( ) {
int n = 0;
daw::algorithm::do_n<20>( [&n] {
++n;
} );
daw::expecting( 20, n );
return true;
}
static_assert( do_n_1_test( ) );
constexpr bool do_n_2_test( ) {
int n = 0;
daw::algorithm::do_n( 100, [&n] {
++n;
} );
daw::expecting( 100, n );
return true;
}
static_assert( do_n_2_test( ) );
constexpr bool find_index_test( ) {
std::array ary = { 1, 2, 3, 4 };
auto const f0 = daw::algorithm::find_index( ary.begin( ), ary.end( ), 3 );
daw::expecting( f0 );
daw::expecting( *f0, 2U );
auto const f1 = daw::algorithm::find_index( ary.begin( ), ary.end( ), 100 );
daw::expecting( not f1 );
return true;
}
static_assert( find_index_test( ) );
constexpr bool adjacent_find_test( ) {
std::array ary = { 1, 2, 3, 4 };
auto const item = daw::algorithm::adjacent_find(
ary.begin( ), ary.end( ), []( auto lhs, auto rhs ) {
return lhs == rhs;
} );
return item == ary.end( );
}
static_assert( adjacent_find_test( ) );
constexpr bool find_some_test( ) {
auto ary =
std::array{ 1, 2, 4, 8, 16, 15, 14, 13, 12, 11, 10, 9, 7, 6, 5, 3 };
auto is_even = []( int i ) -> bool {
return i % 2 == 0;
};
auto is_div3 = []( int i ) {
return i % 3 == 0;
};
auto result =
daw::algorithm::find_some( ary.begin( ), ary.end( ), is_even, is_div3 );
if( result.position != std::next( ary.begin( ), 1 ) ) {
return false;
}
static_assert(
std::tuple_size_v<daw::remove_cvref_t<decltype( result.results )>> == 2 );
if( not result.results[0] ) {
return false;
}
if( result.results[1] ) {
return false;
}
return true;
}
static_assert( find_some_test( ) );
constexpr bool find_transform_test( ) {
auto ary =
std::array{ 1, 2, 4, 8, 16, 15, 14, 13, 12, 11, 10, 9, 7, 6, 5, 3 };
auto is_even = []( int i ) -> bool {
return i % 2 == 0;
};
auto result = daw::algorithm::find_transform(
ary.begin( ),
ary.end( ),
[]( auto v ) {
return v * 3;
},
is_even );
if( not result ) {
return false;
}
if( result.iterator == ary.end( ) ) {
return false;
}
return result.value == 6;
}
static_assert( find_transform_test( ) );
int main( ) {
daw_extract_to_001( );
}