diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/test_clojure/logic.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,205 @@
     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/29/2009
    1.16 +
    1.17 +(ns clojure.test-clojure.logic
    1.18 +  (:use clojure.test
    1.19 +        [clojure.test-clojure.helpers :only (exception)]))
    1.20 +
    1.21 +
    1.22 +;; *** Tests ***
    1.23 +
    1.24 +(deftest test-if
    1.25 +  ; true/false/nil
    1.26 +  (are [x y] (= x y)
    1.27 +      (if true :t) :t
    1.28 +      (if true :t :f) :t
    1.29 +      (if true :t (exception)) :t
    1.30 +
    1.31 +      (if false :t) nil
    1.32 +      (if false :t :f) :f
    1.33 +      (if false (exception) :f) :f
    1.34 +
    1.35 +      (if nil :t) nil
    1.36 +      (if nil :t :f) :f
    1.37 +      (if nil (exception) :f) :f )
    1.38 +
    1.39 +  ; zero/empty is true
    1.40 +  (are [x] (= (if x :t :f) :t)
    1.41 +      (byte 0)
    1.42 +      (short 0)
    1.43 +      (int 0)
    1.44 +      (long 0)
    1.45 +      (bigint 0)
    1.46 +      (float 0)
    1.47 +      (double 0)
    1.48 +      (bigdec 0)
    1.49 +
    1.50 +      0/2
    1.51 +      ""
    1.52 +      #""
    1.53 +      (symbol "")
    1.54 +
    1.55 +      ()
    1.56 +      []
    1.57 +      {}
    1.58 +      #{}
    1.59 +      (into-array []) )
    1.60 +
    1.61 +  ; anything except nil/false is true
    1.62 +  (are [x]  (= (if x :t :f) :t)
    1.63 +      (byte 2)
    1.64 +      (short 2)
    1.65 +      (int 2)
    1.66 +      (long 2)
    1.67 +      (bigint 2)
    1.68 +      (float 2)
    1.69 +      (double 2)
    1.70 +      (bigdec 2)
    1.71 +
    1.72 +      2/3
    1.73 +      \a
    1.74 +      "abc"
    1.75 +      #"a*b"
    1.76 +      'abc
    1.77 +      :kw
    1.78 +
    1.79 +      '(1 2)
    1.80 +      [1 2]
    1.81 +      {:a 1 :b 2}
    1.82 +      #{1 2}
    1.83 +      (into-array [1 2])
    1.84 +
    1.85 +      (new java.util.Date) ))
    1.86 +
    1.87 +
    1.88 +(deftest test-nil-punning
    1.89 +  (are [x y]  (= (if x :no :yes) y)
    1.90 +    (first []) :yes
    1.91 +    (next [1]) :yes
    1.92 +    (rest [1]) :no
    1.93 +
    1.94 +    (butlast [1]) :yes
    1.95 +
    1.96 +    (seq nil) :yes
    1.97 +    (seq []) :yes
    1.98 +
    1.99 +    (sequence nil) :no
   1.100 +    (sequence []) :no
   1.101 +
   1.102 +    (lazy-seq nil) :no
   1.103 +    (lazy-seq []) :no
   1.104 +
   1.105 +    (filter #(> % 10) [1 2 3]) :no
   1.106 +    (map identity []) :no
   1.107 +    (apply concat []) :no
   1.108 +
   1.109 +    (concat) :no
   1.110 +    (concat []) :no
   1.111 +
   1.112 +    (reverse nil) :no
   1.113 +    (reverse []) :no
   1.114 +
   1.115 +    (sort nil) :no
   1.116 +    (sort []) :no ))
   1.117 +
   1.118 +
   1.119 +(deftest test-and
   1.120 +  (are [x y] (= x y)
   1.121 +      (and) true
   1.122 +      (and true) true
   1.123 +      (and nil) nil
   1.124 +      (and false) false
   1.125 +
   1.126 +      (and true nil) nil
   1.127 +      (and true false) false
   1.128 +
   1.129 +      (and 1 true :kw 'abc "abc") "abc"
   1.130 +
   1.131 +      (and 1 true :kw nil 'abc "abc") nil
   1.132 +      (and 1 true :kw nil (exception) 'abc "abc") nil
   1.133 +
   1.134 +      (and 1 true :kw 'abc "abc" false) false
   1.135 +      (and 1 true :kw 'abc "abc" false (exception)) false ))
   1.136 +
   1.137 +
   1.138 +(deftest test-or
   1.139 +  (are [x y] (= x y)
   1.140 +      (or) nil
   1.141 +      (or true) true
   1.142 +      (or nil) nil
   1.143 +      (or false) false
   1.144 +
   1.145 +      (or nil false true) true
   1.146 +      (or nil false 1 2) 1
   1.147 +      (or nil false "abc" :kw) "abc"
   1.148 +
   1.149 +      (or false nil) nil
   1.150 +      (or nil false) false
   1.151 +      (or nil nil nil false) false
   1.152 +
   1.153 +      (or nil true false) true
   1.154 +      (or nil true (exception) false) true
   1.155 +      (or nil false "abc" (exception)) "abc" ))
   1.156 +
   1.157 +
   1.158 +(deftest test-not
   1.159 +  (is (thrown? IllegalArgumentException (not)))
   1.160 +  (are [x] (= (not x) true)
   1.161 +      nil
   1.162 +      false )
   1.163 +  (are [x]  (= (not x) false)
   1.164 +      true
   1.165 +
   1.166 +      ; numbers
   1.167 +      0
   1.168 +      0.0
   1.169 +      42
   1.170 +      1.2
   1.171 +      0/2
   1.172 +      2/3
   1.173 +
   1.174 +      ; characters
   1.175 +      \space
   1.176 +      \tab
   1.177 +      \a
   1.178 +
   1.179 +      ; strings
   1.180 +      ""
   1.181 +      "abc"
   1.182 +
   1.183 +      ; regexes
   1.184 +      #""
   1.185 +      #"a*b"
   1.186 +
   1.187 +      ; symbols
   1.188 +      (symbol "")
   1.189 +      'abc
   1.190 +
   1.191 +      ; keywords
   1.192 +      :kw
   1.193 +
   1.194 +      ; collections/arrays
   1.195 +      ()
   1.196 +      '(1 2)
   1.197 +      []
   1.198 +      [1 2]
   1.199 +      {}
   1.200 +      {:a 1 :b 2}
   1.201 +      #{}
   1.202 +      #{1 2}
   1.203 +      (into-array [])
   1.204 +      (into-array [1 2])
   1.205 +
   1.206 +      ; Java objects
   1.207 +      (new java.util.Date) ))
   1.208 +