Skip to content

Commit b31af57

Browse files
committed
assertContentEquals
1 parent 1e8e092 commit b31af57

8 files changed

+93
-4
lines changed

split-a-circular-linked-list/ArrayToCircularLinkedList.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export auto ArrayToCircularLinkedList(
2020
cur->next = head;
2121
return head;
2222
}
23-
}
23+
}

split-a-circular-linked-list/CircularLinkedListToArray.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ export auto CircularLinkedListToArray(
2121
cur&& array.emplace_back(cur->val);
2222
return array;
2323
}
24-
}
24+
}

split-a-circular-linked-list/ListNode.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export struct ListNode {
1010
{
1111
}
1212
};
13-
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module;
2+
#include <functional>
3+
export module leetcode_test.split_a_circular_linked_list.TraversalCircularListNode;
4+
import leetcode_test.split_a_circular_linked_list.ListNode;
5+
using std::function;
6+
namespace leetcode_test::split_a_circular_linked_list {
7+
export auto TraversalCircularListNode(ListNode* list, function<void(ListNode*)> callback)
8+
{
9+
if (!list)
10+
return;
11+
12+
auto head = list;
13+
auto cur = list;
14+
15+
while (cur && cur->next != head) {
16+
callback(cur);
17+
cur = cur->next;
18+
}
19+
if (cur)
20+
callback(cur);
21+
return;
22+
};
23+
}

split-a-circular-linked-list/index.ixx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ public:
3131
return { list, secondHead };
3232
}
3333
};
34-
}
34+
}

split-a-circular-linked-list/split-a-circular-linked-list.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9191
<WarningLevel>Level3</WarningLevel>
9292
<LanguageStandard>stdcpp20</LanguageStandard>
93+
<CompileAs>CompileAsCppModule</CompileAs>
9394
</ClCompile>
9495
<Link>
9596
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -125,6 +126,8 @@
125126
<ClCompile Include="CircularLinkedListToArray.ixx" />
126127
<ClCompile Include="index.ixx" />
127128
<ClCompile Include="ListNode.ixx" />
129+
<ClCompile Include="test.cpp" />
130+
<ClCompile Include="TraversalCircularListNode.ixx" />
128131
</ItemGroup>
129132
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
130133
<ImportGroup Label="ExtensionTargets">

split-a-circular-linked-list/split-a-circular-linked-list.vcxproj.filters

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
<ClCompile Include="ArrayToCircularLinkedList.ixx">
2222
<Filter>Source Files</Filter>
2323
</ClCompile>
24+
<ClCompile Include="test.cpp">
25+
<Filter>Source Files</Filter>
26+
</ClCompile>
27+
<ClCompile Include="TraversalCircularListNode.ixx" />
2428
</ItemGroup>
2529
</Project>

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <ranges>
2+
import leetcode_test.split_a_circular_linked_list.Solution;
3+
import leetcode_test.split_a_circular_linked_list.ArrayToCircularLinkedList;
4+
import leetcode_test.split_a_circular_linked_list.CircularLinkedListToArray;
5+
#include <gtest/gtest.h>
6+
#include <vector>
7+
using std::ranges::views::transform;
8+
using namespace leetcode_test::split_a_circular_linked_list;
9+
using std::vector;
10+
11+
template <class T>
12+
concept sized = requires(T& t) {
13+
{
14+
t.size()
15+
} -> std::same_as<size_t>;
16+
};
17+
18+
template <class T, typename Y>
19+
concept equalable = requires(T& t, Y& y, size_t i) {
20+
{
21+
t[i] == y[i]
22+
} -> std::same_as<bool>;
23+
};
24+
template <typename T, typename Y>
25+
requires sized<T> and sized<Y> and equalable<T, Y>
26+
auto assertContentEquals(T& left, Y& right)
27+
{
28+
29+
ASSERT_EQ(
30+
left.size(),
31+
right.size());
32+
for (auto i = 0; i < left.size(); ++i) {
33+
ASSERT_EQ(left[i], right[i]);
34+
}
35+
}
36+
TEST(split_a_circular_linked_list, test1)
37+
{
38+
auto input = vector<int> { 1, 5, 7 };
39+
auto output = vector<vector<int>> { { 1, 5 }, { 7 } };
40+
auto* list = ArrayToCircularLinkedList(input);
41+
auto result = Solution().splitCircularLinkedList(list) | transform(CircularLinkedListToArray);
42+
43+
assertContentEquals(result, output);
44+
}
45+
TEST(split_a_circular_linked_list, test2)
46+
{
47+
auto input = vector<int> { 2, 6, 1, 5 };
48+
auto output = vector<vector<int>> { { 2, 6 }, { 1, 5 } };
49+
auto* list = ArrayToCircularLinkedList(input);
50+
auto result = Solution().splitCircularLinkedList(list) | transform(CircularLinkedListToArray);
51+
52+
assertContentEquals(result, output);
53+
}
54+
55+
int main(int argc, char** argv)
56+
{
57+
testing::InitGoogleTest(&argc, argv);
58+
return RUN_ALL_TESTS();
59+
}

0 commit comments

Comments
 (0)