@@ -130,9 +130,8 @@ public void RFC9562_V6_Uniqueness() {
130130
131131 [ Fact ( DisplayName = "RFC9562 Section 5.7 - V7 contains Unix Epoch timestamp" ) ]
132132 public void RFC9562_V7_ContainsTimestamp ( ) {
133- // Note: V7 only stores 48 bits for timestamp. When using FileTimeUtc, this limits the range.
134- // Use a timestamp that fits within 48-bit FileTime range for this test
135- var inputTime = DateTime . FromFileTimeUtc ( 1645557742000 ) ; // Known good value from RFC test vector
133+ // V7 uses Unix epoch milliseconds (RFC9562 Section 5.7)
134+ var inputTime = DateTime . UtcNow ;
136135 var uuid = V7 . Generate ( inputTime ) ;
137136
138137 Assert . Equal ( DaanV2 . UUID . Version . V7 , uuid . Version ) ;
@@ -141,34 +140,39 @@ public void RFC9562_V7_ContainsTimestamp() {
141140 // Extract timestamp and verify round-trip accuracy
142141 var extractedTime = V7 . Extract ( uuid ) ;
143142
144- // V7 timestamps should round-trip correctly
145- Assert . Equal ( inputTime , extractedTime ) ;
143+ // V7 only stores milliseconds, so allow up to 1ms difference
144+ var timeDiff = Math . Abs ( ( extractedTime - inputTime ) . TotalMilliseconds ) ;
145+ Assert . True ( timeDiff < 2 ,
146+ $ "Time difference { timeDiff } ms is too large. Input: { inputTime } , Extracted: { extractedTime } ") ;
146147 }
147148
148149 [ Fact ( DisplayName = "RFC9562 Section 5.7 - V7 UUIDs are sortable by time" ) ]
149150 public void RFC9562_V7_Sortable ( ) {
150- var uuids = new List < UUID > ( ) ;
151+ var uuids = new List < ( UUID uuid , DateTime time ) > ( ) ;
151152
152- for ( int i = 0 ; i < 100 ; i ++ ) {
153- uuids . Add ( V7 . Generate ( ) ) ;
154- // Small delay to ensure time progression
155- if ( i % 10 == 0 ) {
156- Thread . Sleep ( 1 ) ;
157- }
153+ // Generate UUIDs with known time gaps
154+ for ( int i = 0 ; i < 20 ; i ++ ) {
155+ var time = DateTime . UtcNow ;
156+ uuids . Add ( ( V7 . Generate ( time ) , time ) ) ;
157+ Thread . Sleep ( 5 ) ; // 5ms delay to ensure time progression
158158 }
159159
160- // V7 UUIDs should be in ascending order when generated sequentially
161- // Check that most UUIDs maintain order
162- int inOrder = 0 ;
163- for ( int i = 0 ; i < uuids . Count - 1 ; i ++ ) {
164- if ( string . Compare ( uuids [ i ] . ToString ( ) , uuids [ i + 1 ] . ToString ( ) , StringComparison . Ordinal ) <= 0 ) {
165- inOrder ++ ;
166- }
167- }
160+ // Verify that UUIDs generated at different times maintain time order
161+ // Group by millisecond and verify cross-group ordering
162+ var groups = uuids . GroupBy ( x => x . time . Ticks / TimeSpan . TicksPerMillisecond ) . ToList ( ) ;
168163
169- // At least 90% should be in order
170- Assert . True ( inOrder >= ( uuids . Count - 1 ) * 0.9 ,
171- $ "Expected at least 90% in order, got { inOrder } /{ uuids . Count - 1 } ") ;
164+ // With 5ms delays, we should have multiple distinct timestamps
165+ Assert . True ( groups . Count >= 10 , $ "Expected at least 10 distinct timestamps, got { groups . Count } ") ;
166+
167+ // Compare first UUID from each group - they should be in time order
168+ for ( int i = 0 ; i < groups . Count - 1 ; i ++ ) {
169+ var earlier = groups [ i ] . First ( ) . uuid ;
170+ var later = groups [ i + 1 ] . First ( ) . uuid ;
171+
172+ // UUIDs from earlier time should compare less than UUIDs from later time
173+ Assert . True ( string . Compare ( earlier . ToString ( ) , later . ToString ( ) , StringComparison . Ordinal ) < 0 ,
174+ $ "UUID from earlier time should be less than UUID from later time") ;
175+ }
172176 }
173177
174178 [ Fact ( DisplayName = "RFC9562 Section 5.7 - V7 uniqueness" ) ]
0 commit comments