Skip to content

Commit

Permalink
Address CA1036 for IPAddressRange and Subnet
Browse files Browse the repository at this point in the history
CA1036 states that  IPAddressRange and Subnet should override the comparison operators, specifically op_Equality, op_Inequality, op_LessThan and op_GreaterThan.

Added less than or equal to and greater than or equal to so as not to disappoint.

Issue #31
  • Loading branch information
rheone authored and ahsteele committed Nov 6, 2019
1 parent 3d6f1e1 commit 5a9db52
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 66 deletions.
130 changes: 129 additions & 1 deletion src/Arcus.Tests/IPAddressRangeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -94,10 +94,138 @@ public void Implementation_Test()
Assert.True(typeof(IIPAddressRange).IsAssignableFrom(ipAddressRangeType));
Assert.True(typeof(IComparable<IPAddressRange>).IsAssignableFrom(ipAddressRangeType));
Assert.True(typeof(IEquatable<IPAddressRange>).IsAssignableFrom(ipAddressRangeType));
Assert.True(typeof(IComparable).IsAssignableFrom(ipAddressRangeType));
}

#endregion //end: Class

#region CompareTo / Operators

public static IEnumerable<object[]> Comparison_Values()
{
var ipv4Slash16 = Subnet.Parse("192.168.0.0/16");
var ipv6Slash64 = Subnet.Parse("ab:cd::/64");
var ipv4Slash20 = Subnet.Parse("192.168.0.0/20");
var ipv6Slash96 = Subnet.Parse("ab:cd::/96");
var ipv4All = Subnet.Parse("0.0.0.0/0");
var ipv6All = Subnet.Parse("::/0");
var ipv4Single = Subnet.Parse("0.0.0.0/32");
var ipv6Single = Subnet.Parse("::/128");

yield return new object[] {0, new IPAddressRange(ipv4Slash16.Head, ipv4Slash16.Tail), new IPAddressRange(ipv4Slash16.Head, ipv4Slash16.Tail)};
yield return new object[] {0, new IPAddressRange(ipv6Slash64.Head, ipv6Slash64.Tail), new IPAddressRange(ipv6Slash64.Head, ipv6Slash64.Tail)};
yield return new object[] {1, new IPAddressRange(ipv4Slash16.Head, ipv4Slash16.Tail), null};
yield return new object[] {1, new IPAddressRange(ipv6Slash64.Head, ipv6Slash64.Tail), null};
yield return new object[] {1, new IPAddressRange(ipv4Slash16.Head, ipv4Slash16.Tail), new IPAddressRange(ipv4Slash20.Head, ipv4Slash20.Tail)};
yield return new object[] {-1, new IPAddressRange(ipv4Slash20.Head, ipv4Slash20.Tail), new IPAddressRange(ipv4Slash16.Head, ipv4Slash16.Tail)};
yield return new object[] {1, new IPAddressRange(ipv6Slash64.Head, ipv6Slash64.Tail), new IPAddressRange(ipv6Slash96.Head, ipv6Slash96.Tail)};
yield return new object[] {-1, new IPAddressRange(ipv6Slash96.Head, ipv6Slash96.Tail), new IPAddressRange(ipv6Slash64.Head, ipv6Slash64.Tail)};
yield return new object[] {-1, new IPAddressRange(ipv4All.Head, ipv4All.Tail), new IPAddressRange(ipv6All.Head, ipv6All.Tail)};
yield return new object[] {1, new IPAddressRange(ipv6All.Head, ipv6All.Tail), new IPAddressRange(ipv4All.Head, ipv4All.Tail)};
yield return new object[] {-1, new IPAddressRange(ipv4Single.Head, ipv4Single.Tail), new IPAddressRange(ipv6Single.Head, ipv6Single.Tail)};
yield return new object[] {1, new IPAddressRange(ipv6Single.Head, ipv6Single.Tail), new IPAddressRange(ipv4Single.Head, ipv4Single.Tail)};
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void CompareTo_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left.CompareTo(right);

// Assert
Assert.Equal(expected, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_Equals_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left == right;

// Assert
Assert.Equal(expected == 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_NotEquals_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left != right;

// Assert
Assert.Equal(expected != 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_GreaterThan_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left > right;

// Assert
Assert.Equal(expected > 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_GreaterThanOrEqual_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left >= right;

// Assert
Assert.Equal(expected >= 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_LessThan_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left < right;

// Assert
Assert.Equal(expected < 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_LessThanOrEqual_Test(int expected,
IPAddressRange left,
IPAddressRange right)
{
// Arrange
// Act
var result = left <= right;

// Assert
Assert.Equal(expected <= 0, result);
}

#endregion end CompareTo / Operators

#region TailOverlappedBy

[Theory]
Expand Down
129 changes: 106 additions & 23 deletions src/Arcus.Tests/SubnetTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -59,42 +59,125 @@ public void Class_Implementation_Test()
Assert.True(typeof(IIPAddressRange).IsAssignableFrom(subnetType));
Assert.True(typeof(IComparable<Subnet>).IsAssignableFrom(subnetType));
Assert.True(typeof(IEquatable<Subnet>).IsAssignableFrom(subnetType));
Assert.True(typeof(IComparable).IsAssignableFrom(subnetType));
}

#endregion // end: Class

#region CompareTo
#region CompareTo / Operators

public static IEnumerable<object[]> Comparison_Values()
{
yield return new object[] {0, Subnet.Parse("192.168.0.0/16"), Subnet.Parse("192.168.0.0/16")};
yield return new object[] {0, Subnet.Parse("ab:cd::/64"), Subnet.Parse("ab:cd::/64")};
yield return new object[] {1, Subnet.Parse("192.168.0.0/16"), null};
yield return new object[] {1, Subnet.Parse("ab:cd::/64"), null};
yield return new object[] {1, Subnet.Parse("192.168.0.0/16"), Subnet.Parse("192.168.0.0/20")};
yield return new object[] {-1, Subnet.Parse("192.168.0.0/20"), Subnet.Parse("192.168.0.0/16")};
yield return new object[] {1, Subnet.Parse("ab:cd::/64"), Subnet.Parse("ab:cd::/96")};
yield return new object[] {-1, Subnet.Parse("ab:cd::/96"), Subnet.Parse("ab:cd::/64")};
yield return new object[] {-1, Subnet.Parse("0.0.0.0/0"), Subnet.Parse("::/0")};
yield return new object[] {1, Subnet.Parse("::/0"), Subnet.Parse("0.0.0.0/0")};
yield return new object[] {-1, Subnet.Parse("0.0.0.0/32"), Subnet.Parse("::/128")};
yield return new object[] {1, Subnet.Parse("::/128"), Subnet.Parse("0.0.0.0/32")};
}

[Theory]
[InlineData(0, "192.168.0.0/16", "192.168.0.0/16")]
[InlineData(0, "ab:cd::/64", "ab:cd::/64")]
[InlineData(1, "192.168.0.0/16", null)]
[InlineData(1, "ab:cd::/64", null)]
[InlineData(1, "192.168.0.0/16", "192.168.0.0/20")]
[InlineData(-1, "192.168.0.0/20", "192.168.0.0/16")]
[InlineData(1, "ab:cd::/64", "ab:cd::/96")]
[InlineData(-1, "ab:cd::/96", "ab:cd::/64")]
[InlineData(-1, "0.0.0.0/0", "::/0")]
[InlineData(1, "::/0", "0.0.0.0/0")]
[InlineData(-1, "0.0.0.0/32", "::/128")]
[InlineData(1, "::/128", "0.0.0.0/32")]
[MemberData(nameof(Comparison_Values))]
public void CompareTo_Test(int expected,
string x,
string y)
Subnet left,
Subnet right)
{
// Arrange
var thisSubnet = Subnet.Parse(x);
// Act
var result = left.CompareTo(right);

if (!Subnet.TryParse(y, out var other))
{
other = null;
}
// Assert
Assert.Equal(expected, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_Equals_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = thisSubnet.CompareTo(other);
var result = left == right;

// Assert
Assert.Equal(expected, result);
Assert.Equal(expected == 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_NotEquals_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = left != right;

// Assert
Assert.Equal(expected != 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_GreaterThan_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = left > right;

// Assert
Assert.Equal(expected > 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_GreaterThanOrEqual_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = left >= right;

// Assert
Assert.Equal(expected >= 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_LessThan_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = left < right;

// Assert
Assert.Equal(expected < 0, result);
}

[Theory]
[MemberData(nameof(Comparison_Values))]
public void Operator_LessThanOrEqual_Test(int expected,
Subnet left,
Subnet right)
{
// Arrange
// Act
var result = left <= right;

// Assert
Assert.Equal(expected <= 0, result);
}

#endregion
Expand Down
Loading

0 comments on commit 5a9db52

Please sign in to comment.