forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEST.cc
94 lines (73 loc) · 2.37 KB
/
TEST.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <vector>
#define CATCH_CONFIG_MAIN
#include "../Catch/single_include/catch.hpp"
#include "solution.h"
using vecIter = std::vector<int>::iterator;
ListNode *createLinkedList(vecIter beg, vecIter end)
{
ListNode *head = beg == end ? NULL : new ListNode(*beg++);
for (ListNode *cur = head; beg != end; cur = cur->next)
cur->next = new ListNode(*beg++);
return head;
}
bool compareTwoLinkedList(ListNode *l1, ListNode *l2)
{
for ( ; l1&&l2; l1 = l1->next, l2 = l2->next)
if (l1->val != l2->val) break;
return l1 || l2 ? false : true;
}
void clear(ListNode *head)
{
while (head)
{
ListNode *del = head;
head = head->next;
delete del;
}
}
TEST_CASE("merge two sorted linkedlist", "[mergeTwoLists]")
{
SECTION( "common" )
{
std::vector<int> vec1 = {1,3,5,7,9};
std::vector<int> vec2 = {0,2,4,6,8};
std::vector<int> vec3 = {0,1,2,3,4,5,6,7,8,9};
ListNode *l1 = createLinkedList(vec1.begin(), vec1.end());
ListNode *l2 = createLinkedList(vec2.begin(), vec2.end());
ListNode *l3 = createLinkedList(vec3.begin(), vec3.end());
Solution s;
ListNode *lm = s.mergeTwoLists(l1,l2);
REQUIRE( compareTwoLinkedList(l3, lm) == true );
clear(l1);
clear(l2);
clear(l3);
clear(lm);
}
SECTION( "one linkedlist is empty" )
{
std::vector<int> vec1 = {1,3,5,7,9};
ListNode *l1 = createLinkedList(vec1.begin(), vec1.end());
ListNode *l2 = NULL;
Solution s;
ListNode *lm = s.mergeTwoLists(l1,l2);
REQUIRE( compareTwoLinkedList(l1, lm) == true );
clear(l1);
clear(lm);
}
SECTION( "small and large" )
{
std::vector<int> vec1 = {0,3,5};
std::vector<int> vec2 = {1,2,4,6,7,8,9,10,11,12};
std::vector<int> vec3 = {0,1,2,3,4,5,6,7,8,9,10,11,12};
ListNode *l1 = createLinkedList(vec1.begin(), vec1.end());
ListNode *l2 = createLinkedList(vec2.begin(), vec2.end());
ListNode *l3 = createLinkedList(vec3.begin(), vec3.end());
Solution s;
ListNode *lm = s.mergeTwoLists(l1,l2);
REQUIRE( compareTwoLinkedList(l3, lm) == true );
clear(l1);
clear(l2);
clear(l3);
clear(lm);
}
}