;;;; string-utils-test.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '18 ;;;; Kon Lovett, Oct '17 (import test) (test-begin "String Utils") ;;; (import unicode-utils to-hex string-hexadecimal) (test-group "Unicode" (test-assert (ascii-codepoint? #\a)) (test "abc" (unicode-string #\a #\b #\c)) (test "cebb" (string->hex (char->unicode-string #\u03BB))) (test "cebbcebbcebb" (string->hex (unicode-string #\u03BB #\u03BB #\u03BB))) (test "cebbcebb" (string->hex (unicode-make-string 2 #\u03BB))) ) (import memoized-string) (test-group "Memoized" (test "a" (make-string+ 1 #\a)) (define a5 (make-string+ 5 #\a)) (test "aaaaa" a5) (define spc5 (make-string+ 5)) (test " " spc5) (test-assert (eq? a5 (make-string+ 5 #\a))) (test-assert (eq? spc5 (make-string+ 5 #\space))) (define tststr1 (string+ #\我 #\你)) (test "(string+ #\\我 #\\你)" tststr1 "我你") (test-assert (eq? tststr1 (string+ #\我 #\你))) (define tststr2 "上海的东西很便宜") (test-assert (eq? tststr2 (global-string tststr2))) ) (import string-hexadecimal (chicken blob) (srfi 4)) (test-group "To Hex" (let ((t (make-string (* 2 3)))) (blob_to_hex t (string->blob "12abc34") 2 5) (test "616263" t) ) ; (let ((t (make-string (* 2 3)))) (u8vec_to_hex t (u8vector 1 2 #x61 #x62 #x63 3 4) 2 5) (test "616263" t) ) ; (let ((t (make-string (* 2 2)))) (s8vec_to_hex t (s8vector 1 2 -45 -54 3 4) 2 4) (test "d3ca" t) ) ) (test-group "String -> Hex" (test "616263" (string->hex "12abc34" 2 5)) (test "414243444546" (string->hex "ABCDEF")) (test "4243444546" (string->hex "ABCDEF" 1)) (test "4243" (string->hex "ABCDEF" 1 3)) ) (test-group "Hex -> String" (test "abc" (hex->string "616263")) (test "ABCDEF" (hex->string "414243444546")) (test "BCDEF" (hex->string "4243444546")) (test "BC" (hex->string "4243")) (test "jkL]" (hex->string "6a6B4c5D")) (test-error (hex->string "424")) ) ;; (import string-interpolation) (test-group "String Interpolation" (let ((res '(##sys#print-to-string (list "foo " (+ 1 2) " bar")))) (test res (string-interpolate "foo #(+ 1 2) bar")) (test res (string-interpolate "foo #(+ 1 2) bar")) (test res (string-interpolate "foo ${(+ 1 2)} bar" eval-tag: #\$)) (test res (string-interpolate "foo $(+ 1 2) bar" eval-tag: #\$)) ) ) (import (prefix utf8-string-interpolation utf8::)) (test-group "String Interpolation (UTF-8)" (let ((res '(##sys#print-to-string (list "听说上海的 " (+ 1 2) " 东西很贵")))) (test res (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵")) (test res (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵")) (test res (utf8::string-interpolate "听说上海的 ${(+ 1 2)} 东西很贵" eval-tag: #\$)) (test res (utf8::string-interpolate "听说上海的 $(+ 1 2) 东西很贵" eval-tag: #\$)) ) ) (import string-interpolation-syntax (chicken port)) ;must be "top level"; cannot be test-group (test-begin "String Interpolation Syntax") (set-sharp-string-interpolation-syntax string-interpolate) (test '((##sys#print-to-string (list "foo " (+ 1 2) " bar"))) (list (call-with-input-string "#\"foo #{(+ 1 2)} bar\"" read))) (set-sharp-string-interpolation-syntax #f) (set-sharp-string-interpolation-syntax (cute string-interpolate <> eval-tag: #\$)) (test '((##sys#print-to-string (list "foo " (+ 1 2) " bar"))) (list (call-with-input-string "#\"foo ${(+ 1 2)} bar\"" read))) (set-sharp-string-interpolation-syntax #f) (test-end "String Interpolation Syntax") ;; (import string-utils) (test-group "string-utils" (import utf8) (test "foo" (string-fixed-length "abcde" 1 #:trailing "foo")) (test "a..." (string-fixed-length "abcde" 4)) (test "abc " (string-fixed-length "abc" 4)) (test "👤..." (string-fixed-length "👤👩👨📷📺" 4)) (test "👤👩🎑🎍" (string-fixed-length "👤👩👨📷📺" 4 #:trailing "🎑🎍")) #; ;FIXME char is not 24-bit (test "👤👩👨📷📺🎋" (string-fixed-length "👤👩👨📷📺" 6 #:pad-char #\U0001F38B)) (let ((strs '("foobaz" "foobar" "barbaz" "barfoo" "bazfoo"))) (test "foobar" (string-longest-common-prefix "foobarbaz" strs)) #; ;BUG '("ba" "bar" "fooba") should be '(bar" "fooba") ! (test '("ba" "bar" "fooba") (string-longest-common-prefixes strs)) ) ) ;;; (test-end "String Utils") (test-exit)