read-byte.lsp 5.02 KB
Newer Older
1 2 3 4 5 6 7 8 9
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jan 17 17:30:49 2004
;;;; Contains: Tests of READ-BYTE, WRITE-BYTE

(in-package :cl-test)

(deftest read-byte.1
  (let ((s (open "foo.txt"
sds's avatar
sds committed
10 11 12
                 :direction :output
                 :if-exists :supersede
                 :element-type '(unsigned-byte 8))))
13 14 15 16 17
    (values
     (write-byte 17 s)
     (close s)
     (progn
       (setq s (open "foo.txt"
sds's avatar
sds committed
18 19
                     :direction :input
                     :element-type '(unsigned-byte 8)))
20 21 22 23 24 25
       (read-byte s))
     (close s)))
  17 t 17 t)

(deftest read-byte.2
  (let ((s (open "foo.txt"
sds's avatar
sds committed
26 27 28
                 :direction :output
                 :if-exists :supersede
                 :element-type '(unsigned-byte 8))))
29 30 31 32
    (values
     (close s)
     (progn
        (setq s (open "foo.txt"
sds's avatar
sds committed
33 34 35
                     :direction :input
                     :element-type '(unsigned-byte 8)))
        (read-byte s nil 'foo))
36 37 38 39 40 41
     (read-byte s nil)
     (close s)))
  t foo nil t)

(deftest read-byte.3
  (loop with b1 = 0
sds's avatar
sds committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        and b2 = 0
        for i from 1 to 32
        do (let ((s (open "foo.txt"
                          :direction :output
                          :if-exists :supersede
                          :element-type `(unsigned-byte ,i))))
             (write-byte (1- (ash 1 i)) s)
             (write-byte 1 s)
             (close s))
        unless (let ((s (open "foo.txt"
                              :direction :input
                              :element-type `(unsigned-byte ,i))))
                 (prog1
                   (and (eql (setq b1 (read-byte s)) (1- (ash 1 i)))
                        (eql (setq b2 (read-byte s)) 1))
                   (close s)))
        collect (list i b1 b2))
59 60 61 62
  nil)

(deftest read-byte.4
  (loop with b1 = 0
sds's avatar
sds committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        and b2 = 0
        for i from 33 to 200 by 7
        do (let ((s (open "foo.txt"
                          :direction :output
                          :if-exists :supersede
                          :element-type `(unsigned-byte ,i))))
             (write-byte (1- (ash 1 i)) s)
             (write-byte 1 s)
             (close s))
        unless (let ((s (open "foo.txt"
                              :direction :input
                              :element-type `(unsigned-byte ,i))))
                 (prog1
                     (and (eql (setq b1 (read-byte s)) (1- (ash 1 i)))
                          (eql (setq b2 (read-byte s)) 1))
                   (close s)))
        collect (list i b1 b2))
80 81 82 83 84 85 86 87 88 89 90
  nil)

;;; Error tests

(deftest read-byte.error.1
  (signals-error (read-byte) program-error)
  t)

(deftest read-byte.error.2
  (progn
    (let ((s (open "foo.txt"
sds's avatar
sds committed
91 92 93
                   :direction :output
                   :if-exists :supersede
                  :element-type `(unsigned-byte 8))))
94 95 96
      (close s))
    (signals-error
     (let ((s (open "foo.txt"
sds's avatar
sds committed
97 98
                   :direction :input
                   :element-type '(unsigned-byte 8))))
99 100 101 102 103 104 105
       (read-byte s))
     end-of-file))
  t)

(deftest read-byte.error.3
  (progn
    (let ((s (open "foo.txt"
sds's avatar
sds committed
106 107
                   :direction :output
                   :if-exists :supersede)))
108 109 110 111
      (close s))
    (signals-error
     (let ((s (open "foo.txt" :direction :input)))
       (unwind-protect
sds's avatar
sds committed
112 113
           (read-byte s)
         (close s)))
114 115 116 117
     error))
  t)

(deftest read-byte.error.4
118 119
  (signals-error-always
   (progn
120
     (let ((s (open "foo.txt"
sds's avatar
sds committed
121 122 123
                    :direction :output
                    :if-exists :supersede
                    :element-type '(unsigned-byte 8))))
124 125
       (close s))
     (let ((s (open "foo.txt"
sds's avatar
sds committed
126 127
                    :direction :input
                    :element-type '(unsigned-byte 8))))
128
       (unwind-protect
sds's avatar
sds committed
129 130
           (read-byte s t)
         (close s))))
131 132
   end-of-file)
  t t)
133 134

(deftest read-byte.error.5
pfdietz's avatar
pfdietz committed
135
  (check-type-error #'read-byte #'streamp)
136 137 138 139 140
  nil)

(deftest read-byte.error.6
  (progn
    (let ((s (open "foo.txt"
sds's avatar
sds committed
141 142 143
                   :direction :output
                   :if-exists :supersede
                  :element-type '(unsigned-byte 8))))
144 145 146
      (close s))
    (signals-error
     (let ((s (open "foo.txt"
sds's avatar
sds committed
147 148
                   :direction :input
                   :element-type '(unsigned-byte 8))))
149
       (unwind-protect
sds's avatar
sds committed
150 151
           (read-byte s t t nil)
         (close s)))
152 153 154
     program-error))
  t)

sds's avatar
sds committed
155

156 157 158 159 160 161 162 163 164 165 166
(deftest write-byte.error.1
  (signals-error (write-byte) program-error)
  t)

(deftest write-byte.error.2
  (signals-error (write-byte 0) program-error)
  t)

(deftest write-byte.error.3
  (signals-error
   (let ((s (open "foo.txt"
sds's avatar
sds committed
167 168 169
                  :direction :output
                  :if-exists :supersede
                  :element-type '(unsigned-byte 8))))
170
     (unwind-protect
sds's avatar
sds committed
171
         (write 1 s nil)
172 173 174 175 176
       (close s)))
   program-error)
  t)

(deftest write-byte.error.4
pfdietz's avatar
pfdietz committed
177
  (check-type-error #'(lambda (x) (write-byte 0 x)) #'streamp)
178 179 180 181 182
  nil)

(deftest write-byte.error.5
   (signals-error
    (let ((s (open "foo.txt"
sds's avatar
sds committed
183 184
                   :direction :output
                   :if-exists :supersede)))
185
      (unwind-protect
sds's avatar
sds committed
186 187
          (write 1 s)
        (close s)))
188 189 190 191 192
    error)
   t)



sds's avatar
sds committed
193 194