-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_pipelines_test.cpp
More file actions
773 lines (696 loc) · 26.1 KB
/
Copy pathdaw_pipelines_test.cpp
File metadata and controls
773 lines (696 loc) · 26.1 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
// 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/daw_pipelines.h>
#include <daw/daw_constant.h>
#include <daw/daw_contiguous_view.h>
#include <daw/daw_do_not_optimize.h>
#include <daw/daw_ensure.h>
#include <daw/daw_formatters.h>
#include <daw/daw_print.h>
#include <daw/daw_random.h>
#include <daw/daw_string_view.h>
#include <array>
#include <iterator>
#include <map>
#include <span>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
using namespace daw::pipelines;
using namespace daw::string_view_literals;
struct AlwaysTrue_t {
std::true_type constexpr operator( )( auto const &... ) {
return { };
}
};
static_assert( std::forward_iterator<filter_view<char const *, AlwaysTrue_t>> );
inline constexpr auto vowel =
IsOneOf<'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'>;
inline constexpr auto is_letter = []( char c ) {
return ( 'A' <= c and c <= 'Z' ) or ( 'a' <= c and c <= 'z' );
};
inline constexpr auto to_lower = []( char c ) -> char {
return static_cast<char>( static_cast<unsigned char>( c ) | ' ' );
};
namespace tests {
static constexpr auto prices = std::array{ 100, 200, 150, 180, 130 };
static constexpr auto costs = std::array{ 10, 20, 50, 40, 100 };
DAW_ATTRIB_NOINLINE void test001( ) {
auto m1 = pipeline( zip_view( prices, costs ), To<std::map> );
daw_ensure( m1.size( ) == prices.size( ) );
for( std::size_t n = 0; n < prices.size( ); ++n ) {
daw_ensure( m1[prices[n]] == costs[n] );
}
}
struct ToLetter_t {
DAW_CPP23_STATIC_CALL_OP constexpr char
operator( )( auto i ) DAW_CPP23_STATIC_CALL_OP_CONST {
if( i < 26 ) {
return static_cast<char>( 'a' + i );
} else if( i < 52 ) {
return static_cast<char>( 'A' + ( i - 26 ) );
}
daw::println( stderr,
"{}:{} is out of range",
static_cast<char>( i ),
static_cast<int>( i ) );
std::terminate( );
}
};
inline constexpr auto to_letter = ToLetter_t{ };
DAW_ATTRIB_NOINLINE void test002( ) {
constexpr auto pm2 = pipeline( Map( to_letter ), Enumerate, To<std::map> );
auto const m2 = pm2( iota_view( 0, 26 ) );
for( auto const &[k, v] : m2 ) {
daw_ensure( v == static_cast<char>( 'a' + k ) );
}
constexpr auto pm3 =
pipeline( Map( to_letter ), EnumerateWith<int>, To<std::map> );
auto const m3 = pm3( iota_view( 0, 26 ) );
for( auto const &[k, v] : m3 ) {
daw_ensure( v == static_cast<char>( 'a' + k ) );
}
for( auto vec = std::vector{ 1, 2, 3 };
auto [index, value] : EnumerateWith<int>( vec ) ) {
daw::dump( index * value );
}
}
DAW_ATTRIB_NOINLINE void test003( ) {
constexpr auto q = pipeline( Filter( Not( vowel ) ),
Take( 8 ),
Map( to_lower ),
Filter( is_letter ),
To<std::basic_string>,
Sort,
Unique,
Map( []( long long i ) {
return i;
} ),
Sum );
auto const out0 = q( "Hello World, How are you?" );
auto const out1 = q( "Blah blah blah, how is yah?" );
daw::println(
"\ntest003: pipeline(\n\tFilter( Not( vowel ) ),\n\tTake( 8 ),\n\tMap( "
"to_lower ),Filter( is_letter "
"),\n\tTo<std::basic_string>,\n\tSort,\n\tUnique,\n\tMap( []( long "
"long "
"i ) {{ return i; }}),\n\tSum );" );
daw::println( "\tinput 'Hello World, How are you?' - output {}", out0 );
daw::println( "\tinput 'Blah blah blah, how is yah?' - output {}", out1 );
}
DAW_ATTRIB_NOINLINE void test004( ) {
constexpr auto p = pipeline( Filter( Not( vowel ) ),
Print,
Take( 8 ),
Print,
Map( to_lower ),
Print,
Filter( is_letter ) );
constexpr auto p1 = pipeline( p, Take( 4 ) );
daw::println(
"\ntest004: pipeline(Filter( Not( vowel ) ), Take( 8 ), Map( to_lower "
"), "
"Filter( is_letter ) )" );
auto r0 = p1( "Hello World, How are you?" );
auto r1 = p1( "Blah blah blah, how is yah?" );
daw::println( "\tinput 'Hello World, How are you?': output {}",
daw::fmt_range( r0 ) );
daw::println( "\tinput 'Blah blah blah, how is yah?': output {}",
daw::fmt_range( r1 ) );
}
DAW_ATTRIB_NOINLINE void test005( ) {
// Example from https://youtu.be/itnyR9j8y6E?t=404
constexpr auto p3 =
pipeline( Map( []( auto i ) {
return std::pair{ i, std::to_string( i ) };
} ),
Filter( []( auto const &v ) {
auto lhs = static_cast<std::size_t>( v.first );
auto const h = std::hash<std::string>{ }( v.second );
return lhs < h;
} ),
Map( &std::pair<std::size_t, std::string>::second ),
Take( 4 ),
To<std::array<std::string, 4>> );
auto const result = p3( iota_view( 1, 10001 ) );
daw::println( "\ntest005: input 1 to 10001 : output {}",
daw::fmt_range( result ) );
}
DAW_ATTRIB_NOINLINE void test006( ) {
auto const r2 =
pipeline( iota_view( 1, 10001 ),
Map( []( auto i ) {
return std::pair{ i, std::to_string( i ) };
} ),
Filter( []( auto const &v ) {
auto lhs = static_cast<std::size_t>( v.first );
auto const h = std::hash<std::string>{ }( v.second );
return lhs >= h;
} ),
Map( &std::pair<std::size_t, std::string>::second ),
Take( 4 ) );
daw::println( "\ntest006: input 1 to 10001 : output {}",
daw::fmt_range( r2 ) );
}
consteval void test007( ) {
constexpr auto p4 = pipeline( Filter( []( int x ) {
return x % 2 == 0;
} ),
Map( []( std::optional<int> x ) {
if( not x ) {
return iota_view<int>{ 1 };
}
return iota_view<int>{ *x };
} ),
Count,
ToIota,
Map( []( int x ) {
return x * 2;
} ) );
auto x = p4( std::array{ 1, 2, 3 } );
auto y = *std::next( x.begin( ) );
daw_ensure( y == 2 );
}
DAW_ATTRIB_NOINLINE void test008( ) {
daw::println(
"\ntest008: pipeline(\n\t\tMap( [](unsigned i) {{\n\t\t\treturn "
"std::tuple(i,to_letter(i));\n\t\t}} "
"),\n\t\tPrint,\n\t\tSwizzle<1,0,0>,\n\t\tPrint\n\t)\n\tinput 0 to 52: "
"output\n" );
constexpr auto tp0 = pipeline( Map( []( unsigned i ) {
return std::tuple( i, to_letter( i ) );
} ),
Print,
Swizzle<1, 0, 0>,
Print );
(void)tp0( iota_view<unsigned>( 0, 52 ) );
}
DAW_ATTRIB_NOINLINE void test009( ) {
daw::println(
"\ntest009: pipeline( ZipMore( iota_view<char>( 'A', 'Z' ) ) )" );
constexpr auto tp1 = pipeline( ZipMore( iota_view<char>( 'A', 'Z' ) ) );
constexpr auto tp1r0 = tp1( iota_view<char>( 'a', 'z' ) );
daw::println( "\n\tinput: iota_view<char>( 'a', 'z' ): output\n{}",
daw::fmt_range( tp1r0 ) );
constexpr auto tp1r1 = tp1(
zip_view( iota_view<char>( 'a', 'z' ), iota_view<char>( 'a', 'z' ) ) );
daw::println(
"\n\tinput:zip_view( iota_view<char>( 'a', 'z' ), iota_view<char>( "
"'a', "
"'z' ) ) : output\n{}",
daw::fmt_range( tp1r1 ) );
}
DAW_ATTRIB_NOINLINE void fma( std::size_t N, double const *__restrict a,
double const *__restrict b,
double const *__restrict c,
double *__restrict out ) {
auto const fma_range =
pipeline( zip_view( daw::contiguous_view( a, N ),
daw::contiguous_view( b, N ),
daw::contiguous_view( c, N ) ),
MapApply( []( double x, double y, double z ) {
auto result = x + y * z;
return result;
} ) );
static_assert( std::same_as<daw::range_category_t<DAW_TYPEOF( fma_range )>,
std::random_access_iterator_tag> );
auto first = std::begin( fma_range );
auto const last = std::end( fma_range );
while( first != last ) {
*out = *first;
++out;
++first;
}
}
DAW_ATTRIB_NOINLINE void test010( ) {
daw::println( "\ntest010: fma test" );
auto const make_random =
pipeline( Map( [rnd = daw::RandomFloat<double>{ }]( auto ) mutable {
auto result = rnd( );
return result;
} ),
To<std::vector> );
std::vector<double> const a = make_random( iota_view( 16 ) );
daw::println( "\na: {}", daw::fmt_range( a ) );
daw::do_not_optimize( a );
std::vector<double> const b = make_random( iota_view( 16 ) );
daw::println( "\nb: {}", daw::fmt_range( b ) );
daw::do_not_optimize( b );
std::vector<double> const c = make_random( iota_view( 16 ) );
daw::println( "\nc: {}", daw::fmt_range( c ) );
daw::do_not_optimize( c );
auto o = std::vector<double>( 16ULL );
daw::do_not_optimize( o );
fma( o.size( ),
std::data( a ),
std::data( b ),
std::data( c ),
std::data( o ) );
daw::println( "\no: {}", daw::fmt_range( o ) );
daw::do_not_optimize( o );
daw_ensure( a[0] != o[0] );
}
DAW_ATTRIB_NOINLINE void test011( ) {
constexpr auto a =
std::array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
constexpr auto p0 = pipeline( Skip( 1 ), Take( 5 ), To<std::vector> );
auto v0 = p0( a );
daw::println(
"\ntest011: pipeline( a, Skip( 1 ), Take( 5 ), To<std::vector> "
"):\n\tinput: "
"[1,2,3,4,5,6,7,8,9]\n\touput: {}",
daw::fmt_range( v0 ) );
}
DAW_ATTRIB_INLINE void test012( ) {
auto v = pipeline( iota_view<int>( 1, 101 ), To<std::array<int, 100>> );
daw::do_not_optimize( v );
auto s = Sum( v );
daw::println(
"\ntest012: pipeline( iota_view<int>( 1, 101 ), To<std::array<int, 100>> "
")\nSum: {} of\n{}",
s,
daw::fmt_range( v ) );
}
DAW_ATTRIB_NOINLINE void test013( ) {
static constexpr auto const v = pipeline( zip_view( prices, costs ),
MapApply( []( auto pr, auto co ) {
return pr - co;
} ),
Clamp( 5, 100 ),
To<std::array<int, 5>> );
daw::println( "MapApply, Clamp, To<std::vector>\n{}", daw::fmt_range( v ) );
constexpr auto v1 = pipeline( zip_view( prices, costs, v ), Concat );
daw::println( "Concat\n{}", daw::fmt_range( v1 ) );
auto to_array = []<std::size_t ArraySize, typename C>(
daw::constant<ArraySize>, C const &c ) {
using value_t = typename C::value_type;
return To<std::array<value_t, ArraySize>>( )( c );
};
constexpr auto pm = pipeline( Map( []( int i ) {
return std::pair{ i, to_letter( i ) };
} ),
To<std::map> );
auto const m = pm( iota_view<int>( 0, 26 ) );
daw::println( "To<std::map>\n{}", daw::fmt_range( m ) );
auto va = to_array( daw::constant<std::size_t{ 15 }>{ }, v );
daw::println( "{}", daw::fmt_range( va ) );
}
void test014( ) {
daw::println( "\ntest014 - naive vs Kahan FP summation" );
auto da = std::array{ 1024.123,
0.000000013143,
0.0001123434,
4533.5,
1.0,
1.000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
0.0000000000000000001,
1.657348632812501,
1032.03423423 };
auto s_kahan = SumKahanBabushkaNeumaier( da );
daw::println( "{:.19f}\nThe Kahan sum is {:.19f}",
daw::fmt_range{ da, ",\n\t", "[\t", " ]" },
s_kahan );
auto s_naive = Sum( da );
daw::println( "The naive sum is {:.19f}", s_naive );
}
DAW_ATTRIB_NOINLINE void test015( ) {
constexpr auto a = pipeline( generate_n( 5,
[i = 1]( ) mutable {
return i++;
} ),
To<std::array<std::size_t, 5>> );
daw::println(
"\ntest015: pipeline( generate_n( 5, [&]{{return "
"i++;}}),To<std::array<std::size_t, 5>>);\n{}",
daw::fmt_range{ a } );
}
DAW_ATTRIB_NOINLINE void test016( ) {
constexpr auto a =
std::array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
constexpr auto p0 = pipeline( Skip( 3 ), To<std::vector> );
auto v0 = p0( a );
daw::println(
"\ntest016: pipeline( a, Skip( 3 ), To<std::vector> ):\n\tinput: "
"[1,2,3,4,5,6,7,8,9]\n\touput: {}",
daw::fmt_range( v0 ) );
}
DAW_ATTRIB_NOINLINE void test017( ) {
daw::println(
"\ntest017: pipeline( iota_view<int>{{ -5, 5 }}, Sample( 4, 10 ) )" );
auto const p = pipeline( iota_view<int>{ -5, 5 }, Print, Sample( 4, 10 ) );
daw::println( "{}", daw::fmt_range{ p } );
}
DAW_ATTRIB_NOINLINE void test018( ) {
daw::println( "\ntest018: pipeline( iota_view<int>{{ -5, 5 }}, Reverse )" );
auto i = iota_view<int>{ -5, 5 };
daw::println( "\t{}", daw::fmt_range( i ) );
daw::println( "\treversed" );
daw::println( "\t{}", daw::fmt_range( Reverse( i ) ) );
}
DAW_ATTRIB_NOINLINE void test019( ) {
daw::println(
"\ntest019: pipeline( iota_view<int>{{ -5, 5 }}, Every( 3 ) )" );
auto const p = pipeline( iota_view<int>{ -5, 5 }, Print, Every( 3 ) );
daw::println( "{}", daw::fmt_range{ p } );
}
DAW_ATTRIB_NOINLINE void test020( ) {
daw::println(
"\ntest020: pipeline( iota_view<int>{{ -5, 5 }}, TakeUntil( []( int x ) "
"{{ return x > 0; }} ) ) )" );
daw::println( "\tin: {}", daw::fmt_range{ iota_view<int>{ -5, 5 } } );
auto const p = pipeline( iota_view<int>{ -5, 5 }, TakeUntil( []( int x ) {
return x > 0;
} ) );
daw::println( "\tout: {}", daw::fmt_range{ p } );
}
DAW_ATTRIB_NOINLINE void test021( ) {
daw::println(
"\ntest021: pipeline( iota_view<int>{{ -5, 5 }}, TakeWhile( []( int x ) "
"{{ return x < 0; }} ) ) )" );
daw::println( "\tin: {}", daw::fmt_range{ iota_view<int>{ -5, 5 } } );
auto const p = pipeline( iota_view<int>{ -5, 5 }, TakeWhile( []( int x ) {
return x < 0;
} ) );
daw::println( "\tout: {}", daw::fmt_range{ p } );
}
DAW_ATTRIB_NOINLINE void test022( ) {
daw::println( "\ntest022: pipeline( Print, Flatten, Print )" );
constexpr auto ary = std::array{ iota_view<int>{ 1, 2 },
iota_view<int>{ 6, 10 },
iota_view<int>{ 15, 17 } };
auto r = pipeline( ary, Print, Flatten, Print );
(void)r;
}
DAW_ATTRIB_NOINLINE void test023( ) {
static constexpr auto ary =
To<std::array<std::size_t, 9>>( )( iota_view( 9 ) );
daw::println( "\ntest023: pipeline( Chunk( 3 ), Flatten )" );
daw::println( "\tin: {}", daw::fmt_range( ary ) );
constexpr auto p = pipeline( Chunk( 3 ), Print, Flatten );
auto r = p( ary );
auto const ary2 = To<std::array<std::size_t, 9>>( )( r );
daw_ensure( ary == ary2 );
daw::println( "\tout: {}", daw::fmt_range( ary2 ) );
}
DAW_ATTRIB_NOINLINE void test024( ) {
static constexpr auto ary = std::array{ 1, 2, 3, 4, 4, 5, 6, 6, 7 };
constexpr auto it = FindIf<2>( ary, []( auto const &l, auto const &r ) {
return l == r;
} );
static_assert( it != std::end( ary ) );
static_assert( std::distance( std::begin( ary ), it ) == 3 );
constexpr auto it2 = Find( ary, 2 );
static_assert( it2 != std::end( ary ) );
static_assert( std::distance( std::begin( ary ), it2 ) == 1 );
constexpr auto it3 = Find( 2 )( ary );
static_assert( it2 == it3 );
}
DAW_ATTRIB_NOINLINE void test025( ) {
struct Test {
int value;
explicit( false ) constexpr Test( int v )
: value( v ) {}
};
static constexpr auto ary =
std::array<Test, 9>{ 1, 2, 3, 4, 4, 5, 6, 6, 7 };
constexpr auto it = FindIf<2>( ary, []( auto const &l, auto const &r ) {
return l.value == r.value;
} );
static_assert( it != std::end( ary ) );
static_assert( std::distance( std::begin( ary ), it ) == 3 );
constexpr auto it2 = Find( ary, 2, &Test::value );
static_assert( it2 != std::end( ary ) );
static_assert( std::distance( std::begin( ary ), it2 ) == 1 );
constexpr auto it3 = Find( 2, &Test::value )( ary );
static_assert( it2 == it3 );
}
DAW_ATTRIB_NOINLINE void test026( ) {
static constexpr auto ary =
To<std::array<std::size_t, 9>>( )( iota_view( 9 ) );
daw::println( "\ntest026: pipeline( Slide( 3 ), Flatten )" );
daw::println( "\tin: {}", daw::fmt_range( ary ) );
constexpr auto p = pipeline( Slide( 3 ), Print, Flatten );
auto r = p( ary );
auto const ary2 = To<std::array<std::size_t, 9>>( )( r );
constexpr auto tst_ary =
std::array<std::size_t, 9>{ 0, 1, 2, 1, 2, 3, 2, 3, 4 };
daw_ensure( ary2 == tst_ary );
daw::println( "\tout: {}", daw::fmt_range( ary2 ) );
}
DAW_ATTRIB_NOINLINE void test027( ) {
static constexpr std::array ids = { "597216680271282192"_sv,
"155149108183695360"_sv,
"843841057833877588"_sv,
"1013956772245020774"_sv,
"1013960757127422113"_sv };
static constexpr auto exists = []( daw::string_view needle ) {
return Contains( needle )( ids );
};
constexpr auto x = "12345"_sv;
static_assert( not exists( x ) );
constexpr auto y = "1013960757127422113"_sv;
static_assert( exists( y ) );
}
DAW_ATTRIB_NOINLINE void test028( ) {
daw::dump( 1, 2, 3, 4 );
}
DAW_ATTRIB_NOINLINE void test029( ) {
constexpr auto pm3 =
pipeline( Map( to_letter ), EnumerateFrom( 2 ), To<std::map> );
auto const m3 = pm3( iota_view( 0, 26 ) );
daw::println(
"\ntest029: pipeline( Map( to_letter ), EnumerateFrom( 2 ), "
"To<std::map> "
")\n{}",
daw::fmt_range( m3 ) );
for( auto vec = std::vector{ 1, 2, 3 };
auto [index, value] : EnumerateFrom( 2 )( vec ) ) {
daw::dump( index * value );
}
}
DAW_ATTRIB_NOINLINE void test030( ) {
static constexpr auto comma_splitter =
pipeline( Split( ',' ), Map( []( daw::Range auto r ) {
return daw::string_view( r ).trim( );
} ) );
static constexpr auto values = "1a, 2b,3c, 4d ,5e"_sv;
constexpr auto parts = comma_splitter( values );
daw::println(
"test030: pipeline( Split( ',' ), \"1a, 2b, 3c, 4d, 5e\" )\n{}",
daw::fmt_range{ parts, "|" } );
}
DAW_ATTRIB_NOINLINE void test031( ) {
static constexpr auto nums_to_string =
pipeline( Map( []( int n ) {
assert( n >= 0 and n <= 9 );
return static_cast<char>( n + '0' );
} ),
[]( auto r ) {
return std::format( "\"{}\"", daw::fmt_range( r, "" ) );
} );
static constexpr auto delim = std::array{ 3, 4 };
constexpr auto splitter = pipeline( Split( delim ), Map( nums_to_string ) );
static constexpr auto data =
std::array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 42, 42, 42, 42, 42, 42 };
static constexpr auto parts =
splitter( daw::contiguous_view( data.data( ), 9U ) );
daw::println(
"test031: pipeline( Split( {{3,4}} ), Map( nums_to_string ) ) with [1, "
"2, 3, 4, 5, 6, 7, 8, 9]" );
daw::println( "{}", daw::fmt_range( parts ) );
}
DAW_ATTRIB_NOINLINE void test032( ) {
static constexpr int arr1[] = { 0, 1, 2, 3, 4 };
static constexpr int arr2[] = { 0, 100, 200, 300, 400 };
constexpr auto zipped = pipeline( Zip, MapApply( std::plus<>{ } ) );
daw::println( "test032: Expecting: 0, 101, 202, 303, 404" );
for( auto p : zipped( arr1, arr2 ) ) {
daw::println( "{}", p );
}
constexpr auto zipped2 =
pipeline( Zip( arr1, arr2 ), MapApply( std::plus<>{ } ) );
for( auto p : zipped2 ) {
daw::println( "{}", p );
}
for( auto p : pipeline( Zip, MapApply( []( int x ) {
return x + 1;
} ) )( arr1 ) ) {
daw::println( "{}", p );
}
}
constexpr bool is_control_or_space( const char c ) noexcept {
return static_cast<unsigned char>( c ) <= static_cast<unsigned char>( ' ' );
}
DAW_ATTRIB_NOINLINE void test033( ) {
constexpr daw::string_view s = " Hello ";
constexpr auto p = pipeline( s,
DropWhile( is_control_or_space ),
Reverse,
DropWhile( is_control_or_space ),
Reverse );
constexpr auto s2 = daw::string_view{ std::data( p ), std::size( p ) };
daw_ensure( s2 == "Hello" );
}
DAW_ATTRIB_NOINLINE void test034( ) {
constexpr daw::string_view s = " Hello ";
constexpr auto p = pipeline( s,
DropWhile( is_control_or_space ),
Reverse,
DropWhile( is_control_or_space ),
Reverse,
FirstRef );
daw_ensure( p.has_value( ) and p.value( ) == 'H' );
}
DAW_ATTRIB_NOINLINE void test035( ) {
char buff[] = "Hello";
auto p = pipeline( buff, FirstRef );
daw_ensure( p.has_value( ) and p.value( ) == 'H' );
buff[0] = 'h';
daw_ensure( p.value( ) == 'h' );
}
DAW_ATTRIB_NOINLINE void test036( ) {
constexpr daw::string_view s = " Hello ";
constexpr auto p = pipeline( s,
DropWhile( is_control_or_space ),
Reverse,
DropWhile( is_control_or_space ),
Reverse,
First );
daw_ensure( p.has_value( ) and p.value( ) == 'H' );
}
DAW_ATTRIB_NOINLINE DAW_CONSTEVAL void test037( ) {
char buff[] = "Hello";
auto p = pipeline( buff, First );
daw_ensure( p.has_value( ) and p.value( ) == 'H' );
buff[0] = 'h';
daw_ensure( p.value( ) == 'H' );
}
DAW_ATTRIB_NOINLINE void test038( ) {
constexpr auto values = std::array{ 1, 5, 5, 10, 32 };
daw::do_not_optimize( values );
auto const unique_values =
pipeline( values, Copy, Sort, Unique, To<std::vector> );
daw_ensure( unique_values.size( ) == 4 );
daw_ensure( unique_values == std::vector{ 1, 5, 10, 32 } );
}
DAW_ATTRIB_NOINLINE void test039( ) {
auto z0 = Zip( prices, costs );
auto e0 = Elements<1>( z0 );
daw_ensure( static_cast<std::size_t>( std::ranges::distance( e0 ) ) ==
costs.size( ) );
auto eb0 = std::begin( e0 );
for( std::size_t n = 0; n < costs.size( ); ++n ) {
daw_ensure( std::get<0>( eb0[n] ) == costs[n] );
}
auto e1 = Elements<0>( z0 );
auto eb1 = std::begin( e1 );
daw_ensure( static_cast<std::size_t>( std::ranges::distance( e1 ) ) ==
prices.size( ) );
for( std::size_t n = 0; n < prices.size( ); ++n ) {
daw_ensure( std::get<0>( eb1[n] ) == prices[n] );
}
}
DAW_ATTRIB_NOINLINE void test040( ) {
auto z0 = Zip( prices, costs );
auto e0 = Element<1>( z0 );
daw_ensure( static_cast<std::size_t>( std::ranges::distance( e0 ) ) ==
costs.size( ) );
auto eb0 = std::begin( e0 );
for( std::size_t n = 0; n < costs.size( ); ++n ) {
daw_ensure( eb0[n] == costs[n] );
}
auto e1 = Element<0>( z0 );
auto eb1 = std::begin( e1 );
daw_ensure( static_cast<std::size_t>( std::ranges::distance( e1 ) ) ==
prices.size( ) );
for( std::size_t n = 0; n < prices.size( ); ++n ) {
daw_ensure( eb1[n] == prices[n] );
}
}
DAW_ATTRIB_NOINLINE void test041( ) {
constexpr auto values = std::array{ 1, 5, 5, 10, 32 };
auto cp = pipeline( values,
Map( []( int x ) {
static int map_count = 0;
++map_count;
daw::println( "MapCount = {}", map_count );
return x * x;
} ),
CacheLast );
auto first = std::begin( cp );
auto last = std::end( cp );
while( first != last ) {
daw::println( "{}", *first );
daw::println( "{}", *first );
++first;
}
}
DAW_ATTRIB_NOINLINE void test042( ) {
auto v = std::vector<std::string>{ "Hello", "World" };
auto mapper = Map( +[]( std::string &s ) -> char * {
return s.data( );
} );
auto m = mapper( v );
auto v2 = std::vector<char *>( std::begin( m ), std::end( m ) );
daw_ensure( v2.size( ) == v.size( ) );
}
} // namespace tests
int main( ) {
tests::test001( );
tests::test002( );
tests::test003( );
tests::test004( );
tests::test005( );
tests::test006( );
tests::test007( );
tests::test008( );
tests::test009( );
tests::test010( );
tests::test011( );
tests::test012( );
tests::test013( );
tests::test014( );
tests::test015( );
tests::test016( );
tests::test017( );
tests::test018( );
tests::test019( );
tests::test020( );
tests::test021( );
tests::test022( );
tests::test023( );
tests::test024( );
tests::test025( );
tests::test026( );
tests::test027( );
tests::test028( );
tests::test029( );
tests::test030( );
tests::test031( );
tests::test032( );
tests::test033( );
tests::test034( );
tests::test035( );
tests::test036( );
tests::test037( );
tests::test038( );
tests::test039( );
tests::test040( );
tests::test041( );
tests::test042( );
daw::println( "Done" );
}