file-length.lsp 5.35 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12 13
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Wed Jan 21 06:21:11 2004
;;;; Contains: Tests of FILE-LENGTH

(in-package :cl-test)

(deftest file-length.error.1
  (signals-error (file-length) program-error)
  t)

(deftest file-length.error.2
  (signals-error
14
   (with-open-file (is "file-length.txt" :direction :input)
sds's avatar
sds committed
15
                   (file-length is nil))
pfdietz's avatar
pfdietz committed
16 17 18 19 20
   program-error)
  t)

(deftest file-length.error.3
  (loop for x in *mini-universe*
sds's avatar
sds committed
21 22 23 24 25 26 27 28
        unless (or (typep x 'file-stream)
                   (typep x 'broadcast-stream)
                   (handler-case (progn (file-length x) nil)
                                 (type-error (c)
                                             (assert (not (typep x (type-error-expected-type c))))
                                             t)
                                 (condition () nil)))
        collect x)
pfdietz's avatar
pfdietz committed
29 30 31
  nil)

(deftest file-length.error.4
32
  :notes (:assume-no-simple-streams :assume-no-gray-streams)
pfdietz's avatar
pfdietz committed
33
  (signals-error (with-input-from-string (s "abc") (file-length s))
sds's avatar
sds committed
34
                 type-error)
pfdietz's avatar
pfdietz committed
35 36
  t)

pfdietz's avatar
pfdietz committed
37 38 39
(deftest file-length.error.5
  (signals-error
   (with-open-file
40
    (is "file-length.txt" :direction :input)
pfdietz's avatar
pfdietz committed
41 42 43 44 45 46 47 48 49 50
    (with-open-file
     (os "tmp.txt" :direction :output :if-exists :supersede)
     (let ((s (make-two-way-stream is os)))
       (unwind-protect (file-length s) (close s)))))
   type-error)
  t)

(deftest file-length.error.6
  (signals-error
   (with-open-file
51
    (is "file-length.txt" :direction :input)
pfdietz's avatar
pfdietz committed
52 53 54 55 56 57 58 59
    (with-open-file
     (os "tmp.txt" :direction :output :if-exists :supersede)
     (let ((s (make-echo-stream is os)))
       (unwind-protect (file-length s) (close s)))))
   type-error)
  t)

(deftest file-length.error.8
60 61 62 63
  (with-open-file
   (os "tmp.txt" :direction :output :if-exists :supersede)
   (let ((s (make-broadcast-stream os)))
     (eqlt (file-length s) (file-length os))))
pfdietz's avatar
pfdietz committed
64 65 66
  t)

(deftest file-length.error.9
67
  (signals-type-error s (make-concatenated-stream)
sds's avatar
sds committed
68
                      (unwind-protect (file-length s) (close s)))
pfdietz's avatar
pfdietz committed
69 70 71 72 73
  t)

(deftest file-length.error.10
  (signals-error
   (with-open-file
74
    (is "file-length.txt" :direction :input)
pfdietz's avatar
pfdietz committed
75 76 77 78 79 80
    (let ((s (make-concatenated-stream is)))
      (unwind-protect (file-length s) (close s))))
   type-error)
  t)

(deftest file-length.error.11
81
  :notes (:assume-no-simple-streams :assume-no-gray-streams)
82
  (signals-type-error s (make-string-input-stream "abcde")
sds's avatar
sds committed
83
                      (unwind-protect (file-length s) (close s)))
pfdietz's avatar
pfdietz committed
84 85 86
  t)

(deftest file-length.error.12
87
  :notes (:assume-no-simple-streams :assume-no-gray-streams)
88
  (signals-type-error s (make-string-output-stream)
sds's avatar
sds committed
89
                      (unwind-protect (file-length s) (close s)))
pfdietz's avatar
pfdietz committed
90 91
  t)

pfdietz's avatar
pfdietz committed
92 93 94 95
;;; Non-error tests

(deftest file-length.1
  (let ((results (multiple-value-list
sds's avatar
sds committed
96
                  (with-open-file
97
                   (is "file-length.txt" :direction :input)
sds's avatar
sds committed
98
                   (file-length is)))))
pfdietz's avatar
pfdietz committed
99
    (and (= (length results) 1)
sds's avatar
sds committed
100 101
         (typep (car results) '(integer 1))
         t))
pfdietz's avatar
pfdietz committed
102 103 104 105
  t)

(deftest file-length.2
  (loop for i from 1 to 32
sds's avatar
sds committed
106 107 108 109 110 111 112 113 114 115
        for etype = `(unsigned-byte ,i)
        for e = (max 0 (- (ash 1 i) 5))
        for os = (open "tmp.dat" :direction :output
                               :if-exists :supersede
                               :element-type etype)
        do (loop repeat 17 do (write-byte e os))
        do (finish-output os)
        unless (= (file-length os) 17)
        collect (list i (file-length os))
        do (close os))
pfdietz's avatar
pfdietz committed
116 117 118 119
  nil)

(deftest file-length.3
  (loop for i from 1 to 32
sds's avatar
sds committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
        for etype = `(unsigned-byte ,i)
        for e = (max 0 (- (ash 1 i) 5))
        for os = (open "tmp.dat" :direction :output
                               :if-exists :supersede
                               :element-type etype)
        for len = 0
        do (loop repeat 17 do (write-byte e os))
        do (close os)
        unless (let ((is (open "tmp.dat" :direction :input
                               :element-type etype)))
                 (prog1
                     (= (file-length is) 17)
                   (close is)))
        collect i)
pfdietz's avatar
pfdietz committed
134 135 136 137
  nil)

(deftest file-length.4
  (loop for i from 33 to 100
sds's avatar
sds committed
138 139 140 141 142 143 144 145 146 147
        for etype = `(unsigned-byte ,i)
        for e = (max 0 (- (ash 1 i) 5))
        for os = (open "tmp.dat" :direction :output
                               :if-exists :supersede
                               :element-type etype)
        do (loop repeat 17 do (write-byte e os))
        do (finish-output os)
        unless (= (file-length os) 17)
        collect (list i (file-length os))
        do (close os))
pfdietz's avatar
pfdietz committed
148 149 150 151
  nil)

(deftest file-length.5
  (loop for i from 33 to 100
sds's avatar
sds committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        for etype = `(unsigned-byte ,i)
        for e = (max 0 (- (ash 1 i) 5))
        for os = (open "tmp.dat" :direction :output
                               :if-exists :supersede
                               :element-type etype)
        for len = 0
        do (loop repeat 17 do (write-byte e os))
        do (close os)
        unless (let ((is (open "tmp.dat" :direction :input
                               :element-type etype)))
                 (prog1
                     (= (file-length is) 17)
                   (close is)))
        collect i)
  nil)
pfdietz's avatar
pfdietz committed
167 168 169

(deftest file-length.6
  (with-open-file
170
   (*foo* "file-length.txt" :direction :input)
pfdietz's avatar
pfdietz committed
171 172 173
   (declare (special *foo*))
   (let ((s (make-synonym-stream '*foo*)))
     (unwind-protect
sds's avatar
sds committed
174 175
         (typep* (file-length s) '(integer 1))
        (close s))))
pfdietz's avatar
pfdietz committed
176
  t)