rlm@10: ; Copyright (c) Rich Hickey. All rights reserved. rlm@10: ; The use and distribution terms for this software are covered by the rlm@10: ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ; which can be found in the file epl-v10.html at the root of this distribution. rlm@10: ; By using this software in any fashion, you are agreeing to be bound by rlm@10: ; the terms of this license. rlm@10: ; You must not remove this notice, or any other, from this software. rlm@10: rlm@10: ; Author: Frantisek Sodomka rlm@10: rlm@10: ;; rlm@10: ;; Created 1/29/2009 rlm@10: rlm@10: (ns clojure.test-clojure.logic rlm@10: (:use clojure.test rlm@10: [clojure.test-clojure.helpers :only (exception)])) rlm@10: rlm@10: rlm@10: ;; *** Tests *** rlm@10: rlm@10: (deftest test-if rlm@10: ; true/false/nil rlm@10: (are [x y] (= x y) rlm@10: (if true :t) :t rlm@10: (if true :t :f) :t rlm@10: (if true :t (exception)) :t rlm@10: rlm@10: (if false :t) nil rlm@10: (if false :t :f) :f rlm@10: (if false (exception) :f) :f rlm@10: rlm@10: (if nil :t) nil rlm@10: (if nil :t :f) :f rlm@10: (if nil (exception) :f) :f ) rlm@10: rlm@10: ; zero/empty is true rlm@10: (are [x] (= (if x :t :f) :t) rlm@10: (byte 0) rlm@10: (short 0) rlm@10: (int 0) rlm@10: (long 0) rlm@10: (bigint 0) rlm@10: (float 0) rlm@10: (double 0) rlm@10: (bigdec 0) rlm@10: rlm@10: 0/2 rlm@10: "" rlm@10: #"" rlm@10: (symbol "") rlm@10: rlm@10: () rlm@10: [] rlm@10: {} rlm@10: #{} rlm@10: (into-array []) ) rlm@10: rlm@10: ; anything except nil/false is true rlm@10: (are [x] (= (if x :t :f) :t) rlm@10: (byte 2) rlm@10: (short 2) rlm@10: (int 2) rlm@10: (long 2) rlm@10: (bigint 2) rlm@10: (float 2) rlm@10: (double 2) rlm@10: (bigdec 2) rlm@10: rlm@10: 2/3 rlm@10: \a rlm@10: "abc" rlm@10: #"a*b" rlm@10: 'abc rlm@10: :kw rlm@10: rlm@10: '(1 2) rlm@10: [1 2] rlm@10: {:a 1 :b 2} rlm@10: #{1 2} rlm@10: (into-array [1 2]) rlm@10: rlm@10: (new java.util.Date) )) rlm@10: rlm@10: rlm@10: (deftest test-nil-punning rlm@10: (are [x y] (= (if x :no :yes) y) rlm@10: (first []) :yes rlm@10: (next [1]) :yes rlm@10: (rest [1]) :no rlm@10: rlm@10: (butlast [1]) :yes rlm@10: rlm@10: (seq nil) :yes rlm@10: (seq []) :yes rlm@10: rlm@10: (sequence nil) :no rlm@10: (sequence []) :no rlm@10: rlm@10: (lazy-seq nil) :no rlm@10: (lazy-seq []) :no rlm@10: rlm@10: (filter #(> % 10) [1 2 3]) :no rlm@10: (map identity []) :no rlm@10: (apply concat []) :no rlm@10: rlm@10: (concat) :no rlm@10: (concat []) :no rlm@10: rlm@10: (reverse nil) :no rlm@10: (reverse []) :no rlm@10: rlm@10: (sort nil) :no rlm@10: (sort []) :no )) rlm@10: rlm@10: rlm@10: (deftest test-and rlm@10: (are [x y] (= x y) rlm@10: (and) true rlm@10: (and true) true rlm@10: (and nil) nil rlm@10: (and false) false rlm@10: rlm@10: (and true nil) nil rlm@10: (and true false) false rlm@10: rlm@10: (and 1 true :kw 'abc "abc") "abc" rlm@10: rlm@10: (and 1 true :kw nil 'abc "abc") nil rlm@10: (and 1 true :kw nil (exception) 'abc "abc") nil rlm@10: rlm@10: (and 1 true :kw 'abc "abc" false) false rlm@10: (and 1 true :kw 'abc "abc" false (exception)) false )) rlm@10: rlm@10: rlm@10: (deftest test-or rlm@10: (are [x y] (= x y) rlm@10: (or) nil rlm@10: (or true) true rlm@10: (or nil) nil rlm@10: (or false) false rlm@10: rlm@10: (or nil false true) true rlm@10: (or nil false 1 2) 1 rlm@10: (or nil false "abc" :kw) "abc" rlm@10: rlm@10: (or false nil) nil rlm@10: (or nil false) false rlm@10: (or nil nil nil false) false rlm@10: rlm@10: (or nil true false) true rlm@10: (or nil true (exception) false) true rlm@10: (or nil false "abc" (exception)) "abc" )) rlm@10: rlm@10: rlm@10: (deftest test-not rlm@10: (is (thrown? IllegalArgumentException (not))) rlm@10: (are [x] (= (not x) true) rlm@10: nil rlm@10: false ) rlm@10: (are [x] (= (not x) false) rlm@10: true rlm@10: rlm@10: ; numbers rlm@10: 0 rlm@10: 0.0 rlm@10: 42 rlm@10: 1.2 rlm@10: 0/2 rlm@10: 2/3 rlm@10: rlm@10: ; characters rlm@10: \space rlm@10: \tab rlm@10: \a rlm@10: rlm@10: ; strings rlm@10: "" rlm@10: "abc" rlm@10: rlm@10: ; regexes rlm@10: #"" rlm@10: #"a*b" rlm@10: rlm@10: ; symbols rlm@10: (symbol "") rlm@10: 'abc rlm@10: rlm@10: ; keywords rlm@10: :kw rlm@10: rlm@10: ; collections/arrays rlm@10: () rlm@10: '(1 2) rlm@10: [] rlm@10: [1 2] rlm@10: {} rlm@10: {:a 1 :b 2} rlm@10: #{} rlm@10: #{1 2} rlm@10: (into-array []) rlm@10: (into-array [1 2]) rlm@10: rlm@10: ; Java objects rlm@10: (new java.util.Date) )) rlm@10: