check-normalization.scm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Get NormalizationTest.txt from http://www.unicode.org/
  3. ; ,config ,load =scheme48/debug/test.scm
  4. ; ,exec (define normalization-tests-filename ".../NormalizationTest.txt")
  5. ; ,exec ,load =scheme48/debug/check-normalization.scm
  6. ; ,exec (done)
  7. (load-package 'testing)
  8. (config '(run
  9. (define-structure check-normalizations (export check-all)
  10. (open scheme testing
  11. (subset srfi-13 (string-skip))
  12. (subset srfi-14 (char-set:hex-digit))
  13. unicode
  14. unicode-normalizations)
  15. (begin
  16. (define (read-line port)
  17. (let loop ((l '()))
  18. (let ((c (read-char port)))
  19. (if (eof-object? c)
  20. c
  21. (if (char=? c #\newline)
  22. (list->string (reverse l))
  23. (loop (cons c l)))))))
  24. (define (parse-scalar-values s)
  25. (let ((size (string-length s)))
  26. (let column-loop ((start 0) (count 0) (rev-columns '()))
  27. (if (= count 5)
  28. (apply values (reverse rev-columns))
  29. (let sv-loop ((start start) (rev-svs '()))
  30. (let* ((i (string-skip s char-set:hex-digit start))
  31. (n (string->number (substring s start i) 16)))
  32. (if (char=? #\space (string-ref s i))
  33. (sv-loop (+ 1 i) (cons n rev-svs))
  34. (column-loop (+ 1 i) (+ 1 count)
  35. (cons (list->string (map scalar-value->char (reverse (cons n rev-svs))))
  36. rev-columns)))))))))
  37. (define (check-line s)
  38. (call-with-values
  39. (lambda ()
  40. (parse-scalar-values s))
  41. (lambda (c1 c2 c3 c4 c5)
  42. (test s equal? #t #t)
  43. (check-one c1 c2 c3 c4 c5))))
  44. (define (check-one c1 c2 c3 c4 c5)
  45. (test "c2 == NFC(c1)" string=? c2 (string-normalize-nfc c1))
  46. (test "c2 == NFC(c2)" string=? c2 (string-normalize-nfc c2))
  47. (test "c2 == NFC(c3)" string=? c2 (string-normalize-nfc c3))
  48. (test "c4 == NFC(c4)" string=? c4 (string-normalize-nfc c4))
  49. (test "c4 == NFC(c5)" string=? c4 (string-normalize-nfc c5))
  50. (test "c3 == NFD(c1)" string=? c3 (string-normalize-nfd c1))
  51. (test "c3 == NFD(c2)" string=? c3 (string-normalize-nfd c2))
  52. (test "c3 == NFD(c3)" string=? c3 (string-normalize-nfd c3))
  53. (test "c5 == NFD(c4)" string=? c5 (string-normalize-nfd c4))
  54. (test "c5 == NFD(c5)" string=? c5 (string-normalize-nfd c5))
  55. (test "c4 == NFKC(c1)" string=? c4 (string-normalize-nfkc c1))
  56. (test "c4 == NFKC(c2)" string=? c4 (string-normalize-nfkc c2))
  57. (test "c4 == NFKC(c3)" string=? c4 (string-normalize-nfkc c3))
  58. (test "c4 == NFKC(c4)" string=? c4 (string-normalize-nfkc c4))
  59. (test "c4 == NFKC(c5)" string=? c4 (string-normalize-nfkc c5))
  60. (test "c5 == NFKD(c1)" string=? c5 (string-normalize-nfkd c1))
  61. (test "c5 == NFKD(c2)" string=? c5 (string-normalize-nfkd c2))
  62. (test "c5 == NFKD(c3)" string=? c5 (string-normalize-nfkd c3))
  63. (test "c5 == NFKD(c4)" string=? c5 (string-normalize-nfkd c4))
  64. (test "c5 == NFKD(c5)" string=? c5 (string-normalize-nfkd c5)))
  65. (define (check-all filename)
  66. (call-with-input-file filename
  67. (lambda (port)
  68. (let loop ()
  69. (let ((thing (read-line port)))
  70. (if (string? thing)
  71. (begin
  72. (if (and (not (string=? "" thing))
  73. (not (char=? (string-ref thing 0) #\#))
  74. (not (char=? (string-ref thing 0) #\@)))
  75. (check-line thing))
  76. (loop))))))))
  77. ))
  78. ))
  79. (open 'check-normalizations)
  80. (check-all normalization-tests-filename)
  81. (if (in 'testing '(run (lost?)))
  82. (display "Some tests failed.")
  83. (display "All tests succeeded."))
  84. (newline)
  85. (define (done)
  86. (exit (if (in 'testing '(run (lost?))) 1 0)))