-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex.3.39.cpp
64 lines (47 loc) · 1.59 KB
/
ex.3.39.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
57
58
59
60
61
62
63
64
// ex.3.39
// en: Solve Exercise 3.38, but make copies of the nodes that pass the
// test and return a link to a list containing those nodes, in the
// order that they appear in the original list.
// ru: Выполните упражнение 3.38, но создайте копии узлов, которые
// прошли проверку и возвратите ссылку на список, содержащий эти узлы,
// в порядке их следования.
#include <catch.hpp>
#include <psv/list.h>
using psv::Node;
using psv::Make;
using psv::Free;
using psv::Equal;
using psv::CopyIf;
TEST_CASE("copy odd nodes") {
const int N = 6;
auto l1 = Make<int, N>({{0, 1, 2, 3, 4, 5}});
auto l2 =
CopyIf(l1, [](const Node<int>* n) { return n->value() % 2 == 0; });
REQUIRE(Equal<N>(l2, {{0, 2, 4}}));
Free(l1);
Free(l2);
}
TEST_CASE("copy half of list") {
const int N = 6;
auto l1 = Make<int, N>({{0, 1, 2, 3, 4, 5}});
auto l2 = CopyIf(l1, [](const Node<int>* n) { return n->value() > 2; });
REQUIRE(Equal<N>(l2, {{3, 4, 5}}));
Free(l1);
Free(l2);
}
TEST_CASE("copy one specific node") {
const int N = 6;
auto l1 = Make<int, N>({{0, 1, 2, 3, 4, 5}});
auto l2 = CopyIf(l1, [](const Node<int>* n) { return n->value() == 0; });
REQUIRE(Equal<N>(l2, {{0}}));
Free(l1);
Free(l2);
}
TEST_CASE("copy last node") {
const int N = 1;
auto l1 = Make<int, N>({{0}});
auto l2 = CopyIf(l1, [](const Node<int>* n) { return n->value() == 0; });
REQUIRE(Equal<N>(l2, {{0}}));
Free(l1);
Free(l2);
}