source: mainline/uspace/lib/cpp/src/__bits/test/numeric.cpp@ c6f23a7

Last change on this file since c6f23a7 was b57ba05, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update headers in C++ files

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