Skip to content

Commit ff31153

Browse files
authored
Adding stream handling (#9)
1 parent 1eae667 commit ff31153

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

DaanV2.UUID.Net.sln

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "API", "API", "{898452FA-EE0
1313
EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{D323591A-620B-4526-9524-759A264CC403}"
1515
EndProject
16-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Debugger", "Debugger", "{F2496212-7EE0-4494-A739-F4D824FCC4A1}"
17-
EndProject
1816
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DaanV2.UUID.Net", "Library\DaanV2.UUID.Net.csproj", "{6D0BED8D-E586-46CA-9CB0-DC67A0FF3DF1}"
1917
EndProject
2018
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Units Tests", "Tests\Units Tests.csproj", "{A03DA89D-313B-406A-8116-479C5AA82D91}"

Library/Static Classes/V4/V4 - Generate.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public static UUID Generate(Random rnd) {
1616
return Generate(bytes);
1717
}
1818

19+
/// <inheritdoc cref="Generate(UInt64, UInt64)"/>
20+
public static UUID Generate(Stream stream) {
21+
Span<Byte> bytes = stackalloc Byte[Format.UUID_BYTE_LENGTH];
22+
stream.Read(bytes);
23+
24+
return Generate(bytes);
25+
}
26+
1927
/// <inheritdoc cref="Generate(UInt64, UInt64)"/>
2028
public static UUID Generate(ReadOnlySpan<Byte> bytes) {
2129
var data = Vector128.Create(bytes);
@@ -28,6 +36,7 @@ public static UUID Generate(ReadOnlySpan<Byte> bytes) {
2836
/// <param name="e1">The higher set of data</param>
2937
/// <param name="bytes">The byte to use to fill the <see cref="UUID"/> expects it to be atleast 16 bytes long</param>
3038
/// <param name="rnd">The randomizer to use to create <see cref="UUID"/></param>
39+
/// <param name="stream">The stream to read from, will read <see cref="Format.UUID_BYTE_LENGTH"/> bytes</param>
3140
/// <returns>A new <see cref="UUID"/></returns>
3241
public static UUID Generate(UInt64 e0, UInt64 e1) {
3342
Vector128<Byte> data = Vector128.Create(e0, e1).AsByte();

Tests/Generation/V4Tests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ public void TestUUIDBatchUnique(Int32 Amount) {
8282
}
8383
}
8484
}
85+
86+
[Fact(DisplayName = "Stream handling should be the same as the array")]
87+
public void TestUUIDStream() {
88+
Byte[] data = new Byte[32];
89+
Random.Shared.NextBytes(data);
90+
91+
UUID expected = DaanV2.UUID.V4.Generate(data);
92+
93+
using (var stream = new MemoryStream(data)) {
94+
UUID actual = DaanV2.UUID.V4.Generate(stream);
95+
96+
Assert.Equal(expected, actual);
97+
}
98+
}
8599
}

0 commit comments

Comments
 (0)