unread-char.lsp 1.79 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Jan 18 20:05:36 2004
;;;; Contains: Tests of UNREAD-CHAR

(in-package :cl-test)

(deftest unread-char.1
  (with-input-from-string
   (*standard-input* "abc")
   (values
    (read-char)
    (unread-char #\a)
    (read-char)
    (read-char)
    (unread-char #\b)
    (read-char)
    (read-char)))
  #\a nil #\a #\b nil #\b #\c)

(deftest unread-char.2
  (with-input-from-string
   (s "abc")
   (values
    (read-char s)
    (unread-char #\a s)
    (read-char s)
    (read-char s)
    (unread-char #\b s)
    (read-char s)
    (read-char s)))
  #\a nil #\a #\b nil #\b #\c)

(deftest unread-char.3
  (with-input-from-string
   (is "abc")
   (with-output-to-string
     (os)
     (let ((s (make-echo-stream is os)))
       (read-char s)
       (unread-char #\a s)
       (read-char s)
       (read-char s)
       (read-char s)
       (unread-char #\c s)
       (read-char s))))
  "abc")

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
(deftest unread-char.4
  (with-input-from-string
   (*standard-input* "abc")
   (values
    (read-char)
    (unread-char #\a nil)
    (read-char)
    (read-char)
    (unread-char #\b nil)
    (read-char)
    (read-char)))
  #\a nil #\a #\b nil #\b #\c)

(deftest unread-char.5
  (with-input-from-string
   (is "abc")
pfdietz's avatar
pfdietz committed
65
   (let ((*terminal-io* (make-two-way-stream
sds's avatar
sds committed
66
                         is (make-string-output-stream))))
67 68 69 70 71 72 73 74 75 76
     (values
      (read-char t)
      (unread-char #\a t)
      (read-char t)
      (read-char t)
      (unread-char #\b t)
      (read-char t)
      (read-char t))))
  #\a nil #\a #\b nil #\b #\c)

pfdietz's avatar
pfdietz committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
;;; Error tests

(deftest unread-char.error.1
  (signals-error (unread-char) program-error)
  t)

(deftest unread-char.error.2
  (signals-error
   (with-input-from-string
    (s "abc")
    (read-char s)
    (unread-char #\a s nil))
   program-error)
  t)