Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cloojure committed Jan 24, 2019
1 parent 9e67f79 commit 41aed61
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 107 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject tupelo "0.9.121"
(defproject tupelo "0.9.122"
:description "Tupelo: Clojure With A Spoonful of Honey"
:url "http://github.com/cloojure/tupelo"
:license {:name "Eclipse Public License"
Expand Down
86 changes: 10 additions & 76 deletions src/clj/tupelo/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,10 @@
(:use tupelo.core)
(:refer-clojure :exclude [read-string])
(:require
[schema.core :as s])
[schema.core :as s]
[tupelo.types :as types] )
(:import [java.io File DataInputStream DataOutputStream InputStream OutputStream]))

;---------------------------------------------------------------------------------------------------
; #todo move interval stuff -> tupelo.types

(def BYTE_UNSIGNED_MIN_VALUE 0)
(def BYTE_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 8)
(dec)
(long)))

(def SHORT_UNSIGNED_MIN_VALUE 0)
(def SHORT_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 16)
(dec)
(long)))

(defrecord ^:no-doc IntervalClosed ; #todo report defrecord "resolve" to Cursive
[lower-bound upper-bound]) ; #todo report to Cursive

(s/defn ^:no-doc within-interval-closed? :- s/Bool
"Returns true if val fits within an IntervalClosed."
[ic :- IntervalClosed
val :- s/Num]
(<= (:lower-bound ic) val (:upper-bound ic)))

(def ^:no-doc interval-closed-byte (->IntervalClosed Byte/MIN_VALUE Byte/MAX_VALUE)) ; #todo "resolve" report to Cursive
(def ^:no-doc interval-closed-byte-unsigned (->IntervalClosed BYTE_UNSIGNED_MIN_VALUE BYTE_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-short (->IntervalClosed Short/MIN_VALUE Short/MAX_VALUE))
(def ^:no-doc interval-closed-short-unsigned (->IntervalClosed SHORT_UNSIGNED_MIN_VALUE SHORT_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-integer (->IntervalClosed Integer/MIN_VALUE Integer/MAX_VALUE))
(def ^:no-doc interval-closed-long (->IntervalClosed Long/MIN_VALUE Long/MAX_VALUE))

(s/defn within-bounds-byte? :- s/Bool
"Returns true if val fits within legal range for a byte (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte val))

(s/defn within-bounds-byte-unsigned? :- s/Bool
"Returns true if val fits within legal range for a byte (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte-unsigned val))

(s/defn within-bounds-short? :- s/Bool
"Returns true if val fits within legal range for a short (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-short val))

(s/defn within-bounds-short-unsigned? :- s/Bool
"Returns true if val fits within legal range for a short (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-short-unsigned val))

(s/defn within-bounds-integer? :- s/Bool
"Returns true if val fits within legal range for a integer (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-integer val))

(s/defn within-bounds-long? :- s/Bool
"Returns true if val fits within legal range for a long (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-long val))

;---------------------------------------------------------------------------------------------------
(s/defn input-stream?
"Returns true if arg implements java.io.InputStream"
Expand All @@ -97,8 +37,7 @@
[id-str :- s/Str]
(let [tmp-file (File/createTempFile id-str nil)]
(.deleteOnExit tmp-file)
tmp-file ))

tmp-file))

(s/defn read-bytes ; #todo type?
"Reads N bytes from the DataInputStream and returns them in a byte array."
Expand All @@ -108,11 +47,10 @@
(.read (validate input-stream? input-stream) bytarr)
bytarr))

(s/defn write-bytes ; #todo type?
(s/defn write-bytes ; #todo type?
"Writes a byte array to a DataInputStream."
[out-stream :- OutputStream
bytarr :- s/Any] ; #todo type

(.write (validate output-stream? out-stream) bytarr)
bytarr)

Expand Down Expand Up @@ -159,47 +97,47 @@
[dos :- DataOutputStream
val :- s/Int]
(.writeByte (validate data-output-stream? dos)
(validate within-bounds-byte? val))
(validate types/within-bounds-byte? val))
val)

(s/defn write-byte-unsigned :- s/Int ; #todo need test
"Writes 1 byte (unsigned) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeByte (validate data-output-stream? dos)
(validate within-bounds-byte-unsigned? val))
(validate types/within-bounds-byte-unsigned? val))
val)

(s/defn write-short :- s/Int ; #todo need test
"Writes 2 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeShort (validate data-output-stream? dos)
(validate within-bounds-short? val))
(validate types/within-bounds-short? val))
val)

(s/defn write-short-unsigned :- s/Int ; #todo need test
"Writes 2 bytes (unsigned) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeShort (validate data-output-stream? dos)
(validate within-bounds-short-unsigned? val))
(validate types/within-bounds-short-unsigned? val))
val)

(s/defn write-int :- s/Int ; #todo need test
"Writes 4 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeInt (validate data-output-stream? dos)
(validate within-bounds-integer? val))
(validate types/within-bounds-integer? val))
val)

(s/defn write-long :- s/Int ; #todo need test
"Writes 8 bytes (signed) to a DataOutputStream"
[dos :- DataOutputStream
val :- s/Int]
(.writeLong (validate data-output-stream? dos)
(validate within-bounds-long? val))
(validate types/within-bounds-long? val))
val)

(s/defn write-string-bytes :- s/Str
Expand All @@ -215,7 +153,3 @@







62 changes: 62 additions & 0 deletions src/clj/tupelo/types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,65 @@
:post [ (string? %) ] }
(String. arg UTF-8-Charset-Name))

;---------------------------------------------------------------------------------------------------

(def BYTE_UNSIGNED_MIN_VALUE 0)
(def BYTE_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 8)
(dec)
(long)))

(def SHORT_UNSIGNED_MIN_VALUE 0)
(def SHORT_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 16)
(dec)
(long)))

(defrecord ^:no-doc IntervalClosed ; #todo report defrecord "resolve" to Cursive
[lower-bound upper-bound]) ; #todo report to Cursive

(s/defn ^:no-doc within-interval-closed? :- s/Bool
"Returns true if val fits within an IntervalClosed."
[ic :- IntervalClosed
val :- s/Num]
(<= (:lower-bound ic) val (:upper-bound ic)))

(def ^:no-doc interval-closed-byte (->IntervalClosed Byte/MIN_VALUE Byte/MAX_VALUE)) ; #todo "resolve" report to Cursive
(def ^:no-doc interval-closed-byte-unsigned (->IntervalClosed BYTE_UNSIGNED_MIN_VALUE BYTE_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-short (->IntervalClosed Short/MIN_VALUE Short/MAX_VALUE))
(def ^:no-doc interval-closed-short-unsigned (->IntervalClosed SHORT_UNSIGNED_MIN_VALUE SHORT_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-integer (->IntervalClosed Integer/MIN_VALUE Integer/MAX_VALUE))
(def ^:no-doc interval-closed-long (->IntervalClosed Long/MIN_VALUE Long/MAX_VALUE))

(s/defn within-bounds-byte? :- s/Bool
"Returns true if val fits within legal range for a byte (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte val))

(s/defn within-bounds-byte-unsigned? :- s/Bool
"Returns true if val fits within legal range for a byte (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte-unsigned val))

(s/defn within-bounds-short? :- s/Bool
"Returns true if val fits within legal range for a short (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-short val))

(s/defn within-bounds-short-unsigned? :- s/Bool
"Returns true if val fits within legal range for a short (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-short-unsigned val))

(s/defn within-bounds-integer? :- s/Bool
"Returns true if val fits within legal range for a integer (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-integer val))

(s/defn within-bounds-long? :- s/Bool
"Returns true if val fits within legal range for a long (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-long val))



43 changes: 13 additions & 30 deletions test/clj/tst/tupelo/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
(:use tupelo.io tupelo.core tupelo.test)
(:refer-clojure :exclude [read-string])
(:require
[clojure.java.io :as io] )
(:import [java.io
DataInputStream DataOutputStream
File FileOutputStream FileInputStream
InputStream OutputStream ]))
[clojure.java.io :as io]
[tupelo.types :as types])
(:import [java.io DataInputStream DataOutputStream FileOutputStream ]))

(def int-val (long (+ 1e9 123456789)))
(def long-val (long 12345e9))
Expand All @@ -21,8 +19,8 @@
(def dummy-file (create-temp-file dummy-file-name))

(dotest
(is= BYTE_UNSIGNED_MAX_VALUE 255)
(is= SHORT_UNSIGNED_MAX_VALUE 65535)
(is= types/BYTE_UNSIGNED_MAX_VALUE 255)
(is= types/SHORT_UNSIGNED_MAX_VALUE 65535)

(let [in-stream (io/input-stream dummy-file)
out-stream (io/output-stream dummy-file)
Expand Down Expand Up @@ -54,11 +52,11 @@
(throws? (write-long dos (* 2M (bigdec Long/MAX_VALUE))))
(throws? (write-long dos (* -2M (bigdec Long/MIN_VALUE))))

(throws? (write-byte-unsigned dos (inc BYTE_UNSIGNED_MAX_VALUE)))
(throws? (write-byte-unsigned dos (dec BYTE_UNSIGNED_MIN_VALUE)))
(throws? (write-byte-unsigned dos (inc types/BYTE_UNSIGNED_MAX_VALUE)))
(throws? (write-byte-unsigned dos (dec types/BYTE_UNSIGNED_MIN_VALUE)))

(throws? (write-short-unsigned dos (inc SHORT_UNSIGNED_MAX_VALUE)))
(throws? (write-short-unsigned dos (dec SHORT_UNSIGNED_MIN_VALUE)))
(throws? (write-short-unsigned dos (inc types/SHORT_UNSIGNED_MAX_VALUE)))
(throws? (write-short-unsigned dos (dec types/SHORT_UNSIGNED_MIN_VALUE)))

(is= (write-byte dos Byte/MIN_VALUE) Byte/MIN_VALUE)
(is= (write-byte dos Byte/MAX_VALUE) Byte/MAX_VALUE)
Expand All @@ -72,11 +70,11 @@
(is= (write-long dos Long/MIN_VALUE) Long/MIN_VALUE)
(is= (write-long dos Long/MAX_VALUE) Long/MAX_VALUE)

(is= (write-byte-unsigned dos BYTE_UNSIGNED_MIN_VALUE) BYTE_UNSIGNED_MIN_VALUE)
(is= (write-byte-unsigned dos BYTE_UNSIGNED_MAX_VALUE) BYTE_UNSIGNED_MAX_VALUE)
(is= (write-byte-unsigned dos types/BYTE_UNSIGNED_MIN_VALUE) types/BYTE_UNSIGNED_MIN_VALUE)
(is= (write-byte-unsigned dos types/BYTE_UNSIGNED_MAX_VALUE) types/BYTE_UNSIGNED_MAX_VALUE)

(is= (write-short-unsigned dos SHORT_UNSIGNED_MIN_VALUE) SHORT_UNSIGNED_MIN_VALUE)
(is= (write-short-unsigned dos SHORT_UNSIGNED_MAX_VALUE) SHORT_UNSIGNED_MAX_VALUE)
(is= (write-short-unsigned dos types/SHORT_UNSIGNED_MIN_VALUE) types/SHORT_UNSIGNED_MIN_VALUE)
(is= (write-short-unsigned dos types/SHORT_UNSIGNED_MAX_VALUE) types/SHORT_UNSIGNED_MAX_VALUE)

(is= (write-string-bytes dos "hello") "hello")))

Expand Down Expand Up @@ -111,19 +109,4 @@



















0 comments on commit 41aed61

Please sign in to comment.