Skip to content

Commit 5db3e7a

Browse files
authored
Enable nullable context in all projects (#131)
* Enable nullable context in all projects * Update to .net 3.1 * Remove BitArray comparison behavior * Add comments to exceptions
1 parent 102fdc1 commit 5db3e7a

16 files changed

+128
-116
lines changed

Diff for: .travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: csharp
22
solution: C-Sharp.sln
33
mono: none
44
dist: xenial
5-
dotnet: 2.2
5+
dotnet: 3.1
66
script:
77
- dotnet build
88
- travis_wait 60 dotnet test --collect:"XPlat Code Coverage"

Diff for: Algorithms.Tests/Algorithms.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<LangVersion>default</LangVersion>
7-
<NullableContextOptions>enable</NullableContextOptions>
87
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
98
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
<Nullable>enable</Nullable>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

Diff for: Algorithms/Algorithms.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netstandard2.1</TargetFramework>
55
<LangVersion>default</LangVersion>
6-
<NullableContextOptions>enable</NullableContextOptions>
76
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
87
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
8+
<Nullable>enable</Nullable>
99
</PropertyGroup>
1010

1111
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

Diff for: Algorithms/DataCompression/HuffmanCompressor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ public ListNode(ListNode leftChild, ListNode rightChild)
174174
/// <summary>
175175
/// Gets tODO. TODO.
176176
/// </summary>
177-
public ListNode RightChild { get; }
177+
public ListNode? RightChild { get; }
178178

179179
/// <summary>
180180
/// Gets tODO. TODO.
181181
/// </summary>
182-
public ListNode LeftChild { get; }
182+
public ListNode? LeftChild { get; }
183183
}
184184

185185
private class ListNodeComparer : IComparer<ListNode>

Diff for: Algorithms/DataCompression/ShannonFanoCompressor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ public class ListNode
139139
/// <summary>
140140
/// Gets or sets tODO. TODO.
141141
/// </summary>
142-
public ListNode RightChild { get; set; }
142+
public ListNode? RightChild { get; set; }
143143

144144
/// <summary>
145145
/// Gets or sets tODO. TODO.
146146
/// </summary>
147-
public ListNode LeftChild { get; set; }
147+
public ListNode? LeftChild { get; set; }
148148
}
149149
}
150150
}

Diff for: Algorithms/Search/AStar/AStar.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public static void ResetNodes(List<Node> nodes)
3131
public static List<Node> GeneratePath(Node target)
3232
{
3333
var ret = new List<Node>();
34-
var current = target;
35-
while (current != null)
34+
Node? current = target;
35+
while (!(current is null))
3636
{
3737
ret.Add(current);
3838
current = current.Parent;

Diff for: Algorithms/Search/AStar/Node.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public Node(VecN position, bool traversable, double traverseMultiplier)
6060
/// <summary>
6161
/// Gets or sets a list of all connected nodes.
6262
/// </summary>
63-
public Node[] ConnectedNodes { get; set; }
63+
public Node[] ConnectedNodes { get; set; } = new Node[0];
6464

6565
/// <summary>
6666
/// Gets or sets he "previous" node that was processed before this node.
6767
/// </summary>
68-
public Node Parent { get; set; }
68+
public Node? Parent { get; set; }
6969

7070
/// <summary>
7171
/// Gets the positional information of the node.

Diff for: DataStructures.Tests/BitArrayTests.cs

+2-18
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public static void TestCompare()
367367
}
368368

369369
[Test]
370-
public static void TestCompareThrowsException()
370+
public static void ArraysOfDifferentLengthsAreNotEqual()
371371
{
372372
// Arrange
373373
var testObj1 = new BitArray("110");
@@ -376,23 +376,7 @@ public static void TestCompareThrowsException()
376376
// Act
377377

378378
// Assert
379-
_ = Assert.Throws<Exception>(() => Assert.IsTrue(testObj1 == testObj2));
380-
}
381-
382-
[Test]
383-
public static void TestCompareTo()
384-
{
385-
// Arrange
386-
var testObj1 = new BitArray("110");
387-
var testObj2 = new BitArray("110");
388-
var testObj3 = new BitArray("100");
389-
390-
// Act
391-
392-
// Assert
393-
Assert.AreEqual(testObj1.CompareTo(testObj3), 1);
394-
Assert.AreEqual(testObj3.CompareTo(testObj1), -1);
395-
Assert.AreEqual(testObj1.CompareTo(testObj2), 0);
379+
Assert.False(testObj1 == testObj2);
396380
}
397381

398382
#endregion COMPARE TESTS

Diff for: DataStructures.Tests/DataStructures.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<IsPackable>false</IsPackable>
66
<LangVersion>default</LangVersion>
7-
<NullableContextOptions>enable</NullableContextOptions>
87
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
98
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
<Nullable>enable</Nullable>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

Diff for: DataStructures/BitArray.cs

+53-38
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
// assumes: the input bit-arrays must have same length.
116116
using System;
117117
using System.Collections;
118+
using System.Collections.Generic;
118119
using System.Linq;
119120
using System.Text;
120121

@@ -125,7 +126,7 @@ namespace DataStructures
125126
/// useful functions/operations to deal with this type of
126127
/// data structure.
127128
/// </summary>
128-
public class BitArray : IComparable, ICloneable, IEnumerator, IEnumerable
129+
public sealed class BitArray : ICloneable, IEnumerator<bool>, IEnumerable<bool>
129130
{
130131
private readonly bool[] field; // the actual bit-field
131132
private int position = -1; // position for enumerator
@@ -193,9 +194,27 @@ public BitArray(string sequence)
193194
public BitArray(bool[] bits) => field = bits;
194195

195196
/// <summary>
196-
/// Gets the current bit of the array.
197+
/// Gets a value indicating whether the current bit of the array is set.
197198
/// </summary>
198-
public object Current
199+
public bool Current
200+
{
201+
get
202+
{
203+
try
204+
{
205+
return field[position];
206+
}
207+
catch (IndexOutOfRangeException)
208+
{
209+
throw new InvalidOperationException();
210+
}
211+
}
212+
}
213+
214+
/// <summary>
215+
/// Gets a value indicating whether the current bit of the array is set.
216+
/// </summary>
217+
object IEnumerator.Current
199218
{
200219
get
201220
{
@@ -492,21 +511,29 @@ public bool this[int offset]
492511
/// <returns>Returns True if there inputs are equal; False otherwise.</returns>
493512
public static bool operator ==(BitArray one, BitArray two)
494513
{
495-
var status = true;
514+
if (ReferenceEquals(one, two))
515+
{
516+
return true;
517+
}
496518

497-
if (one?.Length == two?.Length)
519+
if (one is null || two is null)
498520
{
499-
for (var i = 0; i < one?.Length; i++)
500-
{
501-
if (one[i] != two[i])
502-
{
503-
status = false;
504-
}
505-
}
521+
return false;
506522
}
507-
else
523+
524+
if (one.Length != two.Length)
508525
{
509-
throw new Exception("== : inputs haven't same length!");
526+
return false;
527+
}
528+
529+
var status = true;
530+
for (var i = 0; i < one.Length; i++)
531+
{
532+
if (one[i] != two[i])
533+
{
534+
status = false;
535+
break;
536+
}
510537
}
511538

512539
return status;
@@ -538,36 +565,16 @@ public object Clone()
538565
}
539566

540567
/// <summary>
541-
/// Compares if given bit-array is equal, greater or smaller than current.
542-
/// * CompareTo (interfaces IComparable)
543-
/// assumes: bit-array lentgh must been smaller or equal to 64 bit.
568+
/// Gets a enumerator for this BitArray-Object.
544569
/// </summary>
545-
/// <param name="other">Bit-array object.</param>
546-
/// <returns>0 - if the bit-array a equal; -1 - if this bit-array is smaller; 1 - if this bit-array is greater. .</returns>
547-
public int CompareTo(object other)
548-
{
549-
var status = 0;
550-
var valueThis = ToInt64();
551-
var otherBitArray = (BitArray)other;
552-
var valueOther = otherBitArray.ToInt64();
553-
554-
if (valueThis > valueOther)
555-
{
556-
status = 1;
557-
}
558-
else if (valueOther > valueThis)
559-
{
560-
status = -1;
561-
}
562-
563-
return status;
564-
}
570+
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
571+
public IEnumerator<bool> GetEnumerator() => this;
565572

566573
/// <summary>
567574
/// Gets a enumerator for this BitArray-Object.
568575
/// </summary>
569576
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
570-
public IEnumerator GetEnumerator() => this;
577+
IEnumerator IEnumerable.GetEnumerator() => this;
571578

572579
/// <summary>
573580
/// MoveNext (for interface IEnumerator).
@@ -880,6 +887,14 @@ public override bool Equals(object other)
880887
/// <returns>hash-code for this BitArray instance.</returns>
881888
public override int GetHashCode() => ToInt32();
882889

890+
/// <summary>
891+
/// Disposes object, nothing to dispose here though.
892+
/// </summary>
893+
public void Dispose()
894+
{
895+
// Done
896+
}
897+
883898
/// <summary>
884899
/// Utility method foir checking a given sequence contains only zeros and ones.
885900
/// This method will used in Constructor (sequence : string) and Compile(sequence : string).

Diff for: DataStructures/DataStructures.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netstandard2.1</TargetFramework>
55
<LangVersion>default</LangVersion>
6-
<NullableContextOptions>enable</NullableContextOptions>
76
<CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
87
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
8+
<Nullable>enable</Nullable>
99
</PropertyGroup>
1010

1111
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -23,4 +23,8 @@
2323
</PackageReference>
2424
</ItemGroup>
2525

26+
<ItemGroup>
27+
<ProjectReference Include="..\Utilities\Utilities.csproj" />
28+
</ItemGroup>
29+
2630
</Project>

0 commit comments

Comments
 (0)