Add test for ulid generator

Compares against the `ulid-rs` crate.
This commit is contained in:
Z. Charles Dziura 2022-06-24 16:53:25 -04:00
parent f05691a02c
commit fcf19a59b9
No known key found for this signature in database
GPG key ID: AEE457C6BB04BF6D
3 changed files with 87 additions and 0 deletions

66
Cargo.lock generated
View file

@ -66,6 +66,21 @@ dependencies = [
"getrandom", "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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.40" version = "1.0.40"
@ -84,6 +99,36 @@ dependencies = [
"proc-macro2", "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]] [[package]]
name = "syn" name = "syn"
version = "1.0.98" version = "1.0.98"
@ -95,11 +140,32 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "ulid-bin" name = "ulid-bin"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"nanorand", "nanorand",
"ulid",
] ]
[[package]] [[package]]

View file

@ -9,6 +9,9 @@ license = "GPL-3.0-or-later"
[dependencies] [dependencies]
nanorand = { version = "0.7", features = ["getrandom", "std", "chacha"] } nanorand = { version = "0.7", features = ["getrandom", "std", "chacha"] }
[dev-dependencies]
ulid = "0.6"
[[bin]] [[bin]]
name = "ulid" name = "ulid"
path = "src/main.rs" path = "src/main.rs"

View file

@ -59,3 +59,21 @@ fn format_id(id: u128) -> String {
formatted_id 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);
}
}