Skip to content

Commit 9a5dffe

Browse files
Add comprehensive tests for kf::views::adjacent and kf::views::pairwise (#25)
* Initial plan * Add comprehensive tests for kf::views::adjacent and kf::views::pairwise Co-authored-by: SergiusTheBest <4660722+SergiusTheBest@users.noreply.github.com> * Address PR feedback: Remove std::vector usage and add tests for large adjacent windows Co-authored-by: SergiusTheBest <4660722+SergiusTheBest@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SergiusTheBest <4660722+SergiusTheBest@users.noreply.github.com>
1 parent a414138 commit 9a5dffe

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

test/AdjacentView.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,128 @@ SCENARIO("views::adjacent")
3030
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
3131
}
3232
}
33+
34+
WHEN("create adjacent view for 3 elements")
35+
{
36+
auto view = arr | kf::views::adjacent<3>;
37+
38+
THEN("view contains 2 tuples: (1,2,3)(2,3,4)")
39+
{
40+
std::array<std::tuple<int, int, int>, 2> expected = { { {1, 2, 3}, {2, 3, 4} } };
41+
42+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
43+
}
44+
}
45+
46+
WHEN("create adjacent view for 4 elements")
47+
{
48+
auto view = arr | kf::views::adjacent<4>;
49+
50+
THEN("view contains 1 tuple: (1,2,3,4)")
51+
{
52+
std::array<std::tuple<int, int, int, int>, 1> expected = { { {1, 2, 3, 4} } };
53+
54+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
55+
}
56+
}
57+
}
58+
}
59+
60+
SCENARIO("views::pairwise")
61+
{
62+
GIVEN("array with 5 elements: 10,20,30,40,50")
63+
{
64+
std::array<int, 5> arr = { 10, 20, 30, 40, 50 };
65+
66+
WHEN("create pairwise view")
67+
{
68+
auto view = arr | kf::views::pairwise;
69+
70+
THEN("view contains 4 pairs: (10,20)(20,30)(30,40)(40,50)")
71+
{
72+
std::array<std::tuple<int, int>, 4> expected = { { {10, 20}, {20, 30}, {30, 40}, {40, 50} } };
73+
74+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
75+
}
76+
}
77+
}
78+
}
79+
80+
SCENARIO("views::adjacent edge cases")
81+
{
82+
GIVEN("array with 1 element: 42")
83+
{
84+
std::array<int, 1> arr = { 42 };
85+
86+
WHEN("create adjacent view for 1 element")
87+
{
88+
auto view = arr | kf::views::adjacent<1>;
89+
90+
THEN("view contains 1 tuple: (42)")
91+
{
92+
std::array<std::tuple<int>, 1> expected = { { {42} } };
93+
94+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
95+
}
96+
}
97+
}
98+
99+
GIVEN("array with 2 elements: 5,7")
100+
{
101+
std::array<int, 2> arr = { 5, 7 };
102+
103+
WHEN("create adjacent view for 2 elements")
104+
{
105+
auto view = arr | kf::views::adjacent<2>;
106+
107+
THEN("view contains 1 tuple: (5,7)")
108+
{
109+
std::array<std::tuple<int, int>, 1> expected = { { {5, 7} } };
110+
111+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
112+
}
113+
}
114+
115+
WHEN("create pairwise view")
116+
{
117+
auto view = arr | kf::views::pairwise;
118+
119+
THEN("view contains 1 pair: (5,7)")
120+
{
121+
std::array<std::tuple<int, int>, 1> expected = { { {5, 7} } };
122+
123+
REQUIRE(std::equal(view.begin(), view.end(), expected.begin(), expected.end()));
124+
}
125+
}
126+
}
127+
128+
GIVEN("array with 2 elements but adjacent window of 3")
129+
{
130+
std::array<int, 2> arr = { 10, 20 };
131+
132+
WHEN("create adjacent view for 3 elements")
133+
{
134+
auto view = arr | kf::views::adjacent<3>;
135+
136+
THEN("view is empty since window is larger than input")
137+
{
138+
REQUIRE(view.begin() == view.end());
139+
}
140+
}
141+
}
142+
143+
GIVEN("array with 1 element but adjacent window of 2")
144+
{
145+
std::array<int, 1> arr = { 99 };
146+
147+
WHEN("create pairwise view")
148+
{
149+
auto view = arr | kf::views::pairwise;
150+
151+
THEN("view is empty since window is larger than input")
152+
{
153+
REQUIRE(view.begin() == view.end());
154+
}
155+
}
33156
}
34157
}

0 commit comments

Comments
 (0)