| 1 |
|
| 2 |
(library (tests normalization) |
| 3 |
(export run-tests) |
| 4 |
(import (ikarus) (tests framework) (unicode-data)) |
| 5 |
|
| 6 |
(define (reset) (error 'reset "yukk")) |
| 7 |
(define (enumerate ls) |
| 8 |
(let f ([ls ls] [i 0]) |
| 9 |
(cond |
| 10 |
[(null? ls) '()] |
| 11 |
[else (cons i (f (cdr ls) (+ i 1)))]))) |
| 12 |
(define (list-head ls n) |
| 13 |
(if (zero? n) |
| 14 |
'() |
| 15 |
(cons (car ls) (list-head (cdr ls) (- n 1))))) |
| 16 |
|
| 17 |
(define (split str) |
| 18 |
(remove "" |
| 19 |
(let f ([i 0] [n (string-length str)]) |
| 20 |
(cond |
| 21 |
[(= i n) (list (substring str 0 n))] |
| 22 |
[(char=? (string-ref str i) #\space) |
| 23 |
(cons (substring str 0 i) |
| 24 |
(split (substring str (+ i 1) n)))] |
| 25 |
[else (f (add1 i) n)])))) |
| 26 |
|
| 27 |
(define (conv x) |
| 28 |
(list->string |
| 29 |
(map (lambda (x) (integer->char (string->number x 16))) |
| 30 |
(split x)))) |
| 31 |
|
| 32 |
(define (run-tests) |
| 33 |
(let ([data (map (lambda (x) (map conv (list-head x 5))) |
| 34 |
(filter (lambda (x) (>= (length x) 5)) |
| 35 |
(get-unicode-data |
| 36 |
(src-file "unicode/UNIDATA/NormalizationTest.txt"))))]) |
| 37 |
(define NFD string-normalize-nfd) |
| 38 |
(define NFKD string-normalize-nfkd) |
| 39 |
(define NFC string-normalize-nfc) |
| 40 |
(define NFKC string-normalize-nfkc) |
| 41 |
|
| 42 |
(define (test1) |
| 43 |
(for-each |
| 44 |
(lambda (x testno) |
| 45 |
(apply |
| 46 |
(lambda (c1 c2 c3 c4 c5) |
| 47 |
(unless (and (string=? c2 (NFC c1) (NFC c2) (NFC c3)) |
| 48 |
(string=? c4 (NFC c4) (NFC c5))) |
| 49 |
(parameterize ([print-unicode #f]) |
| 50 |
(printf "test 1[~s] failed for ~s\n" testno x) |
| 51 |
(printf " c2 = ~s\n" c2) |
| 52 |
(printf " NFC(c1) = ~s\n" (NFC c1)) |
| 53 |
(printf " NFC(c2) = ~s\n" (NFC c2)) |
| 54 |
(printf " NFC(c3) = ~s\n" (NFC c3)) |
| 55 |
(printf " c4 = ~s\n" c4) |
| 56 |
(printf " NFC(c4) = ~s\n" (NFC c4)) |
| 57 |
(printf " NFC(c5) = ~s\n" (NFC c5)) |
| 58 |
(reset)))) |
| 59 |
x)) |
| 60 |
data (enumerate data))) |
| 61 |
|
| 62 |
(define (test2) |
| 63 |
(for-each |
| 64 |
(lambda (x testno) |
| 65 |
(apply |
| 66 |
(lambda (c1 c2 c3 c4 c5) |
| 67 |
(unless (and (string=? c3 (NFD c1) (NFD c2) (NFD c3)) |
| 68 |
(string=? c5 (NFD c4) (NFD c5))) |
| 69 |
(parameterize ([print-unicode #f]) |
| 70 |
(printf "test 2[~s] failed for ~s\n" testno x) |
| 71 |
(printf " c3 = ~s\n" c3) |
| 72 |
(printf " NFD(c1) = ~s\n" (NFD c1)) |
| 73 |
(printf " NFD(c2) = ~s\n" (NFD c2)) |
| 74 |
(printf " NFD(c3) = ~s\n" (NFD c3)) |
| 75 |
(printf " c5 = ~s\n" c5) |
| 76 |
(printf " NFD(c4) = ~s\n" (NFD c4)) |
| 77 |
(printf " NFD(c5) = ~s\n" (NFD c5)) |
| 78 |
(reset)))) |
| 79 |
x)) |
| 80 |
data (enumerate data))) |
| 81 |
|
| 82 |
(define (test3) |
| 83 |
(for-each |
| 84 |
(lambda (x testno) |
| 85 |
(apply |
| 86 |
(lambda (c1 c2 c3 c4 c5) |
| 87 |
(unless (string=? c4 (NFKC c1) (NFKC c2) (NFKC c3) (NFKC c4) (NFKC c5)) |
| 88 |
(parameterize ([print-unicode #f]) |
| 89 |
(printf "test 3[~s] failed for ~s\n" testno x) |
| 90 |
(printf " c4 = ~s\n" c4) |
| 91 |
(printf " NFKC(c1) = ~s\n" (NFKC c1)) |
| 92 |
(printf " NFKC(c2) = ~s\n" (NFKC c2)) |
| 93 |
(printf " NFKC(c3) = ~s\n" (NFKC c3)) |
| 94 |
(printf " NFKC(c4) = ~s\n" (NFKC c4)) |
| 95 |
(printf " NFKC(c5) = ~s\n" (NFKC c5)) |
| 96 |
(reset)))) |
| 97 |
x)) |
| 98 |
data (enumerate data))) |
| 99 |
|
| 100 |
(define (test4) |
| 101 |
(for-each |
| 102 |
(lambda (x testno) |
| 103 |
(apply |
| 104 |
(lambda (c1 c2 c3 c4 c5) |
| 105 |
(unless (string=? c5 (NFKD c1) (NFKD c2) (NFKD c3) (NFKD c4) (NFKD c5)) |
| 106 |
(parameterize ([print-unicode #f]) |
| 107 |
(printf "test 4[~s] failed for ~s\n" testno x) |
| 108 |
(printf " c5 = ~s\n" c5) |
| 109 |
(printf " NFKD(c1) = ~s\n" (NFKD c1)) |
| 110 |
(printf " NFKD(c2) = ~s\n" (NFKD c2)) |
| 111 |
(printf " NFKD(c3) = ~s\n" (NFKD c3)) |
| 112 |
(printf " NFKD(c4) = ~s\n" (NFKD c4)) |
| 113 |
(printf " NFKD(c5) = ~s\n" (NFKD c5)) |
| 114 |
(reset)))) |
| 115 |
x)) |
| 116 |
data (enumerate data))) |
| 117 |
|
| 118 |
(printf " running ~s tests ..." (length data)) |
| 119 |
(test1) |
| 120 |
(test2) |
| 121 |
(test3) |
| 122 |
(test4)))) |
| 123 |
|