diff --git a/X10D.Tests/src/Collections/EnumerableTests.cs b/X10D.Tests/src/Collections/EnumerableTests.cs index 40cb35e..6d990ee 100644 --- a/X10D.Tests/src/Collections/EnumerableTests.cs +++ b/X10D.Tests/src/Collections/EnumerableTests.cs @@ -7,21 +7,20 @@ namespace X10D.Tests.Collections; public class EnumerableTests { [TestMethod] - public void EnumerableShuffledShouldBeDifferentOrder() - { - var list = new List(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order - var shuffled = new List(list); - - CollectionAssert.AreEqual(list, shuffled); - - shuffled = new List(list.Shuffled()); - - CollectionAssert.AreNotEqual(list, shuffled); - } - - [TestMethod] - public void NullShuffledShouldThrow() + public void Shuffled_ShouldThrow_GivenNull() { Assert.ThrowsException(() => ((List?)null)!.Shuffled()); } + + [TestMethod] + public void Shuffled_ShouldReorder_GivenNotNull() + { + int[] array = Enumerable.Range(1, 52).ToArray(); // 52! chance of being shuffled to the same order + int[] shuffled = array[..]; + + CollectionAssert.AreEqual(array, shuffled); + + shuffled = array.Shuffled().ToArray(); + CollectionAssert.AreNotEqual(array, shuffled); + } } diff --git a/X10D.Tests/src/Collections/ListTests.cs b/X10D.Tests/src/Collections/ListTests.cs index 6455361..3be91f8 100644 --- a/X10D.Tests/src/Collections/ListTests.cs +++ b/X10D.Tests/src/Collections/ListTests.cs @@ -39,6 +39,33 @@ public class ListTests CollectionAssert.AreEqual(comparison, args[1..]); } + [TestMethod] + public void Fill_ShouldThrow_GivenExceededCount() + { + int[] array = Array.Empty(); + var list = new List(); + Assert.ThrowsException(() => array.Fill(0, 0, 1)); + Assert.ThrowsException(() => list.Fill(0, 0, 1)); + } + + [TestMethod] + public void Fill_ShouldThrow_GivenNegativeCount() + { + int[] array = Array.Empty(); + var list = new List(); + Assert.ThrowsException(() => array.Fill(0, 0, -1)); + Assert.ThrowsException(() => list.Fill(0, 0, -1)); + } + + [TestMethod] + public void Fill_ShouldThrow_GivenNegativeStartIndex() + { + int[] array = Array.Empty(); + var list = new List(); + Assert.ThrowsException(() => array.Fill(0, -1, 0)); + Assert.ThrowsException(() => list.Fill(0, -1, 0)); + } + [TestMethod] public void Fill_ShouldThrow_GivenNull() { @@ -46,6 +73,8 @@ public class ListTests List? list = null; Assert.ThrowsException(() => array!.Fill(0)); Assert.ThrowsException(() => list!.Fill(0)); + Assert.ThrowsException(() => array!.Fill(0, 0, 0)); + Assert.ThrowsException(() => list!.Fill(0, 0, 0)); } [TestMethod] diff --git a/X10D.Tests/src/Core/ByteTests.cs b/X10D.Tests/src/Core/ByteTests.cs index f782838..28a644a 100644 --- a/X10D.Tests/src/Core/ByteTests.cs +++ b/X10D.Tests/src/Core/ByteTests.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; namespace X10D.Tests.Core; diff --git a/X10D.Tests/src/Core/DoubleTests.cs b/X10D.Tests/src/Core/DoubleTests.cs index f8f59a4..ace6244 100644 --- a/X10D.Tests/src/Core/DoubleTests.cs +++ b/X10D.Tests/src/Core/DoubleTests.cs @@ -29,7 +29,7 @@ public class DoubleTests Assert.AreEqual(Complex.Infinity, double.NegativeInfinity.ComplexSqrt()); Assert.AreEqual(Complex.Infinity, double.PositiveInfinity.ComplexSqrt()); } - + /// /// Test for /// @@ -62,17 +62,6 @@ public class DoubleTests Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians()); } - /// - /// Tests for . - /// - [TestMethod] - public void GetBytes() - { - CollectionAssert.AreEqual( - new byte[] {0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40}, - Math.PI.GetBytes()); - } - /// /// Tests for . /// diff --git a/X10D.Tests/src/Core/EnumerableTests.cs b/X10D.Tests/src/Core/EnumerableTests.cs index 1011ba9..d3b4341 100644 --- a/X10D.Tests/src/Core/EnumerableTests.cs +++ b/X10D.Tests/src/Core/EnumerableTests.cs @@ -1,5 +1,4 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using X10D.Collections; using X10D.Core; namespace X10D.Tests.Core; @@ -11,19 +10,11 @@ public class EnumerableTests [DataRow(1)] [DataRow("f")] [DataRow(true)] - public void AsEnumerable(object o) + public void AsEnumerable_ShouldWrapElement_GivenValue(object o) { IEnumerable array = o.AsEnumerable().ToArray(); // prevent multiple enumeration of IEnumerable Assert.IsNotNull(array); Assert.IsTrue(array.Count() == 1); Assert.AreEqual(o, array.ElementAt(0)); } - - [TestMethod] - [DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] - public void Shuffled(params int[] source) - { - int[] shuffled = source.Shuffled().ToArray(); // ToArray forces type match - CollectionAssert.AreNotEqual(source, shuffled); - } } diff --git a/X10D.Tests/src/IO/ByteTests.cs b/X10D.Tests/src/IO/ByteTests.cs new file mode 100644 index 0000000..47c14cb --- /dev/null +++ b/X10D.Tests/src/IO/ByteTests.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class ByteTests +{ + [TestMethod] + public void GetBytes_ReturnsArrayContainingItself() + { + const byte value = 0xFF; + CollectionAssert.AreEqual(new[] {value}, value.GetBytes()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanContainingItself_GivenLargeEnoughSpan() + { + const byte value = 0xFF; + Span buffer = stackalloc byte[1]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(new[] {value}, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const byte value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/DoubleTests.cs b/X10D.Tests/src/IO/DoubleTests.cs new file mode 100644 index 0000000..616fd79 --- /dev/null +++ b/X10D.Tests/src/IO/DoubleTests.cs @@ -0,0 +1,66 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class DoubleTests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const double value = 42.5; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0, 0, 0, 0, 0, 0x40, 0x45, 0x40} + : new byte[] {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const double value = 42.5; + byte[] littleEndian = {0, 0, 0, 0, 0, 0x40, 0x45, 0x40}; + byte[] bigEndian = {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const double value = 42.5; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0, 0, 0, 0, 0, 0x40, 0x45, 0x40} + : new byte[] {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; + + Span buffer = stackalloc byte[8]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const double value = 42.5; + byte[] littleEndian = {0, 0, 0, 0, 0, 0x40, 0x45, 0x40}; + byte[] bigEndian = {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; + + Span buffer = stackalloc byte[8]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const double value = 42.5; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/Int16Tests.cs b/X10D.Tests/src/IO/Int16Tests.cs new file mode 100644 index 0000000..346e95a --- /dev/null +++ b/X10D.Tests/src/IO/Int16Tests.cs @@ -0,0 +1,62 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class Int16Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const short value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const short value = 0x0F; + byte[] littleEndian = {0x0F, 0}; + byte[] bigEndian = {0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const short value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; + + Span buffer = stackalloc byte[2]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const short value = 0x0F; + byte[] littleEndian = {0x0F, 0}; + byte[] bigEndian = {0, 0x0F}; + + Span buffer = stackalloc byte[2]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const short value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/Int32Tests.cs b/X10D.Tests/src/IO/Int32Tests.cs new file mode 100644 index 0000000..7135b7c --- /dev/null +++ b/X10D.Tests/src/IO/Int32Tests.cs @@ -0,0 +1,62 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class Int32Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const int value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const int value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const int value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[4]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const int value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[4]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const int value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/Int64Tests.cs b/X10D.Tests/src/IO/Int64Tests.cs new file mode 100644 index 0000000..1f7623b --- /dev/null +++ b/X10D.Tests/src/IO/Int64Tests.cs @@ -0,0 +1,66 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class Int64Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const long value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} + : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const long value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const long value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} + : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[8]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const long value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[8]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const long value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/SByteTests.cs b/X10D.Tests/src/IO/SByteTests.cs new file mode 100644 index 0000000..8f28976 --- /dev/null +++ b/X10D.Tests/src/IO/SByteTests.cs @@ -0,0 +1,33 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +[CLSCompliant(false)] +public class SByteTests +{ + [TestMethod] + public void GetBytes_ReturnsArrayContainingItself() + { + const sbyte value = 0x0F; + CollectionAssert.AreEqual(new[] {(byte)value}, value.GetBytes()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanContainingItself_GivenLargeEnoughSpan() + { + const sbyte value = 0x0F; + Span buffer = stackalloc byte[1]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(new[] {(byte)value}, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const sbyte value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/SingleTests.cs b/X10D.Tests/src/IO/SingleTests.cs new file mode 100644 index 0000000..0bca2ce --- /dev/null +++ b/X10D.Tests/src/IO/SingleTests.cs @@ -0,0 +1,66 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +public class SingleTests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const float value = 42.5f; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0, 0, 0x2A, 0x42} + : new byte[] {0x42, 0x2A, 0, 0}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const float value = 42.5f; + byte[] littleEndian = {0, 0, 0x2A, 0x42}; + byte[] bigEndian = {0x42, 0x2A, 0, 0}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const float value = 42.5f; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0, 0, 0x2A, 0x42} + : new byte[] {0x42, 0x2A, 0, 0}; + + Span buffer = stackalloc byte[4]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const float value = 42.5f; + byte[] littleEndian = {0, 0, 0x2A, 0x42}; + byte[] bigEndian = {0x42, 0x2A, 0, 0}; + + Span buffer = stackalloc byte[4]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const float value = 42.5f; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/UInt16Tests.cs b/X10D.Tests/src/IO/UInt16Tests.cs new file mode 100644 index 0000000..08c0e61 --- /dev/null +++ b/X10D.Tests/src/IO/UInt16Tests.cs @@ -0,0 +1,63 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +[CLSCompliant(false)] +public class UInt16Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const ushort value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const ushort value = 0x0F; + byte[] littleEndian = {0x0F, 0}; + byte[] bigEndian = {0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const ushort value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; + + Span buffer = stackalloc byte[2]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const ushort value = 0x0F; + byte[] littleEndian = {0x0F, 0}; + byte[] bigEndian = {0, 0x0F}; + + Span buffer = stackalloc byte[2]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const ushort value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/UInt32Tests.cs b/X10D.Tests/src/IO/UInt32Tests.cs new file mode 100644 index 0000000..a9b3cc9 --- /dev/null +++ b/X10D.Tests/src/IO/UInt32Tests.cs @@ -0,0 +1,63 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +[CLSCompliant(false)] +public class UInt32Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const uint value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const uint value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const uint value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[4]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const uint value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[4]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const uint value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/IO/UInt64Tests.cs b/X10D.Tests/src/IO/UInt64Tests.cs new file mode 100644 index 0000000..d31a209 --- /dev/null +++ b/X10D.Tests/src/IO/UInt64Tests.cs @@ -0,0 +1,67 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.IO; + +namespace X10D.Tests.IO; + +[TestClass] +[CLSCompliant(false)] +public class UInt64Tests +{ + [TestMethod] + public void GetBytes_ReturnsCorrectValue() + { + const ulong value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} + : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; + CollectionAssert.AreEqual(bytes, value.GetBytes()); + } + + [TestMethod] + public void GetBytes_ReturnsCorrectValue_WithEndianness() + { + const ulong value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); + CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + { + const ulong value = 0x0F; + byte[] bytes = BitConverter.IsLittleEndian + ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} + : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[8]; + Assert.IsTrue(value.TryWriteBytes(buffer)); + CollectionAssert.AreEqual(bytes, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + { + const ulong value = 0x0F; + byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; + byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + + Span buffer = stackalloc byte[8]; + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.LittleEndian)); + CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); + + Assert.IsTrue(value.TryWriteBytes(buffer, Endianness.BigEndian)); + CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + } + + [TestMethod] + public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + { + const ulong value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.IsFalse(value.TryWriteBytes(buffer)); + } +} diff --git a/X10D.Tests/src/Time/ByteTests.cs b/X10D.Tests/src/Time/ByteTests.cs index 1fddfc6..602db04 100644 --- a/X10D.Tests/src/Time/ByteTests.cs +++ b/X10D.Tests/src/Time/ByteTests.cs @@ -7,19 +7,19 @@ namespace X10D.Tests.Time; public class ByteTests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() { - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Ticks()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Seconds()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Minutes()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Days()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Hours()); - Assert.AreEqual(TimeSpan.Zero, ((byte)0).Weeks()); + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((byte)0).FromUnixTimeMilliseconds()); } [TestMethod] - public void OneShouldBePositive() + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((byte)0).FromUnixTimeSeconds()); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(((byte)1).Ticks() > TimeSpan.Zero); Assert.IsTrue(((byte)1).Milliseconds() > TimeSpan.Zero); @@ -29,4 +29,16 @@ public class ByteTests Assert.IsTrue(((byte)1).Hours() > TimeSpan.Zero); Assert.IsTrue(((byte)1).Weeks() > TimeSpan.Zero); } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() + { + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Ticks()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Seconds()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Minutes()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Days()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Hours()); + Assert.AreEqual(TimeSpan.Zero, ((byte)0).Weeks()); + } } diff --git a/X10D.Tests/src/Time/DateTimeOffsetTests.cs b/X10D.Tests/src/Time/DateTimeOffsetTests.cs index ccd9206..eb9f1cc 100644 --- a/X10D.Tests/src/Time/DateTimeOffsetTests.cs +++ b/X10D.Tests/src/Time/DateTimeOffsetTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Time; public class DateTimeOffsetTests { [TestMethod] - public void AgeShouldBeCorrect() + public void Age_ShouldBeDifference_Given1Jan2000() { DateTimeOffset birthday = new DateTime(2000, 1, 1); DateTimeOffset today = DateTime.Now.Date; @@ -16,7 +16,7 @@ public class DateTimeOffsetTests } [TestMethod] - public void FirstShouldBeCorrect() + public void First_ShouldBeSaturday_Given1Jan2000() { DateTimeOffset date = new DateTime(2000, 1, 1); @@ -30,7 +30,7 @@ public class DateTimeOffsetTests } [TestMethod] - public void FirstDayOfMonthShouldBeCorrect() + public void FirstDayOfMonth_ShouldBe1st_GivenToday() { DateTimeOffset today = DateTime.Now.Date; @@ -38,7 +38,7 @@ public class DateTimeOffsetTests } [TestMethod] - public void LastShouldBeCorrect() + public void LastSaturday_ShouldBe29th_Given1Jan2000() { DateTimeOffset date = new DateTime(2000, 1, 1); @@ -52,15 +52,65 @@ public class DateTimeOffsetTests } [TestMethod] - public void LastDayOfMonthShouldBeCorrect() + public void LastDayOfMonth_ShouldBe28th_GivenFebruary1999() { - DateTimeOffset date = new DateTime(2000, 1, 1); + DateTimeOffset february = new DateTime(1999, 2, 1); - Assert.AreEqual(new DateTime(date.Year, date.Month, 31), date.LastDayOfMonth()); + Assert.AreEqual(new DateTime(february.Year, february.Month, 28), february.LastDayOfMonth()); } [TestMethod] - public void NextShouldBeCorrect() + public void LastDayOfMonth_ShouldBe29th_GivenFebruary2000() + { + DateTimeOffset february = new DateTime(2000, 2, 1); + + Assert.AreEqual(new DateTime(february.Year, february.Month, 29), february.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe28th_GivenFebruary2001() + { + DateTimeOffset february = new DateTime(2001, 2, 1); + + Assert.AreEqual(new DateTime(february.Year, february.Month, 28), february.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe30th_GivenAprilJuneSeptemberNovember() + { + DateTimeOffset april = new DateTime(2000, 4, 1); + DateTimeOffset june = new DateTime(2000, 6, 1); + DateTimeOffset september = new DateTime(2000, 9, 1); + DateTimeOffset november = new DateTime(2000, 11, 1); + + Assert.AreEqual(new DateTime(april.Year, april.Month, 30), april.LastDayOfMonth()); + Assert.AreEqual(new DateTime(june.Year, june.Month, 30), june.LastDayOfMonth()); + Assert.AreEqual(new DateTime(september.Year, september.Month, 30), september.LastDayOfMonth()); + Assert.AreEqual(new DateTime(november.Year, november.Month, 30), november.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe31st_GivenJanuaryMarchMayJulyAugustOctoberDecember() + { + DateTimeOffset january = new DateTime(2000, 1, 1); + DateTimeOffset march = new DateTime(2000, 3, 1); + DateTimeOffset may = new DateTime(2000, 5, 1); + DateTimeOffset july = new DateTime(2000, 7, 1); + DateTimeOffset august = new DateTime(2000, 8, 1); + DateTimeOffset october = new DateTime(2000, 10, 1); + DateTimeOffset december = new DateTime(2000, 12, 1); + + Assert.AreEqual(new DateTime(january.Year, january.Month, 31), january.LastDayOfMonth()); + Assert.AreEqual(new DateTime(march.Year, march.Month, 31), march.LastDayOfMonth()); + Assert.AreEqual(new DateTime(may.Year, may.Month, 31), may.LastDayOfMonth()); + Assert.AreEqual(new DateTime(july.Year, july.Month, 31), july.LastDayOfMonth()); + Assert.AreEqual(new DateTime(august.Year, august.Month, 31), august.LastDayOfMonth()); + Assert.AreEqual(new DateTime(october.Year, october.Month, 31), october.LastDayOfMonth()); + Assert.AreEqual(new DateTime(december.Year, december.Month, 31), december.LastDayOfMonth()); + } + + [TestMethod] + public void NextSaturday_ShouldBe8th_Given1Jan2000() { DateTimeOffset date = new DateTime(2000, 1, 1); diff --git a/X10D.Tests/src/Time/DateTimeTests.cs b/X10D.Tests/src/Time/DateTimeTests.cs index f2e6aac..0ea064a 100644 --- a/X10D.Tests/src/Time/DateTimeTests.cs +++ b/X10D.Tests/src/Time/DateTimeTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Time; public class DateTimeTests { [TestMethod] - public void AgeShouldBeCorrect() + public void Age_ShouldBeDifference_Given1Jan2000() { var birthday = new DateTime(2000, 1, 1); DateTime today = DateTime.Now.Date; @@ -16,7 +16,7 @@ public class DateTimeTests } [TestMethod] - public void FirstShouldBeCorrect() + public void First_ShouldBeSaturday_Given1Jan2000() { var date = new DateTime(2000, 1, 1); @@ -30,7 +30,7 @@ public class DateTimeTests } [TestMethod] - public void FirstDayOfMonthShouldBeCorrect() + public void FirstDayOfMonth_ShouldBe1st_GivenToday() { DateTime today = DateTime.Now.Date; @@ -38,7 +38,7 @@ public class DateTimeTests } [TestMethod] - public void LastShouldBeCorrect() + public void LastSaturday_ShouldBe29th_Given1Jan2000() { var date = new DateTime(2000, 1, 1); @@ -52,15 +52,65 @@ public class DateTimeTests } [TestMethod] - public void LastDayOfMonthShouldBeCorrect() + public void LastDayOfMonth_ShouldBe28th_GivenFebruary1999() { - var date = new DateTime(2000, 1, 1); + var february = new DateTime(1999, 2, 1); - Assert.AreEqual(new DateTime(date.Year, date.Month, 31), date.LastDayOfMonth()); + Assert.AreEqual(new DateTime(february.Year, february.Month, 28), february.LastDayOfMonth()); } [TestMethod] - public void NextShouldBeCorrect() + public void LastDayOfMonth_ShouldBe29th_GivenFebruary2000() + { + var february = new DateTime(2000, 2, 1); + + Assert.AreEqual(new DateTime(february.Year, february.Month, 29), february.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe28th_GivenFebruary2001() + { + var february = new DateTime(2001, 2, 1); + + Assert.AreEqual(new DateTime(february.Year, february.Month, 28), february.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe30th_GivenAprilJuneSeptemberNovember() + { + var april = new DateTime(2000, 4, 1); + var june = new DateTime(2000, 6, 1); + var september = new DateTime(2000, 9, 1); + var november = new DateTime(2000, 11, 1); + + Assert.AreEqual(new DateTime(april.Year, april.Month, 30), april.LastDayOfMonth()); + Assert.AreEqual(new DateTime(june.Year, june.Month, 30), june.LastDayOfMonth()); + Assert.AreEqual(new DateTime(september.Year, september.Month, 30), september.LastDayOfMonth()); + Assert.AreEqual(new DateTime(november.Year, november.Month, 30), november.LastDayOfMonth()); + } + + [TestMethod] + public void LastDayOfMonth_ShouldBe31st_GivenJanuaryMarchMayJulyAugustOctoberDecember() + { + var january = new DateTime(2000, 1, 1); + var march = new DateTime(2000, 3, 1); + var may = new DateTime(2000, 5, 1); + var july = new DateTime(2000, 7, 1); + var august = new DateTime(2000, 8, 1); + var october = new DateTime(2000, 10, 1); + var december = new DateTime(2000, 12, 1); + + Assert.AreEqual(new DateTime(january.Year, january.Month, 31), january.LastDayOfMonth()); + Assert.AreEqual(new DateTime(march.Year, march.Month, 31), march.LastDayOfMonth()); + Assert.AreEqual(new DateTime(may.Year, may.Month, 31), may.LastDayOfMonth()); + Assert.AreEqual(new DateTime(july.Year, july.Month, 31), july.LastDayOfMonth()); + Assert.AreEqual(new DateTime(august.Year, august.Month, 31), august.LastDayOfMonth()); + Assert.AreEqual(new DateTime(october.Year, october.Month, 31), october.LastDayOfMonth()); + Assert.AreEqual(new DateTime(december.Year, december.Month, 31), december.LastDayOfMonth()); + } + + [TestMethod] + public void NextSaturday_ShouldBe8th_Given1Jan2000() { var date = new DateTime(2000, 1, 1); @@ -68,7 +118,7 @@ public class DateTimeTests } [TestMethod] - public void ToUnixTimeMillisecondsShouldBeCorrect() + public void ToUnixTimeMilliseconds_ShouldBe946684800000_Given1Jan2000() { var date = new DateTime(2000, 1, 1); @@ -76,7 +126,7 @@ public class DateTimeTests } [TestMethod] - public void ToUnixTimeSecondsShouldBeCorrect() + public void ToUnixTimeSeconds_ShouldBe946684800_Given1Jan2000() { var date = new DateTime(2000, 1, 1); diff --git a/X10D.Tests/src/Time/DecimalTests.cs b/X10D.Tests/src/Time/DecimalTests.cs index 80435c4..cd8cf8b 100644 --- a/X10D.Tests/src/Time/DecimalTests.cs +++ b/X10D.Tests/src/Time/DecimalTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Time; public class DecimalTests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { Assert.AreEqual(TimeSpan.Zero, 0m.Milliseconds()); Assert.AreEqual(TimeSpan.Zero, 0m.Seconds()); @@ -18,7 +18,7 @@ public class DecimalTests } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1m.Milliseconds() > TimeSpan.Zero); Assert.IsTrue(1m.Seconds() > TimeSpan.Zero); @@ -29,7 +29,7 @@ public class DecimalTests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() { Assert.IsTrue((-1m).Milliseconds() < TimeSpan.Zero); Assert.IsTrue((-1m).Seconds() < TimeSpan.Zero); diff --git a/X10D.Tests/src/Time/DoubleTests.cs b/X10D.Tests/src/Time/DoubleTests.cs index 627cb74..17c7f2e 100644 --- a/X10D.Tests/src/Time/DoubleTests.cs +++ b/X10D.Tests/src/Time/DoubleTests.cs @@ -19,7 +19,7 @@ public class DoubleTests } [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { Assert.AreEqual(TimeSpan.Zero, _zero.Milliseconds()); Assert.AreEqual(TimeSpan.Zero, _zero.Seconds()); @@ -30,7 +30,7 @@ public class DoubleTests } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(_one.Milliseconds() > TimeSpan.Zero); Assert.IsTrue(_one.Seconds() > TimeSpan.Zero); @@ -41,7 +41,7 @@ public class DoubleTests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() { Assert.IsTrue((_negativeOne).Milliseconds() < TimeSpan.Zero); Assert.IsTrue((_negativeOne).Seconds() < TimeSpan.Zero); diff --git a/X10D.Tests/src/Time/HalfTests.cs b/X10D.Tests/src/Time/HalfTests.cs index 64a0346..a70aed2 100644 --- a/X10D.Tests/src/Time/HalfTests.cs +++ b/X10D.Tests/src/Time/HalfTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Time; public class HalfTests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { Assert.AreEqual(TimeSpan.Zero, 0.0.Milliseconds()); Assert.AreEqual(TimeSpan.Zero, 0.0.Seconds()); @@ -18,7 +18,7 @@ public class HalfTests } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1.0.Milliseconds() > TimeSpan.Zero); Assert.IsTrue(1.0.Seconds() > TimeSpan.Zero); @@ -29,7 +29,7 @@ public class HalfTests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() { Assert.IsTrue((-1.0).Milliseconds() < TimeSpan.Zero); Assert.IsTrue((-1.0).Seconds() < TimeSpan.Zero); diff --git a/X10D.Tests/src/Time/Int16Tests.cs b/X10D.Tests/src/Time/Int16Tests.cs index 098ee0e..36b2906 100644 --- a/X10D.Tests/src/Time/Int16Tests.cs +++ b/X10D.Tests/src/Time/Int16Tests.cs @@ -7,19 +7,31 @@ namespace X10D.Tests.Time; public class Int16Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() { - Assert.AreEqual(TimeSpan.Zero, ((short)0).Ticks()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Seconds()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Minutes()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Days()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Hours()); - Assert.AreEqual(TimeSpan.Zero, ((short)0).Weeks()); + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((short)0).FromUnixTimeMilliseconds()); + } + + [TestMethod] + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((short)0).FromUnixTimeSeconds()); } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() + { + Assert.IsTrue(((short)-1).Ticks() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Milliseconds() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Seconds() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Minutes() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Days() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Hours() < TimeSpan.Zero); + Assert.IsTrue(((short)-1).Weeks() < TimeSpan.Zero); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(((short)1).Ticks() > TimeSpan.Zero); Assert.IsTrue(((short)1).Milliseconds() > TimeSpan.Zero); @@ -31,14 +43,14 @@ public class Int16Tests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { - Assert.IsTrue(((short)-1).Ticks() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Milliseconds() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Seconds() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Minutes() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Days() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Hours() < TimeSpan.Zero); - Assert.IsTrue(((short)-1).Weeks() < TimeSpan.Zero); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Ticks()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Seconds()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Minutes()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Days()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Hours()); + Assert.AreEqual(TimeSpan.Zero, ((short)0).Weeks()); } } diff --git a/X10D.Tests/src/Time/Int32Tests.cs b/X10D.Tests/src/Time/Int32Tests.cs index 35a1bed..d954f8d 100644 --- a/X10D.Tests/src/Time/Int32Tests.cs +++ b/X10D.Tests/src/Time/Int32Tests.cs @@ -7,19 +7,31 @@ namespace X10D.Tests.Time; public class Int32Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() { - Assert.AreEqual(TimeSpan.Zero, 0.Ticks()); - Assert.AreEqual(TimeSpan.Zero, 0.Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, 0.Seconds()); - Assert.AreEqual(TimeSpan.Zero, 0.Minutes()); - Assert.AreEqual(TimeSpan.Zero, 0.Days()); - Assert.AreEqual(TimeSpan.Zero, 0.Hours()); - Assert.AreEqual(TimeSpan.Zero, 0.Weeks()); + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0.FromUnixTimeMilliseconds()); } [TestMethod] - public void OneShouldBePositive() + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0.FromUnixTimeSeconds()); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() + { + Assert.IsTrue((-1).Ticks() < TimeSpan.Zero); + Assert.IsTrue((-1).Milliseconds() < TimeSpan.Zero); + Assert.IsTrue((-1).Seconds() < TimeSpan.Zero); + Assert.IsTrue((-1).Minutes() < TimeSpan.Zero); + Assert.IsTrue((-1).Days() < TimeSpan.Zero); + Assert.IsTrue((-1).Hours() < TimeSpan.Zero); + Assert.IsTrue((-1).Weeks() < TimeSpan.Zero); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1.Ticks() > TimeSpan.Zero); Assert.IsTrue(1.Milliseconds() > TimeSpan.Zero); @@ -31,14 +43,14 @@ public class Int32Tests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { - Assert.IsTrue((-1).Ticks() < TimeSpan.Zero); - Assert.IsTrue((-1).Milliseconds() < TimeSpan.Zero); - Assert.IsTrue((-1).Seconds() < TimeSpan.Zero); - Assert.IsTrue((-1).Minutes() < TimeSpan.Zero); - Assert.IsTrue((-1).Days() < TimeSpan.Zero); - Assert.IsTrue((-1).Hours() < TimeSpan.Zero); - Assert.IsTrue((-1).Weeks() < TimeSpan.Zero); + Assert.AreEqual(TimeSpan.Zero, 0.Ticks()); + Assert.AreEqual(TimeSpan.Zero, 0.Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, 0.Seconds()); + Assert.AreEqual(TimeSpan.Zero, 0.Minutes()); + Assert.AreEqual(TimeSpan.Zero, 0.Days()); + Assert.AreEqual(TimeSpan.Zero, 0.Hours()); + Assert.AreEqual(TimeSpan.Zero, 0.Weeks()); } } diff --git a/X10D.Tests/src/Time/Int64Tests.cs b/X10D.Tests/src/Time/Int64Tests.cs index 6edfd14..99525cc 100644 --- a/X10D.Tests/src/Time/Int64Tests.cs +++ b/X10D.Tests/src/Time/Int64Tests.cs @@ -7,19 +7,31 @@ namespace X10D.Tests.Time; public class Int64Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() { - Assert.AreEqual(TimeSpan.Zero, 0L.Ticks()); - Assert.AreEqual(TimeSpan.Zero, 0L.Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, 0L.Seconds()); - Assert.AreEqual(TimeSpan.Zero, 0L.Minutes()); - Assert.AreEqual(TimeSpan.Zero, 0L.Days()); - Assert.AreEqual(TimeSpan.Zero, 0L.Hours()); - Assert.AreEqual(TimeSpan.Zero, 0L.Weeks()); + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0L.FromUnixTimeMilliseconds()); } [TestMethod] - public void OneShouldBePositive() + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0L.FromUnixTimeSeconds()); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() + { + Assert.IsTrue((-1L).Ticks() < TimeSpan.Zero); + Assert.IsTrue((-1L).Milliseconds() < TimeSpan.Zero); + Assert.IsTrue((-1L).Seconds() < TimeSpan.Zero); + Assert.IsTrue((-1L).Minutes() < TimeSpan.Zero); + Assert.IsTrue((-1L).Days() < TimeSpan.Zero); + Assert.IsTrue((-1L).Hours() < TimeSpan.Zero); + Assert.IsTrue((-1L).Weeks() < TimeSpan.Zero); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1L.Ticks() > TimeSpan.Zero); Assert.IsTrue(1L.Milliseconds() > TimeSpan.Zero); @@ -31,14 +43,14 @@ public class Int64Tests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { - Assert.IsTrue((-1L).Ticks() < TimeSpan.Zero); - Assert.IsTrue((-1L).Milliseconds() < TimeSpan.Zero); - Assert.IsTrue((-1L).Seconds() < TimeSpan.Zero); - Assert.IsTrue((-1L).Minutes() < TimeSpan.Zero); - Assert.IsTrue((-1L).Days() < TimeSpan.Zero); - Assert.IsTrue((-1L).Hours() < TimeSpan.Zero); - Assert.IsTrue((-1L).Weeks() < TimeSpan.Zero); + Assert.AreEqual(TimeSpan.Zero, 0L.Ticks()); + Assert.AreEqual(TimeSpan.Zero, 0L.Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, 0L.Seconds()); + Assert.AreEqual(TimeSpan.Zero, 0L.Minutes()); + Assert.AreEqual(TimeSpan.Zero, 0L.Days()); + Assert.AreEqual(TimeSpan.Zero, 0L.Hours()); + Assert.AreEqual(TimeSpan.Zero, 0L.Weeks()); } } diff --git a/X10D.Tests/src/Time/SByteTests.cs b/X10D.Tests/src/Time/SByteTests.cs index d17b05b..dd648b7 100644 --- a/X10D.Tests/src/Time/SByteTests.cs +++ b/X10D.Tests/src/Time/SByteTests.cs @@ -8,7 +8,19 @@ namespace X10D.Tests.Time; public class SByteTests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((sbyte)0).FromUnixTimeMilliseconds()); + } + + [TestMethod] + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((sbyte)0).FromUnixTimeSeconds()); + } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { Assert.AreEqual(TimeSpan.Zero, ((sbyte)0).Ticks()); Assert.AreEqual(TimeSpan.Zero, ((sbyte)0).Milliseconds()); @@ -20,7 +32,7 @@ public class SByteTests } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(((sbyte)1).Ticks() > TimeSpan.Zero); Assert.IsTrue(((sbyte)1).Milliseconds() > TimeSpan.Zero); @@ -32,7 +44,7 @@ public class SByteTests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() { Assert.IsTrue(((sbyte)-1).Ticks() < TimeSpan.Zero); Assert.IsTrue(((sbyte)-1).Milliseconds() < TimeSpan.Zero); diff --git a/X10D.Tests/src/Time/SingleTests.cs b/X10D.Tests/src/Time/SingleTests.cs index 5faf8d2..6feb82f 100644 --- a/X10D.Tests/src/Time/SingleTests.cs +++ b/X10D.Tests/src/Time/SingleTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Time; public class SingleTests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() { Assert.AreEqual(TimeSpan.Zero, 0f.Milliseconds()); Assert.AreEqual(TimeSpan.Zero, 0f.Seconds()); @@ -18,7 +18,7 @@ public class SingleTests } [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1f.Milliseconds() > TimeSpan.Zero); Assert.IsTrue(1f.Seconds() > TimeSpan.Zero); @@ -29,7 +29,7 @@ public class SingleTests } [TestMethod] - public void MinusOneShouldBeNegative() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_GivenMinusOne() { Assert.IsTrue((-1f).Milliseconds() < TimeSpan.Zero); Assert.IsTrue((-1f).Seconds() < TimeSpan.Zero); diff --git a/X10D.Tests/src/Time/TimeSpanTests.cs b/X10D.Tests/src/Time/TimeSpanTests.cs index ca8b1f0..cc34618 100644 --- a/X10D.Tests/src/Time/TimeSpanTests.cs +++ b/X10D.Tests/src/Time/TimeSpanTests.cs @@ -15,26 +15,26 @@ public class TimeSpanTests } [TestMethod] - public void AgoShouldBeInThePast() + public void Ago_ShouldBeInPast_GivenNow() { Assert.IsTrue(_timeSpan.Ago() < DateTime.Now); } [TestMethod] - public void FromNowShouldBeInThePast() + public void FromNow_ShouldBeInFuture_GivenNow() { Assert.IsTrue(_timeSpan.FromNow() > DateTime.Now); } [TestMethod] - public void OneDayAgoShouldBeYesterday() + public void Ago_ShouldBeYesterday_GivenYesterday() { DateTime yesterday = DateTime.Now.AddDays(-1); Assert.AreEqual(yesterday.Date, 1.Days().Ago().Date); } [TestMethod] - public void OneDayFromNowShouldBeTomorrow() + public void FromNow_ShouldBeTomorrow_GivenTomorrow() { DateTime tomorrow = DateTime.Now.AddDays(1); Assert.AreEqual(tomorrow.Date, 1.Days().FromNow().Date); diff --git a/X10D.Tests/src/Time/UInt16Tests.cs b/X10D.Tests/src/Time/UInt16Tests.cs index dc94e92..e9003b3 100644 --- a/X10D.Tests/src/Time/UInt16Tests.cs +++ b/X10D.Tests/src/Time/UInt16Tests.cs @@ -8,19 +8,7 @@ namespace X10D.Tests.Time; public class UInt16Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() - { - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Ticks()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Seconds()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Minutes()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Days()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Hours()); - Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Weeks()); - } - - [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(((ushort)1).Ticks() > TimeSpan.Zero); Assert.IsTrue(((ushort)1).Milliseconds() > TimeSpan.Zero); @@ -30,4 +18,16 @@ public class UInt16Tests Assert.IsTrue(((ushort)1).Hours() > TimeSpan.Zero); Assert.IsTrue(((ushort)1).Weeks() > TimeSpan.Zero); } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() + { + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Ticks()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Seconds()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Minutes()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Days()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Hours()); + Assert.AreEqual(TimeSpan.Zero, ((ushort)0).Weeks()); + } } diff --git a/X10D.Tests/src/Time/UInt32Tests.cs b/X10D.Tests/src/Time/UInt32Tests.cs index a1cd05c..25698aa 100644 --- a/X10D.Tests/src/Time/UInt32Tests.cs +++ b/X10D.Tests/src/Time/UInt32Tests.cs @@ -8,19 +8,7 @@ namespace X10D.Tests.Time; public class UInt32Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() - { - Assert.AreEqual(TimeSpan.Zero, 0U.Ticks()); - Assert.AreEqual(TimeSpan.Zero, 0U.Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, 0U.Seconds()); - Assert.AreEqual(TimeSpan.Zero, 0U.Minutes()); - Assert.AreEqual(TimeSpan.Zero, 0U.Days()); - Assert.AreEqual(TimeSpan.Zero, 0U.Hours()); - Assert.AreEqual(TimeSpan.Zero, 0U.Weeks()); - } - - [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1U.Ticks() > TimeSpan.Zero); Assert.IsTrue(1U.Milliseconds() > TimeSpan.Zero); @@ -30,4 +18,16 @@ public class UInt32Tests Assert.IsTrue(1U.Hours() > TimeSpan.Zero); Assert.IsTrue(1U.Weeks() > TimeSpan.Zero); } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() + { + Assert.AreEqual(TimeSpan.Zero, 0U.Ticks()); + Assert.AreEqual(TimeSpan.Zero, 0U.Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, 0U.Seconds()); + Assert.AreEqual(TimeSpan.Zero, 0U.Minutes()); + Assert.AreEqual(TimeSpan.Zero, 0U.Days()); + Assert.AreEqual(TimeSpan.Zero, 0U.Hours()); + Assert.AreEqual(TimeSpan.Zero, 0U.Weeks()); + } } diff --git a/X10D.Tests/src/Time/UInt64Tests.cs b/X10D.Tests/src/Time/UInt64Tests.cs index 1ff7598..ebfac8b 100644 --- a/X10D.Tests/src/Time/UInt64Tests.cs +++ b/X10D.Tests/src/Time/UInt64Tests.cs @@ -8,19 +8,7 @@ namespace X10D.Tests.Time; public class UInt64Tests { [TestMethod] - public void ZeroShouldBeZeroTimeSpan() - { - Assert.AreEqual(TimeSpan.Zero, 0UL.Ticks()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Milliseconds()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Seconds()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Minutes()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Days()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Hours()); - Assert.AreEqual(TimeSpan.Zero, 0UL.Weeks()); - } - - [TestMethod] - public void OneShouldBePositive() + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBePositive_GivenOne() { Assert.IsTrue(1UL.Ticks() > TimeSpan.Zero); Assert.IsTrue(1UL.Milliseconds() > TimeSpan.Zero); @@ -30,4 +18,16 @@ public class UInt64Tests Assert.IsTrue(1UL.Hours() > TimeSpan.Zero); Assert.IsTrue(1UL.Weeks() > TimeSpan.Zero); } + + [TestMethod] + public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeZero_GivenZero() + { + Assert.AreEqual(TimeSpan.Zero, 0UL.Ticks()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Milliseconds()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Seconds()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Minutes()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Days()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Hours()); + Assert.AreEqual(TimeSpan.Zero, 0UL.Weeks()); + } } diff --git a/X10D/src/BooleanExtensions/BooleanExtensions.cs b/X10D/src/BooleanExtensions/BooleanExtensions.cs deleted file mode 100644 index 8dd706b..0000000 --- a/X10D/src/BooleanExtensions/BooleanExtensions.cs +++ /dev/null @@ -1,36 +0,0 @@ -namespace X10D; - -/// -/// Extension methods for . -/// -public static class BooleanExtensions -{ - /// - /// Returns the current Boolean value as a byte array. - /// - /// The Boolean value. - /// A byte array with length 1. - /// - /// The following example converts the bit patterns of values to arrays. - /// - /// bool[] values = { true, false }; - /// - /// Console.WriteLine("{0,10}{1,16}\n", "Boolean", "Bytes"); - /// foreach (var value in values) - /// { - /// byte[] bytes = value.GetBytes(); - /// Console.WriteLine("{0,10}{1,16}", value, bytes.AsString()); - /// } - /// - /// // The example displays the following output: - /// // Boolean Bytes - /// // - /// // True 01 - /// // False 00 - /// - /// - public static byte[] GetBytes(this bool value) - { - return BitConverter.GetBytes(value); - } -} diff --git a/X10D/src/ByteExtensions/ByteExtensions.cs b/X10D/src/ByteExtensions/ByteExtensions.cs deleted file mode 100644 index 7b0e5eb..0000000 --- a/X10D/src/ByteExtensions/ByteExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace X10D; - -/// -/// Extension methods for . -/// -public static class ByteExtensions -{ - /// - /// Returns the current 8-bit unsigned integer value as an array of bytes. - /// - /// The number to convert. - /// An array of bytes with length 1. - public static byte[] GetBytes(this byte value) - { - return new[] { value }; - } -} diff --git a/X10D/src/DoubleExtensions/DoubleExtensions.cs b/X10D/src/DoubleExtensions/DoubleExtensions.cs deleted file mode 100644 index e2c81f2..0000000 --- a/X10D/src/DoubleExtensions/DoubleExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace X10D; - -/// -/// Extension methods for . -/// -public static class DoubleExtensions -{ - /// - /// Converts the to a []. - /// - /// The number to convert. - /// Returns a []. - public static byte[] GetBytes(this double number) - { - return BitConverter.GetBytes(number); - } -} diff --git a/X10D/src/IO/BooleanExtensions.cs b/X10D/src/IO/BooleanExtensions.cs new file mode 100644 index 0000000..f250c15 --- /dev/null +++ b/X10D/src/IO/BooleanExtensions.cs @@ -0,0 +1,28 @@ +namespace X10D; + +/// +/// Extension methods for . +/// +public static class BooleanExtensions +{ + /// + /// Returns the current boolean value as an array of bytes. + /// + /// The value to convert. + /// An array of bytes with length 1. + public static byte[] GetBytes(this bool value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this bool value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } +} diff --git a/X10D/src/IO/ByteExtensions.cs b/X10D/src/IO/ByteExtensions.cs new file mode 100644 index 0000000..0e6cc0b --- /dev/null +++ b/X10D/src/IO/ByteExtensions.cs @@ -0,0 +1,36 @@ +using X10D.Core; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class ByteExtensions +{ + /// + /// Returns the current 8-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 1. + public static byte[] GetBytes(this byte value) + { + return value.AsArray(); + } + + /// + /// Converts a into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this byte value, Span destination) + { + if (destination.Length < 1) + { + return false; + } + + destination[0] = value; + return true; + } +} diff --git a/X10D/src/IO/DoubleExtensions.cs b/X10D/src/IO/DoubleExtensions.cs new file mode 100644 index 0000000..a7a3c95 --- /dev/null +++ b/X10D/src/IO/DoubleExtensions.cs @@ -0,0 +1,59 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class DoubleExtensions +{ + /// + /// Returns the current double-precision floating-point value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 8. + public static byte[] GetBytes(this double value) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current double-precision floating-point value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 8. + public static byte[] GetBytes(this double value, Endianness endianness) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current double-precision floating-point into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this double value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current double-precision floating-point into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this double value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteDoubleBigEndian(destination, value) + : BinaryPrimitives.TryWriteDoubleLittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/Int16Extensions.cs b/X10D/src/IO/Int16Extensions.cs new file mode 100644 index 0000000..bc5e7be --- /dev/null +++ b/X10D/src/IO/Int16Extensions.cs @@ -0,0 +1,59 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class Int16Extensions +{ + /// + /// Returns the current 16-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 2. + public static byte[] GetBytes(this short value) + { + Span buffer = stackalloc byte[2]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 16-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 2. + public static byte[] GetBytes(this short value, Endianness endianness) + { + Span buffer = stackalloc byte[2]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 16-bit signed integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this short value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 16-bit signed integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this short value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteInt16BigEndian(destination, value) + : BinaryPrimitives.TryWriteInt16LittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/Int32Extensions.cs b/X10D/src/IO/Int32Extensions.cs new file mode 100644 index 0000000..573f67d --- /dev/null +++ b/X10D/src/IO/Int32Extensions.cs @@ -0,0 +1,59 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class Int32Extensions +{ + /// + /// Returns the current 32-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 4. + public static byte[] GetBytes(this int value) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 32-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 4. + public static byte[] GetBytes(this int value, Endianness endianness) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 32-bit signed integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this int value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 32-bit signed integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this int value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteInt32BigEndian(destination, value) + : BinaryPrimitives.TryWriteInt32LittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/Int64Extensions.cs b/X10D/src/IO/Int64Extensions.cs new file mode 100644 index 0000000..4021c1e --- /dev/null +++ b/X10D/src/IO/Int64Extensions.cs @@ -0,0 +1,59 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class Int64Extensions +{ + /// + /// Returns the current 64-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 8. + public static byte[] GetBytes(this long value) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 64-bit signed integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 8. + public static byte[] GetBytes(this long value, Endianness endianness) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 64-bit signed integer a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this long value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 64-bit signed integer a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this long value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteInt64BigEndian(destination, value) + : BinaryPrimitives.TryWriteInt64LittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/SByteExtensions.cs b/X10D/src/IO/SByteExtensions.cs new file mode 100644 index 0000000..73e79d2 --- /dev/null +++ b/X10D/src/IO/SByteExtensions.cs @@ -0,0 +1,37 @@ +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +[CLSCompliant(false)] +public static class SByteExtensions +{ + /// + /// Returns the current 16-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 1. + public static byte[] GetBytes(this sbyte value) + { + Span buffer = stackalloc byte[1]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Converts the current 16-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this sbyte value, Span destination) + { + if (destination.Length < 1) + { + return false; + } + + destination[0] = (byte)value; + return true; + } +} diff --git a/X10D/src/IO/SingleExtensions.cs b/X10D/src/IO/SingleExtensions.cs new file mode 100644 index 0000000..b51a6a6 --- /dev/null +++ b/X10D/src/IO/SingleExtensions.cs @@ -0,0 +1,59 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class SingleExtensions +{ + /// + /// Returns the current single-precision floating-point value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 4. + public static byte[] GetBytes(this float value) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current single-precision floating-point value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 4. + public static byte[] GetBytes(this float value, Endianness endianness) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current single-precision floating-point into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this float value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current single-precision floating-point into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this float value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteSingleBigEndian(destination, value) + : BinaryPrimitives.TryWriteSingleLittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/UInt16Extensions.cs b/X10D/src/IO/UInt16Extensions.cs new file mode 100644 index 0000000..85411f3 --- /dev/null +++ b/X10D/src/IO/UInt16Extensions.cs @@ -0,0 +1,60 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt16Extensions +{ + /// + /// Returns the current 16-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 2. + public static byte[] GetBytes(this ushort value) + { + Span buffer = stackalloc byte[2]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 16-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 2. + public static byte[] GetBytes(this ushort value, Endianness endianness) + { + Span buffer = stackalloc byte[2]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 16-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this ushort value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 16-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this ushort value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteUInt16BigEndian(destination, value) + : BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/UInt32Extensions.cs b/X10D/src/IO/UInt32Extensions.cs new file mode 100644 index 0000000..9bf8040 --- /dev/null +++ b/X10D/src/IO/UInt32Extensions.cs @@ -0,0 +1,60 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt32Extensions +{ + /// + /// Returns the current 32-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 4. + public static byte[] GetBytes(this uint value) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 32-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 4. + public static byte[] GetBytes(this uint value, Endianness endianness) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 32-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this uint value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 32-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this uint value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteUInt32BigEndian(destination, value) + : BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value); + } +} diff --git a/X10D/src/IO/UInt64Extensions.cs b/X10D/src/IO/UInt64Extensions.cs new file mode 100644 index 0000000..1fd29fb --- /dev/null +++ b/X10D/src/IO/UInt64Extensions.cs @@ -0,0 +1,60 @@ +using System.Buffers.Binary; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +[CLSCompliant(false)] +public static class UInt64Extensions +{ + /// + /// Returns the current 64-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// An array of bytes with length 8. + public static byte[] GetBytes(this ulong value) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer); + return buffer.ToArray(); + } + + /// + /// Returns the current 64-bit unsigned integer value as an array of bytes. + /// + /// The number to convert. + /// The endianness with which to write the bytes. + /// An array of bytes with length 8. + public static byte[] GetBytes(this ulong value, Endianness endianness) + { + Span buffer = stackalloc byte[8]; + value.TryWriteBytes(buffer, endianness); + return buffer.ToArray(); + } + + /// + /// Converts the current 64-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this ulong value, Span destination) + { + return BitConverter.TryWriteBytes(destination, value); + } + + /// + /// Converts the current 64-bit unsigned integer into a span of bytes. + /// + /// The value. + /// When this method returns, the bytes representing the converted . + /// The endianness with which to write the bytes. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBytes(this ulong value, Span destination, Endianness endianness) + { + return endianness == Endianness.BigEndian + ? BinaryPrimitives.TryWriteUInt64BigEndian(destination, value) + : BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value); + } +} diff --git a/X10D/src/Int16Extensions/Int16Extensions.cs b/X10D/src/Int16Extensions/Int16Extensions.cs index 371c883..93ef82f 100644 --- a/X10D/src/Int16Extensions/Int16Extensions.cs +++ b/X10D/src/Int16Extensions/Int16Extensions.cs @@ -7,16 +7,6 @@ namespace X10D; /// public static class Int16Extensions { - /// - /// Returns the current 16-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// An array of bytes with length 2. - public static byte[] GetBytes(this short value) - { - return BitConverter.GetBytes(value); - } - /// /// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value. /// diff --git a/X10D/src/Int32Extensions/Int32Extensions.cs b/X10D/src/Int32Extensions/Int32Extensions.cs index 41013b2..61211b3 100644 --- a/X10D/src/Int32Extensions/Int32Extensions.cs +++ b/X10D/src/Int32Extensions/Int32Extensions.cs @@ -70,16 +70,6 @@ public static class Int32Extensions return ((float)value).DegreesToRadians(); } - /// - /// Returns the current 32-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// An array of bytes with length 4. - public static byte[] GetBytes(this int value) - { - return BitConverter.GetBytes(value); - } - /// /// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value. /// diff --git a/X10D/src/Int64Extensions/Int64Extensions.cs b/X10D/src/Int64Extensions/Int64Extensions.cs index 645a71b..8322653 100644 --- a/X10D/src/Int64Extensions/Int64Extensions.cs +++ b/X10D/src/Int64Extensions/Int64Extensions.cs @@ -7,16 +7,6 @@ namespace X10D; /// public static class Int64Extensions { - /// - /// Returns the current 64-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// An array of bytes with length 8. - public static byte[] GetBytes(this long value) - { - return BitConverter.GetBytes(value); - } - /// /// Returns the multiplicative persistence of a specified value. /// diff --git a/X10D/src/SingleExtensions/SingleExtensions.cs b/X10D/src/SingleExtensions/SingleExtensions.cs deleted file mode 100644 index 9a258d1..0000000 --- a/X10D/src/SingleExtensions/SingleExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace X10D; - -/// -/// Extension methods for . -/// -public static class SingleExtensions -{ - /// - /// Converts the to a []. - /// - /// The number to convert. - /// Returns a []. - public static byte[] GetBytes(this float number) - { - return BitConverter.GetBytes(number); - } -} diff --git a/X10D/src/Time/ByteExtensions.cs b/X10D/src/Time/ByteExtensions.cs index 0a50aa2..0e097a2 100644 --- a/X10D/src/Time/ByteExtensions.cs +++ b/X10D/src/Time/ByteExtensions.cs @@ -5,6 +5,44 @@ /// public static class ByteExtensions { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, + /// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800,000. + /// -or- + /// is greater than 253,402,300,799,999. + /// + public static DateTimeOffset FromUnixTimeMilliseconds(this byte value) + { + return DateTimeOffset.FromUnixTimeMilliseconds(value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at + /// 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800. + /// -or- + /// is greater than 253,402,300,799. + /// + public static DateTimeOffset FromUnixTimeSeconds(this byte value) + { + return DateTimeOffset.FromUnixTimeSeconds(value); + } + /// /// Returns a that represents this value as the number of ticks. /// diff --git a/X10D/src/Time/SByteExtensions.cs b/X10D/src/Time/SByteExtensions.cs index e651f20..dd0f2f6 100644 --- a/X10D/src/Time/SByteExtensions.cs +++ b/X10D/src/Time/SByteExtensions.cs @@ -6,6 +6,44 @@ [CLSCompliant(false)] public static class SByteExtensions { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, + /// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800,000. + /// -or- + /// is greater than 253,402,300,799,999. + /// + public static DateTimeOffset FromUnixTimeMilliseconds(this sbyte value) + { + return DateTimeOffset.FromUnixTimeMilliseconds(value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at + /// 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800. + /// -or- + /// is greater than 253,402,300,799. + /// + public static DateTimeOffset FromUnixTimeSeconds(this sbyte value) + { + return DateTimeOffset.FromUnixTimeSeconds(value); + } + /// /// Returns a that represents this value as the number of ticks. ///