text-test.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ;; -*- coding: utf-8 -*-
  2. (test-begin "text")
  3. (test-equal #\space (integer->char 32))
  4. (test-equal 5000 (char->integer (integer->char 5000)))
  5. ;; If strict:
  6. ;; (test-error (integer->char #\xD800) )
  7. (test-equal #t (char<? #\z #\ß))
  8. (test-equal #f (char<? #\z #\Z))
  9. (test-equal #t (string<? "z" "ß"))
  10. (test-equal #t (string<? "z" "zz"))
  11. (test-equal #f (string<? "z" "Z"))
  12. (test-equal #f (string=? "Straße" "Strasse"))
  13. (test-equal #\I (char-upcase #\i))
  14. (test-equal #\i (char-downcase #\i))
  15. (test-equal #\I (char-titlecase #\i))
  16. (test-equal #\i (char-foldcase #\i))
  17. (test-equal #\ß (char-upcase #\ß))
  18. (test-equal #\ß (char-downcase #\ß))
  19. (test-equal #\ß (char-titlecase #\ß))
  20. (test-equal #\ß (char-foldcase #\ß))
  21. (test-equal #\Σ (char-upcase #\Σ))
  22. (test-equal #\σ (char-downcase #\Σ))
  23. (test-equal #\Σ (char-titlecase #\Σ))
  24. (test-equal #\σ (char-foldcase #\Σ))
  25. (test-equal #\Σ (char-upcase #\ς))
  26. (test-equal #\ς (char-downcase #\ς))
  27. (test-equal #\Σ (char-titlecase #\ς))
  28. (test-equal #\σ (char-foldcase #\ς))
  29. (test-equal #t (char-alphabetic? #\a))
  30. (test-equal #t (char-numeric? #\1))
  31. (test-equal #t (char-whitespace? #\space))
  32. (test-equal #t (char-whitespace? #\x00A0))
  33. (test-equal #t (char-upper-case? #\Σ))
  34. (test-equal #t (char-lower-case? #\σ))
  35. (test-equal #t (char-lower-case? #\x00AA))
  36. (test-equal #f (char-title-case? #\I))
  37. (test-equal #t (char-title-case? #\x01C5))
  38. (test-equal 'Ll (char-general-category #\a))
  39. (test-equal 'Zs (char-general-category #\space))
  40. (test-equal 'Cn (char-general-category #\x10FFFF))
  41. (test-equal "HI" (string-upcase "Hi"))
  42. (test-equal "hi" (string-downcase "Hi"))
  43. (test-equal "hi" (string-foldcase "Hi"))
  44. (test-equal "STRASSE" (string-upcase "Straße"))
  45. (test-equal "straße" (string-downcase "Straße"))
  46. (test-equal "strasse" (string-foldcase "Straße"))
  47. (test-equal "strasse" (string-downcase "STRASSE"))
  48. (test-equal "σ" (string-downcase "Σ"))
  49. (test-equal "ΧΑΟΣ" (string-upcase "ΧΑΟΣ"))
  50. (test-equal "χαος" (string-downcase "ΧΑΟΣ"))
  51. (test-equal "χαοσς" (string-downcase "ΧΑΟΣΣ"))
  52. (test-equal "χαος σ" (string-downcase "ΧΑΟΣ Σ"))
  53. (test-equal "χαοσσ" (string-foldcase "ΧΑΟΣΣ"))
  54. (test-equal "ΧΑΟΣ" (string-upcase "χαος"))
  55. (test-equal "ΧΑΟΣ" (string-upcase "χαοσ"))
  56. (test-equal "Knock Knock" (string-titlecase "kNock KNoCK"))
  57. (test-equal "Who's There?" (string-titlecase "who's there?"))
  58. (test-equal "R6rs" (string-titlecase "r6rs"))
  59. (test-equal "R6rs" (string-titlecase "R6RS"))
  60. (test-expect-fail 1)
  61. (test-equal "If\xFB01; Flat Fire"
  62. (string-titlecase "if\xFB01; \xFB02;at \xFB01;re"))
  63. (test-equal #f (string-ci<? "z" "Z"))
  64. (test-equal #t (string-ci=? "z" "Z"))
  65. (test-equal #t (string-ci=? "Straße" "Strasse"))
  66. (test-equal #t (string-ci=? "Straße" "STRASSE"))
  67. (test-equal #t (string-ci=? "ΧΑΟΣ" "χαοσ"))
  68. (cond-expand (string-normalize-unicode)
  69. (else
  70. (test-expect-fail 4)))
  71. (test-equal "\x65;\x301;" (string-normalize-nfd "\xE9;"))
  72. (test-equal "\xE9;" (string-normalize-nfc "\xE9;"))
  73. (test-equal "\x65;\x301;" (string-normalize-nfd "\x65;\x301;"))
  74. (test-equal "\xE9;" (string-normalize-nfc "\x65;\x301;"))
  75. (define str1 "a😂b😼c")
  76. (test-equal 5 (string-length str1))
  77. (test-equal #\c (string-ref str1 4))
  78. (test-equal #\😼 (string-ref str1 3))
  79. (test-equal "😼bc😂" (str1 [3 2 4 1]))
  80. (test-equal "😂b😼" (str1 [1 <: 4]))
  81. (define str1lst '())
  82. (string-for-each (lambda (x)
  83. (set! str1lst (cons (char->integer x) str1lst)))
  84. str1)
  85. (test-equal '(97 128514 98 128572 99) (reverse str1lst))
  86. (test-equal "😂b😼" (string-copy str1 1 4))
  87. ;; Test various cominations of replacing characters that are 1-char
  88. ;; or 2-char (i.e. surrogate pairs).
  89. (define strx2 (string-copy str1 0))
  90. (test-equal str1 strx2)
  91. (test-equal "😼bc😂" (strx2 [3 2 4 1]))
  92. (test-equal "😂b😼" (strx2 [1 <: 4]))
  93. (string-set! strx2 3 #\y)
  94. (test-equal "a😂byc" strx2)
  95. (string-set! strx2 2 #\x)
  96. (test-equal "a😂xyc" strx2)
  97. (string-set! strx2 4 #\😂)
  98. (test-equal "a😂xy😂" strx2)
  99. (string-set! strx2 1 #\😼)
  100. (test-equal "a😼xy😂" strx2)
  101. (let ((str (make-string 3 #\😂)))
  102. (test-equal 3 (string-length str))
  103. (test-equal #\😂 (string-ref str 2)))
  104. (let ((str '()))
  105. (string-for-each (lambda (x y)
  106. (set! str (cons (char->integer x) str))
  107. (set! str (cons (char->integer y) str)))
  108. str1 "ABC")
  109. (test-equal '(97 65 128514 66 98 67) (reverse str)))
  110. (let ((str '()))
  111. ;; SRFI-13 extension
  112. (string-for-each (lambda (x)
  113. (set! str (cons (char->integer x) str)))
  114. str1 1 4)
  115. (test-equal '(128514 98 128572) (reverse str)))
  116. (let ((str (make-string 3 #\😂)))
  117. (test-equal 3 (string-length str))
  118. (test-equal 6 (str:length))
  119. (test-equal #\x1f602 (str 2)))
  120. (let ((str1 (string-copy "abcdef")))
  121. (test-equal "ef" (str1 [4 <:]))
  122. (test-equal "bfdc" (str1 [1 5 3 2]))
  123. (test-equal "cbd" ((str1 [1 5 3 2]) [3 0 2]))
  124. (test-equal "bcde" (str1 [1 <: 5]))
  125. (test-equal "edbd" (str1 [4 3 1 3]))
  126. (test-equal "bcd" (str1 [1 <=: 3]))
  127. (set! (str1 2) #\D)
  128. (test-equal "abDdef" str1)
  129. (test-equal "bDd" (str1 [1 <: 4]))
  130. (set! (str1 [2 <: 2]) (str1 [1 <: 4]))
  131. (test-equal "abbDdDdef" str1)
  132. (! str2 (str1 [8 2 4 0 5]))
  133. (! str3 (array-index-share str1 [8 2 4 0 5]))
  134. (test-equal "gnu.lists.FString" (invoke (invoke str1 'getClass) 'getName))
  135. (test-error (set! (str2 4) #\x))
  136. (set! (str3 4) #\x)
  137. (test-equal "fbdaD" str2)
  138. (test-equal "abbDdxdef" str1))
  139. (test-begin "pretty-printing")
  140. (import (kawa pprint))
  141. (define (format-pretty form width)
  142. (fluid-let ((*print-right-margin* width))
  143. (! swr (java.io.StringWriter))
  144. (! out (gnu.kawa.io.OutPort swr #t #f))
  145. (out:setPrettyPrinting #t)
  146. (pprint form out)
  147. (out:close)
  148. (swr:toString)))
  149. (define-syntax test-pretty-print
  150. (syntax-rules ()
  151. ((_ form width expected)
  152. (test-equal expected (format-pretty form width)))))
  153. (define form-1
  154. '(define-private (foo fdsf add) (list b 23) (let ((xy (+ dadasd asdasd)) (xz 12)) (list b 22) ABCD (vector 42343 23423423 234324 989))))
  155. (test-pretty-print form-1 30 &{
  156. &|(define-private (foo fdsf
  157. &| add)
  158. &| (list b 23)
  159. &| (let ((xy
  160. &| (+ dadasd asdasd))
  161. &| (xz 12))
  162. &| (list b 22)
  163. &| ABCD
  164. &| (vector 42343 23423423
  165. &| 234324 989)))})
  166. (test-pretty-print form-1 50 &{
  167. &|(define-private (foo fdsf add)
  168. &| (list b 23)
  169. &| (let ((xy (+ dadasd asdasd)) (xz 12))
  170. &| (list b 22)
  171. &| ABCD
  172. &| (vector 42343 23423423 234324 989)))})
  173. (define form-2
  174. '(if (equal? fdfds sdfsdf) (cond (aa (list bb)) ((null? cc) dd)) (vector xx sxasxs (+ 454 435) dsadd)))
  175. (test-pretty-print form-2 20 &{
  176. &|(if (equal? fdfds
  177. &| sdfsdf)
  178. &| (cond (aa
  179. &| (list
  180. &| bb))
  181. &| ((null?
  182. &| cc)
  183. &| dd))
  184. &| (vector xx
  185. &| sxasxs
  186. &| (+ 454 435)
  187. &| dsadd))})
  188. (test-pretty-print form-2 40 &{
  189. &|(if (equal? fdfds sdfsdf)
  190. &| (cond (aa (list bb))
  191. &| ((null? cc) dd))
  192. &| (vector xx sxasxs (+ 454 435)
  193. &| dsadd))})
  194. (test-pretty-print form-2 80 &{
  195. &|(if (equal? fdfds sdfsdf)
  196. &| (cond (aa (list bb)) ((null? cc) dd))
  197. &| (vector xx sxasxs (+ 454 435) dsadd))})
  198. (test-pretty-print form-2 200 &{
  199. &|(if (equal? fdfds sdfsdf) (cond (aa (list bb)) ((null? cc) dd)) (vector xx sxasxs (+ 454 435) dsadd))})
  200. (define form-3
  201. '(if (equal? xyz (list asdsads bccc)) (list xyz xyz) (list 987 xy)))
  202. (test-pretty-print form-3 200 &{
  203. &|(if (equal? xyz (list asdsads bccc)) (list xyz xyz) (list 987 xy))})
  204. (test-pretty-print form-3 40 &{
  205. &|(if (equal? xyz (list asdsads bccc))
  206. &| (list xyz xyz)
  207. &| (list 987 xy))})
  208. (test-pretty-print form-3 30 &{
  209. &|(if (equal? xyz
  210. &| (list asdsads bccc))
  211. &| (list xyz xyz)
  212. &| (list 987 xy))})
  213. (test-end)
  214. (test-end)