Mercurial > lasercutter
view src/clojure/test_clojure/serialization.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ; Copyright (c) Rich Hickey. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ; which can be found in the file epl-v10.html at the root of this distribution.5 ; By using this software in any fashion, you are agreeing to be bound by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ;; Author: Chas Emerick10 ;; cemerick@snowtide.com12 (ns clojure.test-clojure.serialization13 (:use clojure.test)14 (:import (java.io ObjectOutputStream ObjectInputStream15 ByteArrayOutputStream ByteArrayInputStream)))17 (defn- serialize18 "Serializes a single object, returning a byte array."19 [v]20 (with-open [bout (ByteArrayOutputStream.)21 oos (ObjectOutputStream. bout)]22 (.writeObject oos v)23 (.flush oos)24 (.toByteArray bout)))26 (defn- deserialize27 "Deserializes and returns a single object from the given byte array."28 [bytes]29 (with-open [ois (-> bytes ByteArrayInputStream. ObjectInputStream.)]30 (.readObject ois)))32 (defrecord SerializationRecord [a b c])33 (defstruct SerializationStruct :a :b :c)35 (defn- build-via-transient36 [coll]37 (persistent!38 (reduce conj! (transient coll) (map vec (partition 2 (range 1000))))))40 (defn- roundtrip41 [v]42 (let [rt (-> v serialize deserialize)43 rt-seq (-> v seq serialize deserialize)]44 (and (= v rt)45 (= (seq v) (seq rt))46 (= (seq v) rt-seq))))48 (deftest sequable-serialization49 (are [val] (roundtrip val)50 ; lists and related51 (list)52 (apply list (range 10))53 (cons 0 nil)54 (clojure.lang.Cons. 0 nil)56 ; vectors57 []58 (into [] (range 10))59 (into [] (range 25))60 (into [] (range 100))61 (into [] (range 500))62 (into [] (range 1000))64 ; maps65 {}66 {:a 5 :b 0}67 (apply array-map (range 100))68 (apply hash-map (range 100))70 ; sets71 #{}72 #{'a 'b 'c}73 (set (range 10))74 (set (range 25))75 (set (range 100))76 (set (range 500))77 (set (range 1000))78 (sorted-set)79 (sorted-set 'a 'b 'c)80 (apply sorted-set (reverse (range 10)))81 (apply sorted-set (reverse (range 25)))82 (apply sorted-set (reverse (range 100)))83 (apply sorted-set (reverse (range 500)))84 (apply sorted-set (reverse (range 1000)))86 ; queues87 clojure.lang.PersistentQueue/EMPTY88 (into clojure.lang.PersistentQueue/EMPTY (range 50))90 ; lazy seqs91 (lazy-seq nil)92 (lazy-seq (range 50))94 ; transient / persistent! round-trip95 (build-via-transient [])96 (build-via-transient {})97 (build-via-transient #{})99 ; array-seqs100 (seq (make-array Object 10))101 (seq (make-array Boolean/TYPE 10))102 (seq (make-array Byte/TYPE 10))103 (seq (make-array Character/TYPE 10))104 (seq (make-array Double/TYPE 10))105 (seq (make-array Float/TYPE 10))106 (seq (make-array Integer/TYPE 10))107 (seq (make-array Long/TYPE 10))109 ; "records"110 (SerializationRecord. 0 :foo (range 20))111 (struct SerializationStruct 0 :foo (range 20))113 ; misc seqs114 (seq "s11n")115 (range 50)116 (rseq (apply sorted-set (reverse (range 100))))))118 (deftest misc-serialization119 (are [v] (= v (-> v serialize deserialize))120 25/3121 :keyword122 ::namespaced-keyword123 'symbol))125 (deftest interned-serializations126 (are [v] (identical? v (-> v serialize deserialize))127 clojure.lang.RT/DEFAULT_COMPARATOR129 ; namespaces just get deserialized back into the same-named ns in the present runtime130 ; (they're referred to by defrecord instances)131 *ns*))133 (deftest function-serialization134 (let [capture 5]135 (are [f] (= capture ((-> f serialize deserialize)))136 (constantly 5)137 (fn [] 5)138 #(do 5)139 (constantly capture)140 (fn [] capture)141 #(do capture))))143 (deftest check-unserializable-objects144 (are [t] (thrown? java.io.NotSerializableException (serialize t))145 ;; transients146 (transient [])147 (transient {})148 (transient #{})150 ;; reference types151 (atom nil)152 (ref nil)153 (agent nil)154 #'+156 ;; stateful seqs157 (enumeration-seq (java.util.Collections/enumeration (range 50)))158 (iterator-seq (.iterator (range 50)))))