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 <array>
|
---|
30 | #include <initializer_list>
|
---|
31 | #include <internal/test/tests.hpp>
|
---|
32 | #include <numeric>
|
---|
33 | #include <utility>
|
---|
34 |
|
---|
35 | namespace std::test
|
---|
36 | {
|
---|
37 | bool numeric_test::run(bool report)
|
---|
38 | {
|
---|
39 | report_ = report;
|
---|
40 | start();
|
---|
41 |
|
---|
42 | test_algorithms();
|
---|
43 |
|
---|
44 | return end();
|
---|
45 | }
|
---|
46 |
|
---|
47 | const char* numeric_test::name()
|
---|
48 | {
|
---|
49 | return "numeric";
|
---|
50 | }
|
---|
51 |
|
---|
52 | void numeric_test::test_algorithms()
|
---|
53 | {
|
---|
54 | auto data1 = {1, 2, 3, 4, 5};
|
---|
55 |
|
---|
56 | auto res1 = std::accumulate(data1.begin(), data1.end(), 5);
|
---|
57 | test_eq("accumulate pt1", res1, 20);
|
---|
58 |
|
---|
59 | auto res2 = std::accumulate(
|
---|
60 | data1.begin(), data1.end(), 2,
|
---|
61 | [](const auto& lhs, const auto& rhs){
|
---|
62 | return lhs * rhs;
|
---|
63 | }
|
---|
64 | );
|
---|
65 | test_eq("accumulate pt2", res2, 240);
|
---|
66 |
|
---|
67 | auto res3 = std::accumulate(data1.begin(), data1.begin(), 10);
|
---|
68 | test_eq("accumulate pt3", res3, 10);
|
---|
69 |
|
---|
70 | auto data2 = {3, 5, 2, 8, 7};
|
---|
71 | auto data3 = {4, 6, 1, 0, 5};
|
---|
72 |
|
---|
73 | auto res4 = std::inner_product(
|
---|
74 | data2.begin(), data2.end(), data3.begin(), 0
|
---|
75 | );
|
---|
76 | test_eq("inner_product pt1", res4, 79);
|
---|
77 |
|
---|
78 | auto res5 = std::inner_product(
|
---|
79 | data2.begin(), data2.end(), data3.begin(), 10,
|
---|
80 | [](const auto& lhs, const auto& rhs){
|
---|
81 | return lhs + rhs;
|
---|
82 | },
|
---|
83 | [](const auto& lhs, const auto& rhs){
|
---|
84 | return 2 * (lhs + rhs);
|
---|
85 | }
|
---|
86 | );
|
---|
87 | test_eq("inner_product pt2", res5, 92);
|
---|
88 |
|
---|
89 | auto data4 = {1, 3, 2, 4, 5};
|
---|
90 | auto check1 = {1, 4, 6, 10, 15};
|
---|
91 | std::array<int, 5> result{};
|
---|
92 |
|
---|
93 | auto res6 = std::partial_sum(
|
---|
94 | data4.begin(), data4.end(), result.begin()
|
---|
95 | );
|
---|
96 | test_eq(
|
---|
97 | "partial sum pt1",
|
---|
98 | check1.begin(), check1.end(),
|
---|
99 | result.begin(), result.end()
|
---|
100 | );
|
---|
101 | test_eq("partial sum pt2", res6, result.end());
|
---|
102 |
|
---|
103 | auto check2 = {1, 3, 6, 24, 120};
|
---|
104 | auto res7 = std::partial_sum(
|
---|
105 | data4.begin(), data4.end(), result.begin(),
|
---|
106 | [](const auto& lhs, const auto& rhs){
|
---|
107 | return lhs * rhs;
|
---|
108 | }
|
---|
109 | );
|
---|
110 | test_eq(
|
---|
111 | "partial sum pt3",
|
---|
112 | check2.begin(), check2.end(),
|
---|
113 | result.begin(), result.end()
|
---|
114 | );
|
---|
115 | test_eq("partial sum pt4", res7, result.end());
|
---|
116 |
|
---|
117 | auto check3 = {1, 2, -1, 2, 1};
|
---|
118 | auto res8 = std::adjacent_difference(
|
---|
119 | data4.begin(), data4.end(), result.begin()
|
---|
120 | );
|
---|
121 | test_eq(
|
---|
122 | "adjacent_difference pt1",
|
---|
123 | check3.begin(), check3.end(),
|
---|
124 | result.begin(), result.end()
|
---|
125 | );
|
---|
126 | test_eq("adjacent_difference pt2", res8, result.end());
|
---|
127 |
|
---|
128 | auto check4 = {1, 3, 6, 8, 20};
|
---|
129 | auto res9 = std::adjacent_difference(
|
---|
130 | data4.begin(), data4.end(), result.begin(),
|
---|
131 | [](const auto& lhs, const auto& rhs){
|
---|
132 | return lhs * rhs;
|
---|
133 | }
|
---|
134 | );
|
---|
135 | test_eq(
|
---|
136 | "adjacent_difference pt3",
|
---|
137 | check4.begin(), check4.end(),
|
---|
138 | result.begin(), result.end()
|
---|
139 | );
|
---|
140 | test_eq("adjacent_difference pt4", res9, result.end());
|
---|
141 |
|
---|
142 | auto check5 = {4, 5, 6, 7, 8};
|
---|
143 | std::iota(result.begin(), result.end(), 4);
|
---|
144 | test_eq(
|
---|
145 | "iota", check5.begin(), check5.end(),
|
---|
146 | result.begin(), result.end()
|
---|
147 | );
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|