Back

complement (clj)

(source)

function

(complement coll)

Examples

tick
(ns tick.alpha.interval-test
  (:require
   [clojure.spec.alpha :as s]
   [tick.core :as t]
   [tick.protocols :as p]
   [clojure.test
    :refer [deftest is testing run-tests]
    :refer-macros [deftest is testing run-tests]]
   [tick.alpha.interval :as ti]
   #?@(:cljs [[java.time :refer [Instant LocalDateTime LocalTime]]]))
  #?(:clj
     (:import [java.time LocalDateTime Instant LocalTime])))

;; concur is really the complement to disjoint, but we'll test it
;; anywhere to ensure the complement function is working as expected.

(deftest complement-test
  (testing "complement through max of type"
    (is (= [(ti/new-interval (t/time "01:00") (t/max-of-type (t/time "00:00")))]
           (ti/complement [(ti/new-interval (t/time "00:00") (t/time "01:00"))]))))
  (testing "complement ordered disjoint intervals"
    (is (= [(ti/new-interval (t/time "00:00") (t/time "01:00"))
            (ti/new-interval (t/time "02:00") (t/time "03:00"))
            (ti/new-interval (t/time "04:00") (t/max-of-type (t/time "00:00")))]
           (ti/complement [(ti/new-interval (t/time "01:00") (t/time "02:00"))
                        (ti/new-interval (t/time "03:00") (t/time "04:00"))]))))
  (testing "complement meeting intervals"
    (is (= [(ti/new-interval (t/time "00:00") (t/time "01:00"))
            (ti/new-interval (t/time "03:00") (t/max-of-type (t/time "00:00")))]
           (ti/complement [(ti/new-interval (t/time "01:00") (t/time "02:00"))
                        (ti/new-interval (t/time "02:00") (t/time "03:00"))]))))
  (testing "complement empty interval round trip"
    (is (= [] (ti/complement (ti/complement []))))))

;; Can we disturb?
(deftest cannot-disturb-test
  (extend-protocol p/ITimeSpan
    LocalTime
    (beginning [i] i)
    (end [i] i))
  (let
    [disturb-interval [(ti/new-interval (t/time "07:00") (t/time "22:00"))]
     no-disturb-interval (ti/complement disturb-interval)
     can-disturb? (fn [t] (not (some #(t/coincident? % t) no-disturb-interval)))
     ]
    (is (not (can-disturb? (t/time "03:00"))))
    (is (not (can-disturb? (t/time "07:00"))))
    (is (can-disturb? (t/time "07:01")))
    (is (can-disturb? (t/time "12:00")))
    (is (can-disturb? (t/time "21:59")))
    (is (not (can-disturb? (t/time "22:00"))))
    (is (not (can-disturb? (t/time "00:00"))))))