-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms-numeric.cpp
56 lines (46 loc) · 1.43 KB
/
algorithms-numeric.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#define CATCH_CONFIG_MAIN
#include <array>
#include <functional>
#include "catch.hpp"
using namespace std;
TEST_CASE("partial_sum") {
vector<int> num{1, 2, 3, 4}, result;
partial_sum(num.begin(), num.end(), back_inserter(result));
REQUIRE(result == vector<int>{1, 3, 6, 10});
}
TEST_CASE("adjacent_difference") {
vector<int> fib{1, 1, 2, 3, 5, 8}, fib_diff;
adjacent_difference(fib.begin(), fib.end(), back_inserter(fib_diff));
REQUIRE(fib_diff == vector<int>{1, 0, 1, 1, 2, 3});
}
TEST_CASE("inner_product") {
vector<int> nums1{1, 2, 3, 4, 5};
vector<int> nums2{1, 0, -1, 0, 1};
const auto result =
inner_product(nums1.begin(), nums1.end(), nums2.begin(), 10);
REQUIRE(result == 13);
}
TEST_CASE("reduce") {
vector<int> nums{1, 2, 3};
const auto result1 = reduce(nums.begin(), nums.end(), -1);
REQUIRE(result1 == 5);
const auto result2 = reduce(nums.begin(), nums.end(), 2, multiplies<>());
REQUIRE(result2 == 12);
}
TEST_CASE("accumulate") {
vector<int> nums{1, 2, 3};
const auto result1 = accumulate(nums.begin(), nums.end(), -1);
REQUIRE(result1 == 5);
const auto result2 = accumulate(nums.begin(), nums.end(), 2, multiplies<>{});
REQUIRE(result2 == 12);
}
TEST_CASE("iota") {
array<int, 3> easy_as;
iota(easy_as.begin(), easy_as.end(), 1);
REQUIRE(easy_as == array<int, 3>{1, 2, 3});
}
TEST_CASE("plus") {
plus<short> adder;
REQUIRE(3 == adder(1, 2));
REQUIRE(3 == plus<short>{}(1, 2));
}