-
Notifications
You must be signed in to change notification settings - Fork 14
/
tst_kdalgorithms.cpp
2998 lines (2576 loc) · 94.7 KB
/
tst_kdalgorithms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** This file is part of KDAlgorithms
**
** SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
**
** SPDX-License-Identifier: MIT
**
****************************************************************************/
#include "../src/kdalgorithms.h"
#include "ContainerObserver.h"
#include "copy_observer.h"
#include <QList>
#include <QTest>
#include <QVector>
#include <algorithm>
#include <array>
#include <deque>
#include <forward_list>
#include <iostream>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
namespace {
bool isOdd(int x)
{
return x % 2 == 1;
}
int squareItem(int x)
{
return x * x;
}
QString toString(int x)
{
return QString::number(x);
}
std::function<bool(int)> greaterThan(int testValue)
{
return [testValue](int value) { return value > testValue; };
}
const std::vector<int> emptyIntVector;
const std::vector<int> intVector{1, 2, 3, 4};
const std::vector<int> unsortedIntVector{8, 2, 3, 6, 11};
std::vector<int> getIntVector()
{
return std::vector<int>{1, 2, 3, 4};
}
struct Struct
{
int key;
int value;
bool operator==(const Struct &other) const { return key == other.key && value == other.value; }
bool isKeyGreaterThanValue() const { return key > value; }
bool lessThanByXY(const Struct &other) const
{
if (key != other.key)
return key < other.key;
else
return value < other.value;
}
bool hasEqualKeys(const Struct &other) const { return key == other.key; }
bool hasEqualValues(const Struct &other) const { return value == other.value; }
bool hasEqualKeyValuePair() const { return key == value; }
int sumPairs() const { return key + value; }
void print() const { std::cout << "{ " << key << ", " << value << " }" << std::endl; }
};
const std::vector<Struct> structVec{{1, 4}, {2, 3}, {3, 2}, {4, 1}};
const std::vector<Struct> unsortedStructVec{{2, 4}, {1, 3}, {4, 2}, {3, 1}, {5, 4}};
std::vector<Struct> getStruct()
{
return std::vector<Struct>{{1, 2}, {2, 1}, {3, 3}, {4, 4}};
}
struct Person
{
QString name;
int age;
QString yearsToBigBirthday() const { return QString("%1 years").arg(100 - age); }
bool operator==(const Person &other) const { return name == other.name && age == other.age; }
};
} // namespace
class TestAlgorithms : public QObject
{
Q_OBJECT
private Q_SLOTS:
void copy();
void copyAsMove();
void copied();
void filteredChangeContainer();
void filteredSameContainer();
void filteredAsMove();
void filterWithMemberFunction();
void filterOtherContainers();
void filter();
void transformedChangeContainer();
void transformedSameContainer();
void transformedFullContainerTypeSpecified();
void transformedChangeContainerAndDataType();
void transformedChangeContainerAndDataType2();
void transformedChangeDataType();
void transformedWithRValue();
void transformMemberFunction();
void transformOtherContainers();
void transformedMemberVariable();
void transformedStaticFunctions();
void transform();
void filtered_transformed();
void anyOf();
void allOf();
void noneOf();
void anyAllNoneOtherContainers();
void reverse();
void reversed();
void reversedEnsureMoveOnly();
void sort();
void sorted();
void sortWithCompare();
void sortedWithCompare();
void sortedEnsureMoveOnly();
void sortBy();
void is_sorted();
void lvalue();
void contains();
void value_in();
void count();
void count_if();
void max_value();
void min_value();
void maxValueLessThan();
void maxValueLessThanCustomComparisor();
void maxValueLessThanUnordered();
void maxValueLessThanUnorderedCustomComparisor();
void minValueGreaterThan();
void minValueGreaterThanUnordered();
void minValueGreaterThanUnorderedCustomComparisor();
void minValueGreaterThanCustomComparisor();
void isPermutation();
void accumulate();
void accumulateAndMemberFunctions();
void accumulate_if();
void accumulateWithInitialValue();
void accumulateDifferentReturnType();
void accumulateWithAuto();
void accumulateWithMap();
void sum();
void sum_if();
void get_match();
void get_match_or_default();
void remove_duplicates();
void has_duplicates();
void has_duplicates_data();
void erase();
void erase_if();
void combiningTests();
void index_of_match();
void find_if();
void find_if_rvalue();
void find_if_not();
void find_if_not_rvalue();
void iota();
void iota_single_arg();
void partition();
void generate_n();
void generate_until();
void isSame();
void zip();
void for_each();
void invoke();
void multi_partitioned();
void multi_partitioned_with_function_taking_a_value();
void sub_range();
void product();
};
void TestAlgorithms::copy()
{
// normal copy vector -> QVector
{
QVector<int> result;
kdalgorithms::copy(intVector, result);
QVector<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// std::list doesn't have a reserve method
{
std::list<int> list;
kdalgorithms::copy(intVector, list);
std::list<int> expected{1, 2, 3, 4};
QCOMPARE(list, expected);
}
// deque
{
std::deque<int> from{1, 2, 3, 4};
std::deque<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// set
{
std::set<int> from{1, 2, 3, 4, 1, 3};
std::set<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// unordered_set
{
std::unordered_set<int> from{1, 2, 3, 4, 1, 3};
std::unordered_set<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// multiset
{
std::multiset<int> from{1, 2, 3, 4, 1, 3};
std::multiset<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// unordered_multiset
{
std::unordered_multiset<int> from{1, 2, 3, 4, 1, 3};
std::unordered_multiset<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// map
{
std::map<int, int> from{{1, 2}, {2, 3}, {4, 5}};
std::map<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// multimap
{
std::multimap<int, int> from{{1, 2}, {2, 3}, {1, 3}, {4, 5}};
std::multimap<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// unordered_map
{
std::unordered_map<int, int> from{{1, 2}, {2, 3}, {4, 5}};
std::unordered_map<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// unordered_multimap
{
std::unordered_multimap<int, int> from{{1, 2}, {2, 3}, {1, 3}, {4, 5}};
std::unordered_multimap<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// std::vector to std::set
{
std::set<int> set;
kdalgorithms::copy(intVector, set);
std::set<int> expected{1, 2, 3, 4};
QCOMPARE(set, expected);
}
// QSet
{
#if QT_VERSION >= 0x060000
QSet<int> from{1, 2, 3, 4, 1, 3};
QSet<int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
#endif
}
// QMap
{
QMap<int, int> from{{1, 2}, {2, 3}, {4, 5}};
QMap<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
// QHash
{
QHash<int, int> from{{1, 2}, {2, 3}, {4, 5}};
QHash<int, int> to;
kdalgorithms::copy(from, to);
QCOMPARE(from, to);
}
{ // Self copy - OK
std::vector<int> v{1, 2, 3};
kdalgorithms::copy(v, v);
std::vector<int> expected{1, 2, 3, 1, 2, 3};
QCOMPARE(v, expected);
}
{ // std::list self copy - std::list doesn't have a reserve
std::list<int> v{1, 2, 3};
std::list<int> expected{1, 2, 3, 1, 2, 3};
kdalgorithms::copy(v, v);
QCOMPARE(v, expected);
}
{ // std::map self copy - Doesn't make sense, but doesn't fail either
std::map<int, int> v{{1, 1}, {2, 2}, {3, 3}};
std::map<int, int> expected{{1, 1}, {2, 2}, {3, 3}};
kdalgorithms::copy(v, v);
QCOMPARE(v, expected);
}
{ // std::set Self copy - Doesn't make sense, but doesn't fail either
std::set<int> v{1, 2, 3};
std::set<int> expected{1, 2, 3};
kdalgorithms::copy(v, v);
QCOMPARE(v, expected);
}
{ // std::multimap self copy
std::multimap<int, int> v{{1, 1}, {2, 2}, {3, 3}};
std::multimap<int, int> expected{{1, 1}, {2, 2}, {3, 3}, {1, 1}, {2, 2}, {3, 3}};
kdalgorithms::copy(v, v);
QCOMPARE(v, expected);
}
}
std::vector<CopyObserver> getObserverVector()
{
std::vector<CopyObserver> vec;
vec.emplace_back(1);
vec.emplace_back(2);
vec.emplace_back(3);
return vec;
}
const std::vector<CopyObserver> getConstObserverVector()
{
return getObserverVector();
}
void TestAlgorithms::copyAsMove()
{
std::vector<CopyObserver> result;
CopyObserver::reset();
kdalgorithms::copy(getObserverVector(), result);
QCOMPARE(CopyObserver::copies, 0);
CopyObserver::reset();
kdalgorithms::copy(getConstObserverVector(), result);
QCOMPARE(CopyObserver::copies, 3);
}
void TestAlgorithms::copied()
{
// Convert the container - specify the complete type
{
auto result = kdalgorithms::copied<QVector<int>>(intVector);
QVector<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// Convert the container - specify only the container
{
auto result = kdalgorithms::copied<std::deque>(intVector);
std::deque<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// Convert the type in the container
{
auto result = kdalgorithms::copied<std::vector<double>>(intVector);
std::vector<double> expected{1.0, 2.0, 3.0, 4.0};
QCOMPARE(result, expected);
}
// unordered_set
{
std::vector<int> vec{1, 2, 3, 4, 1, 3};
auto result = kdalgorithms::copied<std::unordered_set>(vec);
std::unordered_set<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// map converted to vector
{
std::map<int, int> map{{1, 2}, {2, 3}, {4, 5}};
auto result = kdalgorithms::copied<std::vector>(map);
std::vector<std::pair<const int, int>> expected{{1, 2}, {2, 3}, {4, 5}};
QCOMPARE(result, expected);
}
// r-value
{
auto result = kdalgorithms::copied<std::list>(getIntVector());
std::list<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// r-value Convert the container - specify the complete type
{
auto result = kdalgorithms::copied<QVector<int>>(getIntVector());
QVector<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
// count copies
{
std::vector<CopyObserver> result;
CopyObserver::reset();
(void)kdalgorithms::copied<QVector>(getObserverVector());
QCOMPARE(CopyObserver::copies, 0);
CopyObserver::reset();
(void)kdalgorithms::copied<QList>(getConstObserverVector());
QCOMPARE(CopyObserver::copies, 3);
}
}
void TestAlgorithms::filteredSameContainer()
{
auto result = kdalgorithms::filtered(intVector, isOdd);
std::vector<int> expected{1, 3};
QCOMPARE(result, expected);
// source is an r-value
result = kdalgorithms::filtered(getIntVector(), isOdd);
QCOMPARE(result, expected);
}
void TestAlgorithms::filteredChangeContainer()
{
auto result = kdalgorithms::filtered<QVector>(intVector, isOdd);
QVector<int> expected{1, 3};
QCOMPARE(result, expected);
// source is an r-value
result = kdalgorithms::filtered<QVector>(getIntVector(), isOdd);
QCOMPARE(result, expected);
}
void TestAlgorithms::filteredAsMove()
{
auto isOdd = [](const CopyObserver &observer) { return ::isOdd(observer.value); };
// First with the filtered method maintaining the container type.
{
CopyObserver::reset();
auto result = kdalgorithms::filtered(getObserverVector(), isOdd);
QCOMPARE(CopyObserver::copies, 0);
std::vector<CopyObserver> expected{1, 3};
QCOMPARE(result, expected);
CopyObserver::reset();
result = kdalgorithms::filtered(getConstObserverVector(), isOdd);
QCOMPARE(CopyObserver::copies, 2);
QCOMPARE(result, expected);
}
// filtered version changing the container type.
{
CopyObserver::reset();
auto result = kdalgorithms::filtered<QVector>(getObserverVector(), isOdd);
QCOMPARE(CopyObserver::copies, 0);
QVector<CopyObserver> expected{1, 3};
QCOMPARE(result, expected);
CopyObserver::reset();
result = kdalgorithms::filtered<QVector>(getConstObserverVector(), isOdd);
QCOMPARE(CopyObserver::copies, 2);
QCOMPARE(result, expected);
}
}
void TestAlgorithms::filterWithMemberFunction()
{
std::vector<Struct> vec{{1, 2}, {2, 1}, {3, 3}, {4, 4}};
{
auto result = kdalgorithms::filtered(vec, &Struct::hasEqualKeyValuePair);
std::vector<Struct> expected{{3, 3}, {4, 4}};
QCOMPARE(result, expected);
}
{
auto result = kdalgorithms::filtered<std::list>(vec, &Struct::hasEqualKeyValuePair);
std::list<Struct> expected{{3, 3}, {4, 4}};
QCOMPARE(result, expected);
}
}
void TestAlgorithms::filterOtherContainers()
{
{
auto result = kdalgorithms::filtered<std::set>(intVector, isOdd);
std::set<int> expected{1, 3};
QCOMPARE(result, expected);
}
{
#if QT_VERSION >= 0x060000
auto result = kdalgorithms::filtered<QSet>(intVector, isOdd);
QSet<int> expected{1, 3};
QCOMPARE(result, expected);
#endif
}
{
std::map<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto hasOddKey = [](auto item) { return item.first % 2 == 1; };
auto result = kdalgorithms::filtered(map, hasOddKey);
std::map<int, std::string> expected{{1, "abc"}, {3, "hij"}};
QCOMPARE(result, expected);
}
{
QMap<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto hasOddKey = [](auto item) { return item.first % 2 == 1; };
auto result = kdalgorithms::filtered(map, hasOddKey);
QMap<int, std::string> expected{{1, "abc"}, {3, "hij"}};
QCOMPARE(result, expected);
}
}
void TestAlgorithms::filter()
{
{
std::vector<int> vec{1, 2, 3, 4};
kdalgorithms::filter(vec, isOdd);
std::vector<int> expected{1, 3};
QCOMPARE(vec, expected);
}
{
QList<int> intList{1, 2, 3, 4};
kdalgorithms::filter(intList, isOdd);
QList<int> expected{1, 3};
QCOMPARE(intList, expected);
}
}
void TestAlgorithms::transformedChangeContainer()
{
auto result = kdalgorithms::transformed<QVector>(intVector, squareItem);
QVector<int> expected{1, 4, 9, 16};
QCOMPARE(result, expected);
// r-value
result = kdalgorithms::transformed<QVector>(getIntVector(), squareItem);
QCOMPARE(result, expected);
}
void TestAlgorithms::transformedSameContainer()
{
auto result = kdalgorithms::transformed(intVector, squareItem);
std::vector<int> expected{1, 4, 9, 16};
QCOMPARE(result, expected);
// r-value
result = kdalgorithms::transformed(getIntVector(), squareItem);
QCOMPARE(result, expected);
}
void TestAlgorithms::transformedFullContainerTypeSpecified()
{
{
auto result = kdalgorithms::transformed<QStringList>(intVector, toString);
QStringList expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
}
{
auto result = kdalgorithms::transformed<std::list<int>>(structVec, &Struct::sumPairs);
std::list<int> expected{5, 5, 5, 5};
QCOMPARE(result, expected);
}
}
void TestAlgorithms::transformedChangeContainerAndDataType()
{
auto result = kdalgorithms::transformed<QVector>(intVector, toString);
QVector<QString> expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
}
void TestAlgorithms::transformedChangeContainerAndDataType2()
{
QVector<int> vec{1, 2, 3, 4};
const auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed<std::vector>(vec, toString);
std::vector<QString> expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
}
void TestAlgorithms::transformedChangeDataType()
{
auto result = kdalgorithms::transformed(intVector, toString);
std::vector<QString> expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
}
void TestAlgorithms::transformedWithRValue()
{
using Container = ContainerObserver<int>;
auto create = [] {
Container container;
container.push_back(1);
container.push_back(2);
container.push_back(3);
return container;
};
{
// l-value must create a new container
Container container = create();
Container::reset();
Container result = kdalgorithms::transformed(container, squareItem);
QCOMPARE(result.at(0), 1);
QCOMPARE(result.at(1), 4);
QCOMPARE(result.at(2), 9);
QCOMPARE(Container::copies, 1);
}
{
// r-value same container can reuse the container
Container container = create();
Container::reset();
Container result = kdalgorithms::transformed(std::move(container), squareItem);
QCOMPARE(result.at(0), 1);
QCOMPARE(result.at(1), 4);
QCOMPARE(result.at(2), 9);
QCOMPARE(Container::copies, 0);
}
{
// r-value but different container must create a new one
Container container = create();
Container::reset();
QVector<int> result = kdalgorithms::transformed<QVector>(std::move(container), squareItem);
QCOMPARE(result.at(0), 1);
QCOMPARE(result.at(1), 4);
QCOMPARE(result.at(2), 9);
// We obviously won't create a new ContainerObserver, but a QVector instead, so this is
// just to test that that code path works too.
}
}
void TestAlgorithms::transformMemberFunction()
{
{
auto result = kdalgorithms::transformed(structVec, &Struct::sumPairs);
std::vector<int> expected{5, 5, 5, 5};
QCOMPARE(result, expected);
}
{
auto result = kdalgorithms::transformed<std::list>(structVec, &Struct::sumPairs);
std::list<int> expected{5, 5, 5, 5};
QCOMPARE(result, expected);
}
}
void TestAlgorithms::transformOtherContainers()
{
{
auto result = kdalgorithms::transformed<std::set>(intVector, toString);
std::set<QString> expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
}
{
#if QT_VERSION >= 0x060000
auto result = kdalgorithms::transformed<QSet>(intVector, toString);
QSet<QString> expected{"1", "2", "3", "4"};
QCOMPARE(result, expected);
#endif
}
// This unfortunately doesn't work as I have no way to deduce the result type of the transform
// to match a container We would end up with std::vector<std::map<std::pair<int,string>>
// instead we now have kdalgorithms::transformed_to_same_container
/*
{
std::map<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto doubleKeys = [](const std::pair<int, std::string> &item) {
return std::pair(item.first * 2, item.second);
};
auto result = kdalgorithms::transformed(map, doubleKeys);
std::map<int, std::string> expected{{1, "abc"}, {4, "def"}, {6, "hij"}, {8, "klm"}};
QCOMPARE(result, expected);
}
*/
{
std::map<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto doubleKeys = [](const auto &item) {
return std::make_pair(item.first * 2, item.second);
};
auto result = kdalgorithms::transformed_to_same_container(map, doubleKeys);
std::map<int, std::string> expected{{2, "abc"}, {4, "def"}, {6, "hij"}, {8, "klm"}};
QCOMPARE(result, expected);
}
{
QMap<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto doubleKeys = [](const auto &item) {
return std::make_pair(item.first * 2, item.second);
};
auto result = kdalgorithms::transformed_to_same_container(map, doubleKeys);
QMap<int, std::string> expected{{2, "abc"}, {4, "def"}, {6, "hij"}, {8, "klm"}};
QCOMPARE(result, expected);
}
{
auto squareItem = [](int i) { return i * i; };
auto result = kdalgorithms::transformed_to_same_container(intVector, squareItem);
std::vector<int> expected{1, 4, 9, 16};
QCOMPARE(result, expected);
}
{
std::map<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto result = kdalgorithms::transformed_with_new_return_type<std::map<std::string, int>>(
map, [](const auto &item) { return std::make_pair(item.second, item.first); });
std::map<std::string, int> expected{{"abc", 1}, {"def", 2}, {"hij", 3}, {"klm", 4}};
QCOMPARE(result, expected);
}
{
QMap<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
auto result = kdalgorithms::transformed_with_new_return_type<QMap<std::string, int>>(
map, [](const auto &item) { return std::make_pair(item.second, item.first); });
QMap<std::string, int> expected{{"abc", 1}, {"def", 2}, {"hij", 3}, {"klm", 4}};
QCOMPARE(result, expected);
}
{
std::map<int, std::string> map{{1, "abc"}, {2, "abc"}, {3, "hij"}, {4, "klm"}};
auto result =
kdalgorithms::transformed_with_new_return_type<std::multimap<std::string, int>>(
map, [](const auto &item) { return std::make_pair(item.second, item.first); });
std::multimap<std::string, int> expected{{"abc", 1}, {"abc", 2}, {"hij", 3}, {"klm", 4}};
QCOMPARE(result, expected);
}
}
struct StructWithMemberVariable
{
StructWithMemberVariable(int i)
: foo(i)
{
}
int foo;
};
void TestAlgorithms::transformedMemberVariable()
{
std::vector<StructWithMemberVariable> vec;
vec.push_back({1});
vec.push_back({2});
vec.push_back({3});
{
auto res = kdalgorithms::transformed(vec, std::mem_fn(&StructWithMemberVariable::foo));
std::vector<int> expected{1, 2, 3};
QCOMPARE(res, expected);
}
{
auto res = kdalgorithms::transformed(vec, &StructWithMemberVariable::foo);
std::vector<int> expected{1, 2, 3};
QCOMPARE(res, expected);
}
{
auto res = kdalgorithms::transformed<QVector>(vec, &StructWithMemberVariable::foo);
QVector<int> expected{1, 2, 3};
QCOMPARE(res, expected);
}
}
void TestAlgorithms::transformedStaticFunctions()
{
std::vector<std::string> strings{"abc", "def", "hij"};
auto res = kdalgorithms::transformed<QStringList>(strings, &QString::fromStdString);
QStringList expected{"abc", "def", "hij"};
QCOMPARE(res, expected);
}
void TestAlgorithms::transform()
{
std::vector<int> vec{1, 2, 3, 4};
kdalgorithms::transform(vec, squareItem);
std::vector<int> expected{1, 4, 9, 16};
QCOMPARE(vec, expected);
QStringList strings{"abc", "def", "hij"};
auto to_upper = [](const QString &string) { return string.toUpper(); };
kdalgorithms::transform(strings, to_upper);
QStringList expected_strings{"ABC", "DEF", "HIJ"};
QCOMPARE(strings, expected_strings);
}
void TestAlgorithms::filtered_transformed()
{
{ // simple
auto result = kdalgorithms::filtered_transformed(intVector, squareItem, isOdd);
std::vector<int> expected{1, 9};
QCOMPARE(result, expected);
}
{ // member functions
auto result = kdalgorithms::filtered_transformed(structVec, &Struct::sumPairs,
&Struct::isKeyGreaterThanValue);
std::vector<int> expected{5, 5};
QCOMPARE(result, expected);
}
{
// optimized move functions
using Container = ContainerObserver<int>;
auto create = [] {
Container container;
container.push_back(1);
container.push_back(2);
container.push_back(3);
return container;
};
Container container = create();
Container::reset();
Container result =
kdalgorithms::filtered_transformed(std::move(container), squareItem, isOdd);
QCOMPARE(result.size(), 2);
QCOMPARE(result.at(0), 1);
QCOMPARE(result.at(1), 9);
QCOMPARE(Container::copies, 0);
}
{
// transform container
auto result = kdalgorithms::filtered_transformed<QVector>(intVector, squareItem, isOdd);
QVector<int> expected{1, 9};
QCOMPARE(result, expected);
}
{ // specify full return type
auto result = kdalgorithms::filtered_transformed<QStringList>(
intVector, [](int i) { return QString::number(i); }, isOdd);
QStringList expected{"1", "3"};
QCOMPARE(result, expected);
}
}
void TestAlgorithms::anyOf()
{
bool res = kdalgorithms::any_of(intVector, greaterThan(10));
QCOMPARE(res, false);
res = kdalgorithms::any_of(intVector, greaterThan(3));
QCOMPARE(res, true);
res = kdalgorithms::any_of(emptyIntVector, greaterThan(3));
QCOMPARE(res, false);
res = kdalgorithms::any_of(structVec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, true);
std::vector<Struct> vec{{1, 3}, {2, 4}, {3, 5}, {4, 6}};
res = kdalgorithms::any_of(vec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, false);
}
void TestAlgorithms::allOf()
{
bool res = kdalgorithms::all_of(intVector, greaterThan(3));
QCOMPARE(res, false);
res = kdalgorithms::all_of(intVector, greaterThan(0));
QCOMPARE(res, true);
res = kdalgorithms::all_of(emptyIntVector, greaterThan(3));
QCOMPARE(res, true);
res = kdalgorithms::all_of(structVec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, false);
std::vector<Struct> vec{{3, 1}, {2, 1}, {3, 2}, {4, 1}};
res = kdalgorithms::all_of(vec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, true);
}
void TestAlgorithms::noneOf()
{
bool res = kdalgorithms::none_of(intVector, greaterThan(3));
QCOMPARE(res, false);
res = kdalgorithms::none_of(intVector, greaterThan(10));
QCOMPARE(res, true);
res = kdalgorithms::none_of(emptyIntVector, greaterThan(3));
QCOMPARE(res, true);
res = kdalgorithms::none_of(structVec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, false);
std::vector<Struct> vec{{1, 3}, {2, 4}, {3, 5}, {4, 6}};
res = kdalgorithms::none_of(vec, &Struct::isKeyGreaterThanValue);
QCOMPARE(res, true);
}
void TestAlgorithms::anyAllNoneOtherContainers()
{
auto has_key = [](int value) { return [value](auto item) { return item.first == value; }; };
{
std::map<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
QVERIFY(kdalgorithms::any_of(map, has_key(2)));
QVERIFY(!kdalgorithms::any_of(map, has_key(42)));
QVERIFY(!kdalgorithms::all_of(map, has_key(2)));
QVERIFY(kdalgorithms::none_of(map, has_key(42)));
QVERIFY(!kdalgorithms::none_of(map, has_key(2)));
}
{
QMap<int, std::string> map{{1, "abc"}, {2, "def"}, {3, "hij"}, {4, "klm"}};
QVERIFY(kdalgorithms::any_of(map, has_key(2)));
QVERIFY(!kdalgorithms::any_of(map, has_key(42)));
QVERIFY(!kdalgorithms::all_of(map, has_key(2)));
QVERIFY(kdalgorithms::none_of(map, has_key(42)));
QVERIFY(!kdalgorithms::none_of(map, has_key(2)));
}
}
void TestAlgorithms::reverse()
{
std::vector<int> vec{1, 2, 3, 4};
kdalgorithms::reverse(vec);
std::vector<int> expected{4, 3, 2, 1};
QCOMPARE(vec, expected);
}
void TestAlgorithms::reversed()
{
auto result = kdalgorithms::reversed(intVector);
std::vector<int> expected{4, 3, 2, 1};
QCOMPARE(result, expected);
}
void TestAlgorithms::reversedEnsureMoveOnly()
{
std::vector<CopyObserver> vec{1, 2, 3, 4, 5};
CopyObserver::reset();
auto result = kdalgorithms::reversed(std::move(vec));
QCOMPARE(CopyObserver::copies, 0);
// 3 for each pair I need to swap (1,5) (2,3)
QCOMPARE(CopyObserver::moves, 6);
}
void TestAlgorithms::sort()
{
std::vector<int> vec{3, 2, 4, 1};
kdalgorithms::sort(vec);
std::vector<int> expected{1, 2, 3, 4};
QCOMPARE(vec, expected);
}
void TestAlgorithms::sorted()
{
const std::vector<int> vec{3, 2, 4, 1};
auto result = kdalgorithms::sorted(vec);
std::vector<int> expected{1, 2, 3, 4};
QCOMPARE(result, expected);
}
void TestAlgorithms::sortWithCompare()
{
{
std::vector<int> vec{3, 2, 4, 1};
kdalgorithms::sort(vec, std::greater<int>());