Files
range-difference/test/challenge/discrete_value_range_test.clj

74 lines
2.9 KiB
Clojure

(ns challenge.discrete-value-range-test
(:require
[clojure.test :refer [deftest testing is]]
[challenge.discrete-value-range :as range]))
(deftype IntInclusiveDiscreteValueRange
[^int start ^int end]
range/DiscreteValueRange
(abuts [_this other]
(or (= 1 (abs (- start (.end other))))
(= 1 (abs (- end (.start other))))))
(value-before [__this]
(dec start))
(value-after [_this]
(inc end))
(start [_this]
start)
(end [_this]
end)
(range-type [_this]
:int-range-inclusive)
Object
(toString [_this]
(str start ".." end))
(equals [_this other]
(and (= start (.start other))
(= end (.end other)))))
(defn int-range-inclusive [start end]
(assert (<= start end))
(->IntInclusiveDiscreteValueRange start end))
(deftest integer-ranges-sanity-test
(testing "value equality"
(is (= (int-range-inclusive 0 1)
(int-range-inclusive 0 1))))
(testing "abuts"
(is (= true (range/abuts (int-range-inclusive 0 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts (int-range-inclusive 1 1)
(int-range-inclusive 2 3))))
(is (= true (range/abuts (int-range-inclusive 4 7)
(int-range-inclusive 2 3))))
(is (= false (range/abuts (int-range-inclusive 4 7)
(int-range-inclusive 1 2)))))
(testing "value-before"
(is (= 0 (range/value-before (int-range-inclusive 1 8)))))
(testing "value-after"
(is (= 9 (range/value-after (int-range-inclusive 1 8)))))
(testing "start"
(is (= 1 (range/start (int-range-inclusive 1 8)))))
(testing "end"
(is (= 8 (range/end (int-range-inclusive 1 8))))))
(deftest ordered-range-values
(testing "ordered range values are sorted by value and then the range's start boundary before any end boundary"
(is (= [{:value 4 :boundary-type :start :type :int-range-inclusive}
{:value 5 :boundary-type :start :type :int-range-inclusive}
{:value 5 :boundary-type :start :type :int-range-inclusive}
{:value 5 :boundary-type :end :type :int-range-inclusive}
{:value 5 :boundary-type :end :type :int-range-inclusive}
{:value 8 :boundary-type :end :type :int-range-inclusive}]
(#'range/ordered-range-values [(int-range-inclusive 4 5)
(int-range-inclusive 5 8)
(int-range-inclusive 5 5)])))
(is (= [{:value 5 :boundary-type :start :type :int-range-inclusive}
{:value 5 :boundary-type :start :type :int-range-inclusive}
{:value 5 :boundary-type :end :type :int-range-inclusive}
{:value 5 :boundary-type :end :type :int-range-inclusive}]
(#'range/ordered-range-values [(int-range-inclusive 5 5)
(int-range-inclusive 5 5)])))))