;;;; string-utils-test.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '18 ;;;; Kon Lovett, Oct '17 (import test) (test-begin "String Utils") ;;; (import string-utils) (test-group "Unicode" (test-assert (ascii-codepoint? #\a)) (test "abc" (unicode-string #\a #\b #\c)) (test "cebb" (string->hex (unicode-char->string #\U03BB))) (test "cebbcebbcebb" (string->hex (unicode-string #\U03BB #\U03BB #\U03BB))) (test "cebbcebb" (string->hex (unicode-make-string 2 #\U03BB))) ) (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 (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) ) ) (import string-hexadecimal) (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" (test "foo 3 bar" (string-interpolate "foo #(+ 1 2) bar")) (test "foo 3 bar" (string-interpolate "foo #(+ 1 2) bar" eval-env: (scheme-report-environment 5))) (test "foo 3 bar" (string-interpolate "foo ${(+ 1 2)} bar" eval-tag: #\$)) (test "foo 3 bar" (string-interpolate "foo $(+ 1 2) bar" eval-tag: #\$ eval-env: (scheme-report-environment 5))) ) #| (import (prefix utf8-string-interpolation utf8::)) (test-group "String Interpolation (UTF-8)" (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵")) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 #(+ 1 2) 东西很贵" eval-env: (scheme-report-environment 5))) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 ${(+ 1 2)} 东西很贵" eval-tag: #\$)) (test "听说上海的 3 东西很贵" (utf8::string-interpolate "听说上海的 $(+ 1 2) 东西很贵" eval-tag: #\$ eval-env: (scheme-report-environment 5))) ) |# (import (chicken port) string-interpolation-syntax) ;must be "top level"; cannot be test-group (test-begin "String Interpolation SYNTAX") (set-sharp-string-interpolation-syntax string-interpolate) (test '("foo 3 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: #\$ eval-env: (scheme-report-environment 5))) (test '("foo 3 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-extensions) (test-group "string-utils-extensions" (let* ( (str "12345f") (res (string-copy-over! "abcde" str)) ) (test-assert "string-copy-over! return" (eq? str res)) (test "string-copy-over! operation" "abcdef" res) ) (test "23" (string-copy* "12345f" 1 3)) (test "2345f0000" (string-copy* "12345f" 1 10 #\0)) (test 0 (string-count* (lambda (a b) (char=? a b)) "ac" "bdq")) (test 2 (string-count* (lambda (a b) (char=? a b)) "aca" "adaq")) (test #\z (string-any* (lambda (a b) (and (char=? a b) a)) "aczq" "bdz")) (test #\z (string-every* (lambda (a b) (and (char=? a b) a)) "az" "azq")) ) |# ;;; (test-end "String Utils") (test-exit)