From b0fac71ab0245b9273b8e53d0e97cb0cae2fd596 Mon Sep 17 00:00:00 2001 From: Dave Roberts Date: Fri, 5 Dec 2025 18:46:11 -0600 Subject: [PATCH] Add tests for `<=` --- test/clojure/core_test/lt_eq.cljc | 120 ++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 test/clojure/core_test/lt_eq.cljc diff --git a/test/clojure/core_test/lt_eq.cljc b/test/clojure/core_test/lt_eq.cljc new file mode 100644 index 0000000..f715f5c --- /dev/null +++ b/test/clojure/core_test/lt_eq.cljc @@ -0,0 +1,120 @@ +(ns clojure.core-test.lt-eq + (:require [clojure.test :as t :refer [are deftest is testing]] + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) + +(when-var-exists <= + (deftest test-<= + (testing "arity 1" + ;; Doesn't matter what the argument is, `<=` return `true` for + ;; one argument. + (is (<= 1)) + (is (<= 0)) + (is (<= -1)) + ;; Doesn't check whether arg is a number + (is (<= "abc")) + (is (<= :foo)) + (is (<= nil))) + + (testing "arity 2" + (are [expected x y] (= expected (<= x y)) + true 0 1 + true -1 0 + true 0N 1N + true -1N 0N + true 0.0 1.0 + true -1.0 0.0 + true 0.0M 1.0M + true -1.0M 0.0M + true ##-Inf -1 + true 1 ##Inf + + false 1 0 + false 0 -1 + false 1N 0N + false 0N -1N + false 1.0 0.0 + false 0.0 -1.0 + false 1.0M 0.0M + false 0.0M -1.0M + false -1 ##-Inf + false ##Inf 1 + false 1 ##NaN ; Anything compared with ##NaN is false + false ##NaN 1 + + true 0 0 + true 1 1 + true -1 -1 + true ##Inf ##Inf + true ##-Inf ##-Inf + false ##NaN ##NaN ; ##NaN is never equal, even to itself + + ;; Mixing numeric types should't matter + true 0 1.0 + true -1.0 0 + true 0N 1.0M + true -1N 0.0M) + + #?(:cljs nil + :default + (testing "Rationals" + (are [expected x y] (= expected (<= x y)) + true 1/16 1/2 + true -1/2 -1/16 + true 1/16 0.5 + true -0.5 -1/16 + false 1/2 1/16 + false 0.5 1/16 + false -1/16 -1/2 + false -1/16 -0.5 + true 1/2 1/2 + true 1/3 1/3 + true -1/2 -1/2 + true -1/3 -1/3)))) + + (testing "arity 3 and more" + (are [expected x y z] (= expected (<= x y z)) + true 0 1 2 + true -2 -1 0 + true -1 0 1 + false 1 0 2 + false 1 2 0 + false 0 -2 -1 + false -2 0 -1 + true 0 0 1 + true 0 1 1 + false 1 0 0 + false 1 1 0 + true ##-Inf 0 ##Inf + false ##Inf 0 ##-Inf) + (is (= true (apply <= (range 10)))) + (is (= true (apply <= 0 (range 10)))) + (is (= false (apply <= 100 (range 10))))) + + (testing "negative tests" + ;; `<=` only compares numbers, except in ClojureScript (really + ;; JavaScript under the hood) where comparisons are just a bit + ;; of a mess. + #?@(:cljs + [(is (= true (<= nil 1))) + (is (= false (<= 1 nil))) + (is (= true (<= nil 1 2))) + (is (= false (<= 1 2 nil))) + (is (= true (<= "1" "2"))) + (is (= false (<= "foo" "bar"))) + (is (= false (<= :foo :bar)))] + :cljr + [(is (thrown? Exception (<= nil 1))) + (is (thrown? Exception (<= 1 nil))) + (is (thrown? Exception (<= nil 1 2))) + (is (thrown? Exception (<= 1 2 nil))) + (is (= true (<= "1" "2"))) + (is (thrown? Exception (<= "foo" "bar"))) + (is (thrown? Exception (<= :foo :bar)))] + :default + [(is (thrown? Exception (<= nil 1))) + (is (thrown? Exception (<= 1 nil))) + (is (thrown? Exception (<= nil 1 2))) + (is (thrown? Exception (<= 1 2 nil))) + (is (thrown? Exception (<= "1" "2"))) + (is (thrown? Exception (<= "foo" "bar"))) + (is (thrown? Exception (<= :foo :bar)))]))))