1- use std:: ffi:: CStr ;
21use std:: collections:: HashSet ;
2+ use std:: ffi:: CStr ;
33
44use sqlite_wasm_rs:: {
55 sqlite3, sqlite3_close, sqlite3_column_text, sqlite3_finalize, sqlite3_open_v2,
66 sqlite3_prepare_v3, sqlite3_step, SQLITE_OK , SQLITE_OPEN_CREATE , SQLITE_OPEN_READWRITE ,
77 SQLITE_ROW ,
88} ;
9- use wasm_bindgen:: prelude:: wasm_bindgen;
109use uuid:: { Uuid , Version } ;
10+ use wasm_bindgen:: prelude:: wasm_bindgen;
1111
1212#[ wasm_bindgen]
1313extern "C" {
@@ -21,14 +21,7 @@ macro_rules! console_log {
2121
2222unsafe fn query_uuid ( db : * mut sqlite3 , sql_stmt : * const std:: ffi:: c_char ) -> String {
2323 let mut stmt = std:: ptr:: null_mut ( ) ;
24- let rc = sqlite3_prepare_v3 (
25- db,
26- sql_stmt,
27- -1 ,
28- 0 ,
29- & mut stmt,
30- std:: ptr:: null_mut ( ) ,
31- ) ;
24+ let rc = sqlite3_prepare_v3 ( db, sql_stmt, -1 , 0 , & mut stmt, std:: ptr:: null_mut ( ) ) ;
3225 assert_eq ! ( SQLITE_OK , rc, "Failed to prepare statement" ) ;
3326
3427 let mut res = String :: new ( ) ;
@@ -76,7 +69,7 @@ async fn main() {
7669 console_log ! ( "Generating 1000 UUIDv4..." ) ;
7770 let mut v4_results = Vec :: with_capacity ( 1000 ) ;
7871 let sql = c"SELECT uuid();" ;
79-
72+
8073 for _ in 0 ..1000 {
8174 v4_results. push ( query_uuid ( db, sql. as_ptr ( ) ) ) ;
8275 }
@@ -88,8 +81,15 @@ async fn main() {
8881
8982 // Check version & parsing
9083 for ( i, s) in v4_results. iter ( ) . enumerate ( ) {
91- let u = Uuid :: parse_str ( s) . unwrap_or_else ( |e| panic ! ( "Failed to parse UUIDv4 at index {}: {} - {}" , i, s, e) ) ;
92- assert_eq ! ( u. get_version( ) , Some ( Version :: Random ) , "UUID at index {} is not v4: {}" , i, s) ;
84+ let u = Uuid :: parse_str ( s)
85+ . unwrap_or_else ( |e| panic ! ( "Failed to parse UUIDv4 at index {}: {} - {}" , i, s, e) ) ;
86+ assert_eq ! (
87+ u. get_version( ) ,
88+ Some ( Version :: Random ) ,
89+ "UUID at index {} is not v4: {}" ,
90+ i,
91+ s
92+ ) ;
9393 }
9494 console_log ! ( "Version check passed for UUIDv4" ) ;
9595
@@ -102,52 +102,64 @@ async fn main() {
102102 v7_results. push ( query_uuid ( db, sql. as_ptr ( ) ) ) ;
103103 }
104104
105- // Check uniqueness
105+ // Check uniqueness
106106 let set: HashSet < _ > = v7_results. iter ( ) . cloned ( ) . collect ( ) ;
107107 assert_eq ! ( set. len( ) , 1000 , "All UUIDv7 must be unique" ) ;
108108 console_log ! ( "Unique check passed for UUIDv7" ) ;
109109
110110 // Check sorting (Monotonicity)
111111 // Since UUIDv7 is time-ordered, later generations should generally be greater than previous ones.
112112 // However, if generated in the same millisecond, the random component decides order.
113- // Strict monotonicity isn't guaranteed across all implementations unless they share state,
113+ // Strict monotonicity isn't guaranteed across all implementations unless they share state,
114114 // but uuid::Uuid::now_v7() generally attempts internally to be monotonic if called rapidly.
115115 // Wait, the Rust `uuid` crate documentation says `now_v7` guarantees monotonicity for same-process calls if the feature is enabled?
116116 // Actually `Uuid::now_v7()` uses `context::Context` thread-locally if available, or just random.
117117 // The implementation in extensions/uuid7 uses `Uuid::now_v7()`.
118118 // Let's check if the list is sorted.
119-
119+
120120 let mut sorted_v7 = v7_results. clone ( ) ;
121121 sorted_v7. sort ( ) ;
122-
122+
123123 // This assertion might be flaky if the clock goes backwards or multiple threads (not in WASM mostly).
124124 // But for a single thread tight loop, it should be strictly monotonic or at least non-decreasing.
125125 // Since we assert uniqueness, non-decreasing + unique = strictly increasing.
126-
126+
127127 if v7_results != sorted_v7 {
128- console_log ! ( "Warning: UUIDv7 results were not strictly monotonic." ) ;
129- // Find first violation
130- for i in 0 ..999 {
131- if v7_results[ i] > v7_results[ i+1 ] {
132- console_log ! ( "Violation at index {}: {} > {}" , i, v7_results[ i] , v7_results[ i+1 ] ) ;
133- // Allow slight harmless reordering if any, but UUIDv7 SHOULD be monotonic.
134- // The user asked to "check they are unique and sorted".
135- // So I will assert it.
136- }
137- }
138- panic ! ( "UUIDv7 results are not sorted!" ) ;
128+ console_log ! ( "Warning: UUIDv7 results were not strictly monotonic." ) ;
129+ // Find first violation
130+ for i in 0 ..999 {
131+ if v7_results[ i] > v7_results[ i + 1 ] {
132+ console_log ! (
133+ "Violation at index {}: {} > {}" ,
134+ i,
135+ v7_results[ i] ,
136+ v7_results[ i + 1 ]
137+ ) ;
138+ // Allow slight harmless reordering if any, but UUIDv7 SHOULD be monotonic.
139+ // The user asked to "check they are unique and sorted".
140+ // So I will assert it.
141+ }
142+ }
143+ panic ! ( "UUIDv7 results are not sorted!" ) ;
139144 }
140145 console_log ! ( "Sort order check passed for UUIDv7" ) ;
141146
142147 // Check version
143148 for ( i, s) in v7_results. iter ( ) . enumerate ( ) {
144- let u = Uuid :: parse_str ( s) . unwrap_or_else ( |e| panic ! ( "Failed to parse UUIDv7 at index {}: {} - {}" , i, s, e) ) ;
145- assert_eq ! ( u. get_version( ) , Some ( Version :: SortRand ) , "UUID at index {} is not v7: {}" , i, s) ;
149+ let u = Uuid :: parse_str ( s)
150+ . unwrap_or_else ( |e| panic ! ( "Failed to parse UUIDv7 at index {}: {} - {}" , i, s, e) ) ;
151+ assert_eq ! (
152+ u. get_version( ) ,
153+ Some ( Version :: SortRand ) ,
154+ "UUID at index {} is not v7: {}" ,
155+ i,
156+ s
157+ ) ;
146158 }
147159 console_log ! ( "Version check passed for UUIDv7" ) ;
148-
160+
149161 sqlite3_close ( db) ;
150162 }
151-
163+
152164 console_log ! ( "All tests passed!" ) ;
153165}
0 commit comments