Skip to content

Commit a898bfb

Browse files
committed
Update test.cpp
1 parent b31af57 commit a898bfb

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

split-a-circular-linked-list/test.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,79 @@
22
import leetcode_test.split_a_circular_linked_list.Solution;
33
import leetcode_test.split_a_circular_linked_list.ArrayToCircularLinkedList;
44
import leetcode_test.split_a_circular_linked_list.CircularLinkedListToArray;
5+
import leetcode_test.split_a_circular_linked_list.TraversalCircularListNode;
56
#include <gtest/gtest.h>
7+
#include <iterator>
8+
#include <unordered_set>
69
#include <vector>
710
using std::ranges::views::transform;
811
using namespace leetcode_test::split_a_circular_linked_list;
12+
using namespace std;
913
using std::vector;
10-
1114
template <class T>
12-
concept sized = requires(T& t) {
15+
concept sizable = requires(T& t) {
1316
{
1417
t.size()
1518
} -> std::same_as<size_t>;
1619
};
20+
template <class T>
21+
concept iterable = requires(T& t) {
22+
++t.begin();
23+
{
24+
t.begin() != t.end()
25+
26+
} -> std::same_as<bool>;
27+
};
1728

1829
template <class T, typename Y>
1930
concept equalable = requires(T& t, Y& y, size_t i) {
2031
{
21-
t[i] == y[i]
32+
*t.begin() == *y.begin()
2233
} -> std::same_as<bool>;
2334
};
2435
template <typename T, typename Y>
25-
requires sized<T> and sized<Y> and equalable<T, Y>
36+
requires sizable<T> and sizable<Y> and equalable<T, Y> and iterable<T> and iterable<Y>
2637
auto assertContentEquals(T& left, Y& right)
2738
{
2839

2940
ASSERT_EQ(
3041
left.size(),
3142
right.size());
32-
for (auto i = 0; i < left.size(); ++i) {
33-
ASSERT_EQ(left[i], right[i]);
43+
auto a = left.begin();
44+
auto b = right.begin();
45+
for (; b != right.end() && a != left.end(); ++a, ++b) {
46+
47+
ASSERT_EQ(*a, *b);
3448
}
3549
}
3650
TEST(split_a_circular_linked_list, test1)
3751
{
3852
auto input = vector<int> { 1, 5, 7 };
3953
auto output = vector<vector<int>> { { 1, 5 }, { 7 } };
4054
auto* list = ArrayToCircularLinkedList(input);
55+
56+
auto nodes = unordered_set<ListNode*> {};
57+
TraversalCircularListNode(list, [&](auto* node) { nodes.insert(node); });
4158
auto result = Solution().splitCircularLinkedList(list) | transform(CircularLinkedListToArray);
4259

4360
assertContentEquals(result, output);
61+
62+
for (auto* node : nodes) {
63+
delete node;
64+
}
4465
}
4566
TEST(split_a_circular_linked_list, test2)
4667
{
4768
auto input = vector<int> { 2, 6, 1, 5 };
4869
auto output = vector<vector<int>> { { 2, 6 }, { 1, 5 } };
4970
auto* list = ArrayToCircularLinkedList(input);
5071
auto result = Solution().splitCircularLinkedList(list) | transform(CircularLinkedListToArray);
51-
72+
auto nodes = unordered_set<ListNode*> {};
73+
TraversalCircularListNode(list, [&](auto* node) { nodes.insert(node); });
5274
assertContentEquals(result, output);
75+
for (auto* node : nodes) {
76+
delete node;
77+
}
5378
}
5479

5580
int main(int argc, char** argv)

0 commit comments

Comments
 (0)