|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite iconv) |
|
#:use-module (ice-9 iconv) |
|
#:use-module (rnrs bytevectors) |
|
#:use-module (test-suite lib)) |
|
|
|
|
|
(define exception:encoding-error |
|
'(encoding-error . "")) |
|
|
|
(define exception:decoding-error |
|
'(decoding-error . "")) |
|
|
|
|
|
(with-test-prefix "ascii string" |
|
(let ((s "Hello, World!")) |
|
|
|
|
|
(pass-if "to ascii bytevector" |
|
(equal? (string->bytevector s "ASCII") |
|
#vu8(72 101 108 108 111 44 32 87 111 114 108 100 33))) |
|
|
|
(pass-if "to ascii bytevector (length check)" |
|
(equal? (string-length s) |
|
(bytevector-length (string->bytevector s "ascii")))) |
|
|
|
(pass-if "from ascii bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "ascii") "ascii"))) |
|
|
|
(pass-if "to utf-8 bytevector" |
|
(equal? (string->bytevector s "ASCII") |
|
(string->bytevector s "utf-8"))) |
|
|
|
(pass-if "to UTF-8 bytevector (testing encoding case sensitivity)" |
|
(equal? (string->bytevector s "ascii") |
|
(string->bytevector s "UTF-8"))) |
|
|
|
(pass-if "from utf-8 bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "utf-8") "utf-8"))) |
|
|
|
(pass-if "to latin1 bytevector" |
|
(equal? (string->bytevector s "ASCII") |
|
(string->bytevector s "latin1"))) |
|
|
|
(pass-if "from latin1 bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "utf-8") "utf-8"))))) |
|
|
|
(with-test-prefix "narrow non-ascii string" |
|
(let ((s "été")) |
|
(pass-if "to latin1 bytevector" |
|
(equal? (string->bytevector s "latin1") |
|
#vu8(233 116 233))) |
|
|
|
(pass-if "to latin1 bytevector (length check)" |
|
(equal? (string-length s) |
|
(bytevector-length (string->bytevector s "latin1")))) |
|
|
|
(pass-if "from latin1 bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "latin1") "latin1"))) |
|
|
|
(pass-if "to utf-8 bytevector" |
|
(equal? (string->bytevector s "utf-8") |
|
#vu8(195 169 116 195 169))) |
|
|
|
(pass-if "from utf-8 bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "utf-8") "utf-8"))) |
|
|
|
(pass-if-exception "encode latin1 as ascii" exception:encoding-error |
|
(string->bytevector s "ascii")) |
|
|
|
(pass-if-exception "misparse latin1 as utf8" exception:decoding-error |
|
(bytevector->string (string->bytevector s "latin1") "utf-8")) |
|
|
|
(pass-if "misparse latin1 as utf8 with substitutions" |
|
(equal? (bytevector->string (string->bytevector s "latin1") |
|
"utf-8" 'substitute) |
|
"\uFFFDt\uFFFD")) |
|
|
|
(pass-if-exception "misparse latin1 as ascii" exception:decoding-error |
|
(bytevector->string (string->bytevector s "latin1") "ascii")))) |
|
|
|
|
|
(with-test-prefix "wide non-ascii string" |
|
(let ((s "ΧΑΟΣ")) |
|
(pass-if "to utf-8 bytevector" |
|
(equal? (string->bytevector s "utf-8") |
|
#vu8(206 167 206 145 206 159 206 163) )) |
|
|
|
(pass-if "from utf-8 bytevector" |
|
(equal? s |
|
(bytevector->string (string->bytevector s "utf-8") "utf-8"))) |
|
|
|
(pass-if-exception "encode as ascii" exception:encoding-error |
|
(string->bytevector s "ascii")) |
|
|
|
(pass-if-exception "encode as latin1" exception:encoding-error |
|
(string->bytevector s "latin1")) |
|
|
|
(pass-if "encode as ascii with substitutions" |
|
(equal? (make-string (string-length s) #\?) |
|
(bytevector->string (string->bytevector s "ascii" 'substitute) |
|
"ascii"))))) |
|
|