File: test_algorithm_impl.hpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (1122 lines) | stat: -rw-r--r-- 35,873 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
/* Copyright 2016-2024 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/poly_collection for library home page.
 */

#ifndef BOOST_POLY_COLLECTION_TEST_TEST_ALGORITHM_IMPL_HPP
#define BOOST_POLY_COLLECTION_TEST_TEST_ALGORITHM_IMPL_HPP

#if defined(_MSC_VER)
#pragma once
#endif

#include <algorithm>
#include <boost/config.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/iterator/function_output_iterator.hpp>
#include <boost/poly_collection/algorithm.hpp>
#include <functional>
#include <iterator>
#include <random>
#include <type_traits>
#include <utility>
#include <vector>
#include "test_utilities.hpp"

using namespace test_utilities;

#if BOOST_WORKAROUND(BOOST_MSVC,>=1910)&&BOOST_WORKAROUND(BOOST_MSVC,<1920)
/* https://lists.boost.org/Archives/boost/2017/06/235687.php */

#define DEFINE_ALGORITHM(name,f)                    \
template<typename... Ts>                            \
struct name                                         \
{                                                   \
  template<typename... Args>                        \
  auto operator()(Args&&... args)const              \
  {                                                 \
    return f<Ts...>(std::forward<Args>(args)...);   \
  }                                                 \
};
#elif BOOST_WORKAROUND(BOOST_GCC_VERSION,<50200)
/* problem here is with return type containing <Ts...> */

#define DEFINE_ALGORITHM(name,f)                    \
template<typename... Ts>                            \
struct name                                         \
{                                                   \
  template<typename... Args>                        \
  auto operator()(Args&&... args)const->            \
    decltype(f(std::forward<Args>(args)...))        \
  {                                                 \
    return f<Ts...>(std::forward<Args>(args)...);   \
  }                                                 \
};
#else
#define DEFINE_ALGORITHM(name,f)                    \
template<typename... Ts>                            \
struct name                                         \
{                                                   \
  template<typename... Args>                        \
  auto operator()(Args&&... args)const->            \
    decltype(f<Ts...>(std::forward<Args>(args)...)) \
  {                                                 \
    return f<Ts...>(std::forward<Args>(args)...);   \
  }                                                 \
};
#endif

DEFINE_ALGORITHM(std_all_of,std::all_of)
DEFINE_ALGORITHM(poly_all_of,boost::poly_collection::all_of)
DEFINE_ALGORITHM(std_any_of,std::any_of)
DEFINE_ALGORITHM(poly_any_of,boost::poly_collection::any_of)
DEFINE_ALGORITHM(std_none_of,std::none_of)
DEFINE_ALGORITHM(poly_none_of,boost::poly_collection::none_of)
DEFINE_ALGORITHM(std_for_each,std::for_each)
DEFINE_ALGORITHM(poly_for_each,boost::poly_collection::for_each)
DEFINE_ALGORITHM(poly_for_each_n,boost::poly_collection::for_each_n)
DEFINE_ALGORITHM(std_find,std::find)
DEFINE_ALGORITHM(poly_find,boost::poly_collection::find)
DEFINE_ALGORITHM(std_find_if,std::find_if)
DEFINE_ALGORITHM(poly_find_if,boost::poly_collection::find_if)
DEFINE_ALGORITHM(std_find_if_not,std::find_if_not)
DEFINE_ALGORITHM(poly_find_if_not,boost::poly_collection::find_if_not)
DEFINE_ALGORITHM(std_find_end,std::find_end)
DEFINE_ALGORITHM(poly_find_end,boost::poly_collection::find_end)
DEFINE_ALGORITHM(std_find_first_of,std::find_first_of)
DEFINE_ALGORITHM(poly_find_first_of,boost::poly_collection::find_first_of)
DEFINE_ALGORITHM(std_adjacent_find,std::adjacent_find)
DEFINE_ALGORITHM(poly_adjacent_find,boost::poly_collection::adjacent_find)
DEFINE_ALGORITHM(std_count,std::count)
DEFINE_ALGORITHM(poly_count,boost::poly_collection::count)
DEFINE_ALGORITHM(std_count_if,std::count_if)
DEFINE_ALGORITHM(poly_count_if,boost::poly_collection::count_if)
DEFINE_ALGORITHM(std_cpp11_mismatch,std::mismatch) /* note std_cpp11 prefix */
DEFINE_ALGORITHM(poly_mismatch,boost::poly_collection::mismatch)
DEFINE_ALGORITHM(std_cpp11_equal,std::equal)       /* note std_cpp11 prefix */
DEFINE_ALGORITHM(poly_equal,boost::poly_collection::equal)
DEFINE_ALGORITHM(std_cpp11_is_permutation,std::is_permutation) /* std_cpp11 */
DEFINE_ALGORITHM(poly_is_permutation,boost::poly_collection::is_permutation)
DEFINE_ALGORITHM(std_search,std::search)
DEFINE_ALGORITHM(poly_search,boost::poly_collection::search)
DEFINE_ALGORITHM(std_search_n,std::search_n)
DEFINE_ALGORITHM(poly_search_n,boost::poly_collection::search_n)
DEFINE_ALGORITHM(std_copy,std::copy)
DEFINE_ALGORITHM(poly_copy,boost::poly_collection::copy)
DEFINE_ALGORITHM(std_copy_n,std::copy_n)
DEFINE_ALGORITHM(poly_copy_n,boost::poly_collection::copy_n)
DEFINE_ALGORITHM(std_copy_if,std::copy_if)
DEFINE_ALGORITHM(poly_copy_if,boost::poly_collection::copy_if)
DEFINE_ALGORITHM(std_move,std::move)
DEFINE_ALGORITHM(poly_move,boost::poly_collection::move)
DEFINE_ALGORITHM(std_transform,std::transform)
DEFINE_ALGORITHM(poly_transform,boost::poly_collection::transform)
DEFINE_ALGORITHM(std_replace_copy,std::replace_copy)
DEFINE_ALGORITHM(poly_replace_copy,boost::poly_collection::replace_copy)
DEFINE_ALGORITHM(std_replace_copy_if,std::replace_copy_if)
DEFINE_ALGORITHM(poly_replace_copy_if,boost::poly_collection::replace_copy_if)
DEFINE_ALGORITHM(std_remove_copy,std::remove_copy)
DEFINE_ALGORITHM(poly_remove_copy,boost::poly_collection::remove_copy)
DEFINE_ALGORITHM(std_remove_copy_if,std::remove_copy_if)
DEFINE_ALGORITHM(poly_remove_copy_if,boost::poly_collection::remove_copy_if)
DEFINE_ALGORITHM(std_unique_copy,std::unique_copy)
DEFINE_ALGORITHM(poly_unique_copy,boost::poly_collection::unique_copy)
DEFINE_ALGORITHM(poly_sample,boost::poly_collection::sample)
DEFINE_ALGORITHM(std_rotate_copy,std::rotate_copy)
DEFINE_ALGORITHM(poly_rotate_copy,boost::poly_collection::rotate_copy)
DEFINE_ALGORITHM(std_is_partitioned,std::is_partitioned)
DEFINE_ALGORITHM(poly_is_partitioned,boost::poly_collection::is_partitioned)
DEFINE_ALGORITHM(std_partition_copy,std::partition_copy)
DEFINE_ALGORITHM(poly_partition_copy,boost::poly_collection::partition_copy)
DEFINE_ALGORITHM(std_partition_point,std::partition_point)
DEFINE_ALGORITHM(poly_partition_point,boost::poly_collection::partition_point)

template<typename...>
struct std_for_each_n
{
  /* implemented here as the algorithm is C++17 */

  template<typename InputIterator,typename Size,typename Function>
  InputIterator operator()(InputIterator first,Size n,Function f)const
  {
    for(;n>0;++first,(void)--n)f(*first);
    return first;
  }
};

template<typename... Ts>
struct std_mismatch:std_cpp11_mismatch<Ts...>
{
  using std_cpp11_mismatch<Ts...>::operator();

  /* C++14 variants */

  template<typename InputIterator1,typename InputIterator2>
  std::pair<InputIterator1,InputIterator2> operator()(
    InputIterator1 first1,InputIterator1 last1,
    InputIterator2 first2,InputIterator2 last2)const
  {
    while(first1!=last1&&first2!=last2&&*first1==*first2){
      ++first1;
      ++first2;
    }
    return {first1,first2};
  }

  template<typename InputIterator1,typename InputIterator2,typename Predicate>
  std::pair<InputIterator1,InputIterator2> operator()(
    InputIterator1 first1,InputIterator1 last1,
    InputIterator2 first2,InputIterator2 last2,Predicate pred)const
  {
    while(first1!=last1&&first2!=last2&&pred(*first1,*first2)){
      ++first1;
      ++first2;
    }
    return {first1,first2};
  }
};

template<typename... Ts>
struct std_equal:std_cpp11_equal<Ts...>
{
  using std_cpp11_equal<Ts...>::operator();

  /* C++14 variants */

  template<typename InputIterator1,typename InputIterator2>
  bool operator()(
    InputIterator1 first1,InputIterator1 last1,
    InputIterator2 first2,InputIterator2 last2)const
  {
    for(;first1!=last1&&first2!=last2;++first1,++first2){
      if(!(*first1==*first2))return false;
    }
    return first1==last1&&first2==last2;
  }

  template<typename InputIterator1,typename InputIterator2,typename Predicate>
  bool operator()(
    InputIterator1 first1,InputIterator1 last1,
    InputIterator2 first2,InputIterator2 last2,Predicate pred)const
  {
    for(;first1!=last1&&first2!=last2;++first1,++first2){
      if(!pred(*first1,*first2))return false;
    }
    return first1==last1&&first2==last2;
  }
};

template<typename... Ts>
struct std_is_permutation:std_cpp11_is_permutation<Ts...>
{
  using std_cpp11_is_permutation<Ts...>::operator();

  /* The implementation of predicate-based std::is_permutation in GCC<=4.8
   * version of libstdc++-v3 incorrectly triggers the instantiation of
   * ForwardIterator2::value_type, which fails when this is an abstract class.
   * The implementation below ripped from libc++ source code.
   */

  template<
    typename ForwardIterator1,typename ForwardIterator2,typename Predicate
  >
  bool operator()(
    ForwardIterator1 first1,ForwardIterator1 last1,
    ForwardIterator2 first2,Predicate pred)const
  {
    using difference_type=
      typename std::iterator_traits<ForwardIterator1>::difference_type;

    for(;first1!=last1;++first1,(void)++first2){
      if(!pred(*first1,*first2))goto not_done;
    }
    return true;

  not_done:
    difference_type l1=std::distance(first1,last1);
    if(l1==difference_type(1))return false;

    ForwardIterator2 last2=std::next(first2,l1);
    for(ForwardIterator1 i=first1;i!= last1;++i){
      for(ForwardIterator1 j=first1;j!=i;++j)if(pred(*j,*i))goto next_iter;
      {
        difference_type c2=0;
        for(ForwardIterator2 j=first2;j!=last2;++j)if(pred(*i,*j))++c2;
        if(c2==0)return false;
        difference_type c1=1;
        for(ForwardIterator1 j=std::next(i);j!=last1;++j)if(pred(*i,*j))++c1;
        if(c1!=c2)return false;
      }
  next_iter:;
    }
    return true;
  }

  /* C++14 variants */

  template<typename ForwardIterator1,typename ForwardIterator2>
  bool operator()(
    ForwardIterator1 first1,ForwardIterator1 last1,
    ForwardIterator2 first2,ForwardIterator2 last2)const
  {
    if(std::distance(first1,last1)!=std::distance(first2,last2))return false;
    return (*this)(first1,last1,first2);
  }

  template<
    typename ForwardIterator1,typename ForwardIterator2,typename Predicate
  >
  bool operator()(
    ForwardIterator1 first1,ForwardIterator1 last1,
    ForwardIterator2 first2,ForwardIterator2 last2,Predicate pred)const
  {
    if(std::distance(first1,last1)!=std::distance(first2,last2))return false;
    return (*this)(first1,last1,first2,pred);
  }
};

template<typename...>
struct std_sample
{
  /* Implemented here as the algorithm is C++17 and because we need to control
   * the implementation to do white-box testing. Only the ForwardIterator
   * version required.
   */

  template<
    typename ForwardIterator,typename OutputIterator,
    typename Distance, typename UniformRandomBitGenerator
  >
  OutputIterator operator()(
    ForwardIterator first,ForwardIterator last,
    OutputIterator res, Distance n,UniformRandomBitGenerator&& g)const
  {
    Distance m=std::distance(first,last);
    for(n=(std::min)(n,m);n!=0;++first){
      auto r=std::uniform_int_distribution<Distance>(0,--m)(g);
      if (r<n){
        *res++=*first;
        --n;
      }
    }
    return res;
  }
};

template<
  typename ControlAlgorithm,typename Algorithm,
  typename PolyCollection,typename... Args
>
void test_algorithm(PolyCollection& p,Args&&... args)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto last=first;;++last){
      BOOST_TEST(
        alg(first,last,std::forward<Args>(args)...)==
        control(first,last,std::forward<Args>(args)...));
      if(last==end)break;
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename PolyCollection,typename... Args
>
void test_algorithms(PolyCollection& p,Args&&... args)
{
  do_((test_algorithm<ControlAlgorithm,Algorithms>(
    p,std::forward<Args>(args)...),0)...);
  do_((test_algorithm<ControlAlgorithm,Algorithms>(
    const_cast<const PolyCollection&>(p),std::forward<Args>(args)...),0)...);
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename PolyCollection,typename... Args
>
void test_algorithms_with_equality_impl(
  std::true_type,PolyCollection& p,Args&&... args)
{
  test_algorithms<ControlAlgorithm,Algorithms...>(
    p,std::forward<Args>(args)...);
}

template<
  typename ControlAlgorithm,typename... Algorithm,
  typename PolyCollection,typename... Args
>
void test_algorithms_with_equality_impl(
  std::false_type,PolyCollection&,Args&&...)
{
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename PolyCollection,typename... Args
>
void test_algorithms_with_equality(PolyCollection& p,Args&&... args)
{
  test_algorithms_with_equality_impl<ControlAlgorithm,Algorithms...>(
    is_equality_comparable<typename PolyCollection::value_type>{},
    p,std::forward<Args>(args)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection
>
void test_for_each_n_algorithm(ToInt to_int,PolyCollection& p)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto n=std::distance(first,end);n>=0;--n){
      int  res1=0,res2=0;
      auto acc1=compose(to_int,[&](int x){res1+=x;});
      auto acc2=compose(to_int,[&](int x){res2+=x;});
      auto it1=alg(first,n,acc1),
           it2=control(first,n,acc2);
      BOOST_TEST(it1==it2);
      BOOST_TEST(res1==res2);
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection
>
void test_for_each_n_algorithms(ToInt to_int,PolyCollection& p)
{
  do_((test_for_each_n_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p),0)...);
  do_((test_for_each_n_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p)),0)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_algorithm(ToInt to_int,PolyCollection& p,Args&&... args)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto last=first;;++last){
      using vector=std::vector<int>;

      vector v1,v2;
      auto   insert1=compose(to_int,[&](int x){v1.push_back(x);});
      auto   insert2=compose(to_int,[&](int x){v2.push_back(x);});
      auto   out1=boost::make_function_output_iterator(std::ref(insert1));
      auto   out2=boost::make_function_output_iterator(std::ref(insert2));

      out1=alg(first,last,out1,std::forward<Args>(args)...);
      out2=control(first,last,out2,std::forward<Args>(args)...);
      BOOST_TEST(v1==v2);
      if(last==end)break;
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_algorithms(ToInt to_int,PolyCollection& p,Args&&... args)
{
  do_((test_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p,std::forward<Args>(args)...),0)...);
  do_((test_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p),
    std::forward<Args>(args)...),0)...);
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_algorithms_with_equality_impl(
  std::true_type,ToInt to_int,PolyCollection& p,Args&&... args)
{
  test_copy_algorithms<ControlAlgorithm,Algorithms...>(
    to_int,p,std::forward<Args>(args)...);
}

template<
  typename ControlAlgorithm,typename... Algorithm,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_algorithms_with_equality_impl(
  std::false_type,ToInt,PolyCollection&,Args&&...)
{
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_algorithms_with_equality(
  ToInt to_int,PolyCollection& p,Args&&... args)
{
  test_copy_algorithms_with_equality_impl<ControlAlgorithm,Algorithms...>(
    is_equality_comparable<typename PolyCollection::value_type>{},
    to_int,p,std::forward<Args>(args)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_n_algorithm(ToInt to_int,PolyCollection& p,Args&&... args)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(std::ptrdiff_t n=0,m=std::distance(first,end);n<=m;++n){
      using vector=std::vector<int>;

      vector v1,v2;
      auto   insert1=compose(to_int,[&](int x){v1.push_back(x);});
      auto   insert2=compose(to_int,[&](int x){v2.push_back(x);});
      auto   out1=boost::make_function_output_iterator(std::ref(insert1));
      auto   out2=boost::make_function_output_iterator(std::ref(insert2));

      alg(first,n,out1,std::forward<Args>(args)...);
      control(first,n,out2,std::forward<Args>(args)...);
      BOOST_TEST(v1==v2);
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection,typename... Args
>
void test_copy_n_algorithms(ToInt to_int,PolyCollection& p,Args&&... args)
{
  do_((test_copy_n_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p,std::forward<Args>(args)...),0)...);
  do_((test_copy_n_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p),
    std::forward<Args>(args)...),0)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection
>
void test_transform2_algorithm(ToInt to_int,PolyCollection& p)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto last=first;;++last){
      using vector=std::vector<int>;

      auto   op=compose_all(to_int,[](int x,int y){return x+y;});
      vector v1,v2;
      auto   insert1=[&](int x){v1.push_back(x);};
      auto   insert2=[&](int x){v2.push_back(x);};
      auto   out1=boost::make_function_output_iterator(std::ref(insert1));
      auto   out2=boost::make_function_output_iterator(std::ref(insert2));

      out1=alg(first,last,p.begin(),out1,op);
      out2=control(first,last,p.begin(),out2,op);
      BOOST_TEST(v1==v2);
      if(last==end)break;
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection
>
void test_transform2_algorithms(ToInt to_int,PolyCollection& p)
{
  do_((test_transform2_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p),0)...);
  do_((test_transform2_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p)),0)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection
>
void test_rotate_copy_algorithm(ToInt to_int,PolyCollection& p)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto last=first;;++last){
      for(auto middle=first;;++middle){
        using vector=std::vector<int>;

        vector v1,v2;
        auto   insert1=compose(to_int,[&](int x){v1.push_back(x);});
        auto   insert2=compose(to_int,[&](int x){v2.push_back(x);});
        auto   out1=boost::make_function_output_iterator(std::ref(insert1));
        auto   out2=boost::make_function_output_iterator(std::ref(insert2));

        out1=alg(first,middle,last,out1);
        out2=control(first,middle,last,out2);
        BOOST_TEST(v1==v2);

        if(middle==last)break;
      }
      if(last==end)break;
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection
>
void test_rotate_copy_algorithms(ToInt to_int,PolyCollection& p)
{
  do_((test_rotate_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p),0)...);
  do_((test_rotate_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p)),0)...);
}

template<
  typename ControlAlgorithm,typename Algorithm,
  typename ToInt,typename PolyCollection,typename Predicate
>
void test_partition_copy_algorithm(
  ToInt to_int,PolyCollection& p,Predicate pred)
{
  Algorithm        alg;
  ControlAlgorithm control;
  for(auto first=p.begin(),end=p.end();;++first){
    for(auto last=first;;++last){
      using vector=std::vector<int>;

      vector v11,v12,v21,v22;
      auto   insert11=compose(to_int,[&](int x){v11.push_back(x);});
      auto   insert12=compose(to_int,[&](int x){v12.push_back(x);});
      auto   insert21=compose(to_int,[&](int x){v21.push_back(x);});
      auto   insert22=compose(to_int,[&](int x){v22.push_back(x);});
      auto   out11=boost::make_function_output_iterator(std::ref(insert11));
      auto   out12=boost::make_function_output_iterator(std::ref(insert12));
      auto   out21=boost::make_function_output_iterator(std::ref(insert21));
      auto   out22=boost::make_function_output_iterator(std::ref(insert22));

      std::tie(out11,out12)=alg(first,last,out11,out12,pred);
      std::tie(out21,out22)=control(first,last,out21,out22,pred);
      BOOST_TEST(v11==v21);
      BOOST_TEST(v12==v22);
      if(last==end)break;
    }
    if(first==end)break;
  }
}

template<
  typename ControlAlgorithm,typename... Algorithms,
  typename ToInt,typename PolyCollection,typename Predicate
>
void test_partition_copy_algorithms(
  ToInt to_int,PolyCollection& p,Predicate pred)
{
  do_((test_partition_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,p,pred),0)...);
  do_((test_partition_copy_algorithm<ControlAlgorithm,Algorithms>(
    to_int,const_cast<const PolyCollection&>(p),pred),0)...);
}

template<typename ToInt>
struct poly_accumulator_class
{
  poly_accumulator_class(const ToInt& to_int):res{0},to_int(to_int){}
  bool operator==(const poly_accumulator_class& x)const{return res==x.res;}

  template<typename T> void operator()(const T& x){res+=to_int(x);}

  int   res;
  ToInt to_int;
};

template<typename ToInt>
poly_accumulator_class<ToInt> poly_accumulator(const ToInt& to_int)
{
  return to_int;
}

template<typename Algorithm>
struct force_arg_copying
{
  Algorithm alg;

  template<typename T>
  static T copy(const T& x){return x;}

  template<typename... Args>
  auto operator()(Args&&... args)const
    ->decltype(std::declval<Algorithm>()(copy(args)...))
  {
    return alg(copy(args)...);
  }
};

template<
  typename PolyCollection,typename ValueFactory,typename ToInt,
  typename... Types
>
void test_algorithm()
{
  PolyCollection p;
  ValueFactory   v;
  ToInt          to_int;

  fill<constraints<>,Types...>(p,v,2);

  {
    auto always_true=compose(to_int,[](int){return true;});
    auto always_false=compose(to_int,[](int){return false;});
    auto pred=compose(to_int,[](int x){return x%2==0;});

    test_algorithms<std_all_of<>,poly_all_of<>,poly_all_of<Types...>>(
      p,always_true);
    test_algorithms<std_all_of<>,poly_all_of<>,poly_all_of<Types...>>(
      p,always_false);
    test_algorithms<std_all_of<>,poly_all_of<>,poly_all_of<Types...>>(
      p,pred);

    test_algorithms<std_any_of<>,poly_any_of<>,poly_any_of<Types...>>(
      p,always_true);
    test_algorithms<std_any_of<>,poly_any_of<>,poly_any_of<Types...>>(
      p,always_false);
    test_algorithms<std_any_of<>,poly_any_of<>,poly_any_of<Types...>>(
      p,pred);

    test_algorithms<std_none_of<>,poly_none_of<>,poly_none_of<Types...>>(
      p,always_true);
    test_algorithms<std_none_of<>,poly_none_of<>,poly_none_of<Types...>>(
      p,always_false);
    test_algorithms<std_none_of<>,poly_none_of<>,poly_none_of<Types...>>(
      p,pred);
  }
  {
    test_algorithms<std_for_each<>,poly_for_each<>,poly_for_each<Types...>>(
      p,poly_accumulator(to_int));

    test_for_each_n_algorithms<
      std_for_each_n<>,poly_for_each_n<>,poly_for_each_n<Types...>
    >(to_int,p);
  }
  {
    for(const auto& x:p){
      test_algorithms_with_equality<
        std_find<>,poly_find<>,only_eq_comparable<poly_find,Types...>
      >(p,x);
    }
  }
  {
    auto it=p.begin();
    std::advance(it,p.size()/2);
    int n=to_int(*it);
    auto pred=compose(to_int,[=](int x){return x==n;});

    test_algorithms<std_find_if<>,poly_find_if<>,poly_find_if<Types...>>(
      p,pred);

    test_algorithms<
      std_find_if_not<>,poly_find_if_not<>,poly_find_if_not<Types...>
    >(p,pred);
  }
  {
    auto first=p.begin(),end=first;
    std::advance(end,+p.size()/2);
    for(;first!=end;++first){
      test_algorithms_with_equality<
        std_find_end<>,poly_find_end<>,
        only_eq_comparable<poly_find_end,Types...>
      >(p,first,end);

      test_algorithms_with_equality<
        std_search<>,poly_search<>,
        only_eq_comparable<poly_search,Types...>
      >(p,first,end);
    }
  }
  {
    std::vector<int> v;
    for(const auto& x:p)v.push_back(to_int(x));
    v.erase(v.begin()+v.size()/2,v.end());

    for(auto first=v.begin(),end=v.begin()+v.size()/2;first!=end;++first){
      for(int i=1;i<4;++i){
        auto pred=compose(to_int,[&](int x,int y){return x%i==y%i;});

        test_algorithms<
          std_find_end<>,poly_find_end<>,poly_find_end<Types...>
        >(p,first,end,pred);

        test_algorithms<std_search<>,poly_search<>,poly_search<Types...>>(
          p,first,end,pred);
      }
    }
  }
  {
    using value_type=typename PolyCollection::value_type;
    using vector=std::vector<std::reference_wrapper<const value_type>>;

    vector v;
    for(const auto& x:p){
      switch(v.size()%3){
        case 0:v.push_back(x);break;
        case 1:v.push_back(*p.begin());break;
        default:{}
      }
    }

    for(auto first=v.begin(),end=v.end();;++first){
      for(auto last=first;;++last){
        test_algorithms_with_equality<
          std_find_first_of<>,poly_find_first_of<>,
          only_eq_comparable<poly_find_first_of,Types...>
        >(p,first,last);        
        if(last==end)break;
      }
      if(first==end)break;
    }
  }
  {
    std::vector<int> v;
    for(const auto& x:p){
      switch(v.size()%3){
        case 0:v.push_back(to_int(x));break;
        case 1:v.push_back(-1);break;
        default:{}
      }
    }

    for(auto first=v.begin(),end=v.end();;++first){
      for(auto last=first;;++last){
        test_algorithms<
          std_find_first_of<>,poly_find_first_of<>,poly_find_first_of<Types...>
        >(p,first,last,compose(to_int,std::equal_to<int>{}));        
        if(last==end)break;
      }
      if(first==end)break;
    }
  }
  {
    test_algorithms_with_equality<
      std_adjacent_find<>,poly_adjacent_find<>,
      only_eq_comparable<poly_adjacent_find,Types...>
    >(p);        
  }
  {
    std::vector<int> v;
    for(const auto& x:p)v.push_back(to_int(x));
    v.push_back(-1);

    for(auto first=v.begin(),end=v.end()-1;first!=end;++first){
      int n1=*first,n2=*(first+1);
      test_algorithms<
        std_adjacent_find<>,poly_adjacent_find<>,poly_adjacent_find<Types...>
      >(p,compose_all(to_int,[=](int x,int y){return x==n1&&y==n2;}));        
    }
  }
  {
    for(const auto& x:p){
      test_algorithms_with_equality<
        std_count<>,poly_count<>,only_eq_comparable<poly_count,Types...>
      >(p,x);
    }
  }
  {
    for(int i=1;i<4;++i){
      test_algorithms<std_count_if<>,poly_count_if<>,poly_count_if<Types...>>(
        p,compose(to_int,[&](int x){return x%i==0;}));
    }
  }
  {
    using value_type=typename PolyCollection::value_type;
    using vector=std::vector<std::reference_wrapper<const value_type>>;

    vector v;
    for(const auto& x:p)v.push_back(x);
    v.back()=v.front();
    auto w=v;
    v.insert(v.end(),w.begin(),w.end());

    for(auto first=v.begin(),end=v.begin()+v.size()/2;first!=end;++first){
      test_algorithms_with_equality<
        std_mismatch<>,poly_mismatch<>,
        only_eq_comparable<poly_mismatch,Types...>
      >(p,first);

      test_algorithms_with_equality<
        std_mismatch<>,poly_mismatch<>,
        only_eq_comparable<poly_mismatch,Types...>
      >(p,first,end);

      test_algorithms_with_equality<
        std_equal<>,poly_equal<>,only_eq_comparable<poly_equal,Types...>
      >(p,first);

      test_algorithms_with_equality<
        std_equal<>,poly_equal<>,only_eq_comparable<poly_equal,Types...>
      >(p,first,end);
    }

    test_algorithms_with_equality<
      std_mismatch<>,poly_mismatch<>,only_eq_comparable<poly_mismatch,Types...>
    >(p,v.end(),v.end());

    test_algorithms_with_equality<
      std_equal<>,poly_equal<>,only_eq_comparable<poly_equal,Types...>
    >(p,v.end(),v.end());
  }
  {
    std::vector<int> v;
    for(const auto& x:p)v.push_back(to_int(x));
    v.back()=-1;
    auto w=v;
    v.insert(v.end(),w.begin(),w.end());
    auto pred=compose(to_int,std::equal_to<int>{});

    for(auto first=v.begin(),end=v.begin()+v.size()/2;first!=end;++first){
      test_algorithms<std_mismatch<>,poly_mismatch<>,poly_mismatch<Types...>>(
        p,first,pred);

      test_algorithms<std_mismatch<>,poly_mismatch<>,poly_mismatch<Types...>>(
        p,first,end,pred);

      test_algorithms<std_equal<>,poly_equal<>,poly_equal<Types...>>(
        p,first,pred);

      test_algorithms<std_equal<>,poly_equal<>,poly_equal<Types...>>(
        p,first,end,pred);
    }

    test_algorithms<std_mismatch<>,poly_mismatch<>,poly_mismatch<Types...>>(
      p,v.end(),v.end(),pred);

    test_algorithms<std_equal<>,poly_equal<>,poly_equal<Types...>>(
      p,v.end(),v.end(),pred);
  }
  {
    using value_type=typename PolyCollection::value_type;
    using vector=std::vector<std::reference_wrapper<const value_type>>;

    vector v;
    for(const auto& x:p)v.push_back(x);
    auto w=v;
    std::mt19937 gen{73642};
    std::shuffle(w.begin(),w.end(),gen);
    v.insert(v.end(),w.begin(),w.end());
    auto pred=compose_all(to_int,std::equal_to<int>{});

    for(auto first=unwrap_iterator(v.begin()),
             end=unwrap_iterator(v.begin()+v.size()/2);first!=end;++first){
      test_algorithms_with_equality<
        std_is_permutation<>,
        poly_is_permutation<>,
        only_eq_comparable<poly_is_permutation,Types...>
      >(p,first);

      test_algorithms<
        std_is_permutation<>,
        poly_is_permutation<>,poly_is_permutation<Types...>
      >(p,first,pred);

      test_algorithms_with_equality<
        std_is_permutation<>,
        poly_is_permutation<>,
        only_eq_comparable<poly_is_permutation,Types...>
      >(p,first,end);

      test_algorithms<
        std_is_permutation<>,
        poly_is_permutation<>,poly_is_permutation<Types...>
      >(p,first,end,pred);
    }

    test_algorithms_with_equality<
      std_is_permutation<>,
      poly_is_permutation<>,
      only_eq_comparable<poly_is_permutation,Types...>
    >(p,unwrap_iterator(v.end()),unwrap_iterator(v.end()));

    test_algorithms<
      std_is_permutation<>,
      poly_is_permutation<>,poly_is_permutation<Types...>
    >(p,unwrap_iterator(v.end()),unwrap_iterator(v.end()),pred);
  }
  {
    /* search tested above */
  }
  {
    for(const auto&x: p){
      for(int n=0;n<3;++n){
        test_algorithms_with_equality<
          std_search_n<>,poly_search_n<>,
          only_eq_comparable<poly_search_n,Types...>
        >(p,n,x);
      }
    }
  }
  {
    for(int n=0;n<6;++n){
      test_algorithms<std_search_n<>,poly_search_n<>,poly_search_n<Types...>>(
        p,n,0,compose(to_int,[&](int x,int y){return x%(6-n)==y%(6-n);}));
    }
  }
  {
    test_copy_algorithms<std_copy<>,poly_copy<>,poly_copy<Types...>>(
      to_int,p);
  }
  {
    test_copy_n_algorithms<std_copy_n<>,poly_copy_n<>,poly_copy_n<Types...>>(
      to_int,p);
  }
  {
    auto always_true=compose(to_int,[](int){return true;});
    auto always_false=compose(to_int,[](int){return false;});
    auto pred=compose(to_int,[](int x){return x%2==0;});

    test_copy_algorithms<std_copy_if<>,poly_copy_if<>,poly_copy_if<Types...>>(
      to_int,p,always_true);
    test_copy_algorithms<std_copy_if<>,poly_copy_if<>,poly_copy_if<Types...>>(
      to_int,p,always_false);
    test_copy_algorithms<std_copy_if<>,poly_copy_if<>,poly_copy_if<Types...>>(
      to_int,p,pred);
  }
  {
    test_copy_algorithms<std_move<>,poly_move<>,poly_move<Types...>>(
      to_int,p); /* we're not checking std::move is properly used internally */
  }
  {
    auto f=compose(to_int,[](int x){return -x;});
    auto int_id=[](int x){return x;};

    test_copy_algorithms<
      std_transform<>,poly_transform<>,poly_transform<Types...>
    >(int_id,p,f);
  }
  {
    test_transform2_algorithms<
      std_transform<>,poly_transform<>,poly_transform<Types...>
    >(to_int,p);
  }
  {
    const auto& y=*p.begin();
    for(const auto& x:p){
      test_copy_algorithms_with_equality<
        std_replace_copy<>,poly_replace_copy<>,
        only_eq_comparable<poly_replace_copy,Types...>
      >(to_int,p,x,y);

      test_copy_algorithms_with_equality<
        std_remove_copy<>,poly_remove_copy<>,
        only_eq_comparable<poly_remove_copy,Types...>
      >(to_int,p,x);
    }
  }
  {
    auto  always_true=compose(to_int,[](int){return true;});
    auto  always_false=compose(to_int,[](int){return false;});
    auto  pred=compose(to_int,[](int x){return x%2==0;});
    auto& x=*p.begin();

    test_copy_algorithms<
      std_replace_copy_if<>,
      poly_replace_copy_if<>,poly_replace_copy_if<Types...>
    >(to_int,p,always_true,x);
    test_copy_algorithms<
      std_replace_copy_if<>,
      poly_replace_copy_if<>,poly_replace_copy_if<Types...>
    >(to_int,p,always_false,x);
    test_copy_algorithms<
      std_replace_copy_if<>,
      poly_replace_copy_if<>,poly_replace_copy_if<Types...>
    >(to_int,p,pred,x);

    test_copy_algorithms<
      std_remove_copy_if<>,
      poly_remove_copy_if<>,poly_remove_copy_if<Types...>
    >(to_int,p,always_true);
    test_copy_algorithms<
      std_remove_copy_if<>,
      poly_remove_copy_if<>,poly_remove_copy_if<Types...>
    >(to_int,p,always_false);
    test_copy_algorithms<
      std_remove_copy_if<>,
      poly_remove_copy_if<>,poly_remove_copy_if<Types...>
    >(to_int,p,pred);
  }
  {
    test_copy_algorithms_with_equality<
      std_unique_copy<>,poly_unique_copy<>,
      only_eq_comparable<poly_unique_copy,Types...>
    >(to_int,p);
  }
  {
    for(int n=0;n<6;++n){
      test_copy_algorithms<
        std_unique_copy<>,poly_unique_copy<>,poly_unique_copy<Types...>
      >(to_int,p,
        compose_all(to_int,[&](int x,int y){return x%(6-n)==y%(6-n);}));
    }
  }
  {
    test_rotate_copy_algorithms<
      std_rotate_copy<>,poly_rotate_copy<>,poly_rotate_copy<Types...>
    >(to_int,p);
  }
  {
    /* force_arg_copying used to avoid sharing mt19937's state */

    for(auto n=p.size()+1;n-->0;){
      test_copy_algorithms<
        force_arg_copying<std_sample<>>,
        force_arg_copying<poly_sample<>>,
        force_arg_copying<poly_sample<Types...>>
      >(to_int,p,n,std::mt19937{});
    }
  }
  {
    for(int n=0;n<6;++n){
      auto pred=compose(to_int,[&](int x){return x%(6-n)<=(6-n)/2;});

      test_algorithms<
        std_is_partitioned<>,
        poly_is_partitioned<>,poly_is_partitioned<Types...>
      >(p,pred);

      test_partition_copy_algorithms<
        std_partition_copy<>,
        poly_partition_copy<>,poly_partition_copy<Types...>
      >(to_int,p,pred);

      test_algorithms<
        std_partition_point<>,
        poly_partition_point<>,poly_partition_point<Types...>
      >(p,pred);
    }
  }
}

#endif