|
| 1 | +(ns clojure.core-test.gt |
| 2 | + (:require [clojure.test :as t :refer [are deftest is testing]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists > |
| 6 | + (deftest test-> |
| 7 | + (testing "arity 1" |
| 8 | + ;; Doesn't matter what the argument is, `>` return `true` for |
| 9 | + ;; one argument. |
| 10 | + (is (> 1)) |
| 11 | + (is (> 0)) |
| 12 | + (is (> -1)) |
| 13 | + ;; Doesn't check whether arg is a number |
| 14 | + (is (> "abc")) |
| 15 | + (is (> :foo)) |
| 16 | + (is (> nil))) |
| 17 | + |
| 18 | + (testing "arity 2" |
| 19 | + (are [expected x y] (= expected (> x y)) |
| 20 | + true 1 0 |
| 21 | + true 0 -1 |
| 22 | + true 1N 0N |
| 23 | + true 0N -1N |
| 24 | + true 1.0 0.0 |
| 25 | + true 0.0 -1.0 |
| 26 | + true 1.0M 0.0M |
| 27 | + true 0.0M -1.0M |
| 28 | + true -1 ##-Inf |
| 29 | + true ##Inf 1 |
| 30 | + |
| 31 | + false 0 1 |
| 32 | + false -1 0 |
| 33 | + false 0N 1N |
| 34 | + false -1N 0N |
| 35 | + false 0.0 1.0 |
| 36 | + false -1.0 0.0 |
| 37 | + false 0.0M 1.0M |
| 38 | + false -1.0M 0.0M |
| 39 | + false ##-Inf -1 |
| 40 | + false 1 ##Inf |
| 41 | + |
| 42 | + false 1 ##NaN ; Anything compared with ##NaN is false |
| 43 | + false ##NaN 1 |
| 44 | + |
| 45 | + ;; Nothing is greater than itself |
| 46 | + false 0 0 |
| 47 | + false 1 1 |
| 48 | + false -1 -1 |
| 49 | + false ##Inf ##Inf |
| 50 | + false ##-Inf ##-Inf |
| 51 | + false ##NaN ##NaN |
| 52 | + |
| 53 | + ;; Mixing numeric types should't matter |
| 54 | + true 1.0 0 |
| 55 | + true 0 -1.0 |
| 56 | + true 1.0M 0N |
| 57 | + true 0.0M -1N) |
| 58 | + |
| 59 | + #?(:cljs nil |
| 60 | + :default |
| 61 | + (testing "Rationals" |
| 62 | + (are [expected x y] (= expected (> x y)) |
| 63 | + true 1/2 1/16 |
| 64 | + true 0.5 1/16 |
| 65 | + true -1/16 -1/2 |
| 66 | + true -1/16 -0.5 |
| 67 | + |
| 68 | + false 1/16 1/2 |
| 69 | + false -1/2 -1/16 |
| 70 | + false 1/16 0.5 |
| 71 | + false -0.5 -1/16)))) |
| 72 | + |
| 73 | + (testing "arity 3 and more" |
| 74 | + (are [expected x y z] (= expected (> x y z)) |
| 75 | + true 2 1 0 |
| 76 | + true 0 -1 -2 |
| 77 | + true 1 0 -1 |
| 78 | + false 2 0 1 |
| 79 | + false 0 2 1 |
| 80 | + false -1 -2 0 |
| 81 | + false -1 0 -2) |
| 82 | + (is (= true (apply > (reverse (range 10))))) |
| 83 | + (is (= false (apply > -1 (reverse (range 10)))))) |
| 84 | + |
| 85 | + (testing "negative tests" |
| 86 | + ;; `>` only compares numbers, except in ClojureScript (really |
| 87 | + ;; JavaScript under the hood) where comparisons are just a bit |
| 88 | + ;; of a mess. CLR also has some implicit conversions for strings |
| 89 | + ;; and characters to numbers. |
| 90 | + #?@(:cljs |
| 91 | + [(is (= true (> 1 nil))) |
| 92 | + (is (= false (> nil 1))) |
| 93 | + (is (= true (> 2 1 nil))) |
| 94 | + (is (= false (> 1 2 nil))) |
| 95 | + (is (= true (> "2" "1"))) |
| 96 | + (is (= false (> "bar" "foo"))) |
| 97 | + (is (= false (> :bar :foo)))] |
| 98 | + :cljr |
| 99 | + [(is (thrown? Exception (> 1 nil))) |
| 100 | + (is (thrown? Exception (> nil 1))) |
| 101 | + (is (thrown? Exception (> 1 nil 2))) |
| 102 | + (is (thrown? Exception (> 2 1 nil))) |
| 103 | + (is (= true (> "2" "1"))) |
| 104 | + (is (thrown? Exception (> "bar" "foo"))) |
| 105 | + (is (thrown? Exception (> :bar :foo)))] |
| 106 | + :default |
| 107 | + [(is (thrown? Exception (> 1 nil))) |
| 108 | + (is (thrown? Exception (> nil 1))) |
| 109 | + (is (thrown? Exception (> 1 nil 2))) |
| 110 | + (is (thrown? Exception (> 2 1 nil))) |
| 111 | + (is (thrown? Exception (> "2" "1"))) |
| 112 | + (is (thrown? Exception (> "bar" "foo"))) |
| 113 | + (is (thrown? Exception (> :bar :foo)))])))) |
0 commit comments