Skip to content

Commit e9066e3

Browse files
author
Symbolics
committed
Address SBCL warnings, add tests for composite types
1 parent 42d4eae commit e9066e3

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

binary-types.asd

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:description "A library for reading and writing binary records."
88
:long-description #.(uiop:read-file-string
99
(uiop:subpathname *load-pathname* "description.text"))
10-
:version "1.0.0"
10+
:version "1.0.1"
1111
:author "Frode V. Fjeld"
1212
:maintainer "Steven Nunez"
1313
:license :BSD-3-Clause

binary-types.lisp

+30-28
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,6 @@ means that the endianess is determined by the dynamic value of *endian*."
3535
"*endian* must be (dynamically) bound to either :big-endian or
3636
:little-endian while reading endian-sensitive types.")
3737

38-
;;; ----------------------------------------------------------------
39-
;;; Binary Types Namespace
40-
;;; ----------------------------------------------------------------
41-
42-
(defvar *binary-type-namespace* (make-hash-table :test #'eq)
43-
"Maps binary type's names (which are symbols) to their binary-type class object.")
44-
45-
(defun find-binary-type (name &optional (errorp t))
46-
(or (gethash name *binary-type-namespace*)
47-
(if errorp
48-
(error "Unable to find binary type named ~S." name)
49-
nil)))
50-
51-
(defun (setf find-binary-type) (value name)
52-
(check-type value binary-type)
53-
(let ((old-value (find-binary-type name nil)))
54-
(when (and old-value (not (eq (class-of value) (class-of old-value))))
55-
(warn "Redefining binary-type ~A from ~A to ~A."
56-
name (type-of old-value) (type-of value))))
57-
(setf (gethash name *binary-type-namespace*) value))
58-
59-
(defun find-binary-type-name (type)
60-
(maphash #'(lambda (key val)
61-
(when (eq type val)
62-
(return-from find-binary-type-name key)))
63-
*binary-type-namespace*))
64-
6538
;;; ----------------------------------------------------------------
6639
;;; Base Binary Type (Abstract)
6740
;;; ----------------------------------------------------------------
@@ -102,6 +75,33 @@ means that the endianess is determined by the dynamic value of *endian*."
10275
(print-unreadable-object (object stream :type 'binary-type)
10376
(format stream "~A" (binary-type-name object))))
10477

78+
;;; ----------------------------------------------------------------
79+
;;; Binary Types Namespace
80+
;;; ----------------------------------------------------------------
81+
82+
(defvar *binary-type-namespace* (make-hash-table :test #'eq)
83+
"Maps binary type's names (which are symbols) to their binary-type class object.")
84+
85+
(defun find-binary-type (name &optional (errorp t))
86+
(or (gethash name *binary-type-namespace*)
87+
(if errorp
88+
(error "Unable to find binary type named ~S." name)
89+
nil)))
90+
91+
(defun (setf find-binary-type) (value name)
92+
(check-type value binary-type)
93+
(let ((old-value (find-binary-type name nil)))
94+
(when (and old-value (not (eq (class-of value) (class-of old-value))))
95+
(warn "Redefining binary-type ~A from ~A to ~A."
96+
name (type-of old-value) (type-of value))))
97+
(setf (gethash name *binary-type-namespace*) value))
98+
99+
(defun find-binary-type-name (type)
100+
(maphash #'(lambda (key val)
101+
(when (eq type val)
102+
(return-from find-binary-type-name key)))
103+
*binary-type-namespace*))
104+
105105
;;; ----------------------------------------------------------------
106106
;;; Integer Type (Abstract)
107107
;;; ----------------------------------------------------------------
@@ -1042,6 +1042,8 @@ binding is shadowed."
10421042
(funcall ,save-brb-var s)))))
10431043
,@body)))
10441044

1045+
;; Siebel, and SBCL, warn against this style. Consider making them
1046+
;; all keyword parameters in the next release.
10451047
(defmacro with-binary-output-to-vector
10461048
((stream-var &optional (vector-or-size-form 0)
10471049
&key (adjustable (and (integerp vector-or-size-form)
@@ -1153,7 +1155,7 @@ otherwise the value of BODY."
11531155
(check-type size (integer 1 *))
11541156
(check-type endian endianess)
11551157
`(progn
1156-
(deftype ,name () '(ieee-754 ,(* 8 size)))
1158+
(deftype ,name () 'float)
11571159
(setf (find-binary-type ',name)
11581160
(make-instance 'binary-float
11591161
'name ',name

pkgdcl.lisp

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
;; type defining macros
3030
#:define-unsigned ; [macro] declare an unsigned-int type
3131
#:define-signed ; [macro] declare a signed-int type
32+
#:define-float ; [macro] declare a IEEE-754 float
3233
#:define-binary-struct ; [macro] declare a binary defstruct type
3334
#:define-binary-class ; [macro] declare a binary defclass type
3435
#:define-binary-vector ; [macro] declare a binary vector type

tests/tests.lisp

+40
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,43 @@
294294
;; "quad precision" (128-bit) we'll add the tests.
295295

296296

297+
298+
(deftest composite (binary-types)
299+
300+
;; vector of vectors
301+
(let* ((binary-types:*endian* :little-endian)
302+
(test-vector #(#(0.9143338203430176d0 0.21972346305847168d0 0.9707512855529785d0 0.5962116718292236d0 0.6005609035491943d0 0.5940588712692261d0 0.2837725877761841d0 0.009566903114318848d0 0.8435225486755371d0 0.22492897510528564d0)
303+
#(0.8314709663391113d0 0.2795267105102539d0 0.5844146013259888d0 0.7568612098693848d0 0.9189847707748413d0 0.007325291633605957d0 0.3114813566207886d0 0.5958571434020996d0 0.07142329216003418d0 0.7225879430770874d0)
304+
#(0.6982585191726685d0 0.42384862899780273d0 0.8679864406585693d0 0.3627190589904785d0 0.3574702739715576d0 0.7974770069122314d0 0.5154801607131958d0 0.4812943935394287d0 0.48626482486724854d0 0.9495172500610352d0)))
305+
binary-to
306+
binary-from)
307+
308+
(eval `(define-binary-vector bve f64 10)) ;binary vector elements
309+
(eval `(define-binary-vector binary-vec bve 3)) ;the outmost vector
310+
(setf binary-to (with-output-to-sequence (out)
311+
(write-binary 'binary-vec out test-vector)))
312+
(setf binary-from (with-input-from-sequence (in binary-to)
313+
(read-binary 'binary-vec in)))
314+
(assert-true (num= test-vector binary-from)))
315+
316+
;; vector of arrays
317+
;; This really is an optional functionality. If you need to read vectors of arrays, do it in a loop
318+
#+(or)
319+
(let* ((binary-types:*endian* :little-endian)
320+
#+nil
321+
(test-vector `#(,(aops:rand '(3 3)) ;a vector of 4 3x3 arrays of single-float
322+
,(aops:rand '(3 3))
323+
,(aops:rand '(3 3))
324+
,(aops:rand '(3 3))))
325+
binary-to
326+
binary-from)
327+
328+
(eval `(define-binary-array bae f32 '(3 3))) ;binary array elements, a 3x3 array of single-float
329+
(eval `(define-binary-vector binary-vec bae 4)) ;vector of 4 'bae
330+
(setf binary-to (with-output-to-sequence (out)
331+
(write-binary 'binary-vec out test-vector)))
332+
(setf binary-from (with-input-from-sequence (in binary-to)
333+
(read-binary 'binary-vec in)))
334+
(assert-true (num= test-vector binary-from)))
335+
336+
)

0 commit comments

Comments
 (0)