nspkg / tests / coracle / filelist_compatibility_test.v
50 lines · 48 sloc · 1.61 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module coracle
2
3import nspkg as facade
4
5fn test_filelist_lookup_matches_c_for_sorted_names_and_string_boundaries() {
6 mut mismatches := []string{}
7 for names in [
8 []string{},
9 ['one'],
10 ['', 'etc/', 'etc/a', 'etc/aa', 'etc/z', 'usr/', 'usr/bin/tool', 'é', '雪'],
11 ] {
12 mut files := []File{}
13 for index, name in names {
14 files << File{ name: name.str, size: index + 1, mode: u32(0o100644) }
15 }
16 // the API's filelist and file records intentionally retain the C layout.
17 list := Filelist{
18 count: uint(files.len)
19 files: if files.len == 0 { unsafe { nil } } else { files.data }
20 }
21 public := unsafe { &facade.Filelist(&list) }
22 mut queries := ['', '\x00suffix', 'before', 'zzz', 'é', '雪', 'etc', 'etc/a', 'etc/ab',
23 'usr/bin/tool', 'usr/bin/tool\x00ignored', 'missing\x00etc/a']
24 for name in names {
25 queries << [name, name + '\x00suffix', name + '/']
26 }
27 for query in queries {
28 mut expected := -1
29 if found := list.contains(query) {
30 for index in 0 .. files.len {
31 if voidptr(found) == voidptr(unsafe { &files[index] }) { expected = index }
32 }
33 assert expected >= 0
34 }
35 mut actual := -1
36 if found := public.contains(query) {
37 for index in 0 .. files.len {
38 if voidptr(found) == voidptr(unsafe { &files[index] }) { actual = index }
39 }
40 assert actual >= 0
41 }
42 if actual != expected {
43 mismatches << '${names}/${query.bytes()}: ${actual} != ${expected}'
44 }
45 }
46 }
47 assert unsafe { (&Filelist(nil)).contains('one') } == none
48 assert unsafe { (&facade.Filelist(nil)).contains('one') } == none
49 assert mismatches.len == 0, mismatches[..int_min(8, mismatches.len)].join('\n')
50}
51