check-normalization.scm 3.4 KB

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