Skip to content

Commit 3b62f1c

Browse files
committed
Add AddFirstFrom and AddLastFrom methods.
1 parent f2b1d8a commit 3b62f1c

11 files changed

+577
-200
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ bld/
2626

2727
# Visual Studio 2015/2017 cache/options directory
2828
.vs/
29+
# Visual Studio Code cache/options directory
30+
.vscode/
2931
# Uncomment if you have tasks that create the project's static files in wwwroot
3032
#wwwroot/
3133

NetFabric.DoubleLinkedList.Tests/AddFirstTests.cs

+60-14
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ public class AddFirstTests
1616
{ new int[] { 2, 3, 4, 5 }, 1, new int[] { 1, 2, 3, 4, 5 } },
1717
};
1818

19-
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>> CollectionData =>
20-
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>>
21-
{
22-
{ new int[] { }, new int[] { }, false, new int[] { } },
23-
{ new int[] { }, new int[] { 1 }, true, new int[] { 1 } },
24-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
25-
{ new int[] { 1 }, new int[] { }, false, new int[] { 1 } },
26-
{ new int[] { 2 }, new int[] { 1 }, true, new int[] { 1, 2 } },
27-
{ new int[] { 5 }, new int[] { 1, 2, 3, 4 }, true, new int[] { 1, 2, 3, 4, 5 } },
28-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, new int[] { 1, 2, 3, 4, 5 } },
29-
{ new int[] { 2, 3, 4, 5 }, new int[] { 1 }, true, new int[] { 1, 2, 3, 4, 5 } },
30-
{ new int[] { 3, 4, 5 }, new int[] { 1, 2 }, true, new int[] { 1, 2, 3, 4, 5 } },
31-
};
32-
3319
[Theory]
3420
[MemberData(nameof(ItemData))]
3521
void AddItem(IEnumerable<int> collection, int item, IReadOnlyCollection<int> expected)
@@ -48,6 +34,20 @@ void AddItem(IEnumerable<int> collection, int item, IReadOnlyCollection<int> exp
4834
list.EnumerateReversed().Should().Equal(expected.Reverse());
4935
}
5036

37+
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>> CollectionData =>
38+
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>>
39+
{
40+
{ new int[] { }, new int[] { }, false, new int[] { } },
41+
{ new int[] { }, new int[] { 1 }, true, new int[] { 1 } },
42+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
43+
{ new int[] { 1 }, new int[] { }, false, new int[] { 1 } },
44+
{ new int[] { 2 }, new int[] { 1 }, true, new int[] { 1, 2 } },
45+
{ new int[] { 5 }, new int[] { 1, 2, 3, 4 }, true, new int[] { 1, 2, 3, 4, 5 } },
46+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, new int[] { 1, 2, 3, 4, 5 } },
47+
{ new int[] { 2, 3, 4, 5 }, new int[] { 1 }, true, new int[] { 1, 2, 3, 4, 5 } },
48+
{ new int[] { 3, 4, 5 }, new int[] { 1, 2 }, true, new int[] { 1, 2, 3, 4, 5 } },
49+
};
50+
5151
[Theory]
5252
[MemberData(nameof(CollectionData))]
5353
void AddCollection(IEnumerable<int> collection, IEnumerable<int> items, bool isMutated, IReadOnlyCollection<int> expected)
@@ -68,5 +68,51 @@ void AddCollection(IEnumerable<int> collection, IEnumerable<int> items, bool isM
6868
list.EnumerateForward().Should().Equal(expected);
6969
list.EnumerateReversed().Should().Equal(expected.Reverse());
7070
}
71+
72+
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IReadOnlyCollection<int>> ListData =>
73+
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IReadOnlyCollection<int>>
74+
{
75+
{ new int[] { }, new int[] { }, false, false, new int[] { } },
76+
{ new int[] { }, new int[] { 1 }, false, true, new int[] { 1 } },
77+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
78+
{ new int[] { 1 }, new int[] { }, false, false, new int[] { 1 } },
79+
{ new int[] { 2 }, new int[] { 1 }, false, true, new int[] { 1, 2 } },
80+
{ new int[] { 5 }, new int[] { 1, 2, 3, 4 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
81+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, false, new int[] { 1, 2, 3, 4, 5 } },
82+
{ new int[] { 2, 3, 4, 5 }, new int[] { 1 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
83+
{ new int[] { 3, 4, 5 }, new int[] { 1, 2 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
84+
{ new int[] { }, new int[] { }, true, false, new int[] { } },
85+
{ new int[] { }, new int[] { 1 }, true, true, new int[] { 1 } },
86+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, true, new int[] { 5, 4, 3, 2, 1 } },
87+
{ new int[] { 1 }, new int[] { }, true, false, new int[] { 1 } },
88+
{ new int[] { 2 }, new int[] { 1 }, true, true, new int[] { 1, 2 } },
89+
{ new int[] { 5 }, new int[] { 1, 2, 3, 4 }, true, true, new int[] { 4, 3, 2, 1, 5 } },
90+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, true, false, new int[] { 1, 2, 3, 4, 5 } },
91+
{ new int[] { 2, 3, 4, 5 }, new int[] { 1 }, true, true, new int[] { 1, 2, 3, 4, 5 } },
92+
{ new int[] { 3, 4, 5 }, new int[] { 1, 2 }, true, true, new int[] { 2, 1, 3, 4, 5 } },
93+
};
94+
95+
[Theory]
96+
[MemberData(nameof(ListData))]
97+
void AddList(IEnumerable<int> collection, IEnumerable<int> items, bool reversed, bool isMutated, IReadOnlyCollection<int> expected)
98+
{
99+
// Arrange
100+
var left = new DoubleLinkedList<int>(collection);
101+
var version = left.Version;
102+
var right = new DoubleLinkedList<int>(items);
103+
104+
// Act
105+
left.AddFirstFrom(right, reversed);
106+
107+
// Assert
108+
left.Count.Should().Be(expected.Count);
109+
if (isMutated)
110+
left.Version.Should().NotBe(version);
111+
else
112+
left.Version.Should().Be(version);
113+
left.EnumerateForward().Should().Equal(expected);
114+
left.EnumerateReversed().Should().Equal(expected.Reverse());
115+
right.Count.Should().Be(0);
116+
}
71117
}
72118
}

NetFabric.DoubleLinkedList.Tests/AddLastTests.cs

+60-14
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ public class AddLastTests
1616
{ new int[] { 1, 2, 3, 4 }, 5, new int[] { 1, 2, 3, 4, 5 } },
1717
};
1818

19-
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>> CollectionData =>
20-
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>>
21-
{
22-
{ new int[] { }, new int[] { }, false, new int[] { } },
23-
{ new int[] { }, new int[] { 1 }, true, new int[] { 1 } },
24-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
25-
{ new int[] { 1 }, new int[] { }, false, new int[] { 1 } },
26-
{ new int[] { 1 }, new int[] { 2 }, true, new int[] { 1, 2 } },
27-
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
28-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, new int[] { 1, 2, 3, 4, 5 } },
29-
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
30-
{ new int[] { 1, 2, 3 }, new int[] { 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
31-
};
32-
3319
[Theory]
3420
[MemberData(nameof(ItemData))]
3521
void AddItem(IEnumerable<int> collection, int item, IReadOnlyCollection<int> expected)
@@ -48,6 +34,20 @@ void AddItem(IEnumerable<int> collection, int item, IReadOnlyCollection<int> exp
4834
list.EnumerateReversed().Should().Equal(expected.Reverse());
4935
}
5036

37+
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>> CollectionData =>
38+
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, IReadOnlyCollection<int>>
39+
{
40+
{ new int[] { }, new int[] { }, false, new int[] { } },
41+
{ new int[] { }, new int[] { 1 }, true, new int[] { 1 } },
42+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
43+
{ new int[] { 1 }, new int[] { }, false, new int[] { 1 } },
44+
{ new int[] { 1 }, new int[] { 2 }, true, new int[] { 1, 2 } },
45+
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
46+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, new int[] { 1, 2, 3, 4, 5 } },
47+
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
48+
{ new int[] { 1, 2, 3 }, new int[] { 4, 5 }, true, new int[] { 1, 2, 3, 4, 5 } },
49+
};
50+
5151
[Theory]
5252
[MemberData(nameof(CollectionData))]
5353
void AddCollection(IEnumerable<int> collection, IEnumerable<int> items, bool isMutated, IReadOnlyCollection<int> expected)
@@ -68,5 +68,51 @@ void AddCollection(IEnumerable<int> collection, IEnumerable<int> items, bool isM
6868
list.EnumerateForward().Should().Equal(expected);
6969
list.EnumerateReversed().Should().Equal(expected.Reverse());
7070
}
71+
72+
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IReadOnlyCollection<int>> ListData =>
73+
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IReadOnlyCollection<int>>
74+
{
75+
{ new int[] { }, new int[] { }, false, false, new int[] { } },
76+
{ new int[] { }, new int[] { 1 }, false, true, new int[] { 1 } },
77+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
78+
{ new int[] { 1 }, new int[] { }, false, false, new int[] { 1 } },
79+
{ new int[] { 1 }, new int[] { 2 }, false, true, new int[] { 1, 2 } },
80+
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
81+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, false, new int[] { 1, 2, 3, 4, 5 } },
82+
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
83+
{ new int[] { 1, 2, 3 }, new int[] { 4, 5 }, false, true, new int[] { 1, 2, 3, 4, 5 } },
84+
{ new int[] { }, new int[] { }, true, false, new int[] { } },
85+
{ new int[] { }, new int[] { 1 }, true, true, new int[] { 1 } },
86+
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 }, true, true, new int[] { 5, 4, 3, 2, 1 } },
87+
{ new int[] { 1 }, new int[] { }, true, false, new int[] { 1 } },
88+
{ new int[] { 1 }, new int[] { 2 }, true, true, new int[] { 1, 2 } },
89+
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, true, true, new int[] { 1, 5, 4, 3, 2 } },
90+
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, true, false, new int[] { 1, 2, 3, 4, 5 } },
91+
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 }, true, true, new int[] { 1, 2, 3, 4, 5 } },
92+
{ new int[] { 1, 2, 3 }, new int[] { 4, 5 }, true, true, new int[] { 1, 2, 3, 5, 4 } },
93+
};
94+
95+
[Theory]
96+
[MemberData(nameof(ListData))]
97+
void AddList(IEnumerable<int> collection, IEnumerable<int> items, bool reverse, bool isMutated, IReadOnlyCollection<int> expected)
98+
{
99+
// Arrange
100+
var left = new DoubleLinkedList<int>(collection);
101+
var version = left.Version;
102+
var right = new DoubleLinkedList<int>(items);
103+
104+
// Act
105+
left.AddLastFrom(right, reverse);
106+
107+
// Assert
108+
left.Count.Should().Be(expected.Count);
109+
if (isMutated)
110+
left.Version.Should().NotBe(version);
111+
else
112+
left.Version.Should().Be(version);
113+
left.EnumerateForward().Should().Equal(expected);
114+
left.EnumerateReversed().Should().Equal(expected.Reverse());
115+
right.Count.Should().Be(0);
116+
}
71117
}
72118
}

NetFabric.DoubleLinkedList.Tests/AppendTests.cs

-66
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,5 @@ void Append(IEnumerable<int> left, IEnumerable<int> right, IReadOnlyCollection<i
4545
result.EnumerateForward().Should().Equal(expected);
4646
result.EnumerateReversed().Should().Equal(expected.Reverse());
4747
}
48-
49-
public static TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IEnumerable<int>> AppendInPlaceData =>
50-
new TheoryData<IEnumerable<int>, IEnumerable<int>, bool, bool, IEnumerable<int>>
51-
{
52-
{ new int[] { }, new int[] { }, false, false, new int[] { } },
53-
{ new int[] { }, new int[] { 1 } , false, false, new int[] { 1 } },
54-
{ new int[] { 1 }, new int[] { }, false, false, new int[] { 1 } },
55-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 } , false, false, new int[] { 1, 2, 3, 4, 5 } },
56-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, false, new int[] { 1, 2, 3, 4, 5 } },
57-
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, false, false, new int[] { 1, 2, 3, 4, 5 } },
58-
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 } , false, false, new int[] { 1, 2, 3, 4, 5 } },
59-
{ new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, false, false, new int[] { 1, 2, 3, 4, 5, 6 } },
60-
{ new int[] { }, new int[] { }, true, false, new int[] { } },
61-
{ new int[] { }, new int[] { 1 } , true, false, new int[] { 1 } },
62-
{ new int[] { 1 }, new int[] { }, true, false, new int[] { 1 } },
63-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 } , true, false, new int[] { 1, 2, 3, 4, 5 } },
64-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, true, false, new int[] { 5, 4, 3, 2, 1 } },
65-
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, true, false, new int[] { 1, 2, 3, 4, 5 } },
66-
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 } , true, false, new int[] { 4, 3, 2, 1, 5 } },
67-
{ new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, true, false, new int[] { 3, 2, 1, 4, 5, 6 } },
68-
{ new int[] { }, new int[] { }, false, true, new int[] { } },
69-
{ new int[] { }, new int[] { 1 } , false, true, new int[] { 1 } },
70-
{ new int[] { 1 }, new int[] { }, false, true, new int[] { 1 } },
71-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 } , false, true, new int[] { 5, 4, 3, 2, 1 } },
72-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, false, true, new int[] { 1, 2, 3, 4, 5 } },
73-
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, false, true, new int[] { 1, 5, 4, 3, 2 } },
74-
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 } , false, true, new int[] { 1, 2, 3, 4, 5 } },
75-
{ new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, false, true, new int[] { 1, 2, 3, 6, 5, 4 } },
76-
{ new int[] { }, new int[] { }, true, true, new int[] { } },
77-
{ new int[] { }, new int[] { 1 } , true, true, new int[] { 1 } },
78-
{ new int[] { 1 }, new int[] { }, true, true, new int[] { 1 } },
79-
{ new int[] { }, new int[] { 1, 2, 3, 4, 5 } , true, true, new int[] { 5, 4, 3, 2, 1 } },
80-
{ new int[] { 1, 2, 3, 4, 5 }, new int[] { }, true, true, new int[] { 5, 4, 3, 2, 1 } },
81-
{ new int[] { 1 }, new int[] { 2, 3, 4, 5 }, true, true, new int[] { 1, 5, 4, 3, 2 } },
82-
{ new int[] { 1, 2, 3, 4 }, new int[] { 5 } , true, true, new int[] { 4, 3, 2, 1, 5 } },
83-
{ new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, true, true, new int[] {3, 2, 1, 6, 5, 4 } },
84-
};
85-
86-
[Theory]
87-
[MemberData(nameof(AppendInPlaceData))]
88-
void AppendInPlace(IEnumerable<int> left, IEnumerable<int> right, bool reverseLeft, bool reverseRight, IReadOnlyCollection<int> expected)
89-
{
90-
// Arrange
91-
var leftList = new DoubleLinkedList<int>(left);
92-
var leftVersion = leftList.Version;
93-
var rightList = new DoubleLinkedList<int>(right);
94-
var rightVersion = rightList.Version;
95-
96-
// Act
97-
var result = DoubleLinkedList.AppendInPlace(leftList, rightList, reverseLeft, reverseRight);
98-
99-
// Assert
100-
leftList.Count.Should().Be(0);
101-
leftList.Version.Should().NotBe(leftVersion);
102-
leftList.EnumerateForward().Should().Equal(Enumerable.Empty<int>());
103-
leftList.EnumerateReversed().Should().Equal(Enumerable.Empty<int>());
104-
105-
rightList.Count.Should().Be(0);
106-
rightList.Version.Should().NotBe(rightVersion);
107-
rightList.EnumerateForward().Should().Equal(Enumerable.Empty<int>());
108-
rightList.EnumerateReversed().Should().Equal(Enumerable.Empty<int>());
109-
110-
result.Count.Should().Be(leftList.Count + rightList.Count);
111-
result.EnumerateForward().Should().Equal(expected);
112-
result.EnumerateReversed().Should().Equal(expected.Reverse());
113-
}
11448
}
11549
}

0 commit comments

Comments
 (0)