Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/System.Net.IPNetwork/IPNetwork2Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public IPAddress Broadcast
return ToIPAddress(this.InternalBroadcast, this.family);
}
}

/// <summary>
/// Gets first usable IPAddress in Network.
/// </summary>
Expand Down Expand Up @@ -127,7 +127,31 @@ public IPAddress LastUsable
return ToIPAddress(last, this.family);
}
}


/// <summary>
/// Gets first IPAddress in Network.
/// </summary>
public IPAddress First
{
get
{
BigInteger first = this.InternalNetwork;
return ToIPAddress(first, this.family);
}
}

/// <summary>
/// Gets last IPAddress in Network.
/// </summary>
public IPAddress Last
{
get
{
BigInteger last = this.InternalBroadcast;
return ToIPAddress(last, this.family);
}
}

/// <summary>
/// Gets number of usable IPAddress in Network.
///
Expand Down
76 changes: 76 additions & 0 deletions src/System.Net.IPNetwork/IPNetwork2Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,80 @@ public sealed partial class IPNetwork2
}
return [left, right];
}

/// <summary>
/// Behavior
/// The addition operator (+) performs the following operations:
/// Network Expansion: Adds the specified number of IP addresses to the network range
/// Optimal Grouping: Attempts to create the most efficient network representation
/// Multiple Networks: When a single contiguous network cannot represent the result, returns multiple networks
/// CIDR Optimization: Automatically calculates the appropriate subnet mask for the expanded range
/// </summary>
/// <param name="left">left instance.</param>
/// <param name="add">number.</param>
/// <returns>Adds the specified number of IP addresses to the network range.</returns>
public static IEnumerable<IPNetwork2> operator +(IPNetwork2 left, int add)
{
if (add < 0)
{
return left - (add * -1);
}
if (add == 0)
{
return new[] { left };
}
var start = ToBigInteger(left.First);
var last = ToBigInteger(left.Last);
var end = last+ add;

if (end < start)
{
return [];
}

var startIp = ToIPAddress(start, left.AddressFamily);
var endIp = ToIPAddress(end, left.AddressFamily);

var uintStart = ToBigInteger(startIp);
var uintEnd = ToBigInteger(endIp);

if (uintEnd <= uintStart)
{
throw new OverflowException("IPNetwork overflow");
}
InternalParseRange(true, startIp, endIp, out IEnumerable<IPNetwork2> networks);
return networks;
}

/// <summary>
/// Add IPNetwork.
/// </summary>
/// <param name="left">left instance.</param>
/// <param name="subtract">number.</param>
/// <returns>Try to supernet two consecutive cidr equal subnet into a single one, otherwise return both netowkrs.</returns>
public static IEnumerable<IPNetwork2> operator -(IPNetwork2 left, int subtract)
{
if (subtract < 0)
{
return left + (subtract * -1);
}
if (subtract == 0)
{
return new[] { left };
}
var start = ToBigInteger(left.First);
var last = ToBigInteger(left.Last);
var end = last- subtract;

if (end < start)
{
return [];
}

var startIp = ToIPAddress(start, left.AddressFamily);
var endIp = ToIPAddress(end, left.AddressFamily);

InternalParseRange(true, startIp, endIp, out IEnumerable<IPNetwork2> networks);
return networks;
}
}
122 changes: 61 additions & 61 deletions src/TestProject/BigIntegerToUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,136 +16,136 @@ public class BigIntegerToUnitTest
[TestMethod]
public void TestToOctalString1()
{
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToOctalString();
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToOctalString();

Assert.AreEqual("037777777777", result);
}
Assert.AreEqual("037777777777", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToOctalString3()
{
var bigi = BigInteger.Parse("1048576");
bigi++;
string result = bigi.ToOctalString();
var bigi = BigInteger.Parse("1048576");
bigi++;
string result = bigi.ToOctalString();

Assert.AreEqual("04000001", result);
}
Assert.AreEqual("04000001", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToOctalString01()
{
BigInteger bigi = BigInteger.Zero;
bigi++;
string result = bigi.ToOctalString();
BigInteger bigi = BigInteger.Zero;
bigi++;
string result = bigi.ToOctalString();

Assert.AreEqual("01", result);
}
Assert.AreEqual("01", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToOctalString02()
{
BigInteger bigi = BigInteger.Zero;
bigi--;
string result = bigi.ToOctalString();
BigInteger bigi = BigInteger.Zero;
bigi--;
string result = bigi.ToOctalString();

Assert.AreEqual("377", result);
}
Assert.AreEqual("377", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToOctalString03()
{
BigInteger bigi = BigInteger.Zero;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
string result = bigi.ToOctalString();

Assert.AreEqual("371", result);
}
BigInteger bigi = BigInteger.Zero;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
bigi--;
string result = bigi.ToOctalString();

Assert.AreEqual("371", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToHexadecimalString1()
{
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToHexadecimalString();
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToHexadecimalString();

Assert.AreEqual("0FFFFFFFF", result);
}
Assert.AreEqual("0FFFFFFFF", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToBinaryString1()
{
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToBinaryString();
byte[] bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0x00];
var convertme = new BigInteger(bytes);
string result = convertme.ToBinaryString();

Assert.AreEqual("011111111111111111111111111111111", result);
}
Assert.AreEqual("011111111111111111111111111111111", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToBinaryString01()
{
BigInteger bigi = BigInteger.Zero;
bigi++;
string result = bigi.ToBinaryString();
BigInteger bigi = BigInteger.Zero;
bigi++;
string result = bigi.ToBinaryString();

Assert.AreEqual("01", result);
}
Assert.AreEqual("01", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToBinaryString2()
{
var convertme = new BigInteger(-1);
string result = convertme.ToBinaryString();
var convertme = new BigInteger(-1);
string result = convertme.ToBinaryString();

Assert.AreEqual("11111111", result);
}
Assert.AreEqual("11111111", result);
}

/// <summary>
/// Test.
/// </summary>
[TestMethod]
public void TestToBinaryString3()
{
byte[] bytes =
[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
];
var convertme = new BigInteger(bytes);
string result = convertme.ToBinaryString();

Assert.AreEqual("11111111", result);
}
byte[] bytes =
[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
];
var convertme = new BigInteger(bytes);
string result = convertme.ToBinaryString();

Assert.AreEqual("11111111", result);
}
}
25 changes: 25 additions & 0 deletions src/TestProject/IPAddressTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace TestProject;

/// <summary>
/// Test IPAddress Behaviour on MacOS
/// </summary>
[TestClass]
public class IPAddressTest
{
/// <summary>
/// Mixed IPv6 / IPvç notation
/// Why This Happens
/// .NET automatically uses mixed notation for IPv6 addresses when:
/// The first 96 bits are zero (IPv4-compatible)
/// The first 80 bits are zero and bits 81-96 are either all zeros or all ones (IPv4-mapped)
/// The pattern suggests an embedded IPv4 address
/// Your bytes ::0.1.0.0 are being interpreted as an IPv4-compatible IPv6 address.
/// </summary>
[TestMethod]
public void TestIPAddress()
{
byte[] bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0];
var ip = new IPAddress(bytes);
Assert.AreEqual("::0.1.0.0", ip.ToString());
}
}
Loading
Loading