Back

ordered-disjoint-intervals? (clj)

(source)

function

(ordered-disjoint-intervals? s)
Are all the intervals in the given set time-ordered and disjoint? This is a useful property of a collection of intervals. The given collection must contain proper intervals (that is, intervals that have finite greater-than-zero durations).

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])))

(deftest ordered-disjoint-intervals?-test
  (is
    (ti/ordered-disjoint-intervals? []))
  (is
    (ti/ordered-disjoint-intervals?
      [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                        (t/instant "2017-07-30T10:00:00Z"))]))
  (is
    (ti/ordered-disjoint-intervals?
      [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                        (t/instant "2017-07-30T10:00:00Z"))
       (ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                        (t/instant "2017-07-30T13:00:00Z"))]))
  (is
    (ti/ordered-disjoint-intervals?
      [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                        (t/instant "2017-07-30T11:00:00Z"))
       (ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                        (t/instant "2017-07-30T13:00:00Z"))]))
  (is
    (ti/ordered-disjoint-intervals?
      [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                        (t/instant "2017-07-30T11:00:00Z"))
       (ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                        (t/instant "2017-07-30T13:00:00Z"))
       (ti/new-interval (t/instant "2017-07-30T16:00:00Z")
                        (t/instant "2017-07-30T18:00:00Z"))]))
  (is
    (false?
      (ti/ordered-disjoint-intervals?
        [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                          (t/instant "2017-07-30T12:00:00Z"))
         (ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                          (t/instant "2017-07-30T13:00:00Z"))])))

  (is
    (false?
      (ti/ordered-disjoint-intervals?
        [(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                          (t/instant "2017-07-30T13:00:00Z"))
         (ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                          (t/instant "2017-07-30T10:00:00Z"))]))))

(deftest union-test
  (testing "counts"
    (let [coll1 [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
                                  (t/instant "2017-07-30T12:00:00Z"))]
          coll2 [(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
                                  (t/instant "2017-07-30T15:00:00Z"))]
          coll3 [(ti/new-interval (t/instant "2017-07-30T17:00:00Z")
                                  (t/instant "2017-07-30T19:00:00Z"))]]
      (is (= 1 (count (ti/union coll1 coll2))))
      (is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2)))
      (is (= 2 (count (ti/union coll1 coll2 coll3))))
      (is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2 coll3)))))