-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_algorithm.cpp
More file actions
60 lines (47 loc) · 1.48 KB
/
test_algorithm.cpp
File metadata and controls
60 lines (47 loc) · 1.48 KB
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
57
58
59
60
#include "common.h"
#include <numeric>
#include <array>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
using namespace boost::phoenix::arg_names;
TEST(Algorithm, Assign)
{
vector<int> v1(10); iota(v1.begin(), v1.end(), 1);
ASSERT_THAT(v1, ElementsAre(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
vector<int> v2;
v2.assign(v1.begin() + 5, v1.end());
ASSERT_THAT(v2, ElementsAre(6, 7, 8, 9, 10));
}
TEST(Algorithm, ReplaceIf1)
{
array<int, 5> a; iota(a.begin(), a.end(), 1);
ASSERT_THAT(a, ElementsAre(1, 2, 3, 4, 5));
replace_if(a.begin(), a.end(), [](int a) { return a % 2 == 0; }, 100);
ASSERT_THAT(a, ElementsAre(1, 100, 3, 100, 5));
}
TEST(Algorithm, ReplaceIf2)
{
array<int, 5> a; iota(a.begin(), a.end(), 1);
ASSERT_THAT(a, ElementsAre(1, 2, 3, 4, 5));
replace_if(a.begin(), a.end(), arg1 % 2 == 0, 100);
ASSERT_THAT(a, ElementsAre(1, 100, 3, 100, 5));
}
TEST(Algorithm, SwapRange)
{
vector<int> v{1, 2, 3, 4, 5};
list<int> l{-1, -2, -3, -4, -5};
swap_ranges(v.begin(), v.begin() + 3, l.begin());
ASSERT_THAT(l, ElementsAre(1, 2, 3, -4, -5));
}
TEST(Algorithm, PartitionCopy)
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int true_arr[5] = { 0 };
int fals_arr[5] = { 0 };
partition_copy(begin(arr), end(arr),
begin(true_arr),
begin(fals_arr),
[](int i) { return i > 5; });
ASSERT_THAT(true_arr, ElementsAre(6, 7, 8, 9, 10));
ASSERT_THAT(fals_arr, ElementsAre(1, 2, 3, 4, 5));
}