Skip to content

Commit a011125

Browse files
Added collection tests.
1 parent c9b7e37 commit a011125

7 files changed

Lines changed: 185 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using FluentAssertions;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace Open.Collections.Tests;
9+
public abstract class BasicCollectionTests<TCollection>
10+
where TCollection : ICollection<int>
11+
{
12+
public BasicCollectionTests(TCollection collection)
13+
{
14+
Collection = collection;
15+
}
16+
17+
protected readonly TCollection Collection;
18+
19+
[Fact]
20+
public void Add()
21+
{
22+
if(Collection.IsReadOnly)
23+
{
24+
Assert.Throws<Exception>(() => Collection.Add(1));
25+
return;
26+
}
27+
28+
int count = Collection.Count;
29+
Collection.Add(1);
30+
Collection.Count.Should().Be(count + 1);
31+
}
32+
33+
[Fact]
34+
public void Clear()
35+
{
36+
if (Collection.IsReadOnly)
37+
{
38+
Assert.Throws<Exception>(() => Collection.Clear());
39+
return;
40+
}
41+
42+
Collection.Add(1);
43+
Collection.Add(2);
44+
Collection.Clear();
45+
Collection.Count.Should().Be(0);
46+
}
47+
48+
[Fact]
49+
public void Contains()
50+
{
51+
int search;
52+
if (Collection.IsReadOnly)
53+
{
54+
search = Collection.LastOrDefault();
55+
if (search == 0) return;
56+
}
57+
else
58+
{
59+
Collection.Add(1);
60+
Collection.Add(2);
61+
Collection.Add(3);
62+
search = 2;
63+
}
64+
Collection.Contains(2).Should().BeTrue();
65+
}
66+
67+
[Fact]
68+
public void CopyTo()
69+
{
70+
if(!Collection.IsReadOnly)
71+
{
72+
Collection.Add(1);
73+
Collection.Add(2);
74+
}
75+
76+
int[] copy = new int[Collection.Count];
77+
Collection.CopyTo(copy, 0);
78+
copy.Should().BeEquivalentTo(Collection);
79+
}
80+
81+
[Fact]
82+
public void Remove()
83+
{
84+
if (Collection.IsReadOnly)
85+
{
86+
Assert.Throws<Exception>(() => Collection.Remove(1));
87+
return;
88+
}
89+
90+
Collection.Add(1);
91+
Collection.Add(2);
92+
Collection.Remove(1).Should().BeTrue();
93+
Collection.Remove(int.MaxValue).Should().BeFalse();
94+
}
95+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using FluentAssertions;
2+
using System;
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
namespace Open.Collections.Tests;
7+
public abstract class BasicListTests<TList> : BasicCollectionTests<TList>
8+
where TList : IList<int>
9+
{
10+
protected BasicListTests(TList list) : base(list)
11+
{
12+
}
13+
14+
[Fact]
15+
public void SetAndCheck()
16+
{
17+
if (Collection.IsReadOnly) return;
18+
19+
Collection.Add(10);
20+
Collection.IndexOf(10).Should().Be(Collection.Count-1);
21+
int value = Collection[0];
22+
Collection.IndexOf(int.MaxValue).Should().Be(-1);
23+
Collection[0] = int.MaxValue;
24+
Collection[0].Should().Be(int.MaxValue);
25+
Collection.IndexOf(int.MaxValue).Should().Be(0);
26+
Collection[0] = value;
27+
Collection[0].Should().Be(value);
28+
}
29+
30+
[Fact]
31+
public void InsertAndRemoveAt()
32+
{
33+
if (Collection.IsReadOnly)
34+
{
35+
Assert.Throws<Exception>(() => Collection.Insert(0, 1));
36+
Assert.Throws<Exception>(() => Collection.RemoveAt(0));
37+
return;
38+
}
39+
40+
Collection.Insert(0, 1);
41+
Collection.Insert(0, 2);
42+
Collection.Insert(1, 3);
43+
Collection[0].Should().Be(2);
44+
Collection[1].Should().Be(3);
45+
Collection[2].Should().Be(1);
46+
Collection.RemoveAt(1);
47+
Collection[1].Should().Be(1);
48+
}
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Open.Collections.Synchronized;
2+
3+
namespace Open.Collections.Tests.Collections;
4+
public class LockSyncLinkedListTests : BasicCollectionTests<LockSynchronizedLinkedList<int>>
5+
{
6+
public LockSyncLinkedListTests() : base(new LockSynchronizedLinkedList<int>())
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Open.Collections.Synchronized;
2+
3+
namespace Open.Collections.Tests.Collections;
4+
public class LockSyncListTests : BasicListTests<LockSynchronizedList<int>>
5+
{
6+
public LockSyncListTests() : base(new LockSynchronizedList<int>())
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Open.Collections.Synchronized;
2+
3+
namespace Open.Collections.Tests.Collections;
4+
public class ReadWriteSyncLinkedListTests : BasicCollectionTests<LockSynchronizedLinkedList<int>>
5+
{
6+
public ReadWriteSyncLinkedListTests() : base(new LockSynchronizedLinkedList<int>())
7+
{
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Open.Collections.Synchronized;
2+
3+
namespace Open.Collections.Tests.Collections;
4+
public class ReadWriteSyncListTests : BasicListTests<ReadWriteSynchronizedList<int>>
5+
{
6+
public ReadWriteSyncListTests() : base(new ReadWriteSynchronizedList<int>())
7+
{
8+
}
9+
}

testing/Open.Collections.Tests/Open.Collections.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="coverlet.msbuild" Version="3.1.2">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="FluentAssertions" Version="6.5.1" />
1015
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
1116
<PackageReference Include="xunit" Version="2.4.1" />
1217
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

0 commit comments

Comments
 (0)