Skip to content

Commit 8a8b81a

Browse files
committed
Fix a bug and write a test for the :include option.
1 parent 4ac990c commit 8a8b81a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

simple-bit-stream.lisp

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ can be discarded if BYTE-ALIGNED-P returns T."))
5757
(cond
5858
((= (slot-value stream 'bits-left) 0)
5959
(setf (slot-value stream 'last-byte)
60-
(read-byte (slot-value stream 'real-stream)))
60+
(restart-case (read-byte (slot-value stream 'real-stream))
61+
(continue ()
62+
:report "Pretend the read returned a 0 byte."
63+
0)))
6164
(setf (slot-value stream 'bits-left)
6265
(slot-value stream 'element-bits))
6366
(read-partial-byte/big-endian bits stream))
@@ -80,7 +83,10 @@ can be discarded if BYTE-ALIGNED-P returns T."))
8083
(cond
8184
((= (slot-value stream 'bits-left) 0)
8285
(setf (slot-value stream 'last-byte)
83-
(read-byte (slot-value stream 'real-stream)))
86+
(restart-case (read-byte (slot-value stream 'real-stream))
87+
(continue ()
88+
:report "Pretend the read returned a 0 byte."
89+
0)))
8490
(setf (slot-value stream 'bits-left)
8591
(slot-value stream 'element-bits))
8692
(read-partial-byte/little-endian bits stream))

test/basic-test.lisp

+40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
(or (= ,x* ,y*)
2626
(error "~S != ~S" ,x* ,y*)))))
2727

28+
(defmacro assert-eq (x y)
29+
(let ((x* (gensym "X"))
30+
(y* (gensym "Y")))
31+
`(let ((,x* ,x)
32+
(,y* ,y))
33+
(or (eq ,x* ,y*)
34+
(error "~S is not EQ to ~S" ,x* ,y*)))))
35+
2836
(defmacro assert-equal (x y)
2937
(let ((x* (gensym "X"))
3038
(y* (gensym "Y")))
@@ -319,6 +327,38 @@
319327
(read-binary 'implicit-bit-stream
320328
*standard-input*)))))))
321329

330+
331+
(defbinary example (:untyped-struct t)
332+
(a 0 :type (unsigned-byte 24))
333+
(b 0 :type (magic :actual-type (unsigned-byte 4)
334+
:value 5))
335+
(c 0 :type (unsigned-byte 20)))
336+
337+
338+
(unit-test 'untyped-struct-test
339+
(flexi-streams:with-input-from-sequence (in '(0 0 0))
340+
(handler-bind
341+
(((or end-of-file bad-magic-value)
342+
(lambda (cond)
343+
(declare (ignore cond))
344+
(invoke-restart 'continue))))
345+
(let ((obj (read-binary 'example in)))
346+
(assert= (slot-value obj 'a) 0)
347+
(assert= (slot-value obj 'b) 0)
348+
(assert= (slot-value obj 'c) 0)))))
349+
350+
(defbinary example-2 (:include example)
351+
(d 0 :type (unsigned-byte 8)))
352+
353+
(unit-test 'include-test
354+
(let ((obj (make-example-2 :a #xbedead
355+
:b 4
356+
:c #xbefed
357+
:d #xff)))
358+
(test-round-trip ":INCLUDE test"
359+
(write-binary obj *standard-output*)
360+
(assert-equalp obj (read-binary 'example-2 *standard-input*)))))
361+
322362
(defun run-test ()
323363
(let ((test-results (do-tests)))
324364
(format t ">>>>>>>>>>>>>>>>>>>>>>>> TEST RESULTS: ~S~%" test-results)

0 commit comments

Comments
 (0)