code:
;;;; srfi-13.test --- Test suite for Guile's SRFI-13 functions. -*- scheme -*-
;;;; Martin Grabmueller, 2001-05-07
;;;;
;;;; Copyright (C) 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-strings)
  #:use-module (test-suite lib)
  #:use-module (srfi srfi-13)
  #:use-module (srfi srfi-14))

(define exception:strict-infix-grammar
  (cons 'misc-error "^strict-infix"))

;; Create a string from integer char values, eg. (string-ints 65) => "A"
(define (string-ints . args)
  (apply string (map integer->char args)))

;; Some abbreviations
;; BMP - Basic Multilingual Plane (codepoints below U+FFFF)
;; SMP - Suplementary Multilingual Plane (codebpoints from U+10000 to U+1FFFF)

;;;
;;; string-any
;;;

(with-test-prefix "string-any"

  (pass-if "null string"
    (not (string-any #\a "")))

  (pass-if "start index == end index"
    (not (string-any #\a "aaa" 1 1)))

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-any 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-any "zzz" "abcde")))

  (with-test-prefix "char"

    (pass-if "no match"
      (not (string-any #\C "abcde")))

    (pass-if "one match"
      (string-any #\C "abCde"))

    (pass-if "one match: BMP"
      (string-any (integer->char #x0100) "ab\u0100de"))

    (pass-if "one match: SMP"
      (string-any (integer->char #x010300) "ab\U010300de"))

    (pass-if "more than one match"
      (string-any #\X "abXXX"))

    (pass-if "no match, start index"
      (not (string-any #\A "Abcde" 1)))

    (pass-if "one match, start index"
      (string-any #\C "abCde" 1))

    (pass-if "more than one match, start index"
      (string-any #\X "abXXX" 1))

    (pass-if "no match, start and end index"
      (not (string-any #\X "XbcdX" 1 4)))

    (pass-if "one match, start and end index"
      (string-any #\C "abCde" 1 4))

    (pass-if "more than one match, start and end index"
      (string-any #\X "abXXX" 1 4)))

  (with-test-prefix "charset"

    (pass-if "no match"
      (not (string-any char-set:upper-case "abcde")))

    (pass-if "one match"
      (string-any char-set:upper-case "abCde"))

    (pass-if "more than one match"
      (string-any char-set:upper-case "abCDE"))

    (pass-if "no match, start index"
      (not (string-any char-set:upper-case "Abcde" 1)))

    (pass-if "one match, start index"
      (string-any char-set:upper-case "abCde" 1))

    (pass-if "more than one match, start index"
      (string-any char-set:upper-case "abCDE" 1))

    (pass-if "no match, start and end index"
      (not (string-any char-set:upper-case "AbcdE" 1 4)))

    (pass-if "one match, start and end index"
      (string-any char-set:upper-case "abCde" 1 4))

    (pass-if "more than one match, start and end index"
      (string-any char-set:upper-case "abCDE" 1 4)))

  (with-test-prefix "pred"

    (pass-if "no match"
      (not (string-any char-upper-case? "abcde")))

    (pass-if "one match"
      (string-any char-upper-case? "abCde"))

    (pass-if "more than one match"
      (string-any char-upper-case? "abCDE"))

    (pass-if "no match, start index"
      (not (string-any char-upper-case? "Abcde" 1)))

    (pass-if "one match, start index"
      (string-any char-upper-case? "abCde" 1))

    (pass-if "more than one match, start index"
      (string-any char-upper-case? "abCDE" 1))

    (pass-if "no match, start and end index"
      (not (string-any char-upper-case? "AbcdE" 1 4)))

    (pass-if "one match, start and end index"
      (string-any char-upper-case? "abCde" 1 4))

    (pass-if "more than one match, start and end index"
      (string-any char-upper-case? "abCDE" 1 4))))

;;;
;;; string-titlecase
;;;

(with-test-prefix "string-titlecase"

  (pass-if "all-lower"
    (string=? "Foo" (string-titlecase "foo")))

  (pass-if "all-upper"
    (string=? "Foo" (string-titlecase "FOO")))

  (pass-if "two-words"
    (string=? "Hello, World!" (string-titlecase "hello, world!")))

  (pass-if "titlecase-characters"
    (string=? (list->string '(#\762))
	      (string-titlecase (list->string '(#\763))))))

;;;
;;; string-append/shared
;;;

(with-test-prefix "string-append/shared"

  (pass-if "no args"
    (string=? "" (string-append/shared)))

  (with-test-prefix "one arg"
    (pass-if "empty"
      (string=? "" (string-append/shared "")))
    (pass-if "non-empty"
      (string=? "xyz" (string-append/shared "xyz"))))

  (with-test-prefix "two args"
    (pass-if (string=? ""       (string-append/shared ""    "")))
    (pass-if (string=? "xyz"    (string-append/shared "xyz" "")))
    (pass-if (string=? "xyz"    (string-append/shared ""    "xyz")))
    (pass-if (string=? "abcxyz" (string-append/shared "abc" "xyz")))
    (pass-if (string=? "abc\u0100\u0101"
                       (string-append/shared "abc" "\u0100\u0101"))))

  (with-test-prefix "three args"
    (pass-if (string=? ""   	(string-append/shared ""   ""   "")))
    (pass-if (string=? "xy" 	(string-append/shared "xy" ""   "")))
    (pass-if (string=? "xy" 	(string-append/shared ""   "xy" "")))
    (pass-if (string=? "abxy"   (string-append/shared "ab" "xy" "")))
    (pass-if (string=? "ab"     (string-append/shared ""   ""   "ab")))
    (pass-if (string=? "xyab"   (string-append/shared "xy" ""   "ab")))
    (pass-if (string=? "xyab"   (string-append/shared ""   "xy" "ab")))
    (pass-if (string=? "ghxyab" (string-append/shared "gh" "xy" "ab"))))

  (with-test-prefix "four args"
    (pass-if (string=? ""   	(string-append/shared ""   ""   ""   "")))
    (pass-if (string=? "xy" 	(string-append/shared "xy" ""   ""   "")))
    (pass-if (string=? "xy" 	(string-append/shared ""   "xy" ""   "")))
    (pass-if (string=? "xy" 	(string-append/shared ""   ""   "xy" "")))
    (pass-if (string=? "xy" 	(string-append/shared ""   ""   ""   "xy")))

    (pass-if (string=? "abxy"   (string-append/shared "ab" "xy" ""   "")))
    (pass-if (string=? "abxy"   (string-append/shared "ab" ""   "xy" "")))
    (pass-if (string=? "abxy"   (string-append/shared "ab" ""   ""   "xy")))
    (pass-if (string=? "abxy"   (string-append/shared ""   "ab" ""   "xy")))
    (pass-if (string=? "abxy"   (string-append/shared ""   ""   "ab" "xy")))))

;;;
;;; string-concatenate
;;;

(with-test-prefix "string-concatenate"

  (pass-if-exception "inum" exception:wrong-type-arg
    (string-concatenate 123))

  (pass-if-exception "symbol" exception:wrong-type-arg
    (string-concatenate 'x))

  (pass-if-exception "improper 1" exception:wrong-type-arg
    (string-concatenate '("a" . "b")))

  (pass-if (equal? "abc" (string-concatenate '("a" "b" "c"))))

  (pass-if "concatenate BMP"
    (equal? "a\u0100" (string-concatenate '("a" "\u0100")))))

;;
;; string-compare
;;

(with-test-prefix "string-compare"

  (pass-if "same as char<?"
    (eq? (char<? (integer->char 0) (integer->char 255))
	 (string-compare (string-ints 0) (string-ints 255)
			 (lambda (pos) #t)     ;; lt
			 (lambda (pos) #f)     ;; eq
			 (lambda (pos) #f))))) ;; gt

;;
;; string-compare-ci
;;

(with-test-prefix "string-compare-ci"

  (pass-if "same as char-ci<?"
    (eq? (char-ci<? (integer->char 0) (integer->char 255))
	 (string-compare-ci (string-ints 0) (string-ints 255)
			 (lambda (pos) #t)     ;; lt
			 (lambda (pos) #f)     ;; eq
			 (lambda (pos) #f))))) ;; gt

;;;
;;; string-concatenate/shared
;;;

(with-test-prefix "string-concatenate/shared"

  (pass-if-exception "inum" exception:wrong-type-arg
    (string-concatenate/shared 123))

  (pass-if-exception "symbol" exception:wrong-type-arg
    (string-concatenate/shared 'x))

  (pass-if-exception "improper 1" exception:wrong-type-arg
    (string-concatenate/shared '("a" . "b")))

  (pass-if (equal? "abc" (string-concatenate/shared '("a" "b" "c"))))

  (pass-if "BMP"
    (equal? "a\u0100c" (string-concatenate/shared '("a" "\u0100" "c")))))

;;;
;;; string-every
;;;

(with-test-prefix "string-every"

  (pass-if "null string"
    (string-every #\a ""))

  (pass-if "start index == end index"
    (string-every #\a "bbb" 1 1))

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-every 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-every "zzz" "abcde")))

  (with-test-prefix "char"

    (pass-if "empty string"
      (string-every #\X ""))

    (pass-if "empty substring"
      (string-every #\X "abc" 1 1))

    (pass-if "no match at all"
      (not (string-every #\X "abcde")))

    (pass-if "not all match"
      (not (string-every #\X "abXXX")))

    (pass-if "all match"
      (string-every #\X "XXXXX"))

    (pass-if "all match BMP"
      (string-every #\200000 "\U010000\U010000"))

    (pass-if "no match at all, start index"
      (not (string-every #\X "Xbcde" 1)))

    (pass-if "not all match, start index"
      (not (string-every #\X "XXcde" 1)))

    (pass-if "all match, start index"
      (string-every #\X "aXXXX" 1))

    (pass-if "no match at all, start and end index"
      (not (string-every #\X "XbcdX" 1 4)))

    (pass-if "not all match, start and end index"
      (not (string-every #\X "XXcde" 1 4)))

    (pass-if "all match, start and end index"
      (string-every #\X "aXXXe" 1 4)))

  (with-test-prefix "charset"

    (pass-if "empty string"
      (string-every char-set:upper-case ""))

    (pass-if "empty substring"
      (string-every char-set:upper-case "abc" 1 1))

    (pass-if "no match at all"
      (not (string-every char-set:upper-case "abcde")))

    (pass-if "not all match"
      (not (string-every char-set:upper-case "abCDE")))

    (pass-if "all match"
      (string-every char-set:upper-case "ABCDE"))

    (pass-if "no match at all, start index"
      (not (string-every char-set:upper-case "Abcde" 1)))

    (pass-if "not all match, start index"
      (not (string-every char-set:upper-case "ABcde" 1)))

    (pass-if "all match, start index"
      (string-every char-set:upper-case "aBCDE" 1))

    (pass-if "no match at all, start and end index"
      (not (string-every char-set:upper-case "AbcdE" 1 4)))

    (pass-if "not all match, start and end index"
      (not (string-every char-set:upper-case "ABcde" 1 4)))

    (pass-if "all match, start and end index"
      (string-every char-set:upper-case "aBCDe" 1 4)))

  (with-test-prefix "pred"

    ;; in guile 1.6.4 and earlier string-every incorrectly returned #f on an
    ;; empty string
    (pass-if "empty string"
      (string-every char-upper-case? ""))
    (pass-if "empty substring"
      (string-every char-upper-case? "abc" 1 1))

    (pass-if "no match at all"
      (not (string-every char-upper-case? "abcde")))

    (pass-if "not all match"
      (not (string-every char-upper-case? "abCDE")))

    (pass-if "all match"
      (string-every char-upper-case? "ABCDE"))

    (pass-if "no match at all, start index"
      (not (string-every char-upper-case? "Abcde" 1)))

    (pass-if "not all match, start index"
      (not (string-every char-upper-case? "ABcde" 1)))

    (pass-if "all match, start index"
      (string-every char-upper-case? "aBCDE" 1))

    (pass-if "no match at all, start and end index"
      (not (string-every char-upper-case? "AbcdE" 1 4)))

    (pass-if "not all match, start and end index"
      (not (string-every char-upper-case? "ABcde" 1 4)))

    (pass-if "all match, start and end index"
      (string-every char-upper-case? "aBCDe" 1 4))))

(with-test-prefix "string-tabulate"

  (with-test-prefix "bad proc"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-tabulate 123 10))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-tabulate "zzz" 10)))

  (pass-if "static fill-char"
    (string=? (string-tabulate (lambda (idx) #\!) 10) "!!!!!!!!!!"))

  (pass-if "variable fill-char"
    (string=? (string-tabulate
	       (lambda (idx) (integer->char (+ idx 32))) 10) " !\"#$%&'()")))

(with-test-prefix "string->list"

  (pass-if "empty"
     (zero? (length (string->list ""))))

  (pass-if "nonempty"
     (= (length (string->list "foo")) 3))

  (pass-if "empty, start index"
     (zero? (length (string->list "foo" 3 3))))

   (pass-if "nonempty, start index"
     (= (length (string->list "foo" 1 3)) 2))

   (pass-if "nonempty, start index, BMP"
     (= (length (string->list "\xff\u0100\u0300" 1 3)) 2))
  )

(with-test-prefix "reverse-list->string"

  (pass-if "empty"
     (string-null? (reverse-list->string '())))

  (pass-if "nonempty"
     (string=? "foo" (reverse-list->string '(#\o #\o #\f))))

  (pass-if "nonempty, BMP"
     (string=? "\u0100\u0101\u0102" (reverse-list->string '(#\402 #\401 #\400)))))

(with-test-prefix "string-join"

  (pass-if "empty list, no delimiter, implicit infix, empty 1"
     (string=? "" (string-join '())))

  (pass-if "empty string, no delimiter, implicit infix, empty 2"
     (string=? "" (string-join '(""))))

  (pass-if "non-empty, no delimiter, implicit infix"
     (string=? "bla" (string-join '("bla"))))

  (pass-if "empty list, implicit infix, empty 1"
     (string=? "" (string-join '() "|delim|")))

  (pass-if "empty string, implicit infix, empty 2"
     (string=? "" (string-join '("") "|delim|")))

  (pass-if "non-empty, implicit infix"
     (string=? "bla" (string-join '("bla") "|delim|")))

  (pass-if "non-empty, implicit infix"
     (string=? "bla" (string-join '("bla") "|delim|")))

  (pass-if "two strings, implicit infix"
     (string=? "bla|delim|fasel" (string-join '("bla" "fasel") "|delim|")))

  (pass-if "empty, explicit infix"
     (string=? "" (string-join '("") "|delim|" 'infix)))

  (pass-if "empty list, explicit infix"
     (string=? "" (string-join '() "|delim|" 'infix)))

  (pass-if "non-empty, explicit infix"
     (string=? "bla" (string-join '("bla") "|delim|" 'infix)))

  (pass-if "two strings, explicit infix"
     (string=? "bla|delim|fasel" (string-join '("bla" "fasel") "|delim|"
					      'infix)))

  (pass-if "two strings, explicit infix, BMP"
     (string=? "\u0100\u0101::\u0102\u0103"
               (string-join '("\u0100\u0101" "\u0102\u0103") "::"
                            'infix)))

  (pass-if-exception "empty list, strict infix"
     exception:strict-infix-grammar
     (string-join '() "|delim|" 'strict-infix))

  (pass-if "empty, strict infix"
     (string=? "" (string-join '("") "|delim|" 'strict-infix)))

  (pass-if "non-empty, strict infix"
     (string=? "foo" (string-join '("foo") "|delim|" 'strict-infix)))

  (pass-if "two strings, strict infix"
     (string=? "foo|delim|bar" (string-join '("foo" "bar") "|delim|"
					    'strict-infix)))

  (pass-if "empty list, prefix"
     (string=? "" (string-join '() "|delim|" 'prefix)))

  (pass-if "empty, prefix"
     (string=? "|delim|" (string-join '("") "|delim|" 'prefix)))

  (pass-if "non-empty, prefix"
     (string=? "|delim|foo" (string-join '("foo") "|delim|" 'prefix)))

  (pass-if "two strings, prefix"
     (string=? "|delim|foo|delim|bar" (string-join '("foo" "bar") "|delim|"
						   'prefix)))

  (pass-if "empty list, suffix"
     (string=? "" (string-join '() "|delim|" 'suffix)))

  (pass-if "empty, suffix"
     (string=? "|delim|" (string-join '("") "|delim|" 'suffix)))

  (pass-if "non-empty, suffix"
     (string=? "foo|delim|" (string-join '("foo") "|delim|" 'suffix)))

  (pass-if "two strings, suffix"
     (string=? "foo|delim|bar|delim|" (string-join '("foo" "bar") "|delim|"
						   'suffix))))

(with-test-prefix "string-copy"

  (pass-if "empty string"
    (string=? "" (string-copy "")))

  (pass-if "full string"
    (string=? "foo-bar" (string-copy "foo-bar")))

  (pass-if "full string, BMP"
    (string=? "foo-\u0100\u0101" (string-copy "foo-\u0100\u0101")))

  (pass-if "start index"
    (string=? "o-bar" (string-copy "foo-bar" 2)))

  (pass-if "start index"
    (string=? "o-bar" (string-copy "\u0100\u0101o-bar" 2)))

  (pass-if "start and end index"
    (string=? "o-ba" (string-copy "foo-bar" 2 6)))
)

(with-test-prefix "substring/shared"

  (pass-if "empty string"
    (eq? "" (substring/shared "" 0)))

  (pass-if "non-empty string"
    (string=? "foo" (substring/shared "foo-bar" 0 3)))

  (pass-if "non-empty string, not eq?"
    (string=? "foo-bar" (substring/shared "foo-bar" 0 7))))

(with-test-prefix "string-copy!"

  (pass-if "non-empty string"
    (string=? "welld, oh yeah!"
	      (let* ((s "hello")
		     (t (string-copy "world, oh yeah!")))
		(string-copy! t 1 s 1 3)
		t))))

(with-test-prefix "string-take"

  (pass-if "empty string"
    (string=? "" (string-take "foo bar braz" 0)))

  (pass-if "non-empty string"
    (string=? "foo " (string-take "foo bar braz" 4)))

  (pass-if "non-empty string BMP"
    (string=? "\u0100oo " (string-take "\u0100oo \u0101ar braz" 4)))

  (pass-if "full string"
    (string=? "foo bar braz" (string-take "foo bar braz" 12))))

(with-test-prefix "string-take-right"

  (pass-if "empty string"
    (string=? "" (string-take-right "foo bar braz" 0)))

  (pass-if "non-empty string"
    (string=? "braz" (string-take-right "foo bar braz" 4)))

  (pass-if "non-empty string"
    (string=? "braz" (string-take-right "foo ba\u0100 braz" 4)))

  (pass-if "full string"
    (string=? "foo bar braz" (string-take-right "foo bar braz" 12))))

(with-test-prefix "string-drop"

  (pass-if "empty string"
    (string=? "" (string-drop "foo bar braz" 12)))

  (pass-if "non-empty string"
    (string=? "braz" (string-drop "foo bar braz" 8)))

  (pass-if "non-empty string BMP"
    (string=? "braz" (string-drop "foo \u0100\u0101\u0102 braz" 8)))

  (pass-if "full string"
    (string=? "foo bar braz" (string-drop "foo bar braz" 0))))

(with-test-prefix "string-drop-right"

  (pass-if "empty string"
    (string=? "" (string-drop-right "foo bar braz" 12)))

  (pass-if "non-empty string"
    (string=? "foo " (string-drop-right "foo bar braz" 8)))

  (pass-if "non-empty string BMP"
    (string=? "foo " (string-drop-right "foo \u0100\u0101\u0102 braz" 8)))

  (pass-if "full string"
    (string=? "foo bar braz" (string-drop-right "foo bar braz" 0))))

(with-test-prefix "string-pad"

  (pass-if "empty string, zero pad"
    (string=? "" (string-pad "" 0)))

  (pass-if "empty string, zero pad, pad char"
    (string=? "" (string-pad "" 0)))

  (pass-if "empty pad string, 2 pad "
    (string=? "  " (string-pad "" 2)))

  (pass-if "empty pad string, 2 pad, pad char"
    (string=? "!!" (string-pad "" 2 #\!)))

  (pass-if "empty pad string, 2 pad, pad char, start index"
    (string=? "!c" (string-pad "abc" 2 #\! 2)))

  (pass-if "empty pad string, 2 pad, pad char, start and end index"
    (string=? "!c" (string-pad "abcd" 2 #\! 2 3)))

  (pass-if "freestyle 1"
    (string=? "32" (string-pad (number->string 532) 2 #\!)))

  (pass-if "freestyle 2"
    (string=? "!532" (string-pad (number->string 532) 4 #\!))))

(with-test-prefix "string-pad-right"

  (pass-if "empty string, zero pad"
    (string=? "" (string-pad-right "" 0)))

  (pass-if "empty string, zero pad, pad char"
    (string=? "" (string-pad-right "" 0)))

  (pass-if "empty pad string, 2 pad "
    (string=? "  " (string-pad-right "" 2)))

  (pass-if "empty pad string, 2 pad, pad char"
    (string=? "!!" (string-pad-right "" 2 #\!)))

  (pass-if "empty pad string, 2 pad, pad char, start index"
    (string=? "c!" (string-pad-right "abc" 2 #\! 2)))

  (pass-if "empty pad string, 2 pad, pad char, start and end index"
    (string=? "c!" (string-pad-right "abcd" 2 #\! 2 3)))

  (pass-if "freestyle 1"
    (string=? "53" (string-pad-right (number->string 532) 2 #\!)))

  (pass-if "freestyle 2"
    (string=? "532!" (string-pad-right (number->string 532) 4 #\!))))

(with-test-prefix "string-trim"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-trim "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-trim "abcde" "zzz")))

  (pass-if "empty string"
    (string=? "" (string-trim "")))

  (pass-if "no char/pred"
    (string=? "foo " (string-trim " \tfoo ")))

  (pass-if "start index, pred"
    (string=? "foo " (string-trim " \tfoo " char-whitespace? 1)))

  (pass-if "start and end index, pred"
    (string=? "f" (string-trim " \tfoo " char-whitespace? 1 3)))

  (pass-if "start index, char"
    (string=? "\tfoo " (string-trim " \tfoo " #\space 1)))

  (pass-if "start and end index, char"
    (string=? "\tf" (string-trim " \tfoo " #\space 1 3)))

  (pass-if "start index, charset"
    (string=? "foo " (string-trim " \tfoo " char-set:whitespace 1)))

  (pass-if "start and end index, charset"
    (string=? "f" (string-trim " \tfoo " char-set:whitespace 1 3))))

(with-test-prefix "string-trim-right"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-trim-right "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-trim-right "abcde" "zzz")))

  (pass-if "empty string"
    (string=? "" (string-trim-right "")))

  (pass-if "no char/pred"
    (string=? " \tfoo" (string-trim-right " \tfoo ")))

  (pass-if "start index, pred"
    (string=? "\tfoo" (string-trim-right " \tfoo " char-whitespace? 1)))

  (pass-if "start and end index, pred"
    (string=? "\tf" (string-trim-right " \tfoo " char-whitespace? 1 3)))

  (pass-if "start index, char"
    (string=? "\tfoo" (string-trim-right " \tfoo " #\space 1)))

  (pass-if "start and end index, char"
    (string=? "\tf" (string-trim-right " \tfoo " #\space 1 3)))

  (pass-if "start index, charset"
    (string=? "\tfoo" (string-trim-right " \tfoo " char-set:whitespace 1)))

  (pass-if "start and end index, charset"
    (string=? "\tf" (string-trim-right " \tfoo " char-set:whitespace 1 3))))

(with-test-prefix "string-trim-both"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-trim-both "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-trim-both "abcde" "zzz")))

  (pass-if "empty string"
    (string=? "" (string-trim-both "")))

  (pass-if "no char/pred"
    (string=? "foo" (string-trim-both " \tfoo ")))

  (pass-if "start index, pred"
    (string=? "foo" (string-trim-both " \tfoo " char-whitespace? 1)))

  (pass-if "start and end index, pred"
    (string=? "f" (string-trim-both " \tfoo " char-whitespace? 1 3)))

  (pass-if "start index, char"
    (string=? "\tfoo" (string-trim-both " \tfoo " #\space 1)))

  (pass-if "start and end index, char"
    (string=? "\tf" (string-trim-both " \tfoo " #\space 1 3)))

  (pass-if "start index, charset"
    (string=? "foo" (string-trim-both " \tfoo " char-set:whitespace 1)))

  (pass-if "start and end index, charset"
    (string=? "f" (string-trim-both " \tfoo " char-set:whitespace 1 3))))

(define s0 (make-string 200 #\!))
(define s1 (make-string 0 #\!))

(with-test-prefix "string-fill!"

  (pass-if "empty string, no indices"
    (string-fill! s1 #\*)
    (= (string-length s1) 0))

  (pass-if "empty string, start index"
    (string-fill! s1 #\* 0)
    (= (string-length s1) 0))

  (pass-if "empty string, start and end index"
    (string-fill! s1 #\* 0 0)
    (= (string-length s1) 0))

  (pass-if "no indices"
    (string-fill! s0 #\*)
    (char=? (string-ref s0 0) #\*))

  (pass-if "start index"
    (string-fill! s0 #\+ 10)
    (char=? (string-ref s0 11) #\+))

  (pass-if "start and end index"
    (string-fill! s0 #\| 12 20)
    (char=? (string-ref s0 13) #\|)))

(with-test-prefix "string-prefix-length"

  (pass-if "empty prefix"
    (= 0 (string-prefix-length "" "foo bar")))

  (pass-if "non-empty prefix - match"
    (= 3 (string-prefix-length "foo" "foo bar")))

  (pass-if "non-empty prefix - no match"
    (= 0 (string-prefix-length "bar" "foo bar"))))

(with-test-prefix "string-prefix-length-ci"

  (pass-if "empty prefix"
    (= 0 (string-prefix-length-ci "" "foo bar")))

  (pass-if "non-empty prefix - match"
    (= 3 (string-prefix-length-ci "fOo" "foo bar")))

  (pass-if "non-empty prefix - no match"
    (= 0 (string-prefix-length-ci "bAr" "foo bar"))))

(with-test-prefix "string-suffix-length"

  (pass-if "empty suffix"
    (= 0 (string-suffix-length "" "foo bar")))

  (pass-if "non-empty suffix - match"
    (= 3 (string-suffix-length "bar" "foo bar")))

  (pass-if "non-empty suffix - no match"
    (= 0 (string-suffix-length "foo" "foo bar"))))

(with-test-prefix "string-suffix-length-ci"

  (pass-if "empty suffix"
    (= 0 (string-suffix-length-ci "" "foo bar")))

  (pass-if "non-empty suffix - match"
    (= 3 (string-suffix-length-ci "bAr" "foo bar")))

  (pass-if "non-empty suffix - no match"
    (= 0 (string-suffix-length-ci "fOo" "foo bar"))))

(with-test-prefix "string-prefix?"

  (pass-if "empty prefix"
    (string-prefix? "" "foo bar"))

  (pass-if "non-empty prefix - match"
    (string-prefix? "foo" "foo bar"))

  (pass-if "non-empty prefix - no match"
    (not (string-prefix? "bar" "foo bar"))))

(with-test-prefix "string-prefix-ci?"

  (pass-if "empty prefix"
    (string-prefix-ci? "" "foo bar"))

  (pass-if "non-empty prefix - match"
    (string-prefix-ci? "fOo" "foo bar"))

  (pass-if "non-empty prefix - no match"
    (not (string-prefix-ci? "bAr" "foo bar"))))

(with-test-prefix "string-suffix?"

  (pass-if "empty suffix"
    (string-suffix? "" "foo bar"))

  (pass-if "non-empty suffix - match"
    (string-suffix? "bar" "foo bar"))

  (pass-if "non-empty suffix - no match"
    (not (string-suffix? "foo" "foo bar"))))

(with-test-prefix "string-suffix-ci?"

  (pass-if "empty suffix"
    (string-suffix-ci? "" "foo bar"))

  (pass-if "non-empty suffix - match"
    (string-suffix-ci? "bAr" "foo bar"))

  (pass-if "non-empty suffix - no match"
    (not (string-suffix-ci? "fOo" "foo bar"))))

(with-test-prefix "string-index"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-index "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-index "abcde" "zzz")))

  (pass-if "empty string - char"
    (not (string-index "" #\a)))

  (pass-if "non-empty - char - match"
    (= 5 (string-index "foo bar" #\a)))

  (pass-if "non-empty - char - no match"
    (not (string-index "frobnicate" #\x)))

  (pass-if "empty string - char - start index"
    (not (string-index "" #\a 0)))

  (pass-if "non-empty - char - match - start index"
    (= 5 (string-index "foo bar" #\a 1)))

  (pass-if "non-empty - char - no match - start index"
    (not (string-index "frobnicate" #\x 2)))

  (pass-if "empty string - char - start and end index"
    (not (string-index "" #\a 0 0)))

  (pass-if "non-empty - char - match - start and end index"
    (= 5 (string-index "foo bar" #\a 1 6)))

  (pass-if "non-empty - char - no match - start and end index"
    (not (string-index "frobnicate" #\a 2 5)))

  (pass-if "empty string - charset"
    (not (string-index "" char-set:letter)))

  (pass-if "non-empty - charset - match"
    (= 0 (string-index "foo bar" char-set:letter)))

  (pass-if "non-empty - charset - no match"
    (not (string-index "frobnicate" char-set:digit)))

  (pass-if "empty string - charset - start index"
    (not (string-index "" char-set:letter 0)))

  (pass-if "non-empty - charset - match - start index"
    (= 1 (string-index "foo bar" char-set:letter 1)))

  (pass-if "non-empty - charset - no match - start index"
    (not (string-index "frobnicate" char-set:digit 2)))

  (pass-if "empty string - charset - start and end index"
    (not (string-index "" char-set:letter 0 0)))

  (pass-if "non-empty - charset - match - start and end index"
    (= 1 (string-index "foo bar" char-set:letter 1 6)))

  (pass-if "non-empty - charset - no match - start and end index"
    (not (string-index "frobnicate" char-set:digit 2 5)))

  (pass-if "empty string - pred"
    (not (string-index "" char-alphabetic?)))

  (pass-if "non-empty - pred - match"
    (= 0 (string-index "foo bar" char-alphabetic?)))

  (pass-if "non-empty - pred - no match"
    (not (string-index "frobnicate" char-numeric?)))

  (pass-if "empty string - pred - start index"
    (not (string-index "" char-alphabetic? 0)))

  (pass-if "non-empty - pred - match - start index"
    (= 1 (string-index "foo bar" char-alphabetic? 1)))

  (pass-if "non-empty - pred - no match - start index"
    (not (string-index "frobnicate" char-numeric? 2)))

  (pass-if "empty string - pred - start and end index"
    (not (string-index "" char-alphabetic? 0 0)))

  (pass-if "non-empty - pred - match - start and end index"
    (= 1 (string-index "foo bar" char-alphabetic? 1 6)))

  (pass-if "non-empty - pred - no match - start and end index"
    (not (string-index "frobnicate" char-numeric? 2 5)))

  ;; in guile 1.6.7 and earlier this resulted in a segv, because
  ;; SCM_MAKE_CHAR didn't cope with "signed char" arguments containing an
  ;; 8-bit value
  (pass-if "8-bit char in string"
    (begin
      (string-index (string (integer->char 200)) char-numeric?)
      #t)))

(with-test-prefix "string-index-right"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-index-right "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-index-right "abcde" "zzz")))

  (pass-if "empty string - char"
    (not (string-index-right "" #\a)))

  (pass-if "non-empty - char - match"
    (= 5 (string-index-right "foo bar" #\a)))

  (pass-if "non-empty - char - no match"
    (not (string-index-right "frobnicate" #\x)))

  (pass-if "empty string - char - start index-right"
    (not (string-index-right "" #\a 0)))

  (pass-if "non-empty - char - match - start index"
    (= 5 (string-index-right "foo bar" #\a 1)))

  (pass-if "non-empty - char - no match - start index"
    (not (string-index-right "frobnicate" #\x 2)))

  (pass-if "empty string - char - start and end index"
    (not (string-index-right "" #\a 0 0)))

  (pass-if "non-empty - char - match - start and end index"
    (= 5 (string-index-right "foo bar" #\a 1 6)))

  (pass-if "non-empty - char - no match - start and end index"
    (not (string-index-right "frobnicate" #\a 2 5)))

  (pass-if "empty string - charset"
    (not (string-index-right "" char-set:letter)))

  (pass-if "non-empty - charset - match"
    (= 6 (string-index-right "foo bar" char-set:letter)))

  (pass-if "non-empty - charset - no match"
    (not (string-index-right "frobnicate" char-set:digit)))

  (pass-if "empty string - charset - start index"
    (not (string-index-right "" char-set:letter 0)))

  (pass-if "non-empty - charset - match - start index"
    (= 6 (string-index-right "foo bar" char-set:letter 1)))

  (pass-if "non-empty - charset - no match - start index"
    (not (string-index-right "frobnicate" char-set:digit 2)))

  (pass-if "empty string - charset - start and end index"
    (not (string-index-right "" char-set:letter 0 0)))

  (pass-if "non-empty - charset - match - start and end index"
    (= 5 (string-index-right "foo bar" char-set:letter 1 6)))

  (pass-if "non-empty - charset - no match - start and end index"
    (not (string-index-right "frobnicate" char-set:digit 2 5)))

  (pass-if "empty string - pred"
    (not (string-index-right "" char-alphabetic?)))

  (pass-if "non-empty - pred - match"
    (= 6 (string-index-right "foo bar" char-alphabetic?)))

  (pass-if "non-empty - pred - no match"
    (not (string-index-right "frobnicate" char-numeric?)))

  (pass-if "empty string - pred - start index"
    (not (string-index-right "" char-alphabetic? 0)))

  (pass-if "non-empty - pred - match - start index"
    (= 6 (string-index-right "foo bar" char-alphabetic? 1)))

  (pass-if "non-empty - pred - no match - start index"
    (not (string-index-right "frobnicate" char-numeric? 2)))

  (pass-if "empty string - pred - start and end index"
    (not (string-index-right "" char-alphabetic? 0 0)))

  (pass-if "non-empty - pred - match - start and end index"
    (= 5 (string-index-right "foo bar" char-alphabetic? 1 6)))

  (pass-if "non-empty - pred - no match - start and end index"
    (not (string-index-right "frobnicate" char-numeric? 2 5))))

(with-test-prefix "string-skip"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-skip "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-skip "abcde" "zzz")))

  (pass-if "empty string - char"
    (not (string-skip "" #\a)))

  (pass-if "non-empty - char - match"
    (= 0 (string-skip "foo bar" #\a)))

  (pass-if "non-empty - char - no match"
    (= 0 (string-skip "frobnicate" #\x)))

  (pass-if "empty string - char - start index"
    (not (string-skip "" #\a 0)))

  (pass-if "non-empty - char - match - start index"
    (= 1 (string-skip "foo bar" #\a 1)))

  (pass-if "non-empty - char - no match - start index"
    (= 2 (string-skip "frobnicate" #\x 2)))

  (pass-if "empty string - char - start and end index"
    (not (string-skip "" #\a 0 0)))

  (pass-if "non-empty - char - match - start and end index"
    (= 1 (string-skip "foo bar" #\a 1 6)))

  (pass-if "non-empty - char - no match - start and end index"
    (= 2 (string-skip "frobnicate" #\a 2 5)))

  (pass-if "empty string - charset"
    (not (string-skip "" char-set:letter)))

  (pass-if "non-empty - charset - match"
    (= 3 (string-skip "foo bar" char-set:letter)))

  (pass-if "non-empty - charset - no match"
    (= 0 (string-skip "frobnicate" char-set:digit)))

  (pass-if "empty string - charset - start index"
    (not (string-skip "" char-set:letter 0)))

  (pass-if "non-empty - charset - match - start index"
    (= 3 (string-skip "foo bar" char-set:letter 1)))

  (pass-if "non-empty - charset - no match - start index"
    (= 2 (string-skip "frobnicate" char-set:digit 2)))

  (pass-if "empty string - charset - start and end index"
    (not (string-skip "" char-set:letter 0 0)))

  (pass-if "non-empty - charset - match - start and end index"
    (= 3 (string-skip "foo bar" char-set:letter 1 6)))

  (pass-if "non-empty - charset - no match - start and end index"
    (= 2 (string-skip "frobnicate" char-set:digit 2 5)))

  (pass-if "empty string - pred"
    (not (string-skip "" char-alphabetic?)))

  (pass-if "non-empty - pred - match"
    (= 3 (string-skip "foo bar" char-alphabetic?)))

  (pass-if "non-empty - pred - no match"
    (= 0 (string-skip "frobnicate" char-numeric?)))

  (pass-if "empty string - pred - start index"
    (not (string-skip "" char-alphabetic? 0)))

  (pass-if "non-empty - pred - match - start index"
    (= 3 (string-skip "foo bar" char-alphabetic? 1)))

  (pass-if "non-empty - pred - no match - start index"
    (= 2 (string-skip "frobnicate" char-numeric? 2)))

  (pass-if "empty string - pred - start and end index"
    (not (string-skip "" char-alphabetic? 0 0)))

  (pass-if "non-empty - pred - match - start and end index"
    (= 3 (string-skip "foo bar" char-alphabetic? 1 6)))

  (pass-if "non-empty - pred - no match - start and end index"
    (= 2 (string-skip "frobnicate" char-numeric? 2 5))))

(with-test-prefix "string-skip-right"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-skip-right "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-skip-right "abcde" "zzz")))

  (pass-if "empty string - char"
    (not (string-skip-right "" #\a)))

  (pass-if "non-empty - char - match"
    (= 6 (string-skip-right "foo bar" #\a)))

  (pass-if "non-empty - char - no match"
    (= 9 (string-skip-right "frobnicate" #\x)))

  (pass-if "empty string - char - start index-right"
    (not (string-skip-right "" #\a 0)))

  (pass-if "non-empty - char - match - start index"
    (= 6 (string-skip-right "foo bar" #\a 1)))

  (pass-if "non-empty - char - no match - start index"
    (= 9 (string-skip-right "frobnicate" #\x 2)))

  (pass-if "empty string - char - start and end index"
    (not (string-skip-right "" #\a 0 0)))

  (pass-if "non-empty - char - match - start and end index"
    (= 4 (string-skip-right "foo bar" #\a 1 6)))

  (pass-if "non-empty - char - no match - start and end index"
    (= 4 (string-skip-right "frobnicate" #\a 2 5)))

  (pass-if "empty string - charset"
    (not (string-skip-right "" char-set:letter)))

  (pass-if "non-empty - charset - match"
    (= 3 (string-skip-right "foo bar" char-set:letter)))

  (pass-if "non-empty - charset - no match"
    (= 9 (string-skip-right "frobnicate" char-set:digit)))

  (pass-if "empty string - charset - start index"
    (not (string-skip-right "" char-set:letter 0)))

  (pass-if "non-empty - charset - match - start index"
    (= 3 (string-skip-right "foo bar" char-set:letter 1)))

  (pass-if "non-empty - charset - no match - start index"
    (= 9 (string-skip-right "frobnicate" char-set:digit 2)))

  (pass-if "empty string - charset - start and end index"
    (not (string-skip-right "" char-set:letter 0 0)))

  (pass-if "non-empty - charset - match - start and end index"
    (= 3 (string-skip-right "foo bar" char-set:letter 1 6)))

  (pass-if "non-empty - charset - no match - start and end index"
    (= 4 (string-skip-right "frobnicate" char-set:digit 2 5)))

  (pass-if "empty string - pred"
    (not (string-skip-right "" char-alphabetic?)))

  (pass-if "non-empty - pred - match"
    (= 3 (string-skip-right "foo bar" char-alphabetic?)))

  (pass-if "non-empty - pred - no match"
    (= 9 (string-skip-right "frobnicate" char-numeric?)))

  (pass-if "empty string - pred - start index"
    (not (string-skip-right "" char-alphabetic? 0)))

  (pass-if "non-empty - pred - match - start index"
    (= 3 (string-skip-right "foo bar" char-alphabetic? 1)))

  (pass-if "non-empty - pred - no match - start index"
    (= 9 (string-skip-right "frobnicate" char-numeric? 2)))

  (pass-if "empty string - pred - start and end index"
    (not (string-skip-right "" char-alphabetic? 0 0)))

  (pass-if "non-empty - pred - match - start and end index"
    (= 3 (string-skip-right "foo bar" char-alphabetic? 1 6)))

  (pass-if "non-empty - pred - no match - start and end index"
    (= 4 (string-skip-right "frobnicate" char-numeric? 2 5))))

;;
;; string-count
;;

(with-test-prefix "string-count"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-count "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-count "abcde" "zzz")))

  (with-test-prefix "char"

    (pass-if (eqv? 0 (string-count "" #\a)))
    (pass-if (eqv? 0 (string-count "-" #\a)))
    (pass-if (eqv? 1 (string-count "a" #\a)))
    (pass-if (eqv? 0 (string-count "--" #\a)))
    (pass-if (eqv? 1 (string-count "a-" #\a)))
    (pass-if (eqv? 1 (string-count "-a" #\a)))
    (pass-if (eqv? 2 (string-count "aa" #\a)))
    (pass-if (eqv? 0 (string-count "---" #\a)))
    (pass-if (eqv? 1 (string-count "-a-" #\a)))
    (pass-if (eqv? 1 (string-count "a--" #\a)))
    (pass-if (eqv? 2 (string-count "aa-" #\a)))
    (pass-if (eqv? 2 (string-count "a-a" #\a)))
    (pass-if (eqv? 3 (string-count "aaa" #\a)))
    (pass-if (eqv? 1 (string-count "--a" #\a)))
    (pass-if (eqv? 2 (string-count "-aa" #\a))))

  (with-test-prefix "charset"

    (pass-if (eqv? 0 (string-count "" char-set:letter)))
    (pass-if (eqv? 0 (string-count "-" char-set:letter)))
    (pass-if (eqv? 1 (string-count "a" char-set:letter)))
    (pass-if (eqv? 0 (string-count "--" char-set:letter)))
    (pass-if (eqv? 1 (string-count "a-" char-set:letter)))
    (pass-if (eqv? 1 (string-count "-a" char-set:letter)))
    (pass-if (eqv? 2 (string-count "aa" char-set:letter)))
    (pass-if (eqv? 0 (string-count "---" char-set:letter)))
    (pass-if (eqv? 1 (string-count "-a-" char-set:letter)))
    (pass-if (eqv? 1 (string-count "a--" char-set:letter)))
    (pass-if (eqv? 2 (string-count "aa-" char-set:letter)))
    (pass-if (eqv? 2 (string-count "a-a" char-set:letter)))
    (pass-if (eqv? 3 (string-count "aaa" char-set:letter)))
    (pass-if (eqv? 1 (string-count "--a" char-set:letter)))
    (pass-if (eqv? 2 (string-count "-aa" char-set:letter))))

  (with-test-prefix "proc"

    (pass-if (eqv? 0 (string-count "" char-alphabetic?)))
    (pass-if (eqv? 0 (string-count "-" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "a" char-alphabetic?)))
    (pass-if (eqv? 0 (string-count "--" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "a-" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "-a" char-alphabetic?)))
    (pass-if (eqv? 2 (string-count "aa" char-alphabetic?)))
    (pass-if (eqv? 0 (string-count "---" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "-a-" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "a--" char-alphabetic?)))
    (pass-if (eqv? 2 (string-count "aa-" char-alphabetic?)))
    (pass-if (eqv? 2 (string-count "a-a" char-alphabetic?)))
    (pass-if (eqv? 3 (string-count "aaa" char-alphabetic?)))
    (pass-if (eqv? 1 (string-count "--a" char-alphabetic?)))
    (pass-if (eqv? 2 (string-count "-aa" char-alphabetic?)))))

(with-test-prefix "string-replace"

  (pass-if "empty string(s), no indices"
    (string=? "" (string-replace "" "")))

  (pass-if "empty string(s), 1 index"
    (string=? "" (string-replace "" "" 0)))

  (pass-if "empty string(s), 2 indices"
    (string=? "" (string-replace "" "" 0 0)))

  (pass-if "empty string(s), 3 indices"
    (string=? "" (string-replace "" "" 0 0 0)))

  (pass-if "empty string(s), 4 indices"
    (string=? "" (string-replace "" "" 0 0 0 0)))

  (pass-if "no indices"
    (string=? "uu" (string-replace "foo bar" "uu")))

  (pass-if "one index"
    (string=? "fuu" (string-replace "foo bar" "uu" 1)))

  (pass-if "two indices"
    (string=? "fuuar" (string-replace "foo bar" "uu" 1 5)))

  (pass-if "three indices"
    (string=? "fuar" (string-replace "foo bar" "uu" 1 5 1)))

  (pass-if "four indices"
    (string=? "fuar" (string-replace "foo bar" "uu" 1 5 1 2))))

(with-test-prefix "string-tokenize"

  (pass-if "empty string, no char/pred"
    (zero? (length (string-tokenize ""))))

  (pass-if "empty string, charset"
    (zero? (length (string-tokenize "" char-set:punctuation))))

  (pass-if "no char/pred"
    (equal? '("foo" "bar" "!a") (string-tokenize "foo\tbar !a")))

  (pass-if "charset"
    (equal? '("foo" "bar" "!a") (string-tokenize "foo\tbar !a"
						char-set:graphic)))

  (pass-if "charset, start index"
    (equal? '("oo" "bar" "!a") (string-tokenize "foo\tbar !a"
						char-set:graphic 1)))

  (pass-if "charset, start and end index"
    (equal? '("oo" "bar" "!") (string-tokenize "foo\tbar !a"
					       char-set:graphic 1 9))))
;;;
;;; string-filter
;;;

(with-test-prefix "string-filter"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-filter "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-filter "abcde" "zzz")))

  (pass-if "empty string, char"
    (string=? "" (string-filter "" #\.)))

  (pass-if "empty string, charset"
    (string=? "" (string-filter "" char-set:punctuation)))

  (pass-if "empty string, pred"
    (string=? "" (string-filter "" char-alphabetic?)))

  (pass-if "char"
    (string=? "..." (string-filter ".foo.bar." #\.)))

  (pass-if "charset"
    (string=? "..." (string-filter ".foo.bar." char-set:punctuation)))

  (pass-if "pred"
    (string=? "foobar" (string-filter ".foo.bar." char-alphabetic?)))

  (pass-if "char, start index"
    (string=? ".." (string-filter ".foo.bar." #\. 2)))

  (pass-if "charset, start index"
    (string=? ".." (string-filter ".foo.bar." char-set:punctuation 2)))

  (pass-if "pred, start index"
    (string=? "oobar" (string-filter ".foo.bar." char-alphabetic? 2)))

  (pass-if "char, start and end index"
    (string=? "" (string-filter ".foo.bar." #\. 2 4)))

  (pass-if "charset, start and end index"
    (string=? "" (string-filter ".foo.bar." char-set:punctuation 2 4)))

  (pass-if "pred, start and end index"
    (string=? "oo" (string-filter ".foo.bar." char-alphabetic? 2 4)))

  (with-test-prefix "char"

    (pass-if (equal? "x" (string-filter "x" #\x)))
    (pass-if (equal? "xx" (string-filter "xx" #\x)))
    (pass-if (equal? "xx" (string-filter "xyx" #\x)))
    (pass-if (equal? "x" (string-filter "xyyy" #\x)))
    (pass-if (equal? "x" (string-filter "yyyx" #\x)))

    (pass-if (equal? "xx" (string-filter "xxx" #\x 1)))
    (pass-if (equal? "xx" (string-filter "xxx" #\x 0 2)))
    (pass-if (equal? "x" (string-filter "xyx" #\x 1)))
    (pass-if (equal? "x" (string-filter "yxx" #\x 0 2)))

    ;; leading and trailing removals
    (pass-if (string=? "" (string-filter "." #\x)))
    (pass-if (string=? "" (string-filter ".." #\x)))
    (pass-if (string=? "" (string-filter "..." #\x)))
    (pass-if (string=? "x" (string-filter ".x" #\x)))
    (pass-if (string=? "x" (string-filter "..x" #\x)))
    (pass-if (string=? "x" (string-filter "...x" #\x)))
    (pass-if (string=? "x" (string-filter "x." #\x)))
    (pass-if (string=? "x" (string-filter "x.." #\x)))
    (pass-if (string=? "x" (string-filter "x..." #\x)))
    (pass-if (string=? "x" (string-filter "...x..." #\x))))

  (with-test-prefix "charset"

    (let ((charset (char-set #\x #\y)))
      (pass-if (equal? "x" (string-filter "x" charset)))
      (pass-if (equal? "xx" (string-filter "xx" charset)))
      (pass-if (equal? "xy" (string-filter "xy" charset)))
      (pass-if (equal? "x" (string-filter "xaaa" charset)))
      (pass-if (equal? "y" (string-filter "aaay" charset)))

      (pass-if (equal? "yx" (string-filter "xyx" charset 1)))
      (pass-if (equal? "xy" (string-filter "xyx" charset 0 2)))
      (pass-if (equal? "x" (string-filter "xax" charset 1)))
      (pass-if (equal? "x" (string-filter "axx" charset 0 2))))

    ;; leading and trailing removals
    (pass-if (string=? "" (string-filter "." char-set:letter)))
    (pass-if (string=? "" (string-filter ".." char-set:letter)))
    (pass-if (string=? "" (string-filter "..." char-set:letter)))
    (pass-if (string=? "x" (string-filter ".x" char-set:letter)))
    (pass-if (string=? "x" (string-filter "..x" char-set:letter)))
    (pass-if (string=? "x" (string-filter "...x" char-set:letter)))
    (pass-if (string=? "x" (string-filter "x." char-set:letter)))
    (pass-if (string=? "x" (string-filter "x.." char-set:letter)))
    (pass-if (string=? "x" (string-filter "x..." char-set:letter)))
    (pass-if (string=? "x" (string-filter "...x..." char-set:letter)))))

;;;
;;; string-delete
;;;

(with-test-prefix "string-delete"

  (with-test-prefix "bad char_pred"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-delete "abcde" 123))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-delete "abcde" "zzz")))

  (pass-if "empty string, char"
    (string=? "" (string-delete "" #\.)))

  (pass-if "empty string, charset"
    (string=? "" (string-delete "" char-set:punctuation)))

  (pass-if "empty string, pred"
    (string=? "" (string-delete "" char-alphabetic?)))

  (pass-if "char"
    (string=? "foobar" (string-delete ".foo.bar." #\.)))

  (pass-if "charset"
    (string=? "foobar" (string-delete ".foo.bar." char-set:punctuation)))

  (pass-if "pred"
    (string=? "..." (string-delete ".foo.bar." char-alphabetic?)))

  (pass-if "char, start index"
    (string=? "oobar" (string-delete ".foo.bar." #\. 2)))

  (pass-if "charset, start index"
    (string=? "oobar" (string-delete ".foo.bar." char-set:punctuation 2)))

  (pass-if "pred, start index"
    (string=? ".." (string-delete ".foo.bar." char-alphabetic? 2)))

  (pass-if "char, start and end index"
    (string=? "oo" (string-delete ".foo.bar." #\. 2 4)))

  (pass-if "charset, start and end index"
    (string=? "oo" (string-delete ".foo.bar." char-set:punctuation 2 4)))

  (pass-if "pred, start and end index"
    (string=? "" (string-delete ".foo.bar." char-alphabetic? 2 4)))

  ;; leading and trailing removals
  (pass-if (string=? "" (string-delete "." #\.)))
  (pass-if (string=? "" (string-delete ".." #\.)))
  (pass-if (string=? "" (string-delete "..." #\.)))
  (pass-if (string=? "x" (string-delete ".x" #\.)))
  (pass-if (string=? "x" (string-delete "..x" #\.)))
  (pass-if (string=? "x" (string-delete "...x" #\.)))
  (pass-if (string=? "x" (string-delete "x." #\.)))
  (pass-if (string=? "x" (string-delete "x.." #\.)))
  (pass-if (string=? "x" (string-delete "x..." #\.)))
  (pass-if (string=? "x" (string-delete "...x..." #\.)))

  ;; leading and trailing removals
  (pass-if (string=? "" (string-delete "." char-set:punctuation)))
  (pass-if (string=? "" (string-delete ".." char-set:punctuation)))
  (pass-if (string=? "" (string-delete "..." char-set:punctuation)))
  (pass-if (string=? "x" (string-delete ".x" char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "..x" char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "...x" char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "x." char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "x.." char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "x..." char-set:punctuation)))
  (pass-if (string=? "x" (string-delete "...x..." char-set:punctuation))))

(with-test-prefix "string-map"

  (with-test-prefix "bad proc"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-map 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-map "zzz" "abcde")))

  (pass-if "constant"
    (string=? "xxx" (string-map (lambda (c) #\x) "foo")))

  (pass-if "identity"
    (string=? "foo" (string-map identity "foo")))

  (pass-if "upcase"
    (string=? "FOO" (string-map char-upcase "foo"))))

(with-test-prefix "string-map!"

  (with-test-prefix "bad proc"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-map 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-map "zzz" "abcde")))

  (pass-if "constant"
    (let ((str (string-copy "foo")))
      (string-map! (lambda (c) #\x) str)
      (string=? str "xxx")))

  (pass-if "identity"
    (let ((str (string-copy "foo")))
      (string-map! identity str)
      (string=? str "foo")))

  (pass-if "upcase"
    (let ((str (string-copy "foo")))
      (string-map! char-upcase str)
      (string=? str "FOO"))))

(with-test-prefix "string-for-each"

  (with-test-prefix "bad proc"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-for-each 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-for-each "zzz" "abcde")))

  (pass-if "copy"
     (let* ((foo "foo")
            (bar (make-string (string-length foo)))
            (i 0))
       (string-for-each
        (lambda (c) (string-set! bar i c) (set! i (1+ i))) foo)
       (string=? foo bar))))

(with-test-prefix "string-for-each-index"

  (with-test-prefix "bad proc"

    (pass-if-exception "integer" exception:wrong-type-arg
      (string-for-each-index 123 "abcde"))

    (pass-if-exception "string" exception:wrong-type-arg
      (string-for-each-index "zzz" "abcde")))

  (pass-if "index"
     (let* ((foo "foo")
            (bar (make-string (string-length foo))))
       (string-for-each-index
        (lambda (i) (string-set! bar i (string-ref foo i))) foo)
       (string=? foo bar))))


code:
;;;; srfi-14.test          -*- mode:scheme; coding: iso-8859-1 -*-
;;;; --- Test suite for Guile's SRFI-14 functions.
;;;; Martin Grabmueller, 2001-07-16
;;;;
;;;; Copyright (C) 2001, 2006, 2009, 2010 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-srfi-14)
  :use-module (srfi srfi-14)
  :use-module (srfi srfi-1) ;; `every'
  :use-module (test-suite lib))

(define exception:invalid-char-set-cursor
  (cons 'misc-error "^invalid character set cursor"))

(define exception:non-char-return
  (cons 'misc-error "returned non-char"))

(with-test-prefix "char set contents"

  (pass-if "empty set"
    (list= eqv?
           (char-set->list (char-set))
           '()))

  (pass-if "single char"
    (list= eqv?
           (char-set->list (char-set #\a))
           (list #\a)))

  (pass-if "contiguous chars"
    (list= eqv?
           (char-set->list (char-set #\a #\b #\c))
           (list #\a #\b #\c)))

  (pass-if "discontiguous chars"
    (list= eqv?
           (char-set->list (char-set #\a #\c #\e))
           (list #\a #\c #\e))))

(with-test-prefix "char set additition"

  (pass-if "empty + x"
    (let ((cs (char-set)))
      (char-set-adjoin! cs #\x)
      (list= eqv?
             (char-set->list cs)
             (list #\x))))

  (pass-if "x + y"
    (let ((cs (char-set #\x)))
      (char-set-adjoin! cs #\y)
      (list= eqv?
             (char-set->list cs)
             (list #\x #\y))))

  (pass-if "x + w"
    (let ((cs (char-set #\x)))
      (char-set-adjoin! cs #\w)
      (list= eqv?
             (char-set->list cs)
             (list #\w #\x))))

  (pass-if "x + z"
    (let ((cs (char-set #\x)))
      (char-set-adjoin! cs #\z)
      (list= eqv?
             (char-set->list cs)
             (list #\x #\z))))

  (pass-if "x + v"
    (let ((cs (char-set #\x)))
      (char-set-adjoin! cs #\v)
      (list= eqv?
             (char-set->list cs)
             (list #\v #\x))))

  (pass-if "uv + w"
    (let ((cs (char-set #\u #\v)))
      (char-set-adjoin! cs #\w)
      (list= eqv?
             (char-set->list cs)
             (list #\u #\v #\w))))

  (pass-if "uv + t"
    (let ((cs (char-set #\u #\v)))
      (char-set-adjoin! cs #\t)
      (list= eqv?
             (char-set->list cs)
             (list #\t #\u #\v))))

  (pass-if "uv + x"
    (let ((cs (char-set #\u #\v)))
      (char-set-adjoin! cs #\x)
      (list= eqv?
             (char-set->list cs)
             (list #\u #\v #\x))))

  (pass-if "uv + s"
    (let ((cs (char-set #\u #\v)))
      (char-set-adjoin! cs #\s)
      (list= eqv?
             (char-set->list cs)
             (list #\s #\u #\v))))

  (pass-if "uvx + w"
    (let ((cs (char-set #\u #\v #\x)))
      (char-set-adjoin! cs #\w)
      (list= eqv?
             (char-set->list cs)
             (list #\u #\v #\w #\x))))

  (pass-if "uvx + y"
    (let ((cs (char-set #\u #\v #\x)))
      (char-set-adjoin! cs #\y)
      (list= eqv?
             (char-set->list cs)
             (list #\u #\v #\x #\y))))

  (pass-if "uvxy + w"
    (let ((cs (char-set #\u #\v #\x #\y)))
      (char-set-adjoin! cs #\w)
      (list= eqv?
             (char-set->list cs)
             (list #\u #\v #\w #\x #\y)))))

(with-test-prefix "char set union"
  (pass-if "null U abc"
    (char-set= (char-set-union (char-set) (->char-set "abc"))
               (->char-set "abc")))

  (pass-if "ab U ab"
    (char-set= (char-set-union (->char-set "ab") (->char-set "ab"))
               (->char-set "ab")))

  (pass-if "ab U bc"
    (char-set= (char-set-union (->char-set "ab") (->char-set "bc"))
               (->char-set "abc")))

  (pass-if "ab U cd"
    (char-set= (char-set-union (->char-set "ab") (->char-set "cd"))
               (->char-set "abcd")))

  (pass-if "ab U de"
    (char-set= (char-set-union (->char-set "ab") (->char-set "de"))
               (->char-set "abde")))

  (pass-if "abc U bcd"
    (char-set= (char-set-union (->char-set "abc") (->char-set "bcd"))
               (->char-set "abcd")))

  (pass-if "abdf U abcdefg"
    (char-set= (char-set-union (->char-set "abdf") (->char-set "abcdefg"))
               (->char-set "abcdefg")))

  (pass-if "abef U cd"
    (char-set= (char-set-union (->char-set "abef") (->char-set "cd"))
               (->char-set "abcdef")))

  (pass-if "abgh U cd"
    (char-set= (char-set-union (->char-set "abgh") (->char-set "cd"))
               (->char-set "abcdgh")))

  (pass-if "bc U ab"
    (char-set= (char-set-union (->char-set "bc") (->char-set "ab"))
               (->char-set "abc")))

  (pass-if "cd U ab"
    (char-set= (char-set-union (->char-set "cd") (->char-set "ab"))
               (->char-set "abcd")))

  (pass-if "de U ab"
    (char-set= (char-set-union (->char-set "de") (->char-set "ab"))
               (->char-set "abde")))

  (pass-if "cd U abc"
    (char-set= (char-set-union (->char-set "cd") (->char-set "abc"))
               (->char-set "abcd")))

  (pass-if "cd U abcd"
    (char-set= (char-set-union (->char-set "cd") (->char-set "abcd"))
               (->char-set "abcd")))

  (pass-if "cde U abcdef"
    (char-set= (char-set-union (->char-set "cde") (->char-set "abcdef"))
               (->char-set "abcdef"))))

(with-test-prefix "char set xor"
  (pass-if "null - xy"
    (char-set= (char-set-xor (char-set) (char-set #\x #\y))
               (char-set #\x #\y)))

  (pass-if "x - x"
    (char-set= (char-set-xor (char-set #\x) (char-set #\x))
               (char-set)))

  (pass-if "xy - x"
    (char-set= (char-set-xor (char-set #\x #\y) (char-set #\x))
               (char-set #\y)))

  (pass-if "xy - y"
    (char-set= (char-set-xor (char-set #\x #\y) (char-set #\y))
               (char-set #\x)))

  (pass-if "wxy - w"
    (char-set= (char-set-xor (char-set #\w #\x #\y) (char-set #\w))
               (char-set #\x #\y)))

  (pass-if "wxy - x"
    (char-set= (char-set-xor (char-set #\w #\x #\y) (char-set #\x))
               (char-set #\w #\y)))

  (pass-if "wxy - y"
    (char-set= (char-set-xor (char-set #\w #\x #\y) (char-set #\y))
               (char-set #\w #\x)))

  (pass-if "uvxy - u"
    (char-set= (char-set-xor (char-set #\u #\v #\x #\y) (char-set #\u))
               (char-set #\v #\x #\y)))

  (pass-if "uvxy - v"
    (char-set= (char-set-xor (char-set #\u #\v #\x #\y) (char-set #\v))
               (char-set #\u #\x #\y)))

  (pass-if "uvxy - x"
    (char-set= (char-set-xor (char-set #\u #\v #\x #\y) (char-set #\x))
               (char-set #\u #\v #\y)))

  (pass-if "uvxy - y"
    (char-set= (char-set-xor (char-set #\u #\v #\x #\y) (char-set #\y))
               (char-set #\u #\v #\x)))

  (pass-if "uwy - u"
    (char-set= (char-set-xor (char-set #\u #\w #\y) (char-set #\u))
               (char-set #\w #\y)))

  (pass-if "uwy - w"
    (char-set= (char-set-xor (char-set #\u #\w #\y) (char-set #\w))
               (char-set #\u #\y)))

  (pass-if "uwy - y"
    (char-set= (char-set-xor (char-set #\u #\w #\y) (char-set #\y))
               (char-set #\u #\w)))

  (pass-if "uvwy - v"
    (char-set= (char-set-xor (char-set #\u #\v #\w #\y) (char-set #\v))
               (char-set #\u #\w #\y))))

(with-test-prefix "char-set?"

  (pass-if "success on empty set"
    (char-set? (char-set)))

  (pass-if "success on non-empty set"
    (char-set? char-set:printing))

  (pass-if "failure on empty set"
    (not (char-set? #t))))

(with-test-prefix "char-set="
  (pass-if "success, no arg"
    (char-set=))

  (pass-if "success, one arg"
    (char-set= char-set:lower-case))

  (pass-if "success, two args"
    (char-set= char-set:upper-case char-set:upper-case))

  (pass-if "failure, first empty"
    (not (char-set= (char-set) (char-set #\a))))

  (pass-if "failure, second empty"
    (not (char-set= (char-set #\a) (char-set))))

  (pass-if "success, more args"
    (char-set= char-set:blank char-set:blank char-set:blank))

  (pass-if "failure, same length, different elements"
    (not (char-set= (char-set #\a #\b #\d) (char-set #\a #\c #\d)))))

(with-test-prefix "char-set<="
  (pass-if "success, no arg"
    (char-set<=))

  (pass-if "success, one arg"
    (char-set<= char-set:lower-case))

  (pass-if "success, two args"
    (char-set<= char-set:upper-case char-set:upper-case))

  (pass-if "success, first empty"
    (char-set<= (char-set) (char-set #\a)))

  (pass-if "failure, second empty"
    (not (char-set<= (char-set #\a) (char-set))))

  (pass-if "success, more args, equal"
    (char-set<= char-set:blank char-set:blank char-set:blank))

  (pass-if "success, more args, not equal"
    (char-set<= char-set:blank
		(char-set-adjoin char-set:blank #\F)
		(char-set-adjoin char-set:blank #\F #\o))))

(with-test-prefix "char-set-hash"
   (pass-if "empty set, bound"
      (let ((h (char-set-hash char-set:empty 31)))
	(and h (number? h) (exact? h) (>= h 0) (< h 31))))

   (pass-if "empty set, no bound"
      (let ((h (char-set-hash char-set:empty)))
	(and h (number? h) (exact? h) (>= h 0))))

   (pass-if "full set, bound"
      (let ((h (char-set-hash char-set:full 31)))
	(and h (number? h) (exact? h) (>= h 0) (< h 31))))

   (pass-if "full set, no bound"
      (let ((h (char-set-hash char-set:full)))
	(and h (number? h) (exact? h) (>= h 0))))

   (pass-if "other set, bound"
      (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r) 31)))
	(and h (number? h) (exact? h) (>= h 0) (< h 31))))

   (pass-if "other set, no bound"
      (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r))))
	(and h (number? h) (exact? h) (>= h 0)))))

(with-test-prefix "char-set cursor"

  (pass-if-exception "invalid character cursor"
     exception:wrong-type-arg
     (let* ((cs (char-set #\B #\r #\a #\z))
	    (cc (char-set-cursor cs)))
       (char-set-ref cs 1000)))

  (pass-if "success"
     (let* ((cs (char-set #\B #\r #\a #\z))
	    (cc (char-set-cursor cs)))
       (char? (char-set-ref cs cc))))

  (pass-if "end of set fails"
     (let* ((cs (char-set #\a))
	    (cc (char-set-cursor cs)))
       (not (end-of-char-set? cc))))

  (pass-if "end of set succeeds, empty set"
     (let* ((cs (char-set))
	    (cc (char-set-cursor cs)))
       (end-of-char-set? cc)))

  (pass-if "end of set succeeds, non-empty set"
     (let* ((cs (char-set #\a))
	    (cc (char-set-cursor cs))
	    (cc (char-set-cursor-next cs cc)))
       (end-of-char-set? cc))))

(with-test-prefix "char-set-fold"

  (pass-if "count members"
     (= (char-set-fold (lambda (c n) (+ n 1)) 0 (char-set #\a #\b)) 2))

  (pass-if "copy set"
     (= (char-set-size (char-set-fold (lambda (c cs) (char-set-adjoin cs c))
				      (char-set) (char-set #\a #\b))) 2)))

(define char-set:256
  (string->char-set (apply string (map integer->char (iota 256)))))

(with-test-prefix "char-set-unfold"

  (pass-if "create char set"
     (char-set= char-set:256
		(char-set-unfold (lambda (s) (= s 256)) integer->char
				 (lambda (s) (+ s 1)) 0)))
  (pass-if "create char set (base set)"
     (char-set= char-set:256
		(char-set-unfold (lambda (s) (= s 256)) integer->char
				 (lambda (s) (+ s 1)) 0 char-set:empty))))

(with-test-prefix "char-set-unfold!"

  (pass-if "create char set"
     (char-set= char-set:256
		(char-set-unfold! (lambda (s) (= s 256)) integer->char
				 (lambda (s) (+ s 1)) 0
				 (char-set-copy char-set:empty))))

  (pass-if "create char set"
     (char-set= char-set:256
		(char-set-unfold! (lambda (s) (= s 32)) integer->char
				 (lambda (s) (+ s 1)) 0
				 (char-set-copy char-set:256)))))

(with-test-prefix "char-set-for-each"

  (pass-if "copy char set"
     (= (char-set-size (let ((cs (char-set)))
			 (char-set-for-each
			  (lambda (c) (char-set-adjoin! cs c))
			  (char-set #\a #\b))
			 cs))
	2)))

(with-test-prefix "char-set-map"

  (pass-if "upper case char set 1"
     (char-set= (char-set-map char-upcase
                              (string->char-set "abcdefghijklmnopqrstuvwxyz"))
                (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))

  (pass-if "upper case char set 2"
     (char-set= (char-set-map char-upcase
                              (string->char-set "�����������������������������"))
                (string->char-set "�����������������������������"))))

(with-test-prefix "string->char-set"

  (pass-if "some char set"
     (let ((chars '(#\g #\u #\i #\l #\e)))
       (char-set= (list->char-set chars)
		  (string->char-set (apply string chars))))))

(with-test-prefix "char-set->string"

  (pass-if "some char set"
     (let ((cs (char-set #\g #\u #\i #\l #\e)))
       (string=? (char-set->string cs)
                 "egilu"))))

(with-test-prefix "list->char-set"

  (pass-if "list->char-set"
    (char-set= (list->char-set '(#\a #\b #\c))
               (->char-set "abc")))

  (pass-if "list->char-set!"
    (let* ((cs (char-set #\a #\z)))
      (list->char-set! '(#\m #\n) cs)
      (char-set= cs
                 (char-set #\a #\m #\n #\z)))))

(with-test-prefix "string->char-set"

  (pass-if "string->char-set"
    (char-set= (string->char-set "foobar")
               (string->char-set "barfoo")))

  (pass-if "string->char-set cs"
    (char-set= (string->char-set "foo" (string->char-set "bar"))
               (string->char-set "barfoo")))

  (pass-if "string->char-set!"
    (let ((cs (string->char-set "bar")))
      (string->char-set! "foo" cs)
      (char-set= cs
                 (string->char-set "barfoo")))))

(with-test-prefix "char-set-filter"

  (pass-if "filter w/o base"
    (char-set=
     (char-set-filter (lambda (c) (char=? c #\x))
                      (->char-set "qrstuvwxyz"))
     (->char-set #\x)))

  (pass-if "filter w/ base"
    (char-set=
     (char-set-filter (lambda (c) (char=? c #\x))
                      (->char-set "qrstuvwxyz")
                      (->char-set "op"))

     (->char-set "opx")))

  (pass-if "filter!"
    (let ((cs (->char-set "abc")))
      (set! cs (char-set-filter! (lambda (c) (char=? c #\x))
                                 (->char-set "qrstuvwxyz")
                                 cs))
      (char-set= (string->char-set "abcx")
                 cs))))

(with-test-prefix "char-set-intersection"

  (pass-if "empty"
    (char-set= (char-set-intersection (char-set) (char-set))
               (char-set)))

  (pass-if "identical, one element"
    (char-set= (char-set-intersection (char-set #\a) (char-set #\a))
               (char-set #\a)))

  (pass-if "identical, two elements"
    (char-set= (char-set-intersection (char-set #\a #\b) (char-set #\a #\b))
               (char-set #\a #\b)))

  (pass-if "identical, two elements"
    (char-set= (char-set-intersection (char-set #\a #\c) (char-set #\a #\c))
               (char-set #\a #\c)))

  (pass-if "one vs null"
    (char-set= (char-set-intersection (char-set #\a) (char-set))
               (char-set)))

  (pass-if "null vs one"
    (char-set= (char-set-intersection (char-set) (char-set #\a))
               (char-set)))

  (pass-if "no elements shared"
    (char-set= (char-set-intersection (char-set #\a #\c) (char-set #\b #\d))
               (char-set)))

  (pass-if "one elements shared"
    (char-set= (char-set-intersection (char-set #\a #\c #\d) (char-set #\b #\d))
               (char-set #\d))))

(with-test-prefix "char-set-complement"

  (pass-if "complement of null"
           (char-set= (char-set-complement (char-set))
                      (char-set-union (ucs-range->char-set 0 #xd800)
                                      (ucs-range->char-set #xe000 #x110000))))

  (pass-if "complement of null (2)"
           (char-set= (char-set-complement (char-set))
                      (ucs-range->char-set 0 #x110000)))

  (pass-if "complement of #\\0"
           (char-set= (char-set-complement (char-set #\nul))
                      (ucs-range->char-set 1 #x110000)))

  (pass-if "complement of U+10FFFF"
           (char-set= (char-set-complement (char-set (integer->char #x10ffff)))
                      (ucs-range->char-set 0 #x10ffff)))

  (pass-if "complement of 'FOO'"
           (char-set= (char-set-complement (->char-set "FOO"))
                      (char-set-union (ucs-range->char-set 0 (char->integer #\F))
                                      (ucs-range->char-set (char->integer #\G)
                                                           (char->integer #\O))
                                      (ucs-range->char-set (char->integer #\P)
                                                            #x110000))))
  (pass-if "complement of #\\a #\\b U+010300"
           (char-set= (char-set-complement (char-set #\a #\b (integer->char #x010300)))
                      (char-set-union (ucs-range->char-set 0 (char->integer #\a))
                                      (ucs-range->char-set (char->integer #\c) #x010300)
                                      (ucs-range->char-set #x010301 #x110000)))))

(with-test-prefix "ucs-range->char-set"
  (pass-if "char-set"
    (char-set= (ucs-range->char-set 65 68)
               (->char-set "ABC")))

  (pass-if "char-set w/ base"
    (char-set= (ucs-range->char-set 65 68 #f (->char-set "DEF"))
               (->char-set "ABCDEF")))

  (pass-if "char-set!"
    (let ((cs (->char-set "DEF")))
      (ucs-range->char-set! 65 68 #f cs)
      (char-set= cs
                 (->char-set "ABCDEF")))))

(with-test-prefix "char-set-count"
  (pass-if "null"
    (= 0 (char-set-count (lambda (c) #t) (char-set))))

  (pass-if "count"
    (= 5 (char-set-count (lambda (c) #t)
                         (->char-set "guile")))))

(with-test-prefix "char-set-contains?"
  (pass-if "#\\a not in null"
    (not (char-set-contains? (char-set) #\a)))

  (pass-if "#\\a is in 'abc'"
    (char-set-contains? (->char-set "abc") #\a)))

(with-test-prefix "any / every"
  (pass-if "char-set-every #t"
    (char-set-every (lambda (c) #t)
                    (->char-set "abc")))

  (pass-if "char-set-every #f"
    (not (char-set-every (lambda (c) (char=? c #\c))
                         (->char-set "abc"))))

  (pass-if "char-set-any #t"
    (char-set-any (lambda (c) (char=? c #\c))
                  (->char-set "abc")))

  (pass-if "char-set-any #f"
    (not (char-set-any (lambda (c) #f)
                       (->char-set "abc")))))

(with-test-prefix "char-set-delete"
  (pass-if "abc - a"
    (char-set= (char-set-delete (->char-set "abc") #\a)
               (char-set #\b #\c)))

  (pass-if "abc - d"
    (char-set= (char-set-delete (->char-set "abc") #\d)
               (char-set #\a #\b #\c)))

  (pass-if "delete! abc - a"
    (let ((cs (char-set #\a #\b #\c)))
      (char-set-delete! cs #\a)
      (char-set= cs (char-set #\b #\c)))))

(with-test-prefix "char-set-difference"
  (pass-if "not different"
    (char-set= (char-set-difference (->char-set "foobar") (->char-set "foobar"))
               (char-set)))

  (pass-if "completely different"
    (char-set= (char-set-difference (->char-set "foo") (->char-set "bar"))
               (->char-set "foo")))

  (pass-if "partially different"
    (char-set= (char-set-difference (->char-set "breakfast") (->char-set "breakroom"))
               (->char-set "fst"))))

(with-test-prefix "standard char sets (ASCII)"

  (pass-if "char-set:lower-case"
     (char-set<= (string->char-set "abcdefghijklmnopqrstuvwxyz")
                 char-set:lower-case))

  (pass-if "char-set:upper-case"
     (char-set<= (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                 char-set:upper-case))

  (pass-if "char-set:title-case"
     (char-set<= (string->char-set "")
                 char-set:title-case))

  (pass-if "char-set:letter"
     (char-set<= (char-set-union
                  (string->char-set "abcdefghijklmnopqrstuvwxyz")
                  (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
                 char-set:letter))

  (pass-if "char-set:digit"
     (char-set<= (string->char-set "0123456789")
                 char-set:digit))

  (pass-if "char-set:hex-digit"
     (char-set<= (string->char-set "0123456789abcdefABCDEF")
                 char-set:hex-digit))

  (pass-if "char-set:letter+digit"
     (char-set<= (char-set-union
                  (string->char-set "abcdefghijklmnopqrstuvwxyz")
                  (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                  (string->char-set "0123456789"))
                 char-set:letter+digit))

  (pass-if "char-set:punctuation"
     (char-set<= (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
                 char-set:punctuation))

  (pass-if "char-set:symbol"
     (char-set<= (string->char-set "$+<=>^`|~")
                 char-set:symbol))

  (pass-if "char-set:graphic"
     (char-set<= (char-set-union
                  (string->char-set "abcdefghijklmnopqrstuvwxyz")
                  (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                  (string->char-set "0123456789")
                  (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
                  (string->char-set "$+<=>^`|~"))
                 char-set:graphic))

  (pass-if "char-set:whitespace"
     (char-set<= (string->char-set
                  (string
                   (integer->char #x09)
                   (integer->char #x0a)
                   (integer->char #x0b)
                   (integer->char #x0c)
                   (integer->char #x0d)
                   (integer->char #x20)))
                 char-set:whitespace))

  (pass-if "char-set:printing"
     (char-set<= (char-set-union
                  (string->char-set "abcdefghijklmnopqrstuvwxyz")
                  (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                  (string->char-set "0123456789")
                  (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
                  (string->char-set "$+<=>^`|~")
                  (string->char-set (string
                                     (integer->char #x09)
                                     (integer->char #x0a)
                                     (integer->char #x0b)
                                     (integer->char #x0c)
                                     (integer->char #x0d)
                                     (integer->char #x20))))
                 char-set:printing))

  (pass-if "char-set:ASCII"
     (char-set= (ucs-range->char-set 0 128)
                char-set:ascii))

  (pass-if "char-set:iso-control"
     (char-set<= (string->char-set
                  (apply string
                         (map integer->char (append
                                             ;; U+0000 to U+001F
                                             (iota #x20)
                                             (list #x7f)))))
                 char-set:iso-control)))

;;;
;;; Non-ASCII codepoints
;;;
;;; Here, we only test ISO-8859-1 (Latin-1), notably because behavior of
;;; SRFI-14 for implementations supporting this charset is well-defined.
;;;

(define (every? pred lst)
  (not (not (every pred lst))))

(define oldlocale #f)
(if (defined? 'setlocale)
    (set! oldlocale (setlocale LC_ALL "")))

(with-test-prefix "Latin-1 (8-bit charset)"

  (pass-if "char-set:lower-case"
    (char-set<= (string->char-set
                 (string-append "abcdefghijklmnopqrstuvwxyz"
                                "���������������������������������")
                 char-set:lower-case)))

  (pass-if "char-set:upper-case"
    (char-set<= (string->char-set
                 (string-append "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                "������������������������������")
                 char-set:lower-case)))

  (pass-if "char-set:title-case"
    (char-set<= (string->char-set "")
                char-set:title-case))

  (pass-if "char-set:letter"
    (char-set<= (string->char-set
                 (string-append
                  ;; Lowercase
                  "abcdefghijklmnopqrstuvwxyz"
                  "���������������������������������"
                  ;; Uppercase
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                  "������������������������������"
                  ;; Uncased
                  "��"))
                char-set:letter))

  (pass-if "char-set:digit"
    (char-set<= (string->char-set "0123456789")
                char-set:digit))

  (pass-if "char-set:hex-digit"
    (char-set<= (string->char-set "0123456789abcdefABCDEF")
                char-set:hex-digit))

  (pass-if "char-set:letter+digit"
    (char-set<= (char-set-union
                 char-set:letter
                 char-set:digit)
                char-set:letter+digit))

  (pass-if "char-set:punctuation"
    (char-set<= (string->char-set
                 (string-append "!\"#%&'()*,-./:;?@[\\]_{}"
                                "�����"))
                char-set:punctuation))

  (pass-if "char-set:symbol"
    (char-set<= (string->char-set
                 (string-append "$+<=>^`|~"
                                "������������������"))
                char-set:symbol))

  ;; Note that SRFI-14 itself is inconsistent here.  Characters that
  ;; are non-digit numbers (such as category No) are clearly 'graphic'
  ;; but don't occur in the letter, digit, punct, or symbol charsets.
  (pass-if "char-set:graphic"
    (char-set<= (char-set-union
                 char-set:letter
                 char-set:digit
                 char-set:punctuation
                 char-set:symbol)
                char-set:graphic))

  (pass-if "char-set:whitespace"
    (char-set<= (string->char-set
                 (string
                  (integer->char #x09)
                  (integer->char #x0a)
                  (integer->char #x0b)
                  (integer->char #x0c)
                  (integer->char #x0d)
                  (integer->char #x20)
                  (integer->char #xa0)))
                char-set:whitespace))

  (pass-if "char-set:printing"
    (char-set<= (char-set-union char-set:graphic char-set:whitespace)
                char-set:printing))

  (pass-if "char-set:iso-control"
    (char-set<= (string->char-set
                 (apply string
                        (map integer->char (append
                                            ;; U+0000 to U+001F
                                            (iota #x20)
                                            (list #x7f)
                                            ;; U+007F to U+009F
                                            (map (lambda (x) (+ #x80 x))
                                                 (iota #x20))))))
                char-set:iso-control)))

(if (defined? 'setlocale)
    (setlocale LC_ALL oldlocale))

license: mit


code:
;;;; srfi-17.test --- test suite for Guile's SRFI-17 functions. -*- scheme -*-
;;;;
;;;; Copyright (C) 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-srfi-17)
  :use-module (test-suite lib)
  :use-module (srfi srfi-17))

(pass-if "cond-expand srfi-17"
  (cond-expand (srfi-17 #t)
	       (else    #f)))

;;
;; car
;;

(with-test-prefix "car"

  ;; this test failed in guile 1.8.1 and 1.6.8 and earlier, since `define'
  ;; didn't set a name on a procedure-with-setter
  (pass-if "procedure-name"
    (if (memq 'procnames (debug-options)) ;; enabled by default
	(eq? 'car (procedure-name car))
	(throw 'unsupported)))

  (pass-if "set! (car x)"
    (let ((lst (list 1)))
      (set! (car lst) 2)
      (eqv? 2 (car lst)))))

;;
;; set!
;;

(define %some-variable #f)

(define exception:bad-quote
  '(syntax-error . "quote: bad syntax"))

(with-test-prefix "set!"

  (with-test-prefix "target is not procedure with setter"

    (pass-if-exception "(set! (symbol->string 'x) 1)"
      exception:wrong-type-arg
      (set! (symbol->string 'x) 1))

    (pass-if-exception "(set! '#f 1)"
      exception:bad-quote
      (eval '(set! '#f 1) (interaction-environment))))

  (with-test-prefix "target uses macro"

    (pass-if "(set! (@@ ...) 1)"
      (eval '(set! (@@ (test-suite test-srfi-17) %some-variable) 1)
            (interaction-environment))
      (equal? %some-variable 1))

    ;; The `(quote x)' below used to be memoized as an infinite list before
    ;; Guile 1.8.3.
    (pass-if-exception "(set! 'x 1)"
      exception:bad-quote
      (eval '(set! 'x 1) (interaction-environment)))))

;;
;; setter
;;

(with-test-prefix "setter"

  (pass-if-exception "set! (setter x)" (cons 'misc-error ".*")
    (set! (setter car) noop))

  (pass-if "car"
    (eq? set-car! (setter car))))


code:
;;;; srfi-18.test --- Test suite for Guile's SRFI-18 functions. -*- scheme -*-
;;;; Julian Graham, 2007-10-26
;;;;
;;;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-srfi-18)
  #:use-module (test-suite lib))

;; two expressions so that the srfi-18 import is in effect for expansion
;; of the rest
(if (provided? 'threads)
    (use-modules (srfi srfi-18)))

(and
 (provided? 'threads)

(with-test-prefix "current-thread"

  (pass-if "current-thread eq current-thread"
    (eq? (current-thread) (current-thread))))

(with-test-prefix "thread?"

  (pass-if "current-thread is thread"
    (thread? (current-thread)))

  (pass-if "foo not thread"
    (not (thread? 'foo))))

(with-test-prefix "make-thread"

  (pass-if "make-thread creates new thread"
    (let* ((n (length (all-threads)))
	   (t (make-thread (lambda () 'foo) 'make-thread-1))
	   (r (> (length (all-threads)) n)))
      (thread-terminate! t) r)))

(with-test-prefix "thread-name"

  (pass-if "make-thread with name binds name"
    (let* ((t (make-thread (lambda () 'foo) 'thread-name-1))
	   (r (eq? (thread-name t) 'thread-name-1)))
      (thread-terminate! t) r))

  (pass-if "make-thread without name does not bind name"
    (let* ((t (make-thread (lambda () 'foo)))
	   (r (not (thread-name t))))
      (thread-terminate! t) r)))

(with-test-prefix "thread-specific"

  (pass-if "thread-specific is initially #f"
    (let* ((t (make-thread (lambda () 'foo) 'thread-specific-1))
	   (r (not (thread-specific t))))
      (thread-terminate! t) r))

  (pass-if "thread-specific-set! can set value"
    (let ((t (make-thread (lambda () 'foo) 'thread-specific-2)))
      (thread-specific-set! t "hello")
      (let ((r (equal? (thread-specific t) "hello")))
	(thread-terminate! t) r))))

(with-test-prefix "thread-start!"

  (pass-if "thread activates only after start"
    (let* ((started #f)
	   (m (make-mutex 'thread-start-mutex))
	   (t (make-thread (lambda () (set! started #t)) 'thread-start-1)))
      (and (not started) (thread-start! t) (thread-join! t) started))))

(with-test-prefix "thread-yield!"

  (pass-if "thread yield suceeds"
    (thread-yield!) #t))

(with-test-prefix "thread-sleep!"

  (pass-if "thread sleep with time"
    (let ((future-time (seconds->time (+ (time->seconds (current-time)) 2))))
      (unspecified? (thread-sleep! future-time))))

  (pass-if "thread sleep with number"
    (let ((old-secs (car (current-time))))
      (unspecified? (thread-sleep! (+ (time->seconds (current-time)))))))

  (pass-if "thread does not sleep on past time"
    (let ((past-time (seconds->time (- (time->seconds (current-time)) 2))))
      (unspecified? (thread-sleep! past-time)))))

(with-test-prefix "thread-terminate!"

  (pass-if "termination destroys non-started thread"
    (let ((t (make-thread (lambda () 'nothing) 'thread-terminate-1))
	  (num-threads (length (all-threads)))
	  (success #f))
      (thread-terminate! t)
      (with-exception-handler
       (lambda (obj) (set! success (terminated-thread-exception? obj)))
       (lambda () (thread-join! t)))
      success))

  (pass-if "termination destroys started thread"
    (let* ((m1 (make-mutex 'thread-terminate-2a))
	   (m2 (make-mutex 'thread-terminate-2b))
	   (c (make-condition-variable 'thread-terminate-2))
	   (t (make-thread (lambda ()
			     (mutex-lock! m1)
			     (condition-variable-signal! c)
			     (mutex-unlock! m1)
			     (mutex-lock! m2))
			   'thread-terminate-2))
	   (success #f))
      (mutex-lock! m1)
      (mutex-lock! m2)
      (thread-start! t)
      (mutex-unlock! m1 c)
      (thread-terminate! t)
      (with-exception-handler
       (lambda (obj) (set! success (terminated-thread-exception? obj)))
       (lambda () (thread-join! t)))
      success)))

(with-test-prefix "thread-join!"

  (pass-if "join receives result of thread"
    (let ((t (make-thread (lambda () 'foo) 'thread-join-1)))
      (thread-start! t)
      (eq? (thread-join! t) 'foo)))

  (pass-if "join receives timeout val if timeout expires"
    (let* ((m (make-mutex 'thread-join-2))
	   (t (make-thread (lambda () (mutex-lock! m)) 'thread-join-2)))
      (mutex-lock! m)
      (thread-start! t)
      (let ((r (thread-join! t (current-time) 'bar)))
	(thread-terminate! t)
	(eq? r 'bar))))

  (pass-if "join throws exception on timeout without timeout val"
    (let* ((m (make-mutex 'thread-join-3))
	   (t (make-thread (lambda () (mutex-lock! m)) 'thread-join-3))
	   (success #f))
      (mutex-lock! m)
      (thread-start! t)
      (with-exception-handler
       (lambda (obj) (set! success (join-timeout-exception? obj)))
       (lambda () (thread-join! t (current-time))))
      (thread-terminate! t)
      success))

  (pass-if "join waits on timeout"
    (let ((t (make-thread (lambda () (sleep 1) 'foo) 'thread-join-4)))
      (thread-start! t)
      (eq? (thread-join! t (+ (time->seconds (current-time)) 2)) 'foo))))

(with-test-prefix "mutex?"

  (pass-if "make-mutex creates mutex"
    (mutex? (make-mutex)))

  (pass-if "symbol not mutex"
    (not (mutex? 'foo))))

(with-test-prefix "mutex-name"

  (pass-if "make-mutex with name binds name"
    (let* ((m (make-mutex 'mutex-name-1)))
      (eq? (mutex-name m) 'mutex-name-1)))

  (pass-if "make-mutex without name does not bind name"
    (let* ((m (make-mutex)))
      (not (mutex-name m)))))

(with-test-prefix "mutex-specific"

  (pass-if "mutex-specific is initially #f"
    (let ((m (make-mutex 'mutex-specific-1)))
      (not (mutex-specific m))))

  (pass-if "mutex-specific-set! can set value"
    (let ((m (make-mutex 'mutex-specific-2)))
      (mutex-specific-set! m "hello")
      (equal? (mutex-specific m) "hello"))))

(with-test-prefix "mutex-state"

  (pass-if "mutex state is initially not-abandoned"
    (let ((m (make-mutex 'mutex-state-1)))
      (eq? (mutex-state m) 'not-abandoned)))

  (pass-if "mutex state of locked, owned mutex is owner thread"
    (let ((m (make-mutex 'mutex-state-2)))
      (mutex-lock! m)
      (eq? (mutex-state m) (current-thread))))

  (pass-if "mutex state of locked, unowned mutex is not-owned"
    (let ((m (make-mutex 'mutex-state-3)))
      (mutex-lock! m #f #f)
      (eq? (mutex-state m) 'not-owned)))

  (pass-if "mutex state of unlocked, abandoned mutex is abandoned"
    (let* ((m (make-mutex 'mutex-state-4))
	   (t (make-thread (lambda () (mutex-lock! m)))))
      (thread-start! t)
      (thread-join! t)
      (eq? (mutex-state m) 'abandoned))))

(with-test-prefix "mutex-lock!"

  (pass-if "mutex-lock! returns true on successful lock"
    (let* ((m (make-mutex 'mutex-lock-1)))
      (mutex-lock! m)))

  (pass-if "mutex-lock! returns false on timeout"
    (let* ((m (make-mutex 'mutex-lock-2))
	   (t (make-thread (lambda () (mutex-lock! m (current-time) #f)))))
      (mutex-lock! m)
      (thread-start! t)
      (not (thread-join! t))))

  (pass-if "mutex-lock! returns true when lock obtained within timeout"
    (let* ((m (make-mutex 'mutex-lock-3))
	   (t (make-thread (lambda ()
			     (mutex-lock! m (+ (time->seconds (current-time))
					       100)
					  #f)))))
      (mutex-lock! m)
      (thread-start! t)
      (mutex-unlock! m)
      (thread-join! t)))

  (pass-if "can lock mutex for non-current thread"
    (let* ((m1 (make-mutex 'mutex-lock-4a))
	   (m2 (make-mutex 'mutex-lock-4b))
	   (t (make-thread (lambda () (mutex-lock! m1)) 'mutex-lock-4)))
      (mutex-lock! m1)
      (thread-start! t)
      (mutex-lock! m2 #f t)
      (let ((success (eq? (mutex-state m2) t)))
	(thread-terminate! t) success)))

  (pass-if "locking abandoned mutex throws exception"
    (let* ((m (make-mutex 'mutex-lock-5))
	   (t (make-thread (lambda () (mutex-lock! m)) 'mutex-lock-5))
	   (success #f))
      (thread-start! t)
      (thread-join! t)
      (with-exception-handler
       (lambda (obj) (set! success (abandoned-mutex-exception? obj)))
       (lambda () (mutex-lock! m)))
      (and success (eq? (mutex-state m) (current-thread)))))

  (pass-if "sleeping threads notified of abandonment"
    (let* ((m1 (make-mutex 'mutex-lock-6a))
	   (m2 (make-mutex 'mutex-lock-6b))
	   (c (make-condition-variable 'mutex-lock-6))
	   (t (make-thread (lambda ()
			     (mutex-lock! m1)
			     (mutex-lock! m2)
			     (condition-variable-signal! c))))
	   (success #f))
      (mutex-lock! m1)
      (thread-start! t)
      (with-exception-handler
       (lambda (obj) (set! success (abandoned-mutex-exception? obj)))
       (lambda () (mutex-unlock! m1 c) (mutex-lock! m2)))
      success)))

(with-test-prefix "mutex-unlock!"

  (pass-if "unlock changes mutex state"
    (let* ((m (make-mutex 'mutex-unlock-1)))
      (mutex-lock! m)
      (mutex-unlock! m)
      (eq? (mutex-state m) 'not-abandoned)))

  (pass-if "can unlock from any thread"
    (let* ((m (make-mutex 'mutex-unlock-2))
	   (t (make-thread (lambda () (mutex-unlock! m)) 'mutex-unlock-2)))
      (mutex-lock! m)
      (thread-start! t)
      (thread-join! t)
      (eq? (mutex-state m) 'not-abandoned)))

  (pass-if "mutex unlock is true when condition is signalled"
    (let* ((m (make-mutex 'mutex-unlock-3))
	   (c (make-condition-variable 'mutex-unlock-3))
	   (t (make-thread (lambda ()
			     (mutex-lock! m)
			     (condition-variable-signal! c)
			     (mutex-unlock! m)))))
      (mutex-lock! m)
      (thread-start! t)
      (mutex-unlock! m c)))

  (pass-if "mutex unlock is false when condition times out"
    (let* ((m (make-mutex 'mutex-unlock-4))
	   (c (make-condition-variable 'mutex-unlock-4)))
      (mutex-lock! m)
      (not (mutex-unlock! m c (+ (time->seconds (current-time)) 1))))))

(with-test-prefix "condition-variable?"

  (pass-if "make-condition-variable creates condition variable"
    (condition-variable? (make-condition-variable)))

  (pass-if "symbol not condition variable"
    (not (condition-variable? 'foo))))

(with-test-prefix "condition-variable-name"

  (pass-if "make-condition-variable with name binds name"
    (let* ((c (make-condition-variable 'condition-variable-name-1)))
      (eq? (condition-variable-name c) 'condition-variable-name-1)))

  (pass-if "make-condition-variable without name does not bind name"
    (let* ((c (make-condition-variable)))
      (not (condition-variable-name c)))))

(with-test-prefix "condition-variable-specific"

  (pass-if "condition-variable-specific is initially #f"
    (let ((c (make-condition-variable 'condition-variable-specific-1)))
      (not (condition-variable-specific c))))

  (pass-if "condition-variable-specific-set! can set value"
    (let ((c (make-condition-variable 'condition-variable-specific-1)))
      (condition-variable-specific-set! c "hello")
      (equal? (condition-variable-specific c) "hello"))))

(with-test-prefix "condition-variable-signal!"

  (pass-if "condition-variable-signal! wakes up single thread"
    (let* ((m (make-mutex 'condition-variable-signal-1))
	   (c (make-condition-variable 'condition-variable-signal-1))
	   (t (make-thread (lambda ()
			     (mutex-lock! m)
			     (condition-variable-signal! c)
			     (mutex-unlock! m)))))
      (mutex-lock! m)
      (thread-start! t)
      (mutex-unlock! m c))))

(with-test-prefix "condition-variable-broadcast!"

  (pass-if "condition-variable-broadcast! wakes up multiple threads"
    (let* ((sem 0)
	   (c1 (make-condition-variable 'condition-variable-broadcast-1-a))
	   (m1 (make-mutex 'condition-variable-broadcast-1-a))
	   (c2 (make-condition-variable 'condition-variable-broadcast-1-b))
	   (m2 (make-mutex 'condition-variable-broadcast-1-b))
	   (inc-sem! (lambda ()
		       (mutex-lock! m1)
		       (set! sem (+ sem 1))
		       (condition-variable-broadcast! c1)
		       (mutex-unlock! m1)))
	   (dec-sem! (lambda ()
		       (mutex-lock! m1)
		       (while (eqv? sem 0) (wait-condition-variable c1 m1))
		       (set! sem (- sem 1))
		       (mutex-unlock! m1)))
	   (t1 (make-thread (lambda ()
			      (mutex-lock! m2)
			      (inc-sem!)
			      (mutex-unlock! m2 c2)
			      (inc-sem!))))
	   (t2 (make-thread (lambda ()
			      (mutex-lock! m2)
			      (inc-sem!)
			      (mutex-unlock! m2 c2)
			      (inc-sem!)))))
      (thread-start! t1)
      (thread-start! t2)
      (dec-sem!)
      (dec-sem!)
      (mutex-lock! m2)
      (condition-variable-broadcast! c2)
      (mutex-unlock! m2)
      (dec-sem!)
      (dec-sem!))))

(with-test-prefix "time?"

  (pass-if "current-time is time" (time? (current-time)))
  (pass-if "number is not time" (not (time? 123)))
  (pass-if "symbol not time" (not (time? 'foo))))

(with-test-prefix "time->seconds"

  (pass-if "time->seconds makes time into rational"
    (rational? (time->seconds (current-time))))

  (pass-if "time->seconds is reversible"
    (let ((t (current-time)))
      (equal? t (seconds->time (time->seconds t))))))

(with-test-prefix "seconds->time"

  (pass-if "seconds->time makes rational into time"
    (time? (seconds->time 123.456)))

  (pass-if "seconds->time is reversible"
    (let ((t (time->seconds (current-time))))
      (equal? t (time->seconds (seconds->time t))))))

(with-test-prefix "current-exception-handler"

  (pass-if "current handler returned at top level"
    (procedure? (current-exception-handler)))

  (pass-if "specified handler set under with-exception-handler"
    (let ((h (lambda (key . args) 'nothing)))
      (with-exception-handler h (lambda () (eq? (current-exception-handler)
						h)))))

  (pass-if "multiple levels of handler nesting"
    (let ((h (lambda (key . args) 'nothing))
	  (i (current-exception-handler)))
      (and (with-exception-handler h (lambda ()
				       (eq? (current-exception-handler) h)))
	   (eq? (current-exception-handler) i))))

  (pass-if "exception handler installation is thread-safe"
    (let* ((h1 (current-exception-handler))
	   (h2 (lambda (key . args) 'nothing-2))
	   (m (make-mutex 'current-exception-handler-4))
	   (c (make-condition-variable 'current-exception-handler-4))
	   (t (make-thread (lambda ()
			     (with-exception-handler
			      h2 (lambda ()
				   (mutex-lock! m)
				   (condition-variable-signal! c)
				   (wait-condition-variable c m)
				   (and (eq? (current-exception-handler) h2)
					(mutex-unlock! m)))))
			   'current-exception-handler-4)))
      (mutex-lock! m)
      (thread-start! t)
      (wait-condition-variable c m)
      (and (eq? (current-exception-handler) h1)
	   (condition-variable-signal! c)
	   (mutex-unlock! m)
	   (thread-join! t)))))

(with-test-prefix "uncaught-exception-reason"

  (pass-if "initial handler captures top level exception"
    (let ((t (make-thread (lambda () (raise 'foo))))
	  (success #f))
      (thread-start! t)
      (with-exception-handler
       (lambda (obj)
	 (and (uncaught-exception? obj)
	      (eq? (uncaught-exception-reason obj) 'foo)
	      (set! success #t)))
       (lambda () (thread-join! t)))
      success))

  (pass-if "initial handler captures non-SRFI-18 throw"
    (let ((t (make-thread (lambda () (throw 'foo))))
	  (success #f))
      (thread-start! t)
      (with-exception-handler
       (lambda (obj)
	 (and (uncaught-exception? obj)
	      (eq? (uncaught-exception-reason obj) 'foo)
	      (set! success #t)))
       (lambda () (thread-join! t)))
      success)))

)