|
1 |
| -open System |
2 |
| -open System.Diagnostics |
3 |
| -open BenchmarkDotNet.Attributes |
| 1 | +module UUIDv7Tests |
| 2 | + |
| 3 | +open System |
| 4 | +open Expecto |
4 | 5 |
|
5 | 6 | open UUIDv7
|
6 |
| -open BenchmarkDotNet.Running |
7 | 7 |
|
8 |
| -[<MemoryDiagnoser>] |
9 |
| -type Benchmark1() = |
| 8 | +let checkInvariants (uuid: UUIDv7) = |
| 9 | + let guid = uuid.ToGuid() |
| 10 | + let uuidString = uuid.ToString() |
| 11 | + let guidString = guid.ToString("D") |
| 12 | + |
| 13 | + "UUIDv7 should be valid Guid" |
| 14 | + |> Expect.equal uuidString guidString |
| 15 | + |
| 16 | + "UUIDv7 should have valid Variant" |
| 17 | + |> Expect.equal uuid.Variant 2UL |
| 18 | + |
| 19 | + "UUIDv7 should have valid Version" |
| 20 | + |> Expect.equal uuid.Version 7UL |
| 21 | + |
| 22 | + "UUID7 Random bits are non-zero" |
| 23 | + |> Expect.isGreaterThan (uuid.Low &&& 0xFFFF_FFFF_FFFFUL) 0UL |
10 | 24 |
|
11 |
| - [<Benchmark>] |
12 |
| - member __.Test1k() = |
13 |
| - let mutable uuid = UUIDv7() |
14 | 25 |
|
15 |
| - for i in 0..1_000 do |
16 |
| - uuid <- UUIDv7.New() |
| 26 | +[<Tests>] |
| 27 | +let tests = |
| 28 | + testList |
| 29 | + "UUIDv7" |
| 30 | + [ |
| 31 | + test "UUIDv7s are generated in order" { |
| 32 | + let rng = Random(123) |
17 | 33 |
|
18 |
| - uuid |
| 34 | + for i in 1..100 do |
| 35 | + let count = rng.Next(2, 10_000) |
| 36 | + let uuids = Array.init count (fun _ -> UUIDv7.New()) |
| 37 | + let sorted = uuids |> Array.sort |
| 38 | + Expect.equal sorted uuids "UUIDv7 should be generated in order" |
| 39 | + for i in 0..count-2 do |
| 40 | + Expect.isLessThan uuids.[i] uuids.[i+1] "UUIDv7 should be generated in order" |
19 | 41 |
|
20 |
| - [<Benchmark>] |
21 |
| - member __.Test1M() = |
22 |
| - let mutable uuid = UUIDv7.New() |
| 42 | + for uuid in uuids do |
| 43 | + checkInvariants uuid |
| 44 | + } |
23 | 45 |
|
24 |
| - for i in 0..1_000_000 do |
25 |
| - uuid <- UUIDv7.New() |
| 46 | + test "UUIDv7 contains unix seconds in first bits" { |
| 47 | + let rng = Random(123) |
26 | 48 |
|
27 |
| - uuid |
| 49 | + for i in 1..100 do |
| 50 | + let uuid = UUIDv7.New() |
| 51 | + let unixTime = uuid.ToUnixTimeSeconds() |
| 52 | + let now = DateTimeOffset.UtcNow.ToUnixTimeSeconds() |
28 | 53 |
|
29 |
| - [<Benchmark>] |
30 |
| - member __.Test10M() = |
31 |
| - let mutable uuid = UUIDv7.New() |
| 54 | + Expect.isLessThanOrEqual |
| 55 | + (abs (unixTime - now)) |
| 56 | + 1L |
| 57 | + "UUIDv7 should contain unix seconds in first bits" |
32 | 58 |
|
33 |
| - for i in 0..10_000_000 do |
34 |
| - uuid <- UUIDv7.New() |
| 59 | + Threading.Thread.Sleep(rng.Next(1, 100)) |
| 60 | + checkInvariants uuid |
| 61 | + } |
35 | 62 |
|
36 |
| - uuid |
| 63 | + test "UUID7 HighBits are always equal or increasing" { |
| 64 | + let highBits = UUIDv7Helpers.HighBits() |
37 | 65 |
|
38 |
| - [<Benchmark>] |
39 |
| - member __.Test1kToString() = |
40 |
| - let mutable s = "" |
| 66 | + for i in 1..10_000_000 do |
| 67 | + let highBits0 = highBits.Next() |
| 68 | + let highBits1 = highBits.Next() |
| 69 | + let highBits2 = highBits.Next() |
| 70 | + let highBits3 = highBits.Next() |
| 71 | + let highBits4 = highBits.Next() |
41 | 72 |
|
42 |
| - for i in 0..1_000 do |
43 |
| - s <- UUIDv7.New().ToString() |
| 73 | + Expect.isLessThanOrEqual highBits0 highBits1 "UUID7 HighBits is always equal or increasing 1" |
| 74 | + Expect.isLessThanOrEqual highBits1 highBits2 "UUID7 HighBits is always equal or increasing 2" |
| 75 | + Expect.isLessThanOrEqual highBits2 highBits3 "UUID7 HighBits is always equal or increasing 3" |
| 76 | + Expect.isLessThanOrEqual highBits3 highBits4 "UUID7 HighBits is always equal or increasing 4" |
| 77 | + } |
44 | 78 |
|
45 |
| - s |
| 79 | + test "UUID7 LowBits are always increasing" { |
| 80 | + let highBits = UUIDv7Helpers.HighBits() |
| 81 | + let lowBits = UUIDv7Helpers.LowBits() |
| 82 | + let mutable highBits0 = highBits.Next() |
46 | 83 |
|
47 |
| - [<Benchmark>] |
48 |
| - member __.Test1kGuid() = |
49 |
| - let mutable s = Guid.Empty |
| 84 | + for i in 1..10_000_000 do |
| 85 | + let lowBits0 = lowBits.TryNext(highBits0) |
| 86 | + let lowBits1 = lowBits.TryNext(highBits0) |
50 | 87 |
|
51 |
| - for i in 0..1_000 do |
52 |
| - s <- Guid.NewGuid() |
| 88 | + match lowBits0, lowBits1 with |
| 89 | + | ValueSome lowBits0, ValueSome lowBits1 -> |
| 90 | + Expect.isLessThan lowBits0 lowBits1 "UUID7 LowBits is always increasing" |
| 91 | + | _ -> highBits0 <- highBits.Next() |
| 92 | + } |
| 93 | + ] |
53 | 94 |
|
54 |
| - s |
55 | 95 |
|
56 | 96 |
|
57 | 97 | [<EntryPoint>]
|
58 |
| -let main _ = |
59 |
| - let _ = BenchmarkRunner.Run<Benchmark1>() |
60 |
| - 0 |
| 98 | +let main args = runTestsInAssemblyWithCLIArgs [] args |
0 commit comments