NEURON
enumerate.cpp
Go to the documentation of this file.
1 #include <vector>
2 
3 #include "utils/enumerate.h"
4 
5 #include <catch2/catch_test_macros.hpp>
6 
7 
8 TEST_CASE("apply_to_first", "[Neuron]") {
9  std::vector<double> x{1.0, 2.0, 2.0, 3.0};
10 
11  apply_to_first(x, 2.0, [](auto it) { *it = 5.0; });
12  REQUIRE(x == std::vector<double>({1.0, 5.0, 2.0, 3.0}));
13 }
14 
15 TEST_CASE("erase_first", "[Neuron]") {
16  std::vector<double> x{1.0, 2.0, 2.0, 3.0};
17 
18  erase_first(x, 2.0);
19  REQUIRE(x == std::vector<double>({1.0, 2.0, 3.0}));
20 }
21 
22 TEST_CASE("reverse", "[Neuron]") {
23  std::vector<double> x{1.0, 2.0, 3.0};
24 
25  for (auto& i: reverse(x)) {
26  i *= -1.0;
27  }
28  REQUIRE(x == std::vector<double>({-1.0, -2.0, -3.0}));
29 }
30 
31 TEST_CASE("reverse; no-copy", "[Neuron]") {
32  std::vector<double> x{1.0, 2.0, 3.0};
33 
34  auto reverse_iterable = reverse(x);
35 
36  for (auto& xx: x) {
37  xx *= -1.0;
38  }
39 
40  for (const auto& xx: reverse_iterable) {
41  REQUIRE(xx < 0.0);
42  }
43 }
44 
45 TEST_CASE("range", "[Neuron]") {
46  std::vector<double> x{1.0, 2.0, 3.0};
47 
48  std::vector<std::size_t> v{};
49  for (std::size_t i: range(x)) {
50  v.push_back(i);
51  }
52  REQUIRE(v == std::vector<size_t>{0, 1, 2});
53 }
54 
55 TEST_CASE("enumerate", "[Neuron]") {
56  std::vector<double> x{1.0, 2.0, 3.0};
57 
58  int j = 0;
59  for (auto&& [i, elem]: enumerate(x)) {
60  if (i == 0)
61  REQUIRE(elem == 1.0);
62  if (i == 1)
63  REQUIRE(elem == 2.0);
64  if (i == 2)
65  REQUIRE(elem == 3.0);
66  REQUIRE(i == j);
67  ++j;
68  }
69 }
70 
71 TEST_CASE("renumerate", "[Neuron]") {
72  std::vector<double> x{1.0, 2.0, 3.0};
73 
74  int j = x.size() - 1;
75  for (auto&& [i, elem]: renumerate(x)) {
76  if (i == 0)
77  REQUIRE(elem == 1.0);
78  if (i == 1)
79  REQUIRE(elem == 2.0);
80  if (i == 2)
81  REQUIRE(elem == 3.0);
82  REQUIRE(i == j);
83  --j;
84  }
85 }
#define v
Definition: md1redef.h:11
#define i
Definition: md1redef.h:19
TEST_CASE("apply_to_first", "[Neuron]")
Definition: enumerate.cpp:8
void apply_to_first(T &&iterable, value_type &&value, F &&f)
Definition: enumerate.h:12
void erase_first(T &&iterable, value_type &&value)
Definition: enumerate.h:22
constexpr auto reverse(T &&iterable)
Definition: enumerate.h:62
constexpr auto range(T &&iterable)
Definition: enumerate.h:32
constexpr auto renumerate(T &&iterable)
Definition: enumerate.h:120
constexpr auto enumerate(T &&iterable)
Definition: enumerate.h:90
size_t j