diff --git a/project.clj b/project.clj index 8128ed0..a012e33 100644 --- a/project.clj +++ b/project.clj @@ -8,7 +8,8 @@ [org.clojure/tools.macro "0.1.1"] [org.clojure/tools.reader "0.7.2"]] :aliases {"testall" ["with-profile" "1.6:1.7:1.8:1.9:1.10" "test"]} - :profiles {:1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]} + :profiles {:dev {:dependencies [[org.clojure/test.check "0.10.0-alpha3"]]} + :1.10 {:dependencies [[org.clojure/clojure "1.10.0-RC3"]]} :1.9 {} :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} :1.7 {:dependencies [[org.clojure/clojure "1.7.0"]]} diff --git a/src/flatland/useful/compress.clj b/src/flatland/useful/compress.clj index 3743810..46a56f7 100644 --- a/src/flatland/useful/compress.clj +++ b/src/flatland/useful/compress.clj @@ -1,7 +1,8 @@ (ns flatland.useful.compress (:import [java.util.zip DeflaterOutputStream InflaterInputStream] [java.io ByteArrayOutputStream ByteArrayInputStream] - [sun.misc BASE64Decoder BASE64Encoder])) + [sun.misc BASE64Decoder BASE64Encoder] + [java.util Base64])) (defn smash [^String str] (let [out (ByteArrayOutputStream.)] @@ -15,3 +16,20 @@ (let [bytes (-> (BASE64Decoder.) (.decodeBuffer str)) in (ByteArrayInputStream. bytes)] (slurp (InflaterInputStream. in)))) + + + + +(defn unsmash-new [^String s] + (let [bytes (.decode (Base64/getDecoder) (subs s 0 (dec (count s)))) + in (ByteArrayInputStream. bytes)] + (slurp (InflaterInputStream. in)))) + +(defn smash-new [^String s] + (let [out (ByteArrayOutputStream.)] + (doto (DeflaterOutputStream. out) + (.write (.getBytes s)) + (.finish)) + (-> (.encodeToString (Base64/getEncoder) (.toByteArray out)) + (str (System/getProperty "line.separator"))))) + diff --git a/test/flatland/useful/compress_test.clj b/test/flatland/useful/compress_test.clj index 512c320..752c765 100644 --- a/test/flatland/useful/compress_test.clj +++ b/test/flatland/useful/compress_test.clj @@ -1,5 +1,9 @@ (ns flatland.useful.compress-test - (:use clojure.test flatland.useful.compress)) + (:use clojure.test + flatland.useful.compress) + (:require [clojure.test.check.clojure-test :refer [defspec]] + [clojure.test.check.generators :as gen] + [clojure.test.check.properties :as prop])) (deftest round-trip @@ -7,3 +11,17 @@ g2 sd35g4szdf sdf4 as89faw76fwfwf210 "] (is (= s (unsmash (smash s)))))) + +(deftest smash-test + (is (= "eJwDAAAAAAE=\n" (smash-new ""))) + (is (= "eJxLBAAAYgBi\n" (smash-new "a")))) + +(deftest unsmash-test + (is (= "" (unsmash-new "eJwDAAAAAAE=\n"))) + (is (= "a" (unsmash-new "eJxLBAAAYgBi\n")))) + +(defspec round-trip-gen + 100 + (prop/for-all [s gen/string] + (println s) + (= s (unsmash-new (smash s)))))