Skip to content

Commit

Permalink
Enable nullable reference types for TreeList
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jul 20, 2020
1 parent 43c834d commit fa6f986
Show file tree
Hide file tree
Showing 49 changed files with 275 additions and 292 deletions.
19 changes: 15 additions & 4 deletions TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs
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.List
{
using System;
Expand Down Expand Up @@ -53,11 +51,21 @@ public void PosTest4()
Assert.Equal(1, listObject.BinarySearch(new MyClass(20)));
}

[Fact]
public void TestComparable()
{
MyClass first = new MyClass(10);
MyClass second = new MyClass(20);
Assert.Equal(-1, first.CompareTo(second));
Assert.Equal(1, second.CompareTo(first));
Assert.Equal(1, first.CompareTo(null));
}

[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
public void PosTest5()
{
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
TreeList<string?> listObject = new TreeList<string?>(strArray);
Assert.Equal(-1, listObject.BinarySearch(null));
}

Expand All @@ -78,8 +86,11 @@ public MyClass(int a)
_value = a;
}

public int CompareTo(object obj)
public int CompareTo(object? obj)
{
if (obj is null)
return 1;

return _value.CompareTo(((MyClass)obj)._value);
}
}
Expand Down
47 changes: 34 additions & 13 deletions TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs
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.List
{
using System;
Expand Down Expand Up @@ -51,18 +49,29 @@ public void PosTest4()
MyClass myclass1 = new MyClass(10);
MyClass myclass2 = new MyClass(20);
MyClass myclass3 = new MyClass(30);
MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 };
TreeList<MyClass> listObject = new TreeList<MyClass>(mc);
MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 };
TreeList<MyClass?> listObject = new TreeList<MyClass?>(mc);
MyClassIC myclassIC = new MyClassIC();
listObject.Sort(myclassIC);
Assert.Equal(2, listObject.BinarySearch(new MyClass(10), myclassIC));
Assert.Equal(3, listObject.BinarySearch(new MyClass(10), myclassIC));
Assert.Equal(0, listObject.BinarySearch(null, myclassIC));
}

[Fact]
public void TestComparable()
{
MyClass first = new MyClass(10);
MyClass second = new MyClass(20);
Assert.Equal(-1, first.CompareTo(second));
Assert.Equal(1, second.CompareTo(first));
Assert.Equal(1, first.CompareTo(null));
}

[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
public void PosTest5()
{
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
TreeList<string?> listObject = new TreeList<string?>(strArray);
listObject.Sort();
StrClass strClass = new StrClass();
Assert.Equal(-1, listObject.BinarySearch(null, strClass));
Expand All @@ -72,8 +81,8 @@ public void PosTest5()
[Fact]
public void PosTest5Ext()
{
string[] strArray = { null, "banana", "chocolate", "dog", "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
string?[] strArray = { null, "banana", "chocolate", "dog", "food" };
TreeList<string?> listObject = new TreeList<string?>(strArray);
listObject.Sort();
StrClass strClass = new StrClass();
Assert.Equal(~1, listObject.BinarySearch(string.Empty, strClass));
Expand Down Expand Up @@ -112,8 +121,11 @@ public MyClass(int a)

public int Value => _value;

public int CompareTo(object obj)
public int CompareTo(object? obj)
{
if (obj is null)
return 1;

return _value.CompareTo(((MyClass)obj)._value);
}
}
Expand All @@ -130,9 +142,9 @@ public int Compare(int x, int y)
}
}

public class StrClass : IComparer<string>
public class StrClass : IComparer<string?>
{
public int Compare(string x, string y)
public int Compare(string? x, string? y)
{
{
if (x == null)
Expand Down Expand Up @@ -182,10 +194,19 @@ public int Compare(string x, string y)
}
}

public class MyClassIC : IComparer<MyClass>
public class MyClassIC : IComparer<MyClass?>
{
public int Compare(MyClass x, MyClass y)
public int Compare(MyClass? x, MyClass? y)
{
if (x is null)
{
return y is null ? 0 : -1;
}
else if (y is null)
{
return 1;
}

return (-1) * x.Value.CompareTo(y.Value);
}
}
Expand Down
49 changes: 35 additions & 14 deletions TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs
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.List
{
using System;
Expand Down Expand Up @@ -51,18 +49,29 @@ public void PosTest4()
MyClass myclass1 = new MyClass(10);
MyClass myclass2 = new MyClass(20);
MyClass myclass3 = new MyClass(30);
MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 };
TreeList<MyClass> listObject = new TreeList<MyClass>(mc);
MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 };
TreeList<MyClass?> listObject = new TreeList<MyClass?>(mc);
MyClassIC myclassIC = new MyClassIC();
listObject.Sort(myclassIC);
Assert.Equal(2, listObject.BinarySearch(0, 3, new MyClass(10), myclassIC));
Assert.Equal(3, listObject.BinarySearch(0, 4, new MyClass(10), myclassIC));
Assert.Equal(0, listObject.BinarySearch(0, 4, null, myclassIC));
}

[Fact]
public void TestComparable()
{
MyClass first = new MyClass(10);
MyClass second = new MyClass(20);
Assert.Equal(-1, first.CompareTo(second));
Assert.Equal(1, second.CompareTo(first));
Assert.Equal(1, first.CompareTo(null));
}

[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
public void PosTest5()
{
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
string?[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
TreeList<string?> listObject = new TreeList<string?>(strArray);
listObject.Sort();
StrClass strClass = new StrClass();
Assert.Equal(-1, listObject.BinarySearch(0, 3, null, strClass));
Expand All @@ -72,8 +81,8 @@ public void PosTest5()
[Fact]
public void PosTest5Ext()
{
string[] strArray = { null, "banana", "chocolate", "dog", "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
string?[] strArray = { null, "banana", "chocolate", "dog", "food" };
TreeList<string?> listObject = new TreeList<string?>(strArray);
listObject.Sort();
StrClass strClass = new StrClass();
Assert.Equal(~1, listObject.BinarySearch(0, 3, string.Empty, strClass));
Expand Down Expand Up @@ -145,8 +154,11 @@ public MyClass(int a)

public int Value => _value;

public int CompareTo(object obj)
public int CompareTo(object? obj)
{
if (obj is null)
return 1;

return _value.CompareTo(((MyClass)obj)._value);
}
}
Expand All @@ -163,9 +175,9 @@ public int Compare(int x, int y)
}
}

public class StrClass : IComparer<string>
public class StrClass : IComparer<string?>
{
public int Compare(string x, string y)
public int Compare(string? x, string? y)
{
{
if (x == null)
Expand Down Expand Up @@ -215,10 +227,19 @@ public int Compare(string x, string y)
}
}

public class MyClassIC : IComparer<MyClass>
public class MyClassIC : IComparer<MyClass?>
{
public int Compare(MyClass x, MyClass y)
public int Compare(MyClass? x, MyClass? y)
{
if (x is null)
{
return y is null ? 0 : -1;
}
else if (y is null)
{
return 1;
}

return (-1) * x.Value.CompareTo(y.Value);
}
}
Expand Down
4 changes: 1 addition & 3 deletions TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs
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.List
{
using System;
Expand Down Expand Up @@ -62,7 +60,7 @@ public void NegTest1()
{
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
TreeList<int> listObject = new TreeList<int>(iArray);
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null));
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null!));
}

[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]
Expand Down
4 changes: 1 addition & 3 deletions TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs
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.List
{
using System;
Expand Down Expand Up @@ -75,7 +73,7 @@ public void NegTest1()
{
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
TreeList<int> listObject = new TreeList<int>(iArray);
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null, 0));
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null!, 0));
}

[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]
Expand Down
4 changes: 1 addition & 3 deletions TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs
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.List
{
using System;
Expand Down Expand Up @@ -73,7 +71,7 @@ public void NegTest1()
{
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
TreeList<int> listObject = new TreeList<int>(iArray);
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(0, null, 0, 10));
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(0, null!, 0, 10));
}

[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]
Expand Down
4 changes: 1 addition & 3 deletions TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs
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.List
{
using System.Collections.Generic;
Expand Down Expand Up @@ -54,7 +52,7 @@ public void PosTest3()
[Fact(DisplayName = "PosTest4: Add null object to the list")]
public void PosTest4()
{
TreeList<string> listObject = new TreeList<string>();
TreeList<string?> listObject = new TreeList<string?>();
listObject.Add(null);
Assert.Null(listObject[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.List
{
using System;
Expand Down Expand Up @@ -57,9 +55,9 @@ public void PosTest3()
[Fact(DisplayName = "NegTest1: The argument is a null reference")]
public void NegTest1()
{
IEnumerable<string> i = null;
IEnumerable<string>? i = null;
TreeList<string> listObject = new TreeList<string>();
Assert.Throws<ArgumentNullException>(() => listObject.AddRange(i));
Assert.Throws<ArgumentNullException>(() => listObject.AddRange(i!));
}

public class MyClass
Expand Down
2 changes: 0 additions & 2 deletions TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs
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.List
{
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

// This file contains tests for Contains...
#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection

Expand Down Expand Up @@ -56,8 +54,8 @@ public void PosTest4()
[Fact(DisplayName = "PosTest5: The argument is a null reference")]
public void PosTest5()
{
string[] strArray = { "apple", "banana", "chocolate", null, "food" };
TreeList<string> listObject = new TreeList<string>(strArray);
string?[] strArray = { "apple", "banana", "chocolate", null, "food" };
TreeList<string?> listObject = new TreeList<string?>(strArray);
Assert.True(listObject.Contains(null));
}

Expand Down
2 changes: 0 additions & 2 deletions TunnelVisionLabs.Collections.Trees.Test/List/TreeListCount.cs
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.List
{
using System.Collections.Generic;
Expand Down
2 changes: 0 additions & 2 deletions TunnelVisionLabs.Collections.Trees.Test/List/TreeListCtor1.cs
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.List
{
using System.Collections.Generic;
Expand Down
Loading

0 comments on commit fa6f986

Please sign in to comment.