-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-linkedlist-intersect-test.c
107 lines (85 loc) · 2.44 KB
/
12-linkedlist-intersect-test.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "stdafx.h"
#include "LinkedList.h"
#include <assert.h>
void TestIntersect() {
testIntersect();
//todo: failed testIntersect2();
}
void testIntersect(){
// case 1
int a1[] = { 3, 4, 6, 7, 8, 12, 99 };
LinkList head1 = CreateByTail(a1, sizeof(a1) / IntSize);
int a2[] = { 3, 5, 7, 8, 20 };
LinkList head2 = CreateByTail(a2, sizeof(a2) / IntSize);
Node* ret = Intersect(head1, head2);
assert(GetNth(ret, 0) == 3);
assert(GetNth(ret, 1) == 7);
assert(GetNth(ret, 2) == 8);
assert(GetLength(ret) == 3);
// case 2
int b1[] = { 4, 6, 7 };
head1 = CreateByTail(b1, sizeof(b1) / IntSize);
int b2[] = { 3, 5, 7, 8, 20 };
head2 = CreateByTail(b2, sizeof(b2) / IntSize);
ret = Intersect(head1, head2);
assert(GetNth(ret, 0) == 7);
assert(GetLength(ret) == 1);
// case 3
ret = Intersect(head1, NULL);
assert(ret == NULL);
// case 4
ret = Intersect(NULL, head2);
assert(ret == NULL);
// case 5
int c1[] = { 4, 6, 7, 8, 12, 140 };
head1 = CreateByTail(c1, sizeof(c1) / IntSize);
int c2[] = { 3, 5, 7, 8, 20, 110, 111, 112, 123, 140 };
head2 = CreateByTail(c2, sizeof(c2) / IntSize);
ret = Intersect(head1, head2);
assert(GetNth(ret, 0) == 7);
assert(GetNth(ret, 1) == 8);
assert(GetNth(ret, 2) == 140);
assert(GetLength(ret) == 3);
free(head1);
free(head2);
free(ret);
}
void testIntersect2(){
// case 1
int a1[] = { 3, 4, 6, 7, 8, 12, 99 };
LinkList head1 = CreateByTail(a1, sizeof(a1) / IntSize);
int a2[] = { 3, 5, 7, 8, 20 };
LinkList head2 = CreateByTail(a2, sizeof(a2) / IntSize);
Node* ret = Intersect2(head1, head2);
assert(GetNth(ret, 0) == 3);
assert(GetNth(ret, 1) == 7);
assert(GetNth(ret, 2) == 8);
assert(GetLength(ret) == 3);
// case 2
int b1[] = { 4, 6, 7 };
head1 = CreateByTail(b1, sizeof(b1) / IntSize);
int b2[] = { 3, 5, 7, 8, 20 };
head2 = CreateByTail(b2, sizeof(b2) / IntSize);
ret = Intersect2(head1, head2);
assert(GetNth(ret, 0) == 7);
assert(GetLength(ret) == 1);
// case 3
ret = Intersect2(head1, NULL);
assert(ret == NULL);
// case 4
ret = Intersect2(NULL, head2);
assert(ret == NULL);
// case 5
int c1[] = { 4, 6, 7, 8, 12, 140 };
head1 = CreateByTail(c1, sizeof(c1) / IntSize);
int c2[] = { 3, 5, 7, 8, 20, 110, 111, 112, 123, 140 };
head2 = CreateByTail(c2, sizeof(c2) / IntSize);
ret = Intersect2(head1, head2);
assert(GetNth(ret, 0) == 7);
assert(GetNth(ret, 1) == 8);
assert(GetNth(ret, 2) == 140);
assert(GetLength(ret) == 3);
free(head1);
free(head2);
free(ret);
}