Skip to content

Commit 96a4de1

Browse files
authored
Add tests for <= (#838)
1 parent c858b63 commit 96a4de1

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

test/clojure/core_test/lt_eq.cljc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
(ns clojure.core-test.lt-eq
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 0 1
21+
true -1 0
22+
true 0N 1N
23+
true -1N 0N
24+
true 0.0 1.0
25+
true -1.0 0.0
26+
true 0.0M 1.0M
27+
true -1.0M 0.0M
28+
true ##-Inf -1
29+
true 1 ##Inf
30+
31+
false 1 0
32+
false 0 -1
33+
false 1N 0N
34+
false 0N -1N
35+
false 1.0 0.0
36+
false 0.0 -1.0
37+
false 1.0M 0.0M
38+
false 0.0M -1.0M
39+
false -1 ##-Inf
40+
false ##Inf 1
41+
false 1 ##NaN ; Anything compared with ##NaN is false
42+
false ##NaN 1
43+
44+
true 0 0
45+
true 1 1
46+
true -1 -1
47+
true ##Inf ##Inf
48+
true ##-Inf ##-Inf
49+
false ##NaN ##NaN ; ##NaN is never equal, even to itself
50+
51+
;; Mixing numeric types should't matter
52+
true 0 1.0
53+
true -1.0 0
54+
true 0N 1.0M
55+
true -1N 0.0M)
56+
57+
#?(:cljs nil
58+
:default
59+
(testing "Rationals"
60+
(are [expected x y] (= expected (<= x y))
61+
true 1/16 1/2
62+
true -1/2 -1/16
63+
true 1/16 0.5
64+
true -0.5 -1/16
65+
false 1/2 1/16
66+
false 0.5 1/16
67+
false -1/16 -1/2
68+
false -1/16 -0.5
69+
true 1/2 1/2
70+
true 1/3 1/3
71+
true -1/2 -1/2
72+
true -1/3 -1/3))))
73+
74+
(testing "arity 3 and more"
75+
(are [expected x y z] (= expected (<= x y z))
76+
true 0 1 2
77+
true -2 -1 0
78+
true -1 0 1
79+
false 1 0 2
80+
false 1 2 0
81+
false 0 -2 -1
82+
false -2 0 -1
83+
true 0 0 1
84+
true 0 1 1
85+
false 1 0 0
86+
false 1 1 0
87+
true ##-Inf 0 ##Inf
88+
false ##Inf 0 ##-Inf)
89+
(is (= true (apply <= (range 10))))
90+
(is (= true (apply <= 0 (range 10))))
91+
(is (= false (apply <= 100 (range 10)))))
92+
93+
(testing "negative tests"
94+
;; `<=` only compares numbers, except in ClojureScript (really
95+
;; JavaScript under the hood) where comparisons are just a bit
96+
;; of a mess.
97+
#?@(:cljs
98+
[(is (= true (<= nil 1)))
99+
(is (= false (<= 1 nil)))
100+
(is (= true (<= nil 1 2)))
101+
(is (= false (<= 1 2 nil)))
102+
(is (= true (<= "1" "2")))
103+
(is (= false (<= "foo" "bar")))
104+
(is (= false (<= :foo :bar)))]
105+
:cljr
106+
[(is (thrown? Exception (<= nil 1)))
107+
(is (thrown? Exception (<= 1 nil)))
108+
(is (thrown? Exception (<= nil 1 2)))
109+
(is (thrown? Exception (<= 1 2 nil)))
110+
(is (= true (<= "1" "2")))
111+
(is (thrown? Exception (<= "foo" "bar")))
112+
(is (thrown? Exception (<= :foo :bar)))]
113+
:default
114+
[(is (thrown? Exception (<= nil 1)))
115+
(is (thrown? Exception (<= 1 nil)))
116+
(is (thrown? Exception (<= nil 1 2)))
117+
(is (thrown? Exception (<= 1 2 nil)))
118+
(is (thrown? Exception (<= "1" "2")))
119+
(is (thrown? Exception (<= "foo" "bar")))
120+
(is (thrown? Exception (<= :foo :bar)))]))))

0 commit comments

Comments
 (0)