source: mainline/uspace/lib/cpp/src/internal/test/numeric.cpp@ 9b5867e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b5867e was 25709c3, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added tests for basic complex arithmetic

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