From d1de1625f2ce868462e98bf2079e7f2b1a9faa3f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 29 Apr 2022 17:45:43 +0100 Subject: [PATCH] Move ToHost/NetworkOrder to Net namespace (#7) --- X10D.Tests/src/Net/Int16Tests.cs | 26 +++++++++++++++++ X10D.Tests/src/Net/Int32Tests.cs | 26 +++++++++++++++++ X10D.Tests/src/Net/Int64Tests.cs | 26 +++++++++++++++++ X10D/src/Int32Extensions/Int32Extensions.cs | 23 +-------------- X10D/src/Int64Extensions/Int64Extensions.cs | 24 +-------------- .../Int16Extensions.cs | 28 +++++++++--------- X10D/src/Net/Int32Extensions.cs | 29 +++++++++++++++++++ X10D/src/Net/Int64Extensions.cs | 29 +++++++++++++++++++ 8 files changed, 152 insertions(+), 59 deletions(-) create mode 100644 X10D.Tests/src/Net/Int16Tests.cs create mode 100644 X10D.Tests/src/Net/Int32Tests.cs create mode 100644 X10D.Tests/src/Net/Int64Tests.cs rename X10D/src/{Int16Extensions => Net}/Int16Extensions.cs (61%) create mode 100644 X10D/src/Net/Int32Extensions.cs create mode 100644 X10D/src/Net/Int64Extensions.cs diff --git a/X10D.Tests/src/Net/Int16Tests.cs b/X10D.Tests/src/Net/Int16Tests.cs new file mode 100644 index 0000000..9dea812 --- /dev/null +++ b/X10D.Tests/src/Net/Int16Tests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Net; + +namespace X10D.Tests.Net; + +[TestClass] +public class Int16Tests +{ + [TestMethod] + public void HostToNetworkOrder_ReturnsCorrectValue() + { + const short hostOrder = 0x0102; + const short networkOrder = 0x0201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? networkOrder : hostOrder, hostOrder.HostToNetworkOrder()); + } + + [TestMethod] + public void NetworkToHostOrder_ReturnsCorrectValue() + { + const short hostOrder = 0x0102; + const short networkOrder = 0x0201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? hostOrder : networkOrder, networkOrder.NetworkToHostOrder()); + } +} diff --git a/X10D.Tests/src/Net/Int32Tests.cs b/X10D.Tests/src/Net/Int32Tests.cs new file mode 100644 index 0000000..7157eef --- /dev/null +++ b/X10D.Tests/src/Net/Int32Tests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Net; + +namespace X10D.Tests.Net; + +[TestClass] +public class Int32Tests +{ + [TestMethod] + public void HostToNetworkOrder_ReturnsCorrectValue() + { + const int hostOrder = 0x01020304; + const int networkOrder = 0x04030201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? networkOrder : hostOrder, hostOrder.HostToNetworkOrder()); + } + + [TestMethod] + public void NetworkToHostOrder_ReturnsCorrectValue() + { + const int hostOrder = 0x01020304; + const int networkOrder = 0x04030201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? hostOrder : networkOrder, networkOrder.NetworkToHostOrder()); + } +} diff --git a/X10D.Tests/src/Net/Int64Tests.cs b/X10D.Tests/src/Net/Int64Tests.cs new file mode 100644 index 0000000..c207125 --- /dev/null +++ b/X10D.Tests/src/Net/Int64Tests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Net; + +namespace X10D.Tests.Net; + +[TestClass] +public class Int64Tests +{ + [TestMethod] + public void HostToNetworkOrder_ReturnsCorrectValue() + { + const long hostOrder = 0x0102030405060708; + const long networkOrder = 0x0807060504030201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? networkOrder : hostOrder, hostOrder.HostToNetworkOrder()); + } + + [TestMethod] + public void NetworkToHostOrder_ReturnsCorrectValue() + { + const long hostOrder = 0x0102030405060708; + const long networkOrder = 0x0807060504030201; + + Assert.AreEqual(BitConverter.IsLittleEndian ? hostOrder : networkOrder, networkOrder.NetworkToHostOrder()); + } +} diff --git a/X10D/src/Int32Extensions/Int32Extensions.cs b/X10D/src/Int32Extensions/Int32Extensions.cs index 3109dea..ff14a94 100644 --- a/X10D/src/Int32Extensions/Int32Extensions.cs +++ b/X10D/src/Int32Extensions/Int32Extensions.cs @@ -1,5 +1,4 @@ -using System.Net; -using X10D.Math; +using X10D.Math; namespace X10D; @@ -69,24 +68,4 @@ public static class Int32Extensions { return ((float)value).DegreesToRadians(); } - - /// - /// Converts an integer value from network byte order to host byte order. - /// - /// The value to convert, expressed in network byte order. - /// An integer value, expressed in host byte order. - public static int ToHostOrder(this int value) - { - return IPAddress.NetworkToHostOrder(value); - } - - /// - /// Converts an integer value from host byte order to network byte order. - /// - /// The value to convert, expressed in host byte order. - /// An integer value, expressed in network byte order. - public static int ToNetworkOrder(this int value) - { - return IPAddress.HostToNetworkOrder(value); - } } diff --git a/X10D/src/Int64Extensions/Int64Extensions.cs b/X10D/src/Int64Extensions/Int64Extensions.cs index b6463cd..6469bb5 100644 --- a/X10D/src/Int64Extensions/Int64Extensions.cs +++ b/X10D/src/Int64Extensions/Int64Extensions.cs @@ -1,6 +1,4 @@ -using System.Net; - -namespace X10D; +namespace X10D; /// /// Extension methods for . @@ -50,24 +48,4 @@ public static class Int64Extensions return persistence; } - - /// - /// Converts an integer value from network byte order to host byte order. - /// - /// The value to convert, expressed in network byte order. - /// An integer value, expressed in host byte order. - public static long ToHostOrder(this long value) - { - return IPAddress.NetworkToHostOrder(value); - } - - /// - /// Converts an integer value from host byte order to network byte order. - /// - /// The value to convert, expressed in host byte order. - /// An integer value, expressed in network byte order. - public static long ToNetworkOrder(this long value) - { - return IPAddress.HostToNetworkOrder(value); - } } diff --git a/X10D/src/Int16Extensions/Int16Extensions.cs b/X10D/src/Net/Int16Extensions.cs similarity index 61% rename from X10D/src/Int16Extensions/Int16Extensions.cs rename to X10D/src/Net/Int16Extensions.cs index 68999ac..1893df3 100644 --- a/X10D/src/Int16Extensions/Int16Extensions.cs +++ b/X10D/src/Net/Int16Extensions.cs @@ -1,29 +1,29 @@ using System.Net; -namespace X10D; +namespace X10D.Net; /// -/// Extension methods for . +/// Network-related extension methods for . /// public static class Int16Extensions { /// - /// Converts an integer value from network byte order to host byte order. - /// - /// The value to convert, expressed in network byte order. - /// An integer value, expressed in host byte order. - public static short ToHostOrder(this short value) - { - return IPAddress.NetworkToHostOrder(value); - } - - /// - /// Converts an integer value from host byte order to network byte order. + /// Converts a 16-bit signed integer value from host byte order to network byte order. /// /// The value to convert, expressed in host byte order. /// An integer value, expressed in network byte order. - public static short ToNetworkOrder(this short value) + public static short HostToNetworkOrder(this short value) { return IPAddress.HostToNetworkOrder(value); } + + /// + /// Converts a 16-bit signed integer value from network byte order to host byte order. + /// + /// The value to convert, expressed in network byte order. + /// An integer value, expressed in host byte order. + public static short NetworkToHostOrder(this short value) + { + return IPAddress.NetworkToHostOrder(value); + } } diff --git a/X10D/src/Net/Int32Extensions.cs b/X10D/src/Net/Int32Extensions.cs new file mode 100644 index 0000000..608af85 --- /dev/null +++ b/X10D/src/Net/Int32Extensions.cs @@ -0,0 +1,29 @@ +using System.Net; + +namespace X10D.Net; + +/// +/// IO-related extension methods for . +/// +public static class Int32Extensions +{ + /// + /// Converts a 32-bit signed integer value from host byte order to network byte order. + /// + /// The value to convert, expressed in host byte order. + /// An integer value, expressed in network byte order. + public static int HostToNetworkOrder(this int value) + { + return IPAddress.HostToNetworkOrder(value); + } + + /// + /// Converts a 32-bit signed integer value from network byte order to host byte order. + /// + /// The value to convert, expressed in network byte order. + /// An integer value, expressed in host byte order. + public static int NetworkToHostOrder(this int value) + { + return IPAddress.NetworkToHostOrder(value); + } +} diff --git a/X10D/src/Net/Int64Extensions.cs b/X10D/src/Net/Int64Extensions.cs new file mode 100644 index 0000000..4365cd6 --- /dev/null +++ b/X10D/src/Net/Int64Extensions.cs @@ -0,0 +1,29 @@ +using System.Net; + +namespace X10D.Net; + +/// +/// IO-related extension methods for . +/// +public static class Int64Extensions +{ + /// + /// Converts a 64-bit signed integer value from host byte order to network byte order. + /// + /// The value to convert, expressed in host byte order. + /// An integer value, expressed in network byte order. + public static long HostToNetworkOrder(this long value) + { + return IPAddress.HostToNetworkOrder(value); + } + + /// + /// Converts a 64-bit signed integer value from network byte order to host byte order. + /// + /// The value to convert, expressed in network byte order. + /// An integer value, expressed in host byte order. + public static long NetworkToHostOrder(this long value) + { + return IPAddress.NetworkToHostOrder(value); + } +}