~theothornhill/cl-bare

25a65feae46caa03af0e1ddd90be78e7eeb1733b — Theodor Thornhill 3 years ago ec77bee
Add bytes check to macro
1 files changed, 65 insertions(+), 36 deletions(-)

M tests/bare-tests.lisp
M tests/bare-tests.lisp => tests/bare-tests.lisp +65 -36
@@ 3,7 3,7 @@
(defmacro buf (vector)
  `(make-in-memory-input-stream ,vector))

(defmacro test-binary-type (type &key stream result)
(defmacro test-binary-type (type &key stream result bytes)
  `(with-input-from-sequence
       (in
        (with-output-to-sequence (out)


@@ 11,103 11,128 @@
            (let ((obj (read-binary ',type ,stream)))
              (ok (write-binary obj out))))))
     (testing (concatenate 'string "reading " (symbol-name ',type))
       (with-slots (value) (read-binary ',type in)
         ,(cond
            ((= (length result) 3)
             ;; If we want a specific form evaluated
             (cons (first result)
                   (list (append `,(second result) `(,(third result))))))
            ((consp (second result))
             ;; append the constructed value
             (cons (first result)
                   (list (append `,(second result) `(value)))))
            (t
             (cons (first result) `(value))))))))
       (multiple-value-bind (int bytes-read) (read-binary ',type in)
         (with-slots (value)int
           ,(cond
              ((= (length result) 3)
               ;; If we want a specific form evaluated
               (cons (first result)
                     (list (append `,(second result) `(,(third result))))))
              ((consp (second result))
               ;; append the constructed value
               (cons (first result)
                     (list (append `,(second result) `(value)))))
              (t
               (cons (first result) `(value)))))
         (ok (= ,bytes bytes-read))))))

(deftest read-uint
  (test-binary-type uint
    :stream (buf #(#x7F #xB7 #x26))
    :result (ok (= #x7f)))
    :result (ok (= #x7f))
    :bytes 1)
  (test-binary-type uint
    :stream (buf #(#xB7 #x26))
    :result (ok (= #x1337)))
    :result (ok (= #x1337))
    :bytes 2)
  (test-binary-type uint
    :stream (buf #(#x80 #x02))
    :result (ok (= #x100))))
    :result (ok (= #x100))
    :bytes 2)
  (test-binary-type uint
    :stream (buf #(#x8B #xCA #x99 #x01))
    :result (ok (= #x26650B))
    :bytes 4))

(deftest read-u8
  (test-binary-type u8
    :stream (buf #(#x42))
    :result (ok (= #x42))))
    :result (ok (= #x42))
    :bytes 1))

(deftest read-u16
  (test-binary-type u16
    :stream (buf #(#xFE #xCA))
    :result (ok (= #xCAFE))))
    :result (ok (= #xCAFE))
    :bytes 2))

(deftest read-u32
  (test-binary-type u32
    :stream (buf #(#xEF #xBE #xAD #xDE))
    :result (ok (= #xDEADBEEF))))
    :result (ok (= #xDEADBEEF))
    :bytes 4))

(deftest read-u64
  (test-binary-type u64
    :stream (buf #(#xEF #xBE #xAD #xDE #xBE #xBA #xFE #xCA))
    :result (ok (= #xcafebabedeadbeef))))
    :result (ok (= #xcafebabedeadbeef))
    :bytes 8))

(deftest read-int
  (test-binary-type int
    :stream (buf #(#x54 #xF1 #x14))
    :result (ok (= 42)))
    :result (ok (= 42))
    :bytes 1)
  (test-binary-type int
    :stream (buf #(#xF1 #x14))
    :result (ok (= -1337))))
    :result (ok (= -1337))
    :bytes 2))

(deftest read-i8
  (test-binary-type i8
    :stream (buf #(#xD6))
    :result (ok (= -42))))
    :result (ok (= -42))
    :bytes 1))

(deftest read-i16
  (test-binary-type i16
    :stream (buf #(#x2E #xFB))
    :result (ok (= -1234))))
    :result (ok (= -1234))
    :bytes 2))

(deftest read-i32
  (test-binary-type i32
    :stream (buf #(#xB2 #x9E #x43 #xFF))
    :result (ok (= -12345678))))
    :result (ok (= -12345678))
    :bytes 4))

(deftest read-i64
  (test-binary-type i64
    :stream (buf #(#x4F #x0B #x6E #x9D #xAB #x23 #xD4 #xFF))
    :result (ok (= -12345678987654321))))
    :result (ok (= -12345678987654321))
    :bytes 8))

(deftest read-f32
  (test-binary-type f32
    :stream (buf #(#x71 #x2D #xA7 #x44))
    :result (ok (= 1337.42f0))))
    :result (ok (= 1337.42f0))
    :bytes 4))

(deftest read-f64
  (test-binary-type f64
    :stream (buf #(#x9B #x6C #xC9 #x20 #xF0 #x21 #x3F #x42))
    :result (ok (= 133713371337.42424242d0))))
    :result (ok (= 133713371337.42424242d0))
    :bytes 8))

(deftest read-bool
  (test-binary-type bool
    :stream (buf #(#x00 #x01))
    :result (ng nil))
    :result (ng nil)
    :bytes 1)
  (test-binary-type bool
    :stream (buf #(#x01 #x02))
    :result (ok t))
    :result (ok t)
    :bytes 1)
  (test-binary-type bool
    :stream (buf #(#x02 #x01))
    :result (ok t)))
    :result (ok t)
    :bytes 1))

(deftest read-enum-value
  (test-binary-type enum
    :stream (buf #(#x01))
    :result (ok (= 1) (slot-value value 'value))))
    :result (ok (= 1) (slot-value value 'value))
    :bytes 1))

(deftest read-str
  (test-binary-type str


@@ 115,20 140,24 @@
		                  #x81 #xAB #xE3 #x81 #xA1 #xE3 #x81 #xAF
                      #xE3 #x80 #x81 #xE4 #xB8 #x96 #xE7 #x95
                      #x8C #xEF #xBC #x81))
    :result (ok (string= "こんにちは、世界!"))))
    :result (ok (string= "こんにちは、世界!"))
    :bytes 28))

(deftest read-data
  (test-binary-type data
    :stream (buf #(#x03 #x13 #x37 #x42))
    :result (ok #(19 55 66))))
    :result (ok #(19 55 66))
    :bytes 4))

(deftest read-data-fixed
  (let ((*data-length* 3))
    (test-binary-type data-fixed
      :stream (buf #(#x13 #x37 #x42))
      :result (ok (equalp #(19 55 66))))))
      :result (ok (equalp #(19 55 66)))
      :bytes 3)))

(deftest read-void
  (test-binary-type void
    :stream (buf #(#xff))
    :result (ok (null))))
    :result (ok (null))
    :bytes 0))