objective categorical abstract machine language personal data server
1module Tid = Mist.Tid
2
3let test_create () =
4 Alcotest.(check string)
5 "tid" "3kztsgrxiyxje"
6 (Tid.of_timestamp_ms 1723819911723L ~clockid:490)
7
8let test_invalid_create () =
9 Alcotest.check_raises "non-negative"
10 (Invalid_argument "timestamp must be within range [0, 2^53)") (fun () ->
11 ignore (Tid.of_timestamp_us (-1L) ~clockid:490) ) ;
12 Alcotest.check_raises "too large"
13 (Invalid_argument "timestamp must be within range [0, 2^53)") (fun () ->
14 ignore (Tid.of_timestamp_us (Int64.shift_left 1L 53) ~clockid:490) )
15
16let test_clockid () =
17 Alcotest.check_raises "clockid too large"
18 (Invalid_argument "clockid must be within range [0, 1023]") (fun () ->
19 ignore (Tid.of_timestamp_ms 1723819911723L ~clockid:1024) ) ;
20 Alcotest.check_raises "clockid too small"
21 (Invalid_argument "clockid must be within range [0, 1023]") (fun () ->
22 ignore (Tid.of_timestamp_ms 1723819911723L ~clockid:(-1)) ) ;
23 (* shouldn't throw *)
24 ignore (Tid.of_timestamp_ms 1723819911723L ~clockid:0) ;
25 (* shouldn't throw *)
26 ignore (Tid.of_timestamp_ms 1723819911723L ~clockid:1023)
27
28let test_parse () =
29 let timestamp, clockid = Tid.to_timestamp_ms "3kztrqxakokct" in
30 Alcotest.(check int64) "timestamp" timestamp 1723819179066L ;
31 Alcotest.(check int) "clockid" clockid 281
32
33let test_validate () =
34 let valid_tids = ["3jzfcijpj2z2a"; "7777777777777"; "3zzzzzzzzzzzz"] in
35 let invalid_tids =
36 [ "3jzfcijpj2z21"
37 ; "0000000000000"
38 ; "3jzfcijpj2z2aa"
39 ; "3jzfcijpj2z2"
40 ; "3jzf-cij-pj2z-2a"
41 ; "zzzzzzzzzzzzz"
42 ; "kjzfcijpj2z2a"
43 ; "kjzfcijpj2z2a" ]
44 in
45 List.iter
46 (fun tid -> Alcotest.(check bool) ("valid " ^ tid) true (Tid.is_valid tid))
47 valid_tids ;
48 List.iter
49 (fun tid ->
50 Alcotest.(check bool) ("invalid " ^ tid) false (Tid.is_valid tid) )
51 invalid_tids
52
53let () =
54 Alcotest.run "tid"
55 [ ( "create"
56 , [ ("create", `Quick, test_create)
57 ; ("invalid timestamp", `Quick, test_invalid_create)
58 ; ("invalid clockid", `Quick, test_clockid) ] )
59 ; ("parse", [("parse", `Quick, test_parse)])
60 ; ("validate", [("validate", `Quick, test_validate)]) ]