1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <__bits/test/tests.hpp>
|
---|
30 | #include <algorithm>
|
---|
31 | #include <array>
|
---|
32 | #include <utility>
|
---|
33 |
|
---|
34 | namespace std::test
|
---|
35 | {
|
---|
36 | bool algorithm_test::run(bool report)
|
---|
37 | {
|
---|
38 | report_ = report;
|
---|
39 | start();
|
---|
40 |
|
---|
41 | test_non_modifying();
|
---|
42 |
|
---|
43 | return end();
|
---|
44 | }
|
---|
45 |
|
---|
46 | const char* algorithm_test::name()
|
---|
47 | {
|
---|
48 | return "algorithm";
|
---|
49 | }
|
---|
50 |
|
---|
51 | void algorithm_test::test_non_modifying()
|
---|
52 | {
|
---|
53 | auto data1 = {1, 2, 3, 4, 5};
|
---|
54 | auto res1 = std::all_of(
|
---|
55 | data1.begin(), data1.end(),
|
---|
56 | [](auto x){ return x > 0; }
|
---|
57 | );
|
---|
58 | auto res2 = std::all_of(
|
---|
59 | data1.begin(), data1.end(),
|
---|
60 | [](auto x){ return x < 4; }
|
---|
61 | );
|
---|
62 |
|
---|
63 | test("all_of pt1", res1);
|
---|
64 | test("all_of pt2", !res2);
|
---|
65 |
|
---|
66 | auto res3 = std::any_of(
|
---|
67 | data1.begin(), data1.end(),
|
---|
68 | [](auto x){ return x > 4; }
|
---|
69 | );
|
---|
70 | auto res4 = std::any_of(
|
---|
71 | data1.begin(), data1.end(),
|
---|
72 | [](auto x){ return x == 10; }
|
---|
73 | );
|
---|
74 |
|
---|
75 | test("any_of pt1", res3);
|
---|
76 | test("any_of pt2", !res4);
|
---|
77 |
|
---|
78 | auto res5 = std::none_of(
|
---|
79 | data1.begin(), data1.end(),
|
---|
80 | [](auto x){ return x < 0; }
|
---|
81 | );
|
---|
82 | auto res6 = std::none_of(
|
---|
83 | data1.begin(), data1.end(),
|
---|
84 | [](auto x){ return x == 4; }
|
---|
85 | );
|
---|
86 |
|
---|
87 | test("none_of pt1", res5);
|
---|
88 | test("none_of pt2", !res6);
|
---|
89 |
|
---|
90 | std::array<int, 5> data2{1, 2, 3, 4, 5};
|
---|
91 | auto check1 = {1, 20, 3, 40, 5};
|
---|
92 | std::for_each(
|
---|
93 | data2.begin(), data2.end(),
|
---|
94 | [](auto& x){
|
---|
95 | if (x % 2 == 0)
|
---|
96 | x *= 10;
|
---|
97 | }
|
---|
98 | );
|
---|
99 |
|
---|
100 | test_eq(
|
---|
101 | "for_each", check1.begin(), check1.end(),
|
---|
102 | data2.begin(), data2.end()
|
---|
103 | );
|
---|
104 |
|
---|
105 | auto res7 = std::find(data2.begin(), data2.end(), 40);
|
---|
106 | test_eq("find", res7, &data2[3]);
|
---|
107 |
|
---|
108 | auto res8 = std::find_if(
|
---|
109 | data2.begin(), data2.end(),
|
---|
110 | [](auto x){ return x > 30; }
|
---|
111 | );
|
---|
112 | test_eq("find_if", res8, &data2[3]);
|
---|
113 |
|
---|
114 | auto res9 = std::find_if_not(
|
---|
115 | data2.begin(), data2.end(),
|
---|
116 | [](auto x){ return x < 30; }
|
---|
117 | );
|
---|
118 | test_eq("find_if_not", res9, &data2[3]);
|
---|
119 |
|
---|
120 | // TODO: find_end, find_first_of
|
---|
121 |
|
---|
122 | std::array<int, 7> data3{1, 2, 3, 3, 4, 6, 5};
|
---|
123 | auto res10 = std::adjacent_find(data3.begin(), data3.end());
|
---|
124 | test_eq("adjacent_find pt1", res10, &data3[2]);
|
---|
125 |
|
---|
126 | auto res11 = std::adjacent_find(
|
---|
127 | data3.begin(), data3.end(),
|
---|
128 | [](auto lhs, auto rhs){
|
---|
129 | return rhs < lhs;
|
---|
130 | }
|
---|
131 | );
|
---|
132 | test_eq("adjacent_find pt2", res11, &data3[5]);
|
---|
133 |
|
---|
134 | auto res12 = std::count(data3.begin(), data3.end(), 3);
|
---|
135 | test_eq("count", res12, 2);
|
---|
136 |
|
---|
137 | auto res13 = std::count_if(
|
---|
138 | data3.begin(), data3.end(),
|
---|
139 | [](auto x){
|
---|
140 | return x % 2 == 1;
|
---|
141 | }
|
---|
142 | );
|
---|
143 | test_eq("count_if", res13, 4);
|
---|
144 |
|
---|
145 | std::array<int, 6> data4{1, 2, 3, 4, 5, 6};
|
---|
146 | std::array<int, 6> data5{1, 2, 3, 4, 6, 5};
|
---|
147 | auto res14 = std::mismatch(
|
---|
148 | data4.begin(), data4.end(),
|
---|
149 | data5.begin(), data5.end()
|
---|
150 | );
|
---|
151 |
|
---|
152 | test_eq("mismatch pt1", res14.first, &data4[4]);
|
---|
153 | test_eq("mismatch pt2", res14.second, &data5[4]);
|
---|
154 |
|
---|
155 | auto res15 = std::equal(
|
---|
156 | data4.begin(), data4.end(),
|
---|
157 | data4.begin(), data4.end()
|
---|
158 | );
|
---|
159 | test("equal pt1", res15);
|
---|
160 |
|
---|
161 | auto res16 = std::equal(
|
---|
162 | data4.begin(), data4.end(),
|
---|
163 | data5.begin(), data5.end()
|
---|
164 | );
|
---|
165 | test("equal pt2", !res16);
|
---|
166 |
|
---|
167 | auto res17 = std::equal(
|
---|
168 | data4.begin(), data4.end(),
|
---|
169 | data4.begin(), data4.end(),
|
---|
170 | [](auto lhs, auto rhs){
|
---|
171 | return lhs == rhs;
|
---|
172 | }
|
---|
173 | );
|
---|
174 | test("equal pt3", res17);
|
---|
175 |
|
---|
176 | auto res18 = std::equal(
|
---|
177 | data4.begin(), data4.end(),
|
---|
178 | data5.begin(), data5.end(),
|
---|
179 | [](auto lhs, auto rhs){
|
---|
180 | return lhs == rhs;
|
---|
181 | }
|
---|
182 | );
|
---|
183 | test("equal pt4", !res18);
|
---|
184 |
|
---|
185 | // TODO: is_permutation, search
|
---|
186 | }
|
---|
187 | }
|
---|