diff --git a/src/Ulid.Tests/Ulid.New.Tests.cs b/src/Ulid.Tests/Ulid.New.Tests.cs index 2bd9fa1..297bb69 100644 --- a/src/Ulid.Tests/Ulid.New.Tests.cs +++ b/src/Ulid.Tests/Ulid.New.Tests.cs @@ -297,6 +297,43 @@ public void New_MonotonicRandom_ShouldOverflowToTimestamp( Assert.Equal(new byte[10], ulid2.Random.ToArray()); } + [Fact] + public void New_WithLongerBytes_ShouldUseFirst16Bytes() + { + // Arrange + var bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; + + // Act + var ulid = Ulid.New(bytes); + + // Assert + Assert.Equal(bytes[..16], ulid.ToByteArray()); + } + + [Theory] + [InlineData(0)] + [InlineData(5)] + [InlineData(15)] + public void New_WithTooShortBytes_ShouldThrowArgumentException(int length) + { + // Arrange + var bytes = new byte[length]; + + // Act & Assert + Assert.Throws("bytes", () => Ulid.New(bytes)); + } + + [Fact] + public void DefaultGenerationOptions_SetNull_ShouldThrowArgumentNullException() + { + // Arrange + var original = Ulid.DefaultGenerationOptions; + + // Act & Assert + Assert.Throws("value", () => Ulid.DefaultGenerationOptions = null!); + Assert.Same(original, Ulid.DefaultGenerationOptions); + } + [Fact] public void New_Monotonic_ShouldRemainUniqueWhenGeneratedConcurrently() { diff --git a/src/Ulid.Tests/Ulid.String.Tests.cs b/src/Ulid.Tests/Ulid.String.Tests.cs index e75d8b6..1210fdd 100644 --- a/src/Ulid.Tests/Ulid.String.Tests.cs +++ b/src/Ulid.Tests/Ulid.String.Tests.cs @@ -19,6 +19,20 @@ public void ToString_ShouldReturnExpectedString() Assert.Equal(_goodUlidString, result); } + [Fact] + public void Parse_NullString_ShouldThrowArgumentNullException() + { + Assert.Throws("s", () => Ulid.Parse((string)null!)); + Assert.Throws(() => (Ulid)(string)null!); + } + + [Fact] + public void TryParse_NullString_ShouldReturnFalse() + { + Assert.False(Ulid.TryParse((string?)null, null, out var ulid)); + Assert.Equal(default, ulid); + } + [Fact] public void Parse_ValidString_ShouldReturnUlid() { diff --git a/src/Ulid/Ulid.New.cs b/src/Ulid/Ulid.New.cs index 29bab46..445476d 100644 --- a/src/Ulid/Ulid.New.cs +++ b/src/Ulid/Ulid.New.cs @@ -16,7 +16,12 @@ public readonly partial struct Ulid /// incremented scenarios during the generation of the ULID. Modifying this property /// affects the global default behavior for ULID generation across the application. /// - public static GenerationOptions DefaultGenerationOptions { get; set; } = new(); + /// Thrown when setting the value to null. + public static GenerationOptions DefaultGenerationOptions + { + get; + set => field = value ?? throw new ArgumentNullException(nameof(value), "The default generation options cannot be null."); + } = new(); // Constant for Unix Epoch (1970-01-01 UTC) in Ticks private const long _unixEpochTicks = 621355968000000000; @@ -24,11 +29,22 @@ public readonly partial struct Ulid /// /// Initializes a new instance of the struct using the specified byte array. /// - /// The byte array to initialize the with. + /// + /// The bytes to initialize the with, in the same order as returns them.
+ /// Must be at least 16 bytes long. If it is longer, only the first 16 bytes are used. + /// /// Given bytes as an instance. + /// Thrown if is shorter than 16 bytes. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Ulid New(ReadOnlySpan bytes) - => MemoryMarshal.Read(bytes); + { + if (bytes.Length < _ulidSize) + { + ThrowBytesTooShort(); + } + + return MemoryMarshal.Read(bytes); + } /// /// Creates a new with the current timestamp. @@ -133,6 +149,11 @@ ref MemoryMarshal.GetReference(random), return ulid; } + [DoesNotReturn] + [SuppressMessage("ReSharper", "NotResolvedInText")] + private static void ThrowBytesTooShort() + => throw new ArgumentException($"The bytes must be at least {_ulidSize} bytes long.", "bytes"); + [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] [SuppressMessage("ReSharper", "NotResolvedInText")] diff --git a/src/Ulid/Ulid.String.cs b/src/Ulid/Ulid.String.cs index 1a0d00c..9e573f0 100644 --- a/src/Ulid/Ulid.String.cs +++ b/src/Ulid/Ulid.String.cs @@ -260,10 +260,24 @@ private static void ThrowInvalidFormat() /// Ignored. The ULID is always formatted in its canonical Crockford's Base32 format. /// A new instance parsed from the specified string. /// + /// Thrown if is null. /// Thrown if the input string is not a valid ULID string representation. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Ulid Parse(string s, IFormatProvider? provider = null) - => Parse(s.AsSpan(), provider); + { + if (s is null) + { + ThrowNullString(); + } + + return Parse(s.AsSpan(), provider); + } + + [DoesNotReturn] + [MethodImpl(MethodImplOptions.NoInlining)] + [SuppressMessage("ReSharper", "NotResolvedInText")] + private static void ThrowNullString() + => throw new ArgumentNullException("s"); /// /// Attempts to parse a string representation of a ULID into a instance. @@ -414,6 +428,7 @@ private void Fill(Span span, T[] map) where T : unmanaged /// /// The ULID string representation. See . /// The parsed . + /// Thrown if is null. /// Thrown if is not a valid ULID string representation. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Ulid(string str) => Parse(str);