|
| 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