Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 57cb461

Browse files
committed
Improve code coverage of ImmutableArray
Scratching an itch... brings the sequence and branch coverage of ImmutableArray and ImmutableArray<T> to within a few branches of 100%.
1 parent 3cb3df1 commit 57cb461

File tree

3 files changed

+112
-13
lines changed

3 files changed

+112
-13
lines changed

src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ public void AddRangeImmutableArray()
129129

130130
builder1.AddRange(array);
131131
Assert.Equal(new[] { 1, 2, 3 }, builder1);
132+
133+
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((int[])null));
134+
Assert.Throws<ArgumentNullException>(() => builder1.AddRange(null, 42));
135+
Assert.Throws<ArgumentOutOfRangeException>(() => builder1.AddRange(new int[0], -1));
136+
Assert.Throws<IndexOutOfRangeException>(() => builder1.AddRange(new int[0], 42));
137+
138+
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((ImmutableArray<int>.Builder)null));
139+
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((IEnumerable<int>)null));
140+
141+
Assert.Throws<NullReferenceException>(() => builder1.AddRange(default(ImmutableArray<int>)));
142+
builder1.AddRange(default(ImmutableArray<int>), 42);
143+
144+
var builder2 = new ImmutableArray<object>.Builder();
145+
builder2.AddRange(default(ImmutableArray<string>));
146+
Assert.Throws<ArgumentNullException>(() => builder2.AddRange((ImmutableArray<string>.Builder)null));
132147
}
133148

134149
[Fact]
@@ -406,6 +421,20 @@ public void ToImmutable()
406421
Assert.True(builder.ToImmutable().IsEmpty);
407422
}
408423

424+
[Fact]
425+
public void CopyTo()
426+
{
427+
var builder = ImmutableArray.Create(1, 2, 3).ToBuilder();
428+
var target = new int[4];
429+
430+
builder.CopyTo(target, 1);
431+
Assert.Equal(new[] { 0, 1, 2, 3 }, target);
432+
433+
Assert.Throws<ArgumentNullException>(() => builder.CopyTo(null, 0));
434+
Assert.Throws<ArgumentOutOfRangeException>(() => builder.CopyTo(target, -1));
435+
Assert.Throws<ArgumentOutOfRangeException>(() => builder.CopyTo(target, 2));
436+
}
437+
409438
[Fact]
410439
public void Clear()
411440
{

src/System.Collections.Immutable/tests/ImmutableArrayTest.cs

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ public class ImmutableArrayTest : SimpleElementImmutablesTestBase
1919
private static readonly ImmutableArray<GenericParameterHelper> s_oneElementRefType = ImmutableArray.Create(new GenericParameterHelper(1));
2020
private static readonly ImmutableArray<string> s_twoElementRefTypeWithNull = ImmutableArray.Create("1", null);
2121

22+
[Fact]
23+
public void Clear()
24+
{
25+
Assert.Equal(ImmutableArray<int>.Empty, ImmutableArray.Create<int>().Clear());
26+
Assert.Equal(ImmutableArray<int>.Empty, ImmutableArray.Create<int>(1).Clear());
27+
Assert.Equal(ImmutableArray<int>.Empty, ImmutableArray.Create<int>(1, 2, 3).Clear());
28+
}
29+
2230
[Fact]
2331
public void CreateEmpty()
2432
{
25-
Assert.Equal(ImmutableArray.Create<int>(), ImmutableArray<int>.Empty);
33+
Assert.Equal(ImmutableArray<int>.Empty, ImmutableArray.Create<int>());
34+
Assert.Equal(ImmutableArray<int>.Empty, ImmutableArray.Create<int>(new int[0]));
2635
}
2736

2837
[Fact]
@@ -534,12 +543,17 @@ public void ObjectEnumerator()
534543
enumerator = ((IEnumerable<int>)s_manyElements).GetEnumerator();
535544
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
536545

537-
Assert.True(enumerator.MoveNext());
538-
Assert.Equal(1, enumerator.Current);
539-
Assert.True(enumerator.MoveNext());
540-
Assert.Equal(2, enumerator.Current);
541-
Assert.True(enumerator.MoveNext());
542-
Assert.Equal(3, enumerator.Current);
546+
for (int i = 0; i < 2; i++)
547+
{
548+
Assert.True(enumerator.MoveNext());
549+
Assert.Equal(1, enumerator.Current);
550+
Assert.True(enumerator.MoveNext());
551+
Assert.Equal(2, enumerator.Current);
552+
Assert.True(enumerator.MoveNext());
553+
Assert.Equal(3, enumerator.Current);
554+
if (i == 0)
555+
enumerator.Reset();
556+
}
543557

544558
Assert.False(enumerator.MoveNext());
545559
Assert.Throws<InvalidOperationException>(() => enumerator.Current);
@@ -641,6 +655,9 @@ public void AddRange()
641655

642656
Assert.Equal(new[] { 1, 2, 3, 4 }, s_manyElements.AddRange(new[] { 4 }));
643657
Assert.Equal(new[] { 1, 2, 3, 4, 5 }, s_manyElements.AddRange(new[] { 4, 5 }));
658+
659+
Assert.Equal(new[] { 1, 2, 3, 4 }, s_manyElements.AddRange(ImmutableArray.Create(4)));
660+
Assert.Equal(new[] { 1, 2, 3, 4, 5 }, s_manyElements.AddRange(ImmutableArray.Create(4, 5)));
644661
}
645662

646663
[Fact]
@@ -727,6 +744,8 @@ public void InsertRangeEmpty()
727744
Assert.Equal(s_manyElements, s_manyElements.InsertRange(0, Enumerable.Empty<int>()));
728745
Assert.Throws<ArgumentOutOfRangeException>(() => s_empty.InsertRange(1, s_oneElement));
729746
Assert.Throws<ArgumentOutOfRangeException>(() => s_empty.InsertRange(-1, s_oneElement));
747+
Assert.Throws<ArgumentOutOfRangeException>(() => s_empty.InsertRange(1, (IEnumerable<int>)s_oneElement));
748+
Assert.Throws<ArgumentOutOfRangeException>(() => s_empty.InsertRange(-1, (IEnumerable<int>)s_oneElement));
730749
}
731750

732751
[Fact]
@@ -795,6 +814,14 @@ public void InsertRangeRight()
795814
Assert.Equal(new[] { 1, 2, 3, 7, 8 }, s_manyElements.InsertRange(3, new[] { 7, 8 }));
796815
}
797816

817+
[Fact]
818+
public void InsertRangeImmutableArray()
819+
{
820+
Assert.Equal(new[] { 7, 8, 1, 2, 3 }, s_manyElements.InsertRange(0, ImmutableArray.Create(7, 8)));
821+
Assert.Equal(new[] { 1, 7, 2, 3 }, s_manyElements.InsertRange(1, ImmutableArray.Create(7)));
822+
Assert.Equal(new[] { 1, 2, 3, 7 }, s_manyElements.InsertRange(3, ImmutableArray.Create(7)));
823+
}
824+
798825
[Fact]
799826
public void RemoveAt()
800827
{
@@ -960,6 +987,20 @@ public void CopyToArray()
960987
}
961988
}
962989

990+
[Fact]
991+
public void CopyToArrayInt()
992+
{
993+
var source = ImmutableArray.Create(1, 2, 3);
994+
var target = new int[4];
995+
source.CopyTo(target, 1);
996+
Assert.Equal(new[] { 0, 1, 2, 3 }, target);
997+
998+
Assert.Throws<NullReferenceException>(() => s_emptyDefault.CopyTo(target, 0));
999+
Assert.Throws<ArgumentNullException>(() => source.CopyTo(null, 0));
1000+
Assert.Throws<ArgumentOutOfRangeException>(() => source.CopyTo(target, -1));
1001+
Assert.Throws<ArgumentException>(() => source.CopyTo(target, 2));
1002+
}
1003+
9631004
[Fact]
9641005
public void CopyToIntArrayIntInt()
9651006
{
@@ -1068,6 +1109,7 @@ public void SortRange()
10681109
{
10691110
var array = ImmutableArray.Create(2, 4, 1, 3);
10701111
Assert.Throws<ArgumentOutOfRangeException>(() => array.Sort(-1, 2, Comparer<int>.Default));
1112+
Assert.Throws<ArgumentOutOfRangeException>(() => array.Sort(1, -1, Comparer<int>.Default));
10711113
Assert.Throws<ArgumentOutOfRangeException>(() => array.Sort(1, 4, Comparer<int>.Default));
10721114
Assert.Equal(new int[] { 2, 4, 1, 3 }, array.Sort(array.Length, 0, Comparer<int>.Default));
10731115
Assert.Equal(new[] { 2, 1, 4, 3 }, array.Sort(1, 2, Comparer<int>.Default));
@@ -1221,13 +1263,40 @@ public void StructuralComparableArrayInterop()
12211263
Assert.Equal(array.CompareTo(equalArray, Comparer<int>.Default), immArray.CompareTo(equalArray, Comparer<int>.Default));
12221264
}
12231265

1224-
[Fact]
1225-
public void BinarySearch()
1266+
[Theory]
1267+
[InlineData(new int[0], 5)]
1268+
[InlineData(new int[] { 3 }, 5)]
1269+
[InlineData(new int[] { 5 }, 5)]
1270+
[InlineData(new int[] { 1, 2, 3 }, 1)]
1271+
[InlineData(new int[] { 1, 2, 3 }, 2)]
1272+
[InlineData(new int[] { 1, 2, 3 }, 3)]
1273+
[InlineData(new int[] { 1, 2, 3, 4 }, 4)]
1274+
public void BinarySearch(int[] array, int value)
12261275
{
1227-
Assert.Throws<ArgumentNullException>(() => Assert.Equal(Array.BinarySearch(new int[0], 5), ImmutableArray.BinarySearch(default(ImmutableArray<int>), 5)));
1228-
Assert.Equal(Array.BinarySearch(new int[0], 5), ImmutableArray.BinarySearch(ImmutableArray.Create<int>(), 5));
1229-
Assert.Equal(Array.BinarySearch(new int[] { 3 }, 5), ImmutableArray.BinarySearch(ImmutableArray.Create(3), 5));
1230-
Assert.Equal(Array.BinarySearch(new int[] { 5 }, 5), ImmutableArray.BinarySearch(ImmutableArray.Create(5), 5));
1276+
Assert.Throws<ArgumentNullException>(() => ImmutableArray.BinarySearch(default(ImmutableArray<int>), value));
1277+
1278+
Assert.Equal(
1279+
Array.BinarySearch(array, value),
1280+
ImmutableArray.BinarySearch(ImmutableArray.Create(array), value));
1281+
1282+
Assert.Equal(
1283+
Array.BinarySearch(array, value, Comparer<int>.Default),
1284+
ImmutableArray.BinarySearch(ImmutableArray.Create(array), value, Comparer<int>.Default));
1285+
1286+
Assert.Equal(
1287+
Array.BinarySearch(array, 0, array.Length, value),
1288+
ImmutableArray.BinarySearch(ImmutableArray.Create(array), 0, array.Length, value));
1289+
1290+
if (array.Length > 0)
1291+
{
1292+
Assert.Equal(
1293+
Array.BinarySearch(array, 1, array.Length - 1, value),
1294+
ImmutableArray.BinarySearch(ImmutableArray.Create(array), 1, array.Length - 1, value));
1295+
}
1296+
1297+
Assert.Equal(
1298+
Array.BinarySearch(array, 0, array.Length, value, Comparer<int>.Default),
1299+
ImmutableArray.BinarySearch(ImmutableArray.Create(array), 0, array.Length, value, Comparer<int>.Default));
12311300
}
12321301

12331302
[Fact]

src/System.Collections.Immutable/tests/IndexOfTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static void LastIndexOfTest<TCollection>(
9595
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
9696
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
9797
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
98+
Assert.Throws<ArgumentOutOfRangeException>(() => lastIndexOfItemIndex(collection1256, 2, 5));
9899

99100
Assert.Equal(-1, lastIndexOfItem(emptyCollection, 5));
100101
Assert.Equal(-1, lastIndexOfItemEQ(emptyCollection, 5, EqualityComparer<int>.Default));

0 commit comments

Comments
 (0)