1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:18:46 +00:00

Move IO related methods to IO namespaces (#7)

Also renames more tests to be more meaningful
This commit is contained in:
Oliver Booth 2022-04-25 17:09:49 +01:00
parent 907687a883
commit c4abef7be6
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
50 changed files with 1573 additions and 289 deletions

View File

@ -7,21 +7,20 @@ namespace X10D.Tests.Collections;
public class EnumerableTests
{
[TestMethod]
public void EnumerableShuffledShouldBeDifferentOrder()
{
var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
var shuffled = new List<int>(list);
CollectionAssert.AreEqual(list, shuffled);
shuffled = new List<int>(list.Shuffled());
CollectionAssert.AreNotEqual(list, shuffled);
}
[TestMethod]
public void NullShuffledShouldThrow()
public void Shuffled_ShouldThrow_GivenNull()
{
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)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);
}
}

View File

@ -39,6 +39,33 @@ public class ListTests
CollectionAssert.AreEqual(comparison, args[1..]);
}
[TestMethod]
public void Fill_ShouldThrow_GivenExceededCount()
{
int[] array = Array.Empty<int>();
var list = new List<int>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.Fill(0, 0, 1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.Fill(0, 0, 1));
}
[TestMethod]
public void Fill_ShouldThrow_GivenNegativeCount()
{
int[] array = Array.Empty<int>();
var list = new List<int>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.Fill(0, 0, -1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.Fill(0, 0, -1));
}
[TestMethod]
public void Fill_ShouldThrow_GivenNegativeStartIndex()
{
int[] array = Array.Empty<int>();
var list = new List<int>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.Fill(0, -1, 0));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => list.Fill(0, -1, 0));
}
[TestMethod]
public void Fill_ShouldThrow_GivenNull()
{
@ -46,6 +73,8 @@ public class ListTests
List<int>? list = null;
Assert.ThrowsException<ArgumentNullException>(() => array!.Fill(0));
Assert.ThrowsException<ArgumentNullException>(() => list!.Fill(0));
Assert.ThrowsException<ArgumentNullException>(() => array!.Fill(0, 0, 0));
Assert.ThrowsException<ArgumentNullException>(() => list!.Fill(0, 0, 0));
}
[TestMethod]

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.IO;
namespace X10D.Tests.Core;

View File

@ -62,17 +62,6 @@ public class DoubleTests
Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.GetBytes" />.
/// </summary>
[TestMethod]
public void GetBytes()
{
CollectionAssert.AreEqual(
new byte[] {0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40},
Math.PI.GetBytes());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.IsEven" />.
/// </summary>

View File

@ -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<object?> 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);
}
}

View File

@ -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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.IsFalse(value.TryWriteBytes(buffer));
}
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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 OneShouldBePositive()
public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero()
{
Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((short)0).FromUnixTimeSeconds());
}
[TestMethod]
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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -1,36 +0,0 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="bool" />.
/// </summary>
public static class BooleanExtensions
{
/// <summary>
/// Returns the current Boolean value as a byte array.
/// </summary>
/// <param name="value">The Boolean value.</param>
/// <returns>A byte array with length 1.</returns>
/// <example>
/// The following example converts the bit patterns of <see cref="bool" /> values to <see cref="byte" /> arrays.
/// <code lang="csharp">
/// 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
/// </code>
/// </example>
public static byte[] GetBytes(this bool value)
{
return BitConverter.GetBytes(value);
}
}

View File

@ -1,17 +0,0 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="byte" />.
/// </summary>
public static class ByteExtensions
{
/// <summary>
/// Returns the current 8-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 1.</returns>
public static byte[] GetBytes(this byte value)
{
return new[] { value };
}
}

View File

@ -1,17 +0,0 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="double" />.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Converts the <see cref="double" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
public static byte[] GetBytes(this double number)
{
return BitConverter.GetBytes(number);
}
}

View File

@ -0,0 +1,28 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="bool" />.
/// </summary>
public static class BooleanExtensions
{
/// <summary>
/// Returns the current boolean value as an array of bytes.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>An array of bytes with length 1.</returns>
public static byte[] GetBytes(this bool value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Converts a <see cref="bool" /> into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="bool" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="bool" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this bool value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
}

View File

@ -0,0 +1,36 @@
using X10D.Core;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="byte" />.
/// </summary>
public static class ByteExtensions
{
/// <summary>
/// Returns the current 8-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 1.</returns>
public static byte[] GetBytes(this byte value)
{
return value.AsArray();
}
/// <summary>
/// Converts a <see cref="byte" /> into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="byte" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="byte" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this byte value, Span<byte> destination)
{
if (destination.Length < 1)
{
return false;
}
destination[0] = value;
return true;
}
}

View File

@ -0,0 +1,59 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="double" />.
/// </summary>
public static class DoubleExtensions
{
/// <summary>
/// Returns the current double-precision floating-point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this double value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current double-precision floating-point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this double value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current double-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="double" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="double" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this double value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current double-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="double" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="double" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this double value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteDoubleBigEndian(destination, value)
: BinaryPrimitives.TryWriteDoubleLittleEndian(destination, value);
}
}

View File

@ -0,0 +1,59 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="short" />.
/// </summary>
public static class Int16Extensions
{
/// <summary>
/// Returns the current 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(this short value)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(this short value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 16-bit signed integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="short" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this short value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 16-bit signed integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="short" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this short value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt16BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt16LittleEndian(destination, value);
}
}

View File

@ -0,0 +1,59 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="int" />.
/// </summary>
public static class Int32Extensions
{
/// <summary>
/// Returns the current 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this int value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this int value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 32-bit signed integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="int" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this int value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 32-bit signed integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="int" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this int value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt32BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt32LittleEndian(destination, value);
}
}

View File

@ -0,0 +1,59 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="long" />.
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Returns the current 64-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this long value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 64-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this long value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 64-bit signed integer a span of bytes.
/// </summary>
/// <param name="value">The <see cref="long" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="long" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this long value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 64-bit signed integer a span of bytes.
/// </summary>
/// <param name="value">The <see cref="long" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="long" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this long value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt64BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt64LittleEndian(destination, value);
}
}

View File

@ -0,0 +1,37 @@
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="sbyte" />.
/// </summary>
[CLSCompliant(false)]
public static class SByteExtensions
{
/// <summary>
/// Returns the current 16-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 1.</returns>
public static byte[] GetBytes(this sbyte value)
{
Span<byte> buffer = stackalloc byte[1];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 16-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="sbyte" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="sbyte" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this sbyte value, Span<byte> destination)
{
if (destination.Length < 1)
{
return false;
}
destination[0] = (byte)value;
return true;
}
}

View File

@ -0,0 +1,59 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="float" />.
/// </summary>
public static class SingleExtensions
{
/// <summary>
/// Returns the current single-precision floating-point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this float value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current single-precision floating-point value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this float value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current single-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="float" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this float value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current single-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="float" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this float value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteSingleBigEndian(destination, value)
: BinaryPrimitives.TryWriteSingleLittleEndian(destination, value);
}
}

View File

@ -0,0 +1,60 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="ushort" />.
/// </summary>
[CLSCompliant(false)]
public static class UInt16Extensions
{
/// <summary>
/// Returns the current 16-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(this ushort value)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 16-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(this ushort value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 16-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ushort" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ushort value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 16-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ushort" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ushort value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt16BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value);
}
}

View File

@ -0,0 +1,60 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="uint" />.
/// </summary>
[CLSCompliant(false)]
public static class UInt32Extensions
{
/// <summary>
/// Returns the current 32-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this uint value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 32-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this uint value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 32-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="uint" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this uint value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 32-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="uint" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this uint value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt32BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value);
}
}

View File

@ -0,0 +1,60 @@
using System.Buffers.Binary;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="ulong" />.
/// </summary>
[CLSCompliant(false)]
public static class UInt64Extensions
{
/// <summary>
/// Returns the current 64-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this ulong value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer);
return buffer.ToArray();
}
/// <summary>
/// Returns the current 64-bit unsigned integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this ulong value, Endianness endianness)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer, endianness);
return buffer.ToArray();
}
/// <summary>
/// Converts the current 64-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ulong" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ulong value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current 64-bit unsigned integer into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ulong" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ulong value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt64BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value);
}
}

View File

@ -7,16 +7,6 @@ namespace X10D;
/// </summary>
public static class Int16Extensions
{
/// <summary>
/// Returns the current 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
public static byte[] GetBytes(this short value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value.
/// </summary>

View File

@ -70,16 +70,6 @@ public static class Int32Extensions
return ((float)value).DegreesToRadians();
}
/// <summary>
/// Returns the current 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
public static byte[] GetBytes(this int value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary>

View File

@ -7,16 +7,6 @@ namespace X10D;
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Returns the current 64-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
public static byte[] GetBytes(this long value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Returns the multiplicative persistence of a specified value.
/// </summary>

View File

@ -1,17 +0,0 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="float" />.
/// </summary>
public static class SingleExtensions
{
/// <summary>
/// Converts the <see cref="float" /> to a <see cref="byte" />[].
/// </summary>
/// <param name="number">The number to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
public static byte[] GetBytes(this float number)
{
return BitConverter.GetBytes(number);
}
}

View File

@ -5,6 +5,44 @@
/// </summary>
public static class ByteExtensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800,000.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
public static DateTimeOffset FromUnixTimeMilliseconds(this byte value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
public static DateTimeOffset FromUnixTimeSeconds(this byte value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns a <see cref="TimeSpan" /> that represents this value as the number of ticks.
/// </summary>

View File

@ -6,6 +6,44 @@
[CLSCompliant(false)]
public static class SByteExtensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800,000.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
public static DateTimeOffset FromUnixTimeMilliseconds(this sbyte value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
public static DateTimeOffset FromUnixTimeSeconds(this sbyte value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns a <see cref="TimeSpan" /> that represents this value as the number of ticks.
/// </summary>