From: Chris Hanson Date: Sat, 16 Mar 2019 08:10:52 +0000 (-0700) Subject: Add test to make sure that string slices print correctly. X-Git-Tag: mit-scheme-pucked-10.1.11~6^2~33 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=0a7ea142030afd2e9fa0b6e41cd59de13f42b31d;p=mit-scheme.git Add test to make sure that string slices print correctly. --- diff --git a/tests/runtime/test-string.scm b/tests/runtime/test-string.scm index 311e41459..da7788586 100644 --- a/tests/runtime/test-string.scm +++ b/tests/runtime/test-string.scm @@ -3093,17 +3093,20 @@ USA. (= "aaafoo" (string-trim-right "aaafooaaa" (char-set #\f #\o))))) (define (string-copy!:all-indices to from) - (let ((to-length (string-length to)) - (from-limit (+ (string-length from) 1))) + (let ((to-length (string-length to))) (append-map (lambda (s+e) (map (lambda (at) (cons at s+e)) (iota (- to-length (- (cadr s+e) (car s+e)))))) - (append-map (lambda (start) - (map (lambda (end) - (list start end)) - (iota (- from-limit start) start))) - (iota from-limit))))) + (substring:all-indices from)))) + +(define (substring:all-indices string) + (let ((limit (+ (string-length string) 1))) + (append-map (lambda (start) + (map (lambda (end) + (list start end)) + (iota (- limit start) start))) + (iota limit)))) (define-test 'string-copy!:different-strings (let ((s1 "abcdefghijklmnopqrstuvwxyz") @@ -3136,15 +3139,29 @@ USA. 'expression expr)))))) (string-copy!:all-indices s1 s1)))) -(define (maybe-expect-failure expect-failure? body) - (if expect-failure? - (expect-failure body) - (body))) - (define (string-copy!:predict s1 at s2 #!optional start end) (list->string (let ((l1 (string->list s1)) (l2 (string->list s2 start end))) (append (list-head l1 at) l2 - (list-tail l1 (+ at (length l2))))))) \ No newline at end of file + (list-tail l1 (+ at (length l2))))))) + +(define-test 'string-slice + (let ((s "abcdefghijklmnopqrstuvwxyz")) + (map (lambda (indices) + (let ((start (car indices)) + (end (cadr indices))) + (let ((expr `(string-slice ,s ,start ,end))) + (lambda () + (let ((sut (string-slice s start end)) + (expected (substring:predict s start end))) + (assert-string= sut expected 'expression expr) + (assert-string= (call-with-output-string + (lambda (port) + (write sut port))) + (string-append "\"" expected "\""))))))) + (substring:all-indices s)))) + +(define (substring:predict s i j) + (list->string (sublist (string->list s) i j))) \ No newline at end of file