Mercurial > lasercutter
view src/clojure/test_clojure/logic.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: Frantisek Sodomka11 ;;12 ;; Created 1/29/200914 (ns clojure.test-clojure.logic15 (:use clojure.test16 [clojure.test-clojure.helpers :only (exception)]))19 ;; *** Tests ***21 (deftest test-if22 ; true/false/nil23 (are [x y] (= x y)24 (if true :t) :t25 (if true :t :f) :t26 (if true :t (exception)) :t28 (if false :t) nil29 (if false :t :f) :f30 (if false (exception) :f) :f32 (if nil :t) nil33 (if nil :t :f) :f34 (if nil (exception) :f) :f )36 ; zero/empty is true37 (are [x] (= (if x :t :f) :t)38 (byte 0)39 (short 0)40 (int 0)41 (long 0)42 (bigint 0)43 (float 0)44 (double 0)45 (bigdec 0)47 0/248 ""49 #""50 (symbol "")52 ()53 []54 {}55 #{}56 (into-array []) )58 ; anything except nil/false is true59 (are [x] (= (if x :t :f) :t)60 (byte 2)61 (short 2)62 (int 2)63 (long 2)64 (bigint 2)65 (float 2)66 (double 2)67 (bigdec 2)69 2/370 \a71 "abc"72 #"a*b"73 'abc74 :kw76 '(1 2)77 [1 2]78 {:a 1 :b 2}79 #{1 2}80 (into-array [1 2])82 (new java.util.Date) ))85 (deftest test-nil-punning86 (are [x y] (= (if x :no :yes) y)87 (first []) :yes88 (next [1]) :yes89 (rest [1]) :no91 (butlast [1]) :yes93 (seq nil) :yes94 (seq []) :yes96 (sequence nil) :no97 (sequence []) :no99 (lazy-seq nil) :no100 (lazy-seq []) :no102 (filter #(> % 10) [1 2 3]) :no103 (map identity []) :no104 (apply concat []) :no106 (concat) :no107 (concat []) :no109 (reverse nil) :no110 (reverse []) :no112 (sort nil) :no113 (sort []) :no ))116 (deftest test-and117 (are [x y] (= x y)118 (and) true119 (and true) true120 (and nil) nil121 (and false) false123 (and true nil) nil124 (and true false) false126 (and 1 true :kw 'abc "abc") "abc"128 (and 1 true :kw nil 'abc "abc") nil129 (and 1 true :kw nil (exception) 'abc "abc") nil131 (and 1 true :kw 'abc "abc" false) false132 (and 1 true :kw 'abc "abc" false (exception)) false ))135 (deftest test-or136 (are [x y] (= x y)137 (or) nil138 (or true) true139 (or nil) nil140 (or false) false142 (or nil false true) true143 (or nil false 1 2) 1144 (or nil false "abc" :kw) "abc"146 (or false nil) nil147 (or nil false) false148 (or nil nil nil false) false150 (or nil true false) true151 (or nil true (exception) false) true152 (or nil false "abc" (exception)) "abc" ))155 (deftest test-not156 (is (thrown? IllegalArgumentException (not)))157 (are [x] (= (not x) true)158 nil159 false )160 (are [x] (= (not x) false)161 true163 ; numbers164 0165 0.0166 42167 1.2168 0/2169 2/3171 ; characters172 \space173 \tab174 \a176 ; strings177 ""178 "abc"180 ; regexes181 #""182 #"a*b"184 ; symbols185 (symbol "")186 'abc188 ; keywords189 :kw191 ; collections/arrays192 ()193 '(1 2)194 []195 [1 2]196 {}197 {:a 1 :b 2}198 #{}199 #{1 2}200 (into-array [])201 (into-array [1 2])203 ; Java objects204 (new java.util.Date) ))