From fcf19a59b936d4129c1f6674570853f12c1639cf Mon Sep 17 00:00:00 2001 From: "Z. Charles Dziura" Date: Fri, 24 Jun 2022 16:53:25 -0400 Subject: [PATCH] Add test for ulid generator Compares against the `ulid-rs` crate. --- Cargo.lock | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 18 +++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 98af7b1..e6358b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,21 @@ dependencies = [ "getrandom", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + [[package]] name = "proc-macro2" version = "1.0.40" @@ -84,6 +99,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + [[package]] name = "syn" version = "1.0.98" @@ -95,11 +140,32 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "time" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217" +dependencies = [ + "libc", + "num_threads", +] + +[[package]] +name = "ulid" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3be932d774bfad49722da2c4915ac7cc77b77dd223890739a0240de2b2a15957" +dependencies = [ + "rand", + "time", +] + [[package]] name = "ulid-bin" version = "0.1.0" dependencies = [ "nanorand", + "ulid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d5e63ba..a7bcb2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ license = "GPL-3.0-or-later" [dependencies] nanorand = { version = "0.7", features = ["getrandom", "std", "chacha"] } +[dev-dependencies] +ulid = "0.6" + [[bin]] name = "ulid" path = "src/main.rs" diff --git a/src/main.rs b/src/main.rs index c0b596b..d1e086a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,3 +59,21 @@ fn format_id(id: u128) -> String { formatted_id } + +#[cfg(test)] +mod tests { + use ulid::Ulid; + + use super::combine_bits; + + const TIMESTAMP: u128 = 0x1F3FFFFFC17; // One millisecond less than the max unix timestamp. + const RANDOM_BITS: u128 = 0x995ca2c6bcf9f1807ffe2b18adb6b77d; // Chosen by fair die roll. Guaranteed to be random. + + #[test] + fn test_generates_same_ulid_as_ulid_crate_given_same_inputs() { + let our_ulid = combine_bits(TIMESTAMP, RANDOM_BITS); + let external_ulid: u128 = Ulid::from_parts(TIMESTAMP as u64, RANDOM_BITS).into(); + + assert_eq!(our_ulid, external_ulid); + } +}