Skip to content

Commit

Permalink
Adapt platform independent tests for run with both implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Antukh committed Feb 28, 2015
1 parent 9220d2f commit bbd00d2
Showing 1 changed file with 103 additions and 76 deletions.
179 changes: 103 additions & 76 deletions test/bytebuf/tests/core.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,46 @@
(:require #+clj [clojure.test :as t]
#+cljs [cljs.test :as t]
[bytebuf.core :as buf]
[bytebuf.buffer :as impl]
[bytebuf.bytes :as bytes])
[bytebuf.buffer :as impl])
#+clj
(:import java.nio.ByteBuffer
io.netty.buffer.ByteBuf))


#+clj
(defn random-bytes
[^long numbytes]
(let [sr (java.util.Random.)
buffer (byte-array numbytes)]
(.nextBytes sr buffer)
buffer))

#+cljs
(defn random-bytes
[^long numbytes]
(let [buffer (js/Int8Array. numbytes)]
(doseq [i (range numbytes)]
(aset buffer i (rand-int 10)))
buffer))

#+clj
(defn equals?
"Check if two byte arrays are equals."
[^bytes a ^bytes b]
(java.util.Arrays/equals a b))

#+cljs
(defn equals?
[^bytes a ^bytes b]
(if (not= (.-length a) (.-length b))
false
(reduce (fn [acc i]
(if (not= (aget a i) (aget b i))
(reduced false)
true))
true
(range (.-length a)))))

#+clj
(t/deftest allocate-heap-nio-buffer
(let [buffer (buf/allocate 16)]
Expand Down Expand Up @@ -51,75 +85,65 @@
(let [buffer (buf/allocate 16 {:impl :es6})]
(t/is (instance? js/DataView buffer))))

;; #+cljs
;; (t/deftest allocate-direct-node-buffer
;; (let [bf (js/require "buffer")
;; bf (.-Buffer bf)
;; buffer (buf/allocate 16 {:impl :node})]
;; (t/is (instance? bf buffer))))

#+clj
(t/deftest spec-constructor
(let [spec (buf/spec :field1 (buf/int32)
:field2 (buf/int64))]
:field2 (buf/int16))]
(t/is (= (count spec) 2))
(t/is (= (buf/size spec) 12))))
(t/is (= (buf/size spec) 6))))

#+clj
(t/deftest associative-specs-write
(let [spec (buf/spec :field1 (buf/int32)
:field2 (buf/int64))
:field2 (buf/int16))
buffer (buf/allocate 12)
data {:field1 1 :field2 4}]
(t/is (= (buf/write! buffer data spec) 12))
(t/is (= (.getInt buffer 0) 1))
(t/is (= (.getLong buffer 4) 4))))
(t/is (= (buf/write! buffer data spec) 6))
(t/is (= (impl/read-int buffer 0) 1))
(t/is (= (impl/read-short buffer 4) 4))))

#+clj
(t/deftest associative-specs-write-with-offset
(let [spec (buf/spec :field1 (buf/int32))
buffer (buf/allocate 12)
data {:field1 500}]
(t/is (= (buf/write! buffer data spec {:offset 3}) 4))
(t/is (= (.getInt buffer 3) 500))))
(t/is (= (impl/read-int buffer 3) 500))))

#+clj
(t/deftest associative-specs-write-wrong-buffer
;; write data to wrong buffer (less space)
(let [spec (buf/spec :field1 (buf/int32))
buffer (buf/allocate 2)
data {:field1 1}]
#+clj
(t/is (thrown? java.lang.IndexOutOfBoundsException
(buf/write! buffer data spec) 12))))
(buf/write! buffer data spec) 12))
#+cljs
(t/is (thrown? js/Error
(buf/write! buffer data spec) 12))))

#+clj
(t/deftest associative-specs-read
(let [spec (buf/spec :field1 (buf/int32)
:field2 (buf/int64))
:field2 (buf/int16))
buffer (buf/allocate 12)]
(.putInt buffer 0 10)
(.putLong buffer 4 100)
(impl/write-int buffer 0 10)
(impl/write-short buffer 4 100)
(let [[readed data] (buf/read* buffer spec)]
(t/is (= readed 12))
(t/is (= readed 6))
(t/is (= data {:field2 100 :field1 10})))))

#+clj
(t/deftest associative-specs-read-with-offset
(let [spec (buf/spec :field1 (buf/int32))
buffer (buf/allocate 12)]
(.putInt buffer 8 1000)
(impl/write-int buffer 8 1000)
(let [[readed data] (buf/read* buffer spec {:offset 8})]
(t/is (= readed 4))
(t/is (= data {:field1 1000})))))

#+clj
(t/deftest indexed-specs-write
(let [spec (buf/spec (buf/int32) (buf/int64))
(let [spec (buf/spec (buf/int32) (buf/int16))
buffer (buf/allocate 12)
data [1 4]]
(t/is (= (buf/write! buffer data spec) 12))
(t/is (= (.getInt buffer 0) 1))
(t/is (= (.getLong buffer 4) 4))))
(t/is (= (buf/write! buffer data spec) 6))
(t/is (= (impl/read-int buffer 0) 1))
(t/is (= (impl/read-short buffer 4) 4))))

#+clj
(t/deftest indexed-specs-write-with-offset
Expand All @@ -129,36 +153,38 @@
(t/is (= (buf/write! buffer data spec {:offset 3}) 4))
(t/is (= (.getInt buffer 3) 500))))

#+clj

(t/deftest indexed-specs-write-wrong-buffer
(let [spec (buf/spec (buf/int32))
buffer (buf/allocate 2)
data [1]]
#+clj
(t/is (thrown? java.lang.IndexOutOfBoundsException
(buf/write! buffer data spec) 12))))
(buf/write! buffer data spec) 12))
#+cljs
(t/is (thrown? js/Error
(buf/write! buffer data spec) 12))))

#+clj
(t/deftest indexed-specs-read
(let [spec (buf/spec (buf/int32) (buf/int64))
(let [spec (buf/spec (buf/int32) (buf/int16))
buffer (buf/allocate 12)]
(.putInt buffer 0 10)
(.putLong buffer 4 100)
(impl/write-int buffer 0 10)
(impl/write-short buffer 4 100)
(let [[readed data] (buf/read* buffer spec)]
(t/is (= readed 12))
(t/is (= readed 6))
(t/is (= data [10 100])))))

#+clj
(t/deftest indexed-specs-read-with-offset
(let [spec (buf/spec (buf/int32))
buffer (buf/allocate 12)]
(.putInt buffer 8 1000)
(impl/write-int buffer 8 1000)
(let [[readed data] (buf/read* buffer spec {:offset 8})]
(t/is (= readed 4))
(t/is (= data [1000])))))

#+clj
(t/deftest spec-data-types
(let [data [;; (buf/string 5) "12345"
(let [data [(buf/string 5) "12345"
(buf/short) 100
(buf/long) 1002
(buf/integer) 1001
Expand All @@ -173,7 +199,6 @@
(buf/allocate (buf/size spec) {:type :heap :impl :netty})
(buf/allocate (buf/size spec) {:type :direct :impl :netty})]]
(doseq [buffer buffers]
;; (println buffer spec)
(let [written (buf/write! buffer data spec)]
(t/is (= written (buf/size spec)))
(let [[readed data'] (buf/read* buffer spec)]
Expand All @@ -182,48 +207,50 @@

#+cljs
(t/deftest spec-data-types
(let [data [;; (buf/string 5) "12345"
(let [data [(buf/string 5) "äåéëþ"
(buf/short) 100
(buf/integer) 1001
(buf/bool) false
(buf/double) (double 4.3)
(buf/float) (float 3.5)
(buf/byte) (byte 32)]]
;; (buf/bytes 5) (bytes/random-bytes 5)]]
(buf/byte) (byte 32)
(buf/bytes 5) (random-bytes 5)]]
(doseq [[spec data] (partition 2 data)]
(let [buffers [(buf/allocate (buf/size spec) {:impl :es6})]]
(doseq [buffer buffers]
(let [written (buf/write! buffer data spec)]
(t/is (= written (buf/size spec)))
(let [[readed data'] (buf/read* buffer spec)]
(t/is (= readed (buf/size spec)))
(t/is (= data data')))))))))
(cond
(instance? js/Int8Array data')
(t/is (equals? data data'))

:else
(t/is (= data data'))))))))))

(t/deftest spec-data-with-dynamic-types-single
(let [spec (buf/spec (buf/string*))
buffer (buf/allocate 20)]
(buf/write! buffer ["1234567890"] spec)
(let [[readed data] (buf/read* buffer spec)]
(t/is (= readed 14))
(t/is (= data ["1234567890"])))))

(t/deftest spec-data-with-dynamic-types-combined
(let [spec (buf/spec (buf/string*) (buf/int32))
buffer (buf/allocate 40)]
(buf/write! buffer ["1234567890" 1000] spec)
(let [[readed data] (buf/read* buffer spec)]
(t/is (= readed 18))
(t/is (= data ["1234567890" 1000])))))

;; #+clj
;; (t/deftest spec-data-with-dynamic-types-single
;; (let [spec (buf/spec (buf/string*))
;; buffer (buf/allocate 20)]
;; (buf/write! buffer ["1234567890"] spec)
;; (let [[readed data] (buf/read* buffer spec)]
;; (t/is (= readed 14))
;; (t/is (= data ["1234567890"])))))

;; #+clj
;; (t/deftest spec-data-with-dynamic-types-combined
;; (let [spec (buf/spec (buf/string*) (buf/int64))
;; buffer (buf/allocate 40)]
;; (buf/write! buffer ["1234567890" 1000] spec)
;; (let [[readed data] (buf/read* buffer spec)]
;; (t/is (= readed 22))
;; (t/is (= data ["1234567890" 1000])))))

;; #+clj
;; (t/deftest spec-composition
;; (let [spec (buf/spec (buf/compose-type (buf/int32) (buf/int32))
;; (buf/compose-type (buf/string 10) (buf/string 5)))
;; buffer (buf/allocate (buf/size spec))
;; data [[100 200] ["foo" "bar"]]]
;; (buf/write! buffer data spec)
;; (let [[readed data'] (buf/read* buffer spec)]
;; (t/is (= readed (buf/size spec)))
;; (t/is (= data' data)))))
(t/deftest spec-composition
(let [spec (buf/spec (buf/compose-type (buf/int32) (buf/int32))
(buf/compose-type (buf/string 10) (buf/string 5)))
buffer (buf/allocate (buf/size spec))
data [[100 200] ["foo" "bar"]]]
(buf/write! buffer data spec)
(let [[readed data'] (buf/read* buffer spec)]
(t/is (= readed (buf/size spec)))
(t/is (= data' data)))))

0 comments on commit bbd00d2

Please sign in to comment.