~theothornhill/cl-bare

eb0f11d18d79853c8cb4c702bdeb77cc00d25872 — Theodor Thornhill 3 years ago fd74520
Array type
3 files changed, 35 insertions(+), 0 deletions(-)

M src/bare.lisp
M src/package.lisp
M tests/bare-tests.lisp
M src/bare.lisp => src/bare.lisp +13 -0
@@ 146,6 146,9 @@ Set this dynamically in the function calling the DATA defbinary.")
    ()
    (value nil :type null))


;;; Aggregate types

(defun read-optional-type (type stream)
  (when (read-byte stream)
    (read-binary type stream)))


@@ 156,3 159,13 @@ Set this dynamically in the function calling the DATA defbinary.")
      (progn
        (write-byte 1 stream)
        (write-binary object stream))))

(defun read-array (type length stream)
  (let (res)
    (dotimes (i length)
      (push (read-binary type stream) res))
    (nreverse res)))

(defun write-array (objects stream)
  (dolist (object objects)
    (write-binary object stream)))

M src/package.lisp => src/package.lisp +2 -0
@@ 22,4 22,6 @@
   :void
   :read-optional-type
   :write-optional-type
   :read-array
   :write-array
   :value))

M tests/bare-tests.lisp => tests/bare-tests.lisp +20 -0
@@ 166,6 166,8 @@
    :result (ok (null))
    :bytes 0))

;;; Aggregate types

(deftest optional-type-test
  (testing "reading and writing optional type when not set"
    (with-input-from-sequence


@@ 187,3 189,21 @@
      (multiple-value-bind (object) (read-optional-type 'u8 in)
        (with-slots (value) object
          (ok (equalp value 112)))))))

(deftest array-type-test
  (testing "reading and writing array type when not set"
    (with-input-from-sequence
        (in
         (with-output-to-sequence (out)
           (write-array
            (read-array 'u8 4 (buf #(#x11 #x22 #x33 #x44)))
            out)))
      (let ((objects (read-array 'u8 4 in)))
        (with-slots (value) (first objects)
          (ok (= value 17)))
        (with-slots (value) (second objects)
          (ok (= value 34)))
        (with-slots (value) (third objects)
          (ok (= value 51)))
        (with-slots (value) (fourth objects)
          (ok (= value 68)))))))