Skip to content

Commit

Permalink
Enable nullable reference types for ImmutableTreeList
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jul 20, 2020
1 parent fa6f986 commit b0f7bf6
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 319 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Benchmarks.Immutable
{
using System.Collections.Immutable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System;
Expand Down Expand Up @@ -60,15 +58,15 @@ public void NegTest1()
{
int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 };
var listObject = ImmutableTreeList.Create(iArray).ToBuilder();
Action<int> action = null;
Assert.Throws<ArgumentNullException>(() => listObject.ForEach(action));
Action<int>? action = null;
Assert.Throws<ArgumentNullException>(() => listObject.ForEach(action!));
}

public class MyClass
{
public int Sum { get; set; } = 0;

public string Result
public string? Result
{
get; set;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System;
Expand Down Expand Up @@ -68,7 +66,7 @@ private static void TestIListInterfaceImpl(IList list, bool supportsNullValues)
list.Remove("Text");
Assert.Equal(originalCount, list.Count);

object removedItem = list[0];
object? removedItem = list[0];
list.Remove(list[0]);
Assert.Equal(originalCount - 1, list.Count);
Assert.True(list.Contains(removedItem));
Expand Down Expand Up @@ -141,7 +139,7 @@ private static void TestICollectionInterfaceImpl(ICollection collection, bool is
Assert.Same(collection.SyncRoot, collection.SyncRoot);
}

Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>("array", () => collection.CopyTo(null!, 0));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new int[collection.Count, 1], 0));

void CopyToArrayWithNonZeroLowerBound() => collection.CopyTo(Array.CreateInstance(typeof(int), lengths: new[] { collection.Count }, lowerBounds: new[] { 1 }), 0);
Expand Down Expand Up @@ -224,40 +222,40 @@ private static void TestICollectionTInterfaceImpl<T>(ICollection<T> collection,
var copy = new T[collection.Count];

Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, -1));
Assert.All(copy, item => Assert.Equal(default, item));
Assert.All(copy, item => Assert.Equal(default!, item));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, 1));
Assert.All(copy, item => Assert.Equal(default, item));
Assert.All(copy, item => Assert.Equal(default!, item));

collection.CopyTo(copy, 0);
Assert.Equal<object>(600, copy[0]);
Assert.Equal<object>(601, copy[1]);
Assert.Equal<object?>(600, copy[0]);
Assert.Equal<object?>(601, copy[1]);

copy = new T[collection.Count + 2];
collection.CopyTo(copy, 1);
Assert.Equal(default, copy[0]);
Assert.Equal<object>(600, copy[1]);
Assert.Equal<object>(601, copy[2]);
Assert.Equal(default, copy[3]);
Assert.Equal(default!, copy[0]);
Assert.Equal<object?>(600, copy[1]);
Assert.Equal<object?>(601, copy[2]);
Assert.Equal(default!, copy[3]);
}
else
{
var copy = new T[collection.Count];

Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, -1));
Assert.All(copy, item => Assert.Equal(default, item));
Assert.All(copy, item => Assert.Equal(default!, item));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(copy, 1));
Assert.All(copy, item => Assert.Equal(default, item));
Assert.All(copy, item => Assert.Equal(default!, item));

collection.CopyTo(copy, 0);
Assert.Equal<object>(600, copy[0]);
Assert.Equal<object>(601, copy[1]);
Assert.Equal<object?>(600, copy[0]);
Assert.Equal<object?>(601, copy[1]);

copy = new T[collection.Count + 2];
collection.CopyTo(copy, 1);
Assert.Equal(default, copy[0]);
Assert.Equal<object>(600, copy[1]);
Assert.Equal<object>(601, copy[2]);
Assert.Equal(default, copy[3]);
Assert.Equal(default!, copy[0]);
Assert.Equal<object?>(600, copy[1]);
Assert.Equal<object?>(601, copy[2]);
Assert.Equal(default!, copy[3]);
}
}

Expand All @@ -282,8 +280,8 @@ public void TestIndexer()
public void TestCopyToValidation()
{
ImmutableTreeList<int>.Builder list = ImmutableTreeList.CreateRange(Enumerable.Range(0, 10)).ToBuilder();
Assert.Throws<ArgumentNullException>("array", () => list.CopyTo(null));
Assert.Throws<ArgumentNullException>("array", () => list.CopyTo(0, null, 0, list.Count));
Assert.Throws<ArgumentNullException>("array", () => list.CopyTo(null!));
Assert.Throws<ArgumentNullException>("array", () => list.CopyTo(0, null!, 0, list.Count));
Assert.Throws<ArgumentOutOfRangeException>("index", () => list.CopyTo(-1, new int[list.Count], 0, list.Count));
Assert.Throws<ArgumentOutOfRangeException>("arrayIndex", () => list.CopyTo(0, new int[list.Count], -1, list.Count));
Assert.Throws<ArgumentOutOfRangeException>("count", () => list.CopyTo(0, new int[list.Count], 0, -1));
Expand Down Expand Up @@ -343,7 +341,7 @@ public void TestAddRange()
ImmutableTreeList<int>.Builder list = ImmutableTreeList.CreateBuilder<int>();
CollectionAssert.EnumeratorInvalidated(list, () => list.AddRange(expected));

Assert.Throws<ArgumentNullException>("collection", () => list.AddRange(null));
Assert.Throws<ArgumentNullException>("collection", () => list.AddRange(null!));
CollectionAssert.EnumeratorNotInvalidated(list, () => list.AddRange(Enumerable.Empty<int>()));

Assert.Equal(expected.Length, list.Count);
Expand Down Expand Up @@ -411,7 +409,7 @@ public void TestInsertRange()
ImmutableTreeList<int>.Builder list = ImmutableTreeList.CreateBuilder<int>();
List<int> reference = new List<int>();

Assert.Throws<ArgumentNullException>("collection", () => list.InsertRange(0, null));
Assert.Throws<ArgumentNullException>("collection", () => list.InsertRange(0, null!));
Assert.Throws<ArgumentOutOfRangeException>("index", () => list.InsertRange(-1, Enumerable.Empty<int>()));
Assert.Throws<ArgumentOutOfRangeException>(null, () => list.InsertRange(1, Enumerable.Empty<int>()));

Expand Down Expand Up @@ -591,9 +589,9 @@ public void TestFindIndex()
reference.Insert(index, i);
}

Assert.Throws<ArgumentNullException>(() => list.FindIndex(null));
Assert.Throws<ArgumentNullException>(() => list.FindIndex(0, null));
Assert.Throws<ArgumentNullException>(() => list.FindIndex(0, 0, null));
Assert.Throws<ArgumentNullException>(() => list.FindIndex(null!));
Assert.Throws<ArgumentNullException>(() => list.FindIndex(0, null!));
Assert.Throws<ArgumentNullException>(() => list.FindIndex(0, 0, null!));

Assert.Throws<ArgumentOutOfRangeException>(() => list.FindIndex(-1, i => true));
Assert.Throws<ArgumentOutOfRangeException>(() => list.FindIndex(0, -1, i => true));
Expand Down Expand Up @@ -632,9 +630,9 @@ public void TestFindLastIndex()
reference.Insert(index, i);
}

Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(null));
Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(-1, null));
Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(-1, 0, null));
Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(null!));
Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(-1, null!));
Assert.Throws<ArgumentNullException>(() => list.FindLastIndex(-1, 0, null!));

Assert.Throws<ArgumentOutOfRangeException>(() => list.FindLastIndex(list.Count, i => true));
Assert.Throws<ArgumentOutOfRangeException>(() => list.FindLastIndex(list.Count - 1, -1, i => true));
Expand Down Expand Up @@ -814,7 +812,7 @@ public void TestSortComparison()
reference.Insert(index, item);
}

Assert.Throws<ArgumentNullException>(() => list.Sort((Comparison<int>)null));
Assert.Throws<ArgumentNullException>(() => list.Sort((Comparison<int>)null!));

Comparison<int> comparison = (x, y) => x - y;
list.Sort(comparison);
Expand Down Expand Up @@ -1073,7 +1071,7 @@ public void TestRemoveValue()
public void TestRemoveAll()
{
var list = ImmutableTreeList.CreateRange(Enumerable.Range(0, 20)).ToBuilder();
Assert.Throws<ArgumentNullException>(() => list.RemoveAll(null));
Assert.Throws<ArgumentNullException>(() => list.RemoveAll(null!));

Assert.Equal(10, list.RemoveAll(i => (i % 2) == 0));
Assert.Equal(new[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }, list);
Expand All @@ -1085,7 +1083,7 @@ public void TestRemoveAll()
public void TestExists()
{
var list = ImmutableTreeList.CreateRange(Enumerable.Range(0, 20)).ToBuilder();
Assert.Throws<ArgumentNullException>(() => list.Exists(null));
Assert.Throws<ArgumentNullException>(() => list.Exists(null!));

Assert.False(list.Exists(value => value < 0));
foreach (var i in list)
Expand All @@ -1100,7 +1098,7 @@ public void TestExists()
public void TestFind()
{
var list = ImmutableTreeList.CreateRange(Enumerable.Range(1, 20)).ToBuilder();
Assert.Throws<ArgumentNullException>(() => list.Find(null));
Assert.Throws<ArgumentNullException>(() => list.Find(null!));

Assert.Equal(0, list.Find(value => value < 0));
foreach (var i in list)
Expand All @@ -1115,7 +1113,7 @@ public void TestFind()
public void TestFindAll()
{
var list = ImmutableTreeList.CreateRange(Enumerable.Range(0, 20)).ToBuilder();
Assert.Throws<ArgumentNullException>(() => list.FindAll(null));
Assert.Throws<ArgumentNullException>(() => list.FindAll(null!));

ImmutableTreeList<int> found = list.FindAll(i => (i % 2) == 0);

Expand All @@ -1131,7 +1129,7 @@ public void TestFindLast()
{
var list = ImmutableTreeList.CreateRange(Enumerable.Range(1, 20)).ToBuilder();
var reference = new List<int>(Enumerable.Range(1, 20));
Assert.Throws<ArgumentNullException>(() => list.FindLast(null));
Assert.Throws<ArgumentNullException>(() => list.FindLast(null!));

Assert.Equal(0, list.FindLast(i => i < 0));
Assert.Equal(0, reference.FindLast(i => i < 0));
Expand Down Expand Up @@ -1166,7 +1164,7 @@ public void TestTrueForAll()
{
var list = ImmutableTreeList.CreateBuilder<int>();
Assert.True(list.TrueForAll(i => false));
Assert.Throws<ArgumentNullException>(() => list.TrueForAll(null));
Assert.Throws<ArgumentNullException>(() => list.TrueForAll(null!));

list.Add(1);
Assert.True(list.TrueForAll(i => i > 0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System;
Expand Down Expand Up @@ -60,15 +58,15 @@ public void NegTest1()
{
int[] iArray = { 1, 9, 3, 6, -1, 8, 7, 1, 2, 4 };
var listObject = ImmutableTreeList.Create(iArray);
Action<int> action = null;
Assert.Throws<ArgumentNullException>(() => listObject.ForEach(action));
Action<int>? action = null;
Assert.Throws<ArgumentNullException>(() => listObject.ForEach(action!));
}

public class MyClass
{
public int Sum { get; set; } = 0;

public string Result
public string? Result
{
get; set;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace TunnelVisionLabs.Collections.Trees.Test.Immutable
{
using System;
Expand Down
Loading

0 comments on commit b0f7bf6

Please sign in to comment.