Skip to content

Commit

Permalink
Add tests for byte buffer wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
athos committed Nov 14, 2023
1 parent c25f6a7 commit 85647f8
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions test/cljam/io/util/byte_buffer_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
(ns cljam.io.util.byte-buffer-test
(:require [cljam.io.util.byte-buffer :as bb]
[clojure.test :refer [deftest is testing]]))

(deftest make-lsb-byte-buffer-test
(let [bb (bb/make-lsb-byte-buffer (byte-array [0x01 0x23 0x45 0x67]))]
(is (= 0x67452301 (.getInt bb)))))

(deftest make-msb-byte-buffer-test
(let [bb (bb/make-msb-byte-buffer (byte-array [0x01 0x23 0x45 0x67]))]
(is (= 0x01234567 (.getInt bb)))))

(deftest allocate-lsb-byte-buffer-test
(let [bb (doto (bb/allocate-lsb-byte-buffer 8)
(.putLong 0x789ABCDEF0123456)
(.flip))]
(is (= (int 0xF0123456) (.getInt bb)))
(is (= (int 0x789ABCDE) (.getInt bb)))))

(deftest allocate-msb-byte-buffer-test
(let [bb (doto (bb/allocate-msb-byte-buffer 8)
(.putLong 0x789ABCDEF0123456)
(.flip))]
(is (= (int 0x789ABCDE) (.getInt bb)))
(is (= (int 0xF0123456) (.getInt bb)))))

(deftest read-ops-test
(let [bb (doto (bb/allocate-lsb-byte-buffer 8)
(.mark)
(.putLong 0x789ABCDEF0123456))]
(testing "read-ubyte"
(.reset bb)
(is (= 0x56 (bb/read-ubyte bb)))
(is (= 0x34 (bb/read-ubyte bb)))
(is (= 0x12 (bb/read-ubyte bb)))
(is (= 0xF0 (bb/read-ubyte bb)))
(is (= 0xDE (bb/read-ubyte bb)))
(is (= 0xBC (bb/read-ubyte bb)))
(is (= 0x9A (bb/read-ubyte bb)))
(is (= 0x78 (bb/read-ubyte bb))))

(testing "read-ushort"
(.reset bb)
(is (= 0x3456 (bb/read-ushort bb)))
(is (= 0xF012 (bb/read-ushort bb)))
(is (= 0xBCDE (bb/read-ushort bb)))
(is (= 0x789A (bb/read-ushort bb))))

(testing "read-unit"
(.reset bb)
(is (= 0xF0123456 (bb/read-uint bb)))
(is (= 0x789ABCDE (bb/read-uint bb))))

(testing "read-bytes"
(.reset bb)
(is (= [0x56 0x34 0x12 0xF0 0xDE 0xBC 0x9A 0x78]
(map #(bit-and % 0xFF) (bb/read-bytes bb 8))))

(.reset bb)
(bb/skip bb 1)
(is (= [0 0 0x34 0x12 0xF0 0]
(map #(bit-and % 0xFF) (bb/read-bytes bb (byte-array 6) 2 3)))))

(testing "read-string"
(.reset bb)
(doseq [c "ABCDEFGH"]
(.put bb (byte (int c))))
(.reset bb)
(is (= "ABCDEFGH" (bb/read-string bb 8))))

(testing "read-null-terminated-string"
(.reset bb)
(doseq [c [\I \J \K \L 0 \M \N \O]]
(.put bb (byte (int c))))
(.reset bb)
(is (= "IJKL" (bb/read-null-terminated-string bb))))))

(deftest write-ops-test
(let [bb (bb/allocate-msb-byte-buffer 8)]
(testing "write-ubyte"
(bb/write-ubyte bb 0x78)
(bb/write-ubyte bb 0x9A)
(bb/write-ubyte bb 0xBC)
(bb/write-ubyte bb 0xDE)
(bb/write-ubyte bb 0xF0)
(bb/write-ubyte bb 0x12)
(bb/write-ubyte bb 0x34)
(bb/write-ubyte bb 0x56)
(.flip bb)
(is (= 0x789ABCDEF0123456 (.getLong bb))))

(testing "write-ushort"
(.flip bb)
(bb/write-ushort bb 0x789A)
(bb/write-ushort bb 0xBCDE)
(bb/write-ushort bb 0xF012)
(bb/write-ushort bb 0x3456)
(.flip bb)
(is (= 0x789ABCDEF0123456 (.getLong bb))))

(testing "write-uint"
(.flip bb)
(bb/write-uint bb 0x789ABCDE)
(bb/write-uint bb 0xF0123456)
(.flip bb)
(is (= 0x789ABCDEF0123456 (.getLong bb))))

(testing "write-string"
(.flip bb)
(bb/write-string bb "ABCDEFGH")
(.flip bb)
(is (= "ABCDEFGH" (bb/read-string bb 8))))))

0 comments on commit 85647f8

Please sign in to comment.