private static void TestParseOne(string ipString, IPv4 expectedIP, bool expectedSuccess) { bool success = false; IPv4 testIP = IPv4.Zero; try { testIP = IPv4.Parse(ipString); success = (testIP == expectedIP); } catch (FormatException) { } if (success != expectedSuccess) { throw new TestFailedException(ipString); } IPv4 dup = IPv4.Parse(testIP.ToString()); if (testIP != dup) { throw new TestFailedException( String.Format("ToString {0}", testIP) ); } }
private static void TestBasics() { IPv4 a = new IPv4(0x1a2b3c4d); IPv4 b = IPv4.Parse("0x1a.0x2b.0x3c.0x4d"); byte [] ipv4Bytes = new byte [4] { 0x1a, 0x2b, 0x3c, 0x4d }; IPv4 c = IPv4.ParseBytes(ipv4Bytes); if ((uint)a != 0x1a2b3c4d) { throw new TestFailedException("a"); } if ((uint)b != 0x1a2b3c4d) { throw new TestFailedException("b"); } if ((uint)c != 0x1a2b3c4d) { throw new TestFailedException("c"); } if (a.Equals(b) == false) { throw new TestFailedException("a Equals b"); } if ((a == c) == false || (a != c)) { throw new TestFailedException("a c == !="); } for (int i = 0; i < ipv4Bytes.Length; i++) { if (ipv4Bytes[i] != c.GetAddressBytes()[i]) { throw new TestFailedException("GetAddressBytes"); } } byte [] ipAddrBytes = ((IPAddress)c).GetAddressBytes(); for (int i = 0; i < ipv4Bytes.Length; i++) { if (ipv4Bytes[i] != ipAddrBytes[i]) { throw new TestFailedException("IPAddress"); } } IPAddress ipa = IPAddress.Parse(c.ToString()); if (new IPv4(ipa) != c) { throw new TestFailedException("IPv4(IPAddress) Constructor"); } }
public static void TestCount() { for (int i = IPv4.BitCount; i >= 0; i--) { IPv4 addr = IPv4.NetMask(i); if (IPv4.GetMaskLength(addr) != i) { throw new TestFailedException( String.Format("TestCount {0}", i) ); } IPv4 addrDup = IPv4.Parse(addr.ToString()); if (addrDup != addr) { throw new TestFailedException( String.Format("TestCountDup {0}", i) ); } } }