port-test.ss
;;;
;;; Time-stamp: <05/12/05 15:23:43 nhw>
;;;
;;; Copyright (C) 2005 by Noel Welsh.
;;;

;;; 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 2.1 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., 59 Temple
;;; Place, Suite 330, Boston, MA 02111-1307 USA

;;; Author: Noel Welsh <noelwelsh@yahoo.com>
;;
;;
;; Commentary:

(module port-test mzscheme
  
  (require (planet "test.ss" ("schematics" "schemeunit.plt" 1 1)))
  (require "port.ss")
  
  (provide port-tests)
  
  (define port-tests
    (make-test-suite
     "All tests for port"
     (make-test-case
      "port->list reads empty port"
      (assert-equal? (port->list read (open-input-string ""))
                     '()))
     (make-test-case
      "port->list applys custom reader"
      (assert-equal? (port->list (lambda (port)
                                   (let ((ip (read port)))
                                     (if (eof-object? ip)
                                         ip
                                         (string->number ip))))
                                 (open-input-string"\"20\""))
                     '(20)))
     (make-test-case
      "port->list reads to end of port"
      (assert-equal? (port->list read (open-input-string "1 2 3 4"))
                     '(1 2 3 4)))
     (make-test-case
      "port->string reads empty port"
      (assert-equal? (port->string (open-input-string ""))
                     ""))
     (make-test-case
      "port->string reads to end of port"
      (let ((data "hello\nthere!\nhow are you?"))
        (assert-equal? (port->string (open-input-string data))
                       data)))
     (make-test-case
      "port->string-list reads empty port"
      (assert-equal? (port->string-list (open-input-string ""))
                     '()))
     (make-test-case
      "port->string-list reads to end of port"
      (let ((data "hello\nthere!\nhow are you?"))
        (assert-equal? (port->string-list (open-input-string data))
                       '("hello" "there!" "how are you?"))))
     (make-test-case
      "port->sexp-list reads empty port"
      (assert-equal? (port->sexp-list (open-input-string ""))
                     '()))
     (make-test-case
      "port->sexp-list reads assorted data"
      (assert-equal?
       (port->sexp-list
        (open-input-string "2 foo \"harry\" (1 2 3 4)"))
       '(2 foo "harry" (1 2 3 4))))
     (make-test-case
      "port-fold returns seed on empty port"
      (assert-eq? (port-fold (open-input-string "")
                             read
                             list
                             'seed)
                  'seed))
     (make-test-case
      "port-fold handles multiple-value reader"
      (assert-equal?
       (let-values (((sum len)
                     (port-fold (open-input-string "1 2 3 4")
                                read
                                (lambda (elt sum len)
                                  (values (+ sum elt)
                                          (add1 len)))
                                0
                                0)))
         (list sum len))
       (list 10 4)))
     ))
  )