]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/collation/collation-tests.factor
unicode.collation: fixes for unicode 13.0.
[factor.git] / basis / unicode / collation / collation-tests.factor
1 USING: arrays assocs fry grouping hash-sets io.encodings.utf8
2 io.files kernel math math.order math.parser sequences sets
3 splitting strings tools.test unicode ;
4 IN: unicode.collation.tests
5
6 : test-equality ( str1 str2 -- ? ? ? ? )
7     { primary= secondary= tertiary= quaternary= }
8     [ execute( a b -- ? ) ] 2with map
9     first4 ;
10
11 { f f f f } [ "hello" "hi" test-equality ] unit-test
12 { t f f f } [ "hello" "h\u0000e9llo" test-equality ] unit-test
13 { t t f f } [ "hello" "HELLO" test-equality ] unit-test
14 { t t t f } [ "hello" "h e l l o." test-equality ] unit-test
15 { t t t t } [ "hello" "\0hello\0" test-equality ] unit-test
16 { { "good bye" "goodbye" "hello" "HELLO" } }
17 [ { "HELLO" "goodbye" "good bye" "hello" } sort-strings ] unit-test
18
19 : collation-test-lines ( -- lines )
20     "vocab:unicode/UCA/CollationTest/CollationTest_SHIFTED.txt" utf8 file-lines
21     [ "#" head? ] reject harvest ;
22
23 : parse-collation-test-shifted ( -- lines )
24     collation-test-lines
25     [ ";" split first " " split [ hex> ] "" map-as ] map ;
26
27 : tail-from-last ( string char -- string' )
28     '[ _ = ] dupd find-last drop 1 + tail ; inline
29
30 : line>test-weights ( string -- pair )
31     ";" split1 [
32         " " split [ hex> ] map
33     ] [
34         "#" split1 nip CHAR: [ tail-from-last
35         "]" split1 drop
36         "|" split 4 head
37         [ " " split harvest [ hex> ] map ] map
38     ] bi* 2array ;
39
40 ! These tests actually would pass if I didn't fix up
41 ! the ducet table for Tibetan. It took me way too long to realize
42 ! that the Unicode committee recommends fixing Tibetan collation
43 ! yet ships tests that collation fails if you fix it.
44 ! (Specifically the ducet entries for { 0x0FB2 0x0F71 } and { 0x0FB3 0x0F71 }
45 ! cause these tests to fail)
46 : xfailed-collation-tests ( -- seq )
47     HS{
48         { 3958 3953 820 }
49
50         { 4018 820 3953 3968 }
51         { 4018 820 3968 3953 }
52         { 4018 3953 1 3968 97 }
53         { 4018 820 3969 }
54
55         { 3960 3953 820 }
56         { 4019 820 3953 3968 }
57         { 4019 820 3968 3953 }
58         { 4019 3953 820 3968 }
59         { 4019 3953 1 3968 97 }
60     } ;
61
62 : parse-collation-test-weights ( -- weights )
63     collation-test-lines
64     [ line>test-weights ] map
65     [ first xfailed-collation-tests in? ] reject ;
66
67 : calculate-collation ( chars collation -- collation-calculated collation-answer )
68     [ >string collation-key/nfd drop ] [ { 0 } join ] bi* ;
69
70 : find-bad-collations ( pairs -- seq )
71     [ first2 calculate-collation sequence= ] reject ;
72
73 { { } }
74 [ parse-collation-test-weights find-bad-collations ] unit-test
75
76 { { } } [
77     parse-collation-test-shifted
78     2 clump >hash-set
79
80     ! Remove these two expected-fail Tibetan collation comparison tests
81     ! They are bad tests once you fix up the ducet table with { 0x0FB2 0x0F71 } and { 0x0FB3 0x0F71 }
82     {
83         { { 4018 820 3969 } { 3959 33 } }
84         { { 4019 3953 820 3968 } { 3961 33 } }
85         { { 4019 98 } { 4019 3953 1 3968 97 } }
86         { { 4028 98 } { 4018 3953 1 3968 97 } }
87     } [ [ >string ] bi@ ] assoc-map >hash-set diff members
88
89     [ string<=> { +lt+ +eq+ } member? ] assoc-reject
90 ] unit-test
91
92 ! XXX: Once again, these tests pass if you don't
93 ! fix up the ducet table for { 0x0FB2 0x0F71 } and { 0x0FB3 0x0F71 }
94 ! { +lt+ } [ { 4018 820 3969 } { 3959 33 } [ >string ] bi@ string<=> ] unit-test
95 ! { +lt+ } [ { 4019 3953 820 3968 } { 3961 33 } [ >string ] bi@ string<=> ] unit-test