Fortran · 2464 bytes Raw Blame History
1 program test_cache_paths
2 use fgof_cache, only : &
3 FGOF_CACHE_ERR_INVALID_OPTIONS, &
4 FGOF_CACHE_ERR_NOT_FOUND, &
5 FGOF_CACHE_OK, &
6 cache_key_token, &
7 cache_path_for_key, &
8 cache_relative_path_for_key, &
9 clear_cache_options, &
10 resolve_cache_entry
11 use fgof_cache_types, only : cache_entry, cache_options
12 implicit none
13
14 type(cache_options) :: options
15 type(cache_entry) :: entry
16 character(len=:), allocatable :: root_path
17
18 if (cache_key_token("abc") /= "616263") error stop "cache_key_token should hex-encode bytes"
19 if (cache_key_token("a ") /= "6120") error stop "cache_key_token should preserve trailing spaces"
20
21 if (cache_relative_path_for_key("abc") /= "61/62/616263") error stop "relative path helper should shard tokenized keys"
22 if (cache_path_for_key("/tmp/cache", "abc") /= "/tmp/cache/61/62/616263") error stop "path helper should join cache roots and sharded keys"
23
24 root_path = unique_root("entry")
25 options = clear_cache_options()
26 options%root_dir = root_path
27 options%namespace = "demo"
28 entry = resolve_cache_entry("abc", options)
29 if (entry%error_code /= FGOF_CACHE_OK) error stop "entry resolution should succeed for normal keys"
30 if (entry%present) error stop "fresh cache entries should not report present"
31 if (entry%root_path /= root_path // "/demo") error stop "entry resolution should report the resolved root path"
32 if (entry%relative_path /= "61/62/616263") error stop "entry resolution should expose the relative cache path"
33 if (entry%path /= root_path // "/demo/61/62/616263") error stop "entry resolution should expose the full cache path"
34
35 entry = resolve_cache_entry("", options)
36 if (entry%error_code /= FGOF_CACHE_ERR_INVALID_OPTIONS) error stop "empty keys should be rejected"
37
38 options = clear_cache_options()
39 options%create_root = .false.
40 options%root_dir = unique_root("missing-entry")
41 entry = resolve_cache_entry("abc", options)
42 if (entry%error_code /= FGOF_CACHE_ERR_NOT_FOUND) error stop "entry resolution should surface missing cache roots"
43
44 contains
45
46 function unique_root(label) result(path)
47 character(len=*), intent(in) :: label
48 character(len=:), allocatable :: path
49 character(len=32) :: count_text
50 integer :: count_value
51
52 call system_clock(count=count_value)
53 write(count_text, "(i0)") count_value
54 path = "build/fgof-cache-path-" // label // "-" // trim(count_text)
55 end function unique_root
56
57 end program test_cache_paths
58