Skip to content

Commit 4fd19c6

Browse files
committed
adds task 11.10
1 parent 402a37d commit 4fd19c6

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ include_directories(shared)
6262

6363

6464
##### subdirectories #####
65-
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11)
65+
set(DIRS week-W week-2 week-3 week-4 week-5 week-6 week-7 week-8 week-9 week-10 week-11 week-13)
6666
foreach(DIR ${DIRS})
6767
add_subdirectory(${DIR})
6868
endforeach()

week-13/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(DIRS task-11-10)
2+
foreach(DIR ${DIRS})
3+
add_subdirectory(${DIR})
4+
endforeach()

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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_directories(include)
2+
3+
add_subdirectory(test)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include <ranges>
3+
4+
namespace my_algorithm {
5+
6+
template <std::ranges::range Range, typename T>
7+
requires std::convertible_to<T, typename Range::value_type>
8+
bool has_one(const Range& range, T&& value) {
9+
return std::ranges::find(range, std::forward<T>(value)) != range.end();
10+
}
11+
12+
template <std::ranges::range Range, typename... Args>
13+
requires(std::convertible_to<Args, typename Range::size_type> && ...)
14+
bool any_of(const Range& range, Args&&... args) {
15+
return (has_one(range, std::forward<Args>(args)) || ...);
16+
}
17+
18+
template <std::ranges::range Range, typename... Args>
19+
requires(std::convertible_to<Args, typename Range::size_type> && ...)
20+
bool all_of(const Range& range, Args&&... args) {
21+
return (has_one(range, std::forward<Args>(args)) && ...);
22+
}
23+
24+
template <std::ranges::range Range, typename... Args>
25+
requires(std::convertible_to<Args, typename Range::size_type> && ...)
26+
bool none_of(const Range& range, Args&&... args) {
27+
return (!has_one(range, std::forward<Args>(args)) && ...);
28+
}
29+
30+
} // namespace my_algorithm
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp_test(test-11-10.cpp)
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <list>
4+
5+
#include "smth_of.hpp"
6+
7+
using my_algorithm::all_of;
8+
using my_algorithm::any_of;
9+
using my_algorithm::has_one;
10+
using my_algorithm::none_of;
11+
12+
TEST(HasOne, Array) {
13+
const std::array<std::string, 5> a{"one", "two", "three", "four", "five"};
14+
EXPECT_TRUE(has_one(a, "two"));
15+
EXPECT_FALSE(has_one(a, "six"));
16+
}
17+
18+
TEST(AnyOfAllOfNoneOf, Vector) {
19+
const std::vector<int> vec = {1, 2, 3, 4, 5}; // NOLINT
20+
21+
EXPECT_TRUE(any_of(vec, 2, 6));
22+
EXPECT_FALSE(any_of(vec, 6, 7));
23+
24+
EXPECT_TRUE(all_of(vec, 1, 2, 3));
25+
EXPECT_FALSE(all_of(vec, 1, 2, 6));
26+
27+
EXPECT_TRUE(none_of(vec, 6, 7, 8));
28+
EXPECT_FALSE(none_of(vec, 1, 6, 7));
29+
}
30+
31+
TEST(AnyOfAllOfNoneOf, Set) {
32+
const std::set<int> s = {10, 20, 30, 40}; // NOLINT
33+
34+
EXPECT_TRUE(any_of(s, 20, 50));
35+
EXPECT_FALSE(any_of(s, 50, 60));
36+
37+
EXPECT_TRUE(all_of(s, 10, 20));
38+
EXPECT_FALSE(all_of(s, 10, 50));
39+
40+
EXPECT_TRUE(none_of(s, 50, 60));
41+
EXPECT_FALSE(none_of(s, 20, 60));
42+
}
43+
44+
TEST(AnyOfAllOfNoneOf, List) {
45+
const std::list<int> lst = {5, 10, 15, 20}; // NOLINT
46+
47+
EXPECT_TRUE(any_of(lst, 10, 25));
48+
EXPECT_FALSE(any_of(lst, 30, 35));
49+
50+
EXPECT_TRUE(all_of(lst, 5, 10));
51+
EXPECT_FALSE(all_of(lst, 5, 30));
52+
53+
EXPECT_TRUE(none_of(lst, 25, 30));
54+
EXPECT_FALSE(none_of(lst, 10, 30));
55+
}
56+
57+
TEST(AnyOfAllOfNoneOf, String) {
58+
const std::string str = "hello"; // NOLINT
59+
60+
EXPECT_TRUE(any_of(str, 'h', 'x'));
61+
EXPECT_FALSE(any_of(str, 'x', 'y'));
62+
63+
EXPECT_TRUE(all_of(str, 'h', 'e'));
64+
EXPECT_FALSE(all_of(str, 'h', 'z'));
65+
66+
EXPECT_TRUE(none_of(str, 'x', 'y'));
67+
EXPECT_FALSE(none_of(str, 'h', 'y'));
68+
}

0 commit comments

Comments
 (0)