Mercurial > lasercutter
diff src/clojure/test_clojure/predicates.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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/test_clojure/predicates.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,142 @@ 1.4 +; Copyright (c) Rich Hickey. All rights reserved. 1.5 +; The use and distribution terms for this software are covered by the 1.6 +; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.7 +; which can be found in the file epl-v10.html at the root of this distribution. 1.8 +; By using this software in any fashion, you are agreeing to be bound by 1.9 +; the terms of this license. 1.10 +; You must not remove this notice, or any other, from this software. 1.11 + 1.12 +; Author: Frantisek Sodomka 1.13 + 1.14 +;; 1.15 +;; Created 1/28/2009 1.16 + 1.17 +(ns clojure.test-clojure.predicates 1.18 + (:use clojure.test)) 1.19 + 1.20 + 1.21 +;; *** Type predicates *** 1.22 + 1.23 +(def myvar 42) 1.24 + 1.25 +(def sample-data { 1.26 + :nil nil 1.27 + 1.28 + :bool-true true 1.29 + :bool-false false 1.30 + 1.31 + :byte (byte 7) 1.32 + :short (short 7) 1.33 + :int (int 7) 1.34 + :long (long 7) 1.35 + :bigint (bigint 7) 1.36 + :float (float 7) 1.37 + :double (double 7) 1.38 + :bigdec (bigdec 7) 1.39 + 1.40 + :ratio 2/3 1.41 + 1.42 + :character \a 1.43 + :symbol 'abc 1.44 + :keyword :kw 1.45 + 1.46 + :empty-string "" 1.47 + :empty-regex #"" 1.48 + :empty-list () 1.49 + :empty-lazy-seq (lazy-seq nil) 1.50 + :empty-vector [] 1.51 + :empty-map {} 1.52 + :empty-set #{} 1.53 + :empty-array (into-array []) 1.54 + 1.55 + :string "abc" 1.56 + :regex #"a*b" 1.57 + :list '(1 2 3) 1.58 + :lazy-seq (lazy-seq [1 2 3]) 1.59 + :vector [1 2 3] 1.60 + :map {:a 1 :b 2 :c 3} 1.61 + :set #{1 2 3} 1.62 + :array (into-array [1 2 3]) 1.63 + 1.64 + :fn (fn [x] (* 2 x)) 1.65 + 1.66 + :class java.util.Date 1.67 + :object (new java.util.Date) 1.68 + 1.69 + :var (var myvar) 1.70 + :delay (delay (+ 1 2)) 1.71 +}) 1.72 + 1.73 + 1.74 +(def type-preds { 1.75 + nil? [:nil] 1.76 + 1.77 + true? [:bool-true] 1.78 + false? [:bool-false] 1.79 + ; boolean? 1.80 + 1.81 + integer? [:byte :short :int :long :bigint] 1.82 + float? [:float :double] 1.83 + decimal? [:bigdec] 1.84 + ratio? [:ratio] 1.85 + rational? [:byte :short :int :long :bigint :ratio :bigdec] 1.86 + number? [:byte :short :int :long :bigint :ratio :bigdec :float :double] 1.87 + 1.88 + ; character? 1.89 + symbol? [:symbol] 1.90 + keyword? [:keyword] 1.91 + 1.92 + string? [:empty-string :string] 1.93 + ; regex? 1.94 + 1.95 + list? [:empty-list :list] 1.96 + vector? [:empty-vector :vector] 1.97 + map? [:empty-map :map] 1.98 + set? [:empty-set :set] 1.99 + 1.100 + coll? [:empty-list :list 1.101 + :empty-lazy-seq :lazy-seq 1.102 + :empty-vector :vector 1.103 + :empty-map :map 1.104 + :empty-set :set] 1.105 + 1.106 + seq? [:empty-list :list 1.107 + :empty-lazy-seq :lazy-seq] 1.108 + ; array? 1.109 + 1.110 + fn? [:fn] 1.111 + ifn? [:fn 1.112 + :empty-vector :vector :empty-map :map :empty-set :set 1.113 + :keyword :symbol :var] 1.114 + 1.115 + class? [:class] 1.116 + var? [:var] 1.117 + delay? [:delay] 1.118 +}) 1.119 + 1.120 + 1.121 +;; Test all type predicates against all data types 1.122 +;; 1.123 +(defn- get-fn-name [f] 1.124 + (str 1.125 + (apply str (nthnext (first (.split (str f) "_")) 1.126 + (count "clojure.core$"))) 1.127 + "?")) 1.128 + 1.129 +(deftest test-type-preds 1.130 + (doseq [tp type-preds] 1.131 + (doseq [dt sample-data] 1.132 + (if (some #(= % (first dt)) (second tp)) 1.133 + (is ((first tp) (second dt)) 1.134 + (pr-str (list (get-fn-name (first tp)) (second dt)))) 1.135 + (is (not ((first tp) (second dt))) 1.136 + (pr-str (list 'not (list (get-fn-name (first tp)) (second dt))))))))) 1.137 + 1.138 + 1.139 +;; Additional tests: 1.140 +;; http://groups.google.com/group/clojure/browse_thread/thread/537761a06edb4b06/bfd4f0705b746a38 1.141 +;; 1.142 +(deftest test-string?-more 1.143 + (are [x] (not (string? x)) 1.144 + (new java.lang.StringBuilder "abc") 1.145 + (new java.lang.StringBuffer "xyz")))