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 <array>
|
---|
31 | #include <complex>
|
---|
32 | #include <initializer_list>
|
---|
33 | #include <numeric>
|
---|
34 | #include <utility>
|
---|
35 |
|
---|
36 | using namespace std::literals;
|
---|
37 | using namespace complex_literals;
|
---|
38 |
|
---|
39 | namespace std::test
|
---|
40 | {
|
---|
41 | bool numeric_test::run(bool report)
|
---|
42 | {
|
---|
43 | report_ = report;
|
---|
44 | start();
|
---|
45 |
|
---|
46 | test_algorithms();
|
---|
47 | test_complex();
|
---|
48 |
|
---|
49 | return end();
|
---|
50 | }
|
---|
51 |
|
---|
52 | const char* numeric_test::name()
|
---|
53 | {
|
---|
54 | return "numeric";
|
---|
55 | }
|
---|
56 |
|
---|
57 | void numeric_test::test_algorithms()
|
---|
58 | {
|
---|
59 | auto data1 = {1, 2, 3, 4, 5};
|
---|
60 |
|
---|
61 | auto res1 = std::accumulate(data1.begin(), data1.end(), 5);
|
---|
62 | test_eq("accumulate pt1", res1, 20);
|
---|
63 |
|
---|
64 | auto res2 = std::accumulate(
|
---|
65 | data1.begin(), data1.end(), 2,
|
---|
66 | [](const auto& lhs, const auto& rhs){
|
---|
67 | return lhs * rhs;
|
---|
68 | }
|
---|
69 | );
|
---|
70 | test_eq("accumulate pt2", res2, 240);
|
---|
71 |
|
---|
72 | auto res3 = std::accumulate(data1.begin(), data1.begin(), 10);
|
---|
73 | test_eq("accumulate pt3", res3, 10);
|
---|
74 |
|
---|
75 | auto data2 = {3, 5, 2, 8, 7};
|
---|
76 | auto data3 = {4, 6, 1, 0, 5};
|
---|
77 |
|
---|
78 | auto res4 = std::inner_product(
|
---|
79 | data2.begin(), data2.end(), data3.begin(), 0
|
---|
80 | );
|
---|
81 | test_eq("inner_product pt1", res4, 79);
|
---|
82 |
|
---|
83 | auto res5 = std::inner_product(
|
---|
84 | data2.begin(), data2.end(), data3.begin(), 10,
|
---|
85 | [](const auto& lhs, const auto& rhs){
|
---|
86 | return lhs + rhs;
|
---|
87 | },
|
---|
88 | [](const auto& lhs, const auto& rhs){
|
---|
89 | return 2 * (lhs + rhs);
|
---|
90 | }
|
---|
91 | );
|
---|
92 | test_eq("inner_product pt2", res5, 92);
|
---|
93 |
|
---|
94 | auto data4 = {1, 3, 2, 4, 5};
|
---|
95 | auto check1 = {1, 4, 6, 10, 15};
|
---|
96 | std::array<int, 5> result{};
|
---|
97 |
|
---|
98 | auto res6 = std::partial_sum(
|
---|
99 | data4.begin(), data4.end(), result.begin()
|
---|
100 | );
|
---|
101 | test_eq(
|
---|
102 | "partial sum pt1",
|
---|
103 | check1.begin(), check1.end(),
|
---|
104 | result.begin(), result.end()
|
---|
105 | );
|
---|
106 | test_eq("partial sum pt2", res6, result.end());
|
---|
107 |
|
---|
108 | auto check2 = {1, 3, 6, 24, 120};
|
---|
109 | auto res7 = std::partial_sum(
|
---|
110 | data4.begin(), data4.end(), result.begin(),
|
---|
111 | [](const auto& lhs, const auto& rhs){
|
---|
112 | return lhs * rhs;
|
---|
113 | }
|
---|
114 | );
|
---|
115 | test_eq(
|
---|
116 | "partial sum pt3",
|
---|
117 | check2.begin(), check2.end(),
|
---|
118 | result.begin(), result.end()
|
---|
119 | );
|
---|
120 | test_eq("partial sum pt4", res7, result.end());
|
---|
121 |
|
---|
122 | auto check3 = {1, 2, -1, 2, 1};
|
---|
123 | auto res8 = std::adjacent_difference(
|
---|
124 | data4.begin(), data4.end(), result.begin()
|
---|
125 | );
|
---|
126 | test_eq(
|
---|
127 | "adjacent_difference pt1",
|
---|
128 | check3.begin(), check3.end(),
|
---|
129 | result.begin(), result.end()
|
---|
130 | );
|
---|
131 | test_eq("adjacent_difference pt2", res8, result.end());
|
---|
132 |
|
---|
133 | auto check4 = {1, 3, 6, 8, 20};
|
---|
134 | auto res9 = std::adjacent_difference(
|
---|
135 | data4.begin(), data4.end(), result.begin(),
|
---|
136 | [](const auto& lhs, const auto& rhs){
|
---|
137 | return lhs * rhs;
|
---|
138 | }
|
---|
139 | );
|
---|
140 | test_eq(
|
---|
141 | "adjacent_difference pt3",
|
---|
142 | check4.begin(), check4.end(),
|
---|
143 | result.begin(), result.end()
|
---|
144 | );
|
---|
145 | test_eq("adjacent_difference pt4", res9, result.end());
|
---|
146 |
|
---|
147 | auto check5 = {4, 5, 6, 7, 8};
|
---|
148 | std::iota(result.begin(), result.end(), 4);
|
---|
149 | test_eq(
|
---|
150 | "iota", check5.begin(), check5.end(),
|
---|
151 | result.begin(), result.end()
|
---|
152 | );
|
---|
153 | }
|
---|
154 |
|
---|
155 | void numeric_test::test_complex()
|
---|
156 | {
|
---|
157 | auto c1 = 1.f + 2.5if;
|
---|
158 | test_eq("complex literals pt1", c1.real(), 1.f);
|
---|
159 | test_eq("complex literals pt2", c1.imag(), 2.5f);
|
---|
160 |
|
---|
161 | std::complex<double> c2{2.0, 0.5};
|
---|
162 | test_eq("complex value initialization", c2, (2.0 + 0.5i));
|
---|
163 |
|
---|
164 | std::complex<double> c3{c2};
|
---|
165 | test_eq("complex copy initialization", c3, (2.0 + 0.5i));
|
---|
166 |
|
---|
167 | std::complex<double> c4{c1};
|
---|
168 | test_eq("complex conversion initialization", c4, (1.0 + 2.5i));
|
---|
169 |
|
---|
170 | test_eq("complex sum", ((1.0 + 2.5i) + (3.0 + 0.5i)), (4.0 + 3.0i));
|
---|
171 | test_eq("complex sub", ((2.0 + 3.0i) - (1.0 + 5.0i)), (1.0 - 2.0i));
|
---|
172 | test_eq("complex mul", ((2.0 + 2.0i) * (2.0 + 3.0i)), (-2.0 + 10.0i));
|
---|
173 | test_eq("complex div", ((2.0 - 1.0i) / (3.0 + 4.0i)), (0.08 - 0.44i));
|
---|
174 | test_eq("complex unary minus", -(1.0 + 1.0i), (-1.0 - 1.0i));
|
---|
175 | test_eq("complex abs", std::abs(2.0 - 4.0i), 20.0);
|
---|
176 | test_eq("complex real", std::real(2.0 + 3.0i), 2.0);
|
---|
177 | test_eq("complex imag", std::imag(2.0 + 3.0i), 3.0);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|