Skip to content

Commit 56d6e87

Browse files
committed
adds task 11-11
1 parent 8211b51 commit 56d6e87

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

week-13/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(DIRS task-11-10 task-11-16)
1+
set(DIRS task-11-10 task-11-16 task-11-11)
22
foreach(DIR ${DIRS})
33
add_subdirectory(${DIR})
44
endforeach()

week-13/task-11-11/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
namespace my_algorithm {
4+
5+
template <class InputIt, class OutputIt, class Fct, class Pred>
6+
void transform_if(InputIt first, InputIt last, OutputIt dest, Fct transform, Pred pred) {
7+
while (first != last) {
8+
if (pred(*first)) {
9+
*(dest++) = transform(*first);
10+
}
11+
++first;
12+
}
13+
}
14+
15+
} // namespace my_algorithm
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-11-11.cpp)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <list>
4+
5+
#include "transform_if.hpp"
6+
7+
using my_algorithm::transform_if;
8+
9+
TEST(TransformIfTest, SimpleTransformAndFilter) {
10+
const std::vector<int> input = {1, 2, 3, 4, 5};
11+
std::vector<int> output;
12+
13+
transform_if(
14+
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * x; },
15+
[](int x) { return x % 2 == 0; });
16+
17+
const std::vector<int> expected = {4, 16};
18+
EXPECT_EQ(output, expected);
19+
}
20+
21+
TEST(TransformIfTest, StringTransformAndFilter) {
22+
const std::vector<std::string> input = {"apple", "banana", "cherry", "date"};
23+
std::vector<std::string> output;
24+
25+
transform_if(
26+
input.begin(), input.end(), std::back_inserter(output),
27+
[](const std::string& s) {
28+
std::string result = s;
29+
std::transform(result.begin(), result.end(), result.begin(),
30+
[](auto&& e) { return std::toupper(e); });
31+
return result;
32+
},
33+
[](const std::string& s) { return s.length() > 5; }); // NOLINT
34+
35+
const std::vector<std::string> expected = {"BANANA", "CHERRY"};
36+
EXPECT_EQ(output, expected);
37+
}
38+
39+
TEST(TransformIfTest, EmptyInputRange) {
40+
constexpr std::vector<int> input;
41+
std::vector<int> output;
42+
43+
transform_if(
44+
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * 2; },
45+
[](int x) { return x > 0; });
46+
47+
EXPECT_TRUE(output.empty());
48+
}
49+
50+
TEST(TransformIfTest, NoElementsSatisfyPredicate) {
51+
const std::vector<int> input = {1, 3, 5, 7};
52+
std::vector<int> output;
53+
54+
transform_if(
55+
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x * 2; },
56+
[](int x) { return x % 2 == 0; });
57+
58+
EXPECT_TRUE(output.empty());
59+
}
60+
61+
TEST(TransformIfTest, AllElementsSatisfyPredicate) {
62+
const std::vector<int> input = {2, 4, 6, 8};
63+
std::vector<int> output;
64+
65+
transform_if(
66+
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x / 2; },
67+
[](int x) { return x % 2 == 0; });
68+
69+
const std::vector<int> expected = {1, 2, 3, 4};
70+
EXPECT_EQ(output, expected);
71+
}
72+
73+
TEST(TransformIfTest, ListInput) {
74+
const std::list<int> input = {10, 20, 30, 40};
75+
std::vector<int> output;
76+
77+
transform_if(
78+
input.begin(), input.end(), std::back_inserter(output), [](int x) { return x + 1; },
79+
[](int x) { return x > 15; }); // NOLINT
80+
81+
const std::vector<int> expected = {21, 31, 41};
82+
EXPECT_EQ(output, expected);
83+
}

0 commit comments

Comments
 (0)