Skip to content

Commit fa6f986

Browse files
committed
Enable nullable reference types for TreeList
1 parent 43c834d commit fa6f986

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+275
-292
lines changed

TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch1.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -53,11 +51,21 @@ public void PosTest4()
5351
Assert.Equal(1, listObject.BinarySearch(new MyClass(20)));
5452
}
5553

54+
[Fact]
55+
public void TestComparable()
56+
{
57+
MyClass first = new MyClass(10);
58+
MyClass second = new MyClass(20);
59+
Assert.Equal(-1, first.CompareTo(second));
60+
Assert.Equal(1, second.CompareTo(first));
61+
Assert.Equal(1, first.CompareTo(null));
62+
}
63+
5664
[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
5765
public void PosTest5()
5866
{
5967
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
60-
TreeList<string> listObject = new TreeList<string>(strArray);
68+
TreeList<string?> listObject = new TreeList<string?>(strArray);
6169
Assert.Equal(-1, listObject.BinarySearch(null));
6270
}
6371

@@ -78,8 +86,11 @@ public MyClass(int a)
7886
_value = a;
7987
}
8088

81-
public int CompareTo(object obj)
89+
public int CompareTo(object? obj)
8290
{
91+
if (obj is null)
92+
return 1;
93+
8394
return _value.CompareTo(((MyClass)obj)._value);
8495
}
8596
}

TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch2.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -51,18 +49,29 @@ public void PosTest4()
5149
MyClass myclass1 = new MyClass(10);
5250
MyClass myclass2 = new MyClass(20);
5351
MyClass myclass3 = new MyClass(30);
54-
MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 };
55-
TreeList<MyClass> listObject = new TreeList<MyClass>(mc);
52+
MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 };
53+
TreeList<MyClass?> listObject = new TreeList<MyClass?>(mc);
5654
MyClassIC myclassIC = new MyClassIC();
5755
listObject.Sort(myclassIC);
58-
Assert.Equal(2, listObject.BinarySearch(new MyClass(10), myclassIC));
56+
Assert.Equal(3, listObject.BinarySearch(new MyClass(10), myclassIC));
57+
Assert.Equal(0, listObject.BinarySearch(null, myclassIC));
58+
}
59+
60+
[Fact]
61+
public void TestComparable()
62+
{
63+
MyClass first = new MyClass(10);
64+
MyClass second = new MyClass(20);
65+
Assert.Equal(-1, first.CompareTo(second));
66+
Assert.Equal(1, second.CompareTo(first));
67+
Assert.Equal(1, first.CompareTo(null));
5968
}
6069

6170
[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
6271
public void PosTest5()
6372
{
6473
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
65-
TreeList<string> listObject = new TreeList<string>(strArray);
74+
TreeList<string?> listObject = new TreeList<string?>(strArray);
6675
listObject.Sort();
6776
StrClass strClass = new StrClass();
6877
Assert.Equal(-1, listObject.BinarySearch(null, strClass));
@@ -72,8 +81,8 @@ public void PosTest5()
7281
[Fact]
7382
public void PosTest5Ext()
7483
{
75-
string[] strArray = { null, "banana", "chocolate", "dog", "food" };
76-
TreeList<string> listObject = new TreeList<string>(strArray);
84+
string?[] strArray = { null, "banana", "chocolate", "dog", "food" };
85+
TreeList<string?> listObject = new TreeList<string?>(strArray);
7786
listObject.Sort();
7887
StrClass strClass = new StrClass();
7988
Assert.Equal(~1, listObject.BinarySearch(string.Empty, strClass));
@@ -112,8 +121,11 @@ public MyClass(int a)
112121

113122
public int Value => _value;
114123

115-
public int CompareTo(object obj)
124+
public int CompareTo(object? obj)
116125
{
126+
if (obj is null)
127+
return 1;
128+
117129
return _value.CompareTo(((MyClass)obj)._value);
118130
}
119131
}
@@ -130,9 +142,9 @@ public int Compare(int x, int y)
130142
}
131143
}
132144

133-
public class StrClass : IComparer<string>
145+
public class StrClass : IComparer<string?>
134146
{
135-
public int Compare(string x, string y)
147+
public int Compare(string? x, string? y)
136148
{
137149
{
138150
if (x == null)
@@ -182,10 +194,19 @@ public int Compare(string x, string y)
182194
}
183195
}
184196

185-
public class MyClassIC : IComparer<MyClass>
197+
public class MyClassIC : IComparer<MyClass?>
186198
{
187-
public int Compare(MyClass x, MyClass y)
199+
public int Compare(MyClass? x, MyClass? y)
188200
{
201+
if (x is null)
202+
{
203+
return y is null ? 0 : -1;
204+
}
205+
else if (y is null)
206+
{
207+
return 1;
208+
}
209+
189210
return (-1) * x.Value.CompareTo(y.Value);
190211
}
191212
}

TunnelVisionLabs.Collections.Trees.Test/List/BinarySearch3.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -51,18 +49,29 @@ public void PosTest4()
5149
MyClass myclass1 = new MyClass(10);
5250
MyClass myclass2 = new MyClass(20);
5351
MyClass myclass3 = new MyClass(30);
54-
MyClass[] mc = new MyClass[3] { myclass1, myclass2, myclass3 };
55-
TreeList<MyClass> listObject = new TreeList<MyClass>(mc);
52+
MyClass?[] mc = new MyClass?[4] { myclass1, null, myclass2, myclass3 };
53+
TreeList<MyClass?> listObject = new TreeList<MyClass?>(mc);
5654
MyClassIC myclassIC = new MyClassIC();
5755
listObject.Sort(myclassIC);
58-
Assert.Equal(2, listObject.BinarySearch(0, 3, new MyClass(10), myclassIC));
56+
Assert.Equal(3, listObject.BinarySearch(0, 4, new MyClass(10), myclassIC));
57+
Assert.Equal(0, listObject.BinarySearch(0, 4, null, myclassIC));
58+
}
59+
60+
[Fact]
61+
public void TestComparable()
62+
{
63+
MyClass first = new MyClass(10);
64+
MyClass second = new MyClass(20);
65+
Assert.Equal(-1, first.CompareTo(second));
66+
Assert.Equal(1, second.CompareTo(first));
67+
Assert.Equal(1, first.CompareTo(null));
5968
}
6069

6170
[Fact(DisplayName = "PosTest5: The item to be search is a null reference")]
6271
public void PosTest5()
6372
{
64-
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
65-
TreeList<string> listObject = new TreeList<string>(strArray);
73+
string?[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
74+
TreeList<string?> listObject = new TreeList<string?>(strArray);
6675
listObject.Sort();
6776
StrClass strClass = new StrClass();
6877
Assert.Equal(-1, listObject.BinarySearch(0, 3, null, strClass));
@@ -72,8 +81,8 @@ public void PosTest5()
7281
[Fact]
7382
public void PosTest5Ext()
7483
{
75-
string[] strArray = { null, "banana", "chocolate", "dog", "food" };
76-
TreeList<string> listObject = new TreeList<string>(strArray);
84+
string?[] strArray = { null, "banana", "chocolate", "dog", "food" };
85+
TreeList<string?> listObject = new TreeList<string?>(strArray);
7786
listObject.Sort();
7887
StrClass strClass = new StrClass();
7988
Assert.Equal(~1, listObject.BinarySearch(0, 3, string.Empty, strClass));
@@ -145,8 +154,11 @@ public MyClass(int a)
145154

146155
public int Value => _value;
147156

148-
public int CompareTo(object obj)
157+
public int CompareTo(object? obj)
149158
{
159+
if (obj is null)
160+
return 1;
161+
150162
return _value.CompareTo(((MyClass)obj)._value);
151163
}
152164
}
@@ -163,9 +175,9 @@ public int Compare(int x, int y)
163175
}
164176
}
165177

166-
public class StrClass : IComparer<string>
178+
public class StrClass : IComparer<string?>
167179
{
168-
public int Compare(string x, string y)
180+
public int Compare(string? x, string? y)
169181
{
170182
{
171183
if (x == null)
@@ -215,10 +227,19 @@ public int Compare(string x, string y)
215227
}
216228
}
217229

218-
public class MyClassIC : IComparer<MyClass>
230+
public class MyClassIC : IComparer<MyClass?>
219231
{
220-
public int Compare(MyClass x, MyClass y)
232+
public int Compare(MyClass? x, MyClass? y)
221233
{
234+
if (x is null)
235+
{
236+
return y is null ? 0 : -1;
237+
}
238+
else if (y is null)
239+
{
240+
return 1;
241+
}
242+
222243
return (-1) * x.Value.CompareTo(y.Value);
223244
}
224245
}

TunnelVisionLabs.Collections.Trees.Test/List/CopyTo1.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -62,7 +60,7 @@ public void NegTest1()
6260
{
6361
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
6462
TreeList<int> listObject = new TreeList<int>(iArray);
65-
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null));
63+
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null!));
6664
}
6765

6866
[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]

TunnelVisionLabs.Collections.Trees.Test/List/CopyTo2.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -75,7 +73,7 @@ public void NegTest1()
7573
{
7674
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
7775
TreeList<int> listObject = new TreeList<int>(iArray);
78-
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null, 0));
76+
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(null!, 0));
7977
}
8078

8179
[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]

TunnelVisionLabs.Collections.Trees.Test/List/CopyTo3.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -73,7 +71,7 @@ public void NegTest1()
7371
{
7472
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
7573
TreeList<int> listObject = new TreeList<int>(iArray);
76-
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(0, null, 0, 10));
74+
Assert.Throws<ArgumentNullException>(() => listObject.CopyTo(0, null!, 0, 10));
7775
}
7876

7977
[Fact(DisplayName = "NegTest2: The number of elements in the source List is greater than the number of elements that the destination array can contain")]

TunnelVisionLabs.Collections.Trees.Test/List/TreeListAdd.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System.Collections.Generic;
@@ -54,7 +52,7 @@ public void PosTest3()
5452
[Fact(DisplayName = "PosTest4: Add null object to the list")]
5553
public void PosTest4()
5654
{
57-
TreeList<string> listObject = new TreeList<string>();
55+
TreeList<string?> listObject = new TreeList<string?>();
5856
listObject.Add(null);
5957
Assert.Null(listObject[0]);
6058
}

TunnelVisionLabs.Collections.Trees.Test/List/TreeListAddRange.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System;
@@ -57,9 +55,9 @@ public void PosTest3()
5755
[Fact(DisplayName = "NegTest1: The argument is a null reference")]
5856
public void NegTest1()
5957
{
60-
IEnumerable<string> i = null;
58+
IEnumerable<string>? i = null;
6159
TreeList<string> listObject = new TreeList<string>();
62-
Assert.Throws<ArgumentNullException>(() => listObject.AddRange(i));
60+
Assert.Throws<ArgumentNullException>(() => listObject.AddRange(i!));
6361
}
6462

6563
public class MyClass

TunnelVisionLabs.Collections.Trees.Test/List/TreeListClear.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace TunnelVisionLabs.Collections.Trees.Test.List
75
{
86
using System.Collections.Generic;

TunnelVisionLabs.Collections.Trees.Test/List/TreeListContains.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
// This file contains tests for Contains...
75
#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
86

@@ -56,8 +54,8 @@ public void PosTest4()
5654
[Fact(DisplayName = "PosTest5: The argument is a null reference")]
5755
public void PosTest5()
5856
{
59-
string[] strArray = { "apple", "banana", "chocolate", null, "food" };
60-
TreeList<string> listObject = new TreeList<string>(strArray);
57+
string?[] strArray = { "apple", "banana", "chocolate", null, "food" };
58+
TreeList<string?> listObject = new TreeList<string?>(strArray);
6159
Assert.True(listObject.Contains(null));
6260
}
6361

0 commit comments

Comments
 (0)