// Expected encodings come from a separate model (Python's `uuid` module and // a base conversion), not from this package. import std/bytes import std/list import std/random.{type random} import std/test.{assert, assert_equal} import std/time.{type clock} import uuid. { type Alphabet , type Uuid , InvalidChar , NotAscending , NotUrlSafe , Repeated , TooLarge , TooShort , WrongLength } /// RFC 9562, appendix A.6. fn example :: -> Uuid { uuid.v7_from 0x017F22E279B0 ( bytes.from_list [0x0C, 0xC3, 0x18, 0xC4, 0xDC, 0x0C, 0x0C, 0x07, 0x39, 0x8F] ) } fn presets :: -> List<(String, Alphabet)> { [ ("hex", uuid.hex ()) , ("crockford", uuid.crockford ()) , ("base58", uuid.base58 ()) , ("base62", uuid.base62 ()) ] } /// 50 IDs of random bytes, the same on every run. fn random_ids :: -> List { random.with_seed 5 fn { list.map (list.range 1 50) fn _ { match uuid.from_bytes (random.bytes 16) { Some(id) -> id, None -> panic "16 bytes" } } } } @test fn the_rfc_example :: { let id = example () assert_equal (uuid.to_rfc id) "017f22e2-79b0-7cc3-98c4-dc0c0c07398f" assert_equal (uuid.version id) 7 assert_equal (uuid.timestamp id) Some(1645557742000) } @test fn nil_and_max :: { assert_equal (uuid.to_rfc (uuid.nil ())) "00000000-0000-0000-0000-000000000000" assert_equal (uuid.to_rfc (uuid.max ())) "ffffffff-ffff-ffff-ffff-ffffffffffff" assert_equal (uuid.timestamp (uuid.nil ())) None // Version 15 and the wrong variant: not a v7 timestamp. assert_equal (uuid.timestamp (uuid.max ())) None assert (uuid.nil () < example () && example () < uuid.max ()) "ordered by bytes" } @test fn bytes_round_trip :: { assert_equal (uuid.from_bytes (uuid.to_bytes (example ()))) Some(example ()) assert_equal (uuid.from_bytes (bytes.from_list [1, 2, 3])) None } @test fn the_rfc_form_round_trips_and_ignores_case :: { assert_equal (uuid.from_rfc "017f22e2-79b0-7cc3-98c4-dc0c0c07398f") Some(example ()) assert_equal (uuid.from_rfc "017F22E2-79B0-7CC3-98C4-DC0C0C07398F") Some(example ()) list.each (random_ids ()) fn id { assert_equal (uuid.from_rfc (uuid.to_rfc id)) Some(id) } } @test fn the_rfc_form_is_strict :: { let refused = [ "" , "017f22e279b07cc398c4dc0c0c07398f" , "\{017f22e2-79b0-7cc3-98c4-dc0c0c07398f\}" , "017f22e2-79b0-7cc3-98c4-dc0c0c07398" , "017f22e2-79b0-7cc3-98c4-dc0c0c07398f0" , "017f22e2_79b0-7cc3-98c4-dc0c0c07398f" , "017f22e2-79b0-7cc3-98c4-dc0c0c07398g" , "017f22e-279b0-7cc3-98c4-dc0c0c07398f" ] list.each refused fn text { assert_equal (uuid.from_rfc text) None } } @test fn each_preset_has_its_width :: { let widths = list.map (presets ()) fn p { match p { (name, a) -> (name, uuid.width a) } } assert_equal widths [("hex", 32), ("crockford", 26), ("base58", 22), ("base62", 22)] } @test fn the_example_and_max_in_each_preset :: { assert_equal (uuid.encode (example ()) (uuid.hex ())) "017f22e279b07cc398c4dc0c0c07398f" assert_equal (uuid.encode (example ()) (uuid.crockford ())) "01FWHE4YDGFK1SHH6W1G60EECF" assert_equal (uuid.encode (example ()) (uuid.base58 ())) "1BihbxwwQ4NZZpKRH9JDCz" assert_equal (uuid.encode (example ()) (uuid.base62 ())) "02p5oQZoHTv0zeY5yG21K3" assert_equal (uuid.encode (uuid.max ()) (uuid.crockford ())) "7ZZZZZZZZZZZZZZZZZZZZZZZZZ" assert_equal (uuid.encode (uuid.max ()) (uuid.base58 ())) "YcVfxkQb6JRzqk5kF2tNLv" assert_equal (uuid.encode (uuid.max ()) (uuid.base62 ())) "7n42DGM5Tflk9n8mt7Fhc7" assert_equal (uuid.encode (uuid.nil ()) (uuid.base58 ())) "1111111111111111111111" assert_equal (show (example ())) "01FWHE4YDGFK1SHH6W1G60EECF" assert_equal (debug (example ())) "uuid.Uuid(017f22e2-79b0-7cc3-98c4-dc0c0c07398f)" } @test fn every_preset_round_trips :: { let ids = [uuid.nil (), uuid.max (), example (), ..random_ids ()] list.each (presets ()) fn p { match p { (_, a) -> list.each ids fn id { assert_equal (uuid.decode (uuid.encode id a) a) Ok(id) } } } } @test fn decode_refuses_what_no_uuid_encodes :: { let c = uuid.crockford () assert_equal (uuid.decode "01FWHE4YDGFK1SHH6W1G60EEC" c) Error(WrongLength(25)) assert_equal (uuid.decode "01FWHE4YDGFK1SHH6W1G60EECF0" c) Error(WrongLength(27)) assert_equal (uuid.decode "01FWHE4YDGFK1SHH6W1G60EECU" c) Error(InvalidChar(25)) assert_equal (uuid.decode "0-FWHE4YDGFK1SHH6W1G60EECF" c) Error(InvalidChar(1)) // 8 * 32^25 = 2^128. assert_equal (uuid.decode "80000000000000000000000000" c) Error(TooLarge) assert_equal (uuid.decode "zzzzzzzzzzzzzzzzzzzzzz" (uuid.base62 ())) Error(TooLarge) // Base 58 is case-sensitive and has no 0. assert_equal (uuid.decode "0111111111111111111111" (uuid.base58 ())) Error(InvalidChar(0)) } @test fn crockford_reads_lowercase_and_look_alikes :: { let c = uuid.crockford () assert_equal (uuid.decode "01fwhe4ydgfk1shh6w1g60eecf" c) Ok(example ()) // I and L read as 1, O as 0. assert_equal (uuid.decode "O1FWHE4YDGFKISHH6W1G6OEECF" c) Ok(example ()) assert_equal (uuid.decode "o1FWHE4YDGFKlSHH6W1G6oEECF" c) Ok(example ()) assert_equal (uuid.decode "017F22E279B07CC398C4DC0C0C07398F" (uuid.hex ())) Ok(example ()) } @test fn alphabets_are_checked :: { let error = fn chars: String { match uuid.alphabet chars { Ok(_) -> None, Error(e) -> Some(e) } } assert_equal (error "") Some(TooShort) assert_equal (error "a") Some(TooShort) assert_equal (error "ab a") Some(NotUrlSafe(" ")) assert_equal (error "ab/") Some(NotUrlSafe("/")) assert_equal (error "abca") Some(Repeated("a")) assert_equal (error "ba") Some(NotAscending("a")) assert_equal (error "-._~09AZaz") Some(NotAscending("0")) assert_equal (error "-.09AZ_az~") None } @test fn a_custom_alphabet_round_trips :: { match uuid.alphabet "01" { Ok(binary) -> { assert_equal (uuid.width binary) 128 list.each [example (), uuid.max (), ..random_ids ()] fn id { assert_equal (uuid.decode (uuid.encode id binary) binary) Ok(id) } } , Error(e) -> panic "{e}" } } /// The presets skip `alphabet`'s checks and carry their width: they must /// agree with the alphabet built from the same characters. @test fn presets_agree_with_their_alphabet :: { list.each [ ("0123456789abcdef", uuid.hex ()) , ("0123456789ABCDEFGHJKMNPQRSTVWXYZ", uuid.crockford ()) , ( "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" , uuid.base58 () ) , ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" , uuid.base62 () ) ] fn pair { let (chars, preset) = pair match uuid.alphabet chars { Ok(built) -> { assert_equal (uuid.width preset) (uuid.width built) list.each [example (), uuid.nil (), uuid.max (), ..random_ids ()] fn id { assert_equal (uuid.encode id preset) (uuid.encode id built) } } , Error(e) -> panic "{chars}: {e}" } } } /// Alphabets of 2^k characters are read and written k bits at a time. When /// k does not divide 128, the first digit only uses its low bits. @test fn power_of_two_alphabets :: { let octal = alphabet_of "01234567" assert_equal (uuid.width octal) 43 assert_equal (uuid.encode (example ()) octal) "0013762134236330174607143046700601401634617" assert_equal (uuid.encode (uuid.max ()) octal) "3777777777777777777777777777777777777777777" assert_equal (uuid.decode "4000000000000000000000000000000000000000000" octal) Error(TooLarge) let quaternary = alphabet_of "0123" assert_equal (uuid.encode (example ()) quaternary) "0001133302023202132123001330300321203010313000300030001303212033" let sixty_four = alphabet_of "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" assert_equal (uuid.width sixty_four) 22 assert_equal (uuid.encode (example ()) sixty_four) "-0UmAXTQ0wktY3r-kB0naE" assert_equal (uuid.encode (uuid.max ()) sixty_four) "2zzzzzzzzzzzzzzzzzzzzz" assert_equal (uuid.decode "3---------------------" sixty_four) Error(TooLarge) list.each [octal, quaternary, sixty_four] fn a { list.each [example (), uuid.nil (), uuid.max (), ..random_ids ()] fn id { assert_equal (uuid.decode (uuid.encode id a) a) Ok(id) } } } /// An invalid character is reported before a value too large, as when the /// whole value was computed first. @test fn an_invalid_character_comes_before_too_large :: { assert_equal (uuid.decode "8000000000000000000000000U" (uuid.crockford ())) Error(InvalidChar(25)) } fn alphabet_of :: chars: String -> Alphabet { match uuid.alphabet chars { Ok(a) -> a, Error(e) -> panic "{chars}: {e}" } } /// Whether text compares like the IDs, for each consecutive pair. fn sorts_alike :: ids: List a: Alphabet -> Bool { match ids { [x, y, ..rest] -> (x < y) == (uuid.encode x a < uuid.encode y a) && sorts_alike [y, ..rest] a , _ -> True } } @test fn text_sorts_like_the_ids :: { let ids = random_ids () list.each (presets ()) fn p { match p { (name, a) -> assert (sorts_alike ids a) name } } } @test fn v7_ids_sort_by_time :: { let r = bytes.from_list [255, 255, 255, 255, 255, 255, 255, 255, 255, 255] let zero = bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // A later millisecond sorts after, whatever the random bits. assert (uuid.v7_from 1000 r < uuid.v7_from 1001 zero) "by time" list.each (presets ()) fn p { match p { (name, a) -> assert ( uuid.encode (uuid.v7_from 1000 r) a < uuid.encode (uuid.v7_from 1001 zero) a ) name } } assert_equal (uuid.timestamp (uuid.v7_from 281474976710655 r)) Some(281474976710655) } @test fn v7_reads_the_clock_and_randomness :: { let id = time.with_fixed_clock (time.from_unix_ms 1700000000000) fn { random.with_seed 9 fn { uuid.v7 () } } assert_equal (uuid.to_rfc id) "018bcfe5-6800-7460-b0be-fe52afae62ea" assert_equal (show id) "01HF7YAT00EHGB1FQYAAQTWRQA" assert_equal (uuid.timestamp id) Some(1700000000000) } @test fn real_v7_ids_carry_the_time :: -> () / { let before = time.unix_ms (time.now ()) let id = uuid.v7 () assert_equal (uuid.version id) 7 match uuid.timestamp id { Some(ms) -> assert (ms >= before) "not before the clock" , None -> panic "a v7 UUID has a timestamp" } } @test @should_panic "not in [0, 2^48)" fn v7_from_refuses_times_before_1970 :: { let _ = uuid.v7_from (-1) (bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) } @test @should_panic "expected 10 random bytes, got 9" fn v7_from_refuses_other_amounts_of_randomness :: { let _ = uuid.v7_from 0 (bytes.from_list [0, 0, 0, 0, 0, 0, 0, 0, 0]) }