timekeep-rs 0.1.0

A Rust library for efficient and reliable timekeeping functionalities, providing data structures and methods for creating, manipulating, and performing set operations on intervals.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! A module containing the `AtomicInterval` struct and its implementations.
//! An atomic interval is a closed or open interval that contains a single value or a range of values.
//! 
//! # Examples
//! ```
//! use timekeep_rs::{AtomicInterval, Bound};
//! 
//! let interval = AtomicInterval::closed(1, 5);
//! assert_eq!(*interval.left(), Bound::Included(1));
//! assert_eq!(*interval.right(), Bound::Included(5));
//! ```
//!
use crate::Bound;

/// A struct representing an atomic interval.
/// An atomic interval is a closed or open interval that contains a single value or a range of values.
/// 
/// # Fields
/// * `left` - The left endpoint of the interval
/// * `right` - The right endpoint of the interval
/// 
/// # Examples
/// ```
/// use timekeep_rs::{AtomicInterval, Bound};
///
/// let interval = AtomicInterval::closed(1, 5);
/// assert_eq!(*interval.left(), Bound::Included(1));
/// assert_eq!(*interval.right(), Bound::Included(5));
/// ```
/// 
#[derive(PartialEq, Debug, Clone)]
pub struct AtomicInterval<T> {
    left: Bound<T>,
    right: Bound<T>,
}


/// Implementation of the `ToString` trait for `AtomicInterval`.
impl<T: ToString> ToString for AtomicInterval<T> {
    /// This allows `AtomicInterval` to be converted to a string.
    /// 
    /// # Returns
    /// A string representation of the `AtomicInterval`
    fn to_string(&self) -> String {
        match (&self.left, &self.right) {
            (Bound::Included(l), Bound::Included(r)) => format!("[{}, {}]", l.to_string(), r.to_string()),
            (Bound::Included(l), Bound::Excluded(r)) => format!("[{}, {})", l.to_string(), r.to_string()),
            (Bound::Excluded(l), Bound::Included(r)) => format!("({}, {}]", l.to_string(), r.to_string()),
            (Bound::Excluded(l), Bound::Excluded(r)) => format!("({}, {})", l.to_string(), r.to_string()),
        }
    }
}


/// A collection of constructors for creating different types of atomic intervals.
impl<T: Clone + PartialOrd> AtomicInterval<T> {
    /// Creates an open interval (a,b) that excludes both endpoints.
    ///
    /// # Arguments
    /// * `left` - The left endpoint of the interval
    /// * `right` - The right endpoint of the interval
    ///
    /// # Returns
    /// A new `AtomicInterval` with excluded endpoints
    pub fn open(left: T, right: T) -> Self {
        if left >= right {
            panic!("The following condition must be valid: `left < right`");
        }
        AtomicInterval { left: Bound::Excluded(left), right: Bound::Excluded(right) }
    }

    /// Creates a closed interval [a,b] that includes both endpoints.
    ///
    /// # Arguments
    /// * `left` - The left endpoint of the interval
    /// * `right` - The right endpoint of the interval
    ///
    /// # Returns
    /// A new `AtomicInterval` with included endpoints
    pub fn closed(left: T, right: T) -> Self {
        if left >= right {
            panic!("The following condition must be valid: `left < right`");
        }
        AtomicInterval { left: Bound::Included(left), right: Bound::Included(right) }
    }

    /// Creates a left-open, right-closed interval (a,b] that excludes the left endpoint and includes the right endpoint.
    ///
    /// # Arguments
    /// * `left` - The left endpoint of the interval
    /// * `right` - The right endpoint of the interval
    ///
    /// # Returns
    /// A new `AtomicInterval` with excluded left endpoint and included right endpoint
    pub fn open_closed(left: T, right: T) -> Self {
        if left >= right {
            panic!("The following condition must be valid: `left < right`");
        }
        AtomicInterval { left: Bound::Excluded(left), right: Bound::Included(right) }
    }

    /// Creates a left-closed, right-open interval [a,b) that includes the left endpoint and excludes the right endpoint.
    ///
    /// # Arguments
    /// * `left` - The left endpoint of the interval
    /// * `right` - The right endpoint of the interval
    ///
    /// # Returns
    /// A new `AtomicInterval` with included left endpoint and excluded right endpoint
    pub fn closed_open(left: T, right: T) -> Self {
        if left >= right {
            panic!("The following condition must be valid: `left < right`");
        }
        AtomicInterval { left: Bound::Included(left), right: Bound::Excluded(right) }
    }

    /// Creates a point interval [a,a] containing a single value.
    ///
    /// # Arguments
    /// * `value` - The value to create a point interval from
    ///
    /// # Returns
    /// A new `AtomicInterval` representing a single point
    pub fn point(value: T) -> Self {
        AtomicInterval { left: Bound::Included(value.clone()), right: Bound::Included(value) }
    }
}


impl<T> AtomicInterval<T> {
    /// Return a reference to the left bound.
    /// 
    /// # Returns
    /// A reference of `Bound` associated to the left bound.
    pub fn left(&self) -> &Bound<T> {
        &self.left
    }

    /// Return a reference to the right bound.
    /// 
    /// # Returns
    /// A reference of `Bound` associated to the right bound.
    pub fn right(&self) -> &Bound<T> {
        &self.right
    }
}

/// A collection of methods for performing set operations on atomic intervals.
impl <T: PartialOrd> AtomicInterval<T> {
    /// Checks if the interval is a superset of another interval.
    /// An interval is a superset of another if it contains all the elements of the other interval.
    /// 
    /// # Arguments
    /// * `other` - The other interval to check if it is a subset of the current interval
    /// 
    /// # Returns
    /// `true` if the current interval is a superset of the other interval, `false` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    ///
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(2, 4);
    /// assert!(interval1.is_superset(&interval2));
    /// ```
    /// 
    pub fn is_superset (&self, other: &AtomicInterval<T>) -> bool {
        match (&self.left, &self.right, &other.left, &other.right) {
            (Bound::Included(l1), Bound::Excluded(r1), _, Bound::Included(r2)) => l1 <= other.left.value() && r1 > r2,
            (Bound::Excluded(l1), Bound::Included(r1), Bound::Included(l2), _) => l1 < l2 && r1 >= other.right.value(),
            (Bound::Excluded(l1), Bound::Excluded(r1), Bound::Included(l2), Bound::Included(r2)) => l1 < l2 && r1 > r2,
            (_, _, _, _) => self.left.value() <= other.left.value() && self.right.value() >= other.right.value(),
        }
    }

    /// Checks if the interval is a subset of another interval.
    /// An interval is a subset of another if it is contained within the other interval.
    /// 
    /// # Arguments
    /// * `other` - The other interval to check if it is a superset of the current interval
    ///
    /// # Returns
    /// `true` if the current interval is a subset of the other interval, `false` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    ///
    /// let interval1 = AtomicInterval::closed(2, 4);
    /// let interval2 = AtomicInterval::closed(1, 5);
    /// assert!(interval1.is_subset(&interval2));
    /// ```
    /// 
    pub fn is_subset (&self, other: &AtomicInterval<T>) -> bool {
        other.is_superset(self)
    }

    /// Checks if the interval is overlapping with another interval.
    /// Two intervals are overlapping if they share at least one common point.
    /// 
    /// # Arguments
    /// * `other` - The other interval to check if it is overlapping with the current interval
    /// 
    /// # Returns
    /// `true` if the current interval is overlapping with the other interval, `false` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(4, 6);
    /// assert!(interval1.is_overlapping(&interval2));
    /// ```
    /// 
    pub fn is_overlapping (&self, other: &AtomicInterval<T>) -> bool {
        // Check if the intervals are overlapping on left side of other
        let cond1_overlapping = match (&self.left, &self.right, &other.left) {
            (Bound::Included(l1), Bound::Included(r1), _) => other.left.value() >= l1 && other.left.value() <= r1,
            (Bound::Included(l1), Bound::Excluded(r1), Bound::Included(l2)) => l2 >= l1 && l2 < r1,
            (Bound::Included(l1), Bound::Excluded(r1), Bound::Excluded(l2)) => l2 >= l1 && l2 <= r1,
            (Bound::Excluded(l1), Bound::Included(r1), Bound::Included(l2)) => l2 > l1 && l2 <= r1,
            (Bound::Excluded(l1), Bound::Included(r1), Bound::Excluded(l2)) => l2 >= l1 && l2 <= r1,
            (Bound::Excluded(l1), Bound::Excluded(r1), Bound::Included(l2)) => l2 > l1 && l2 < r1,
            (Bound::Excluded(l1), Bound::Excluded(r1), Bound::Excluded(l2)) => l2 >= l1 && l2 <= r1,
        };
        // Check if the intervals are overlapping on right side of other
        let cond2_overlapping = match (&self.left, &self.right, &other.right) {
            (Bound::Included(l1), Bound::Included(r1), _) => other.right.value() >= l1 && other.right.value() <= r1,
            (Bound::Included(l1), Bound::Excluded(r1), Bound::Included(r2)) => r2 > l1 && r2 <= r1,
            (Bound::Included(l1), Bound::Excluded(r1), Bound::Excluded(r2)) => r2 >= l1 && r2 <= r1,
            (Bound::Excluded(l1), Bound::Included(r1), Bound::Included(r2)) => r2 >= l1 && r2 < r1,
            (Bound::Excluded(l1), Bound::Included(r1), Bound::Excluded(r2)) => r2 >= l1 && r2 <= r1,
            (Bound::Excluded(l1), Bound::Excluded(r1), Bound::Included(r2)) => r2 > l1 && r2 < r1,
            (Bound::Excluded(l1), Bound::Excluded(r1), Bound::Excluded(r2)) => r2 >= l1 && r2 <= r1,
        };
        // They overlap if either condition is true
        return cond1_overlapping || cond2_overlapping;
    }

    /// Checks if the interval is adjacent to another interval.
    /// Two intervals are adjacent if they share a common boundary, but do not overlap.
    /// 
    /// # Arguments
    /// * `other` - The other interval to check if it is adjacent to the current interval
    /// 
    /// # Returns
    /// `true` if the current interval is adjacent to the other interval, `false` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::open_closed(5, 10);
    /// assert!(interval1.is_adjacent(&interval2));
    /// ```
    pub fn is_adjacent(&self, other: &AtomicInterval<T>) -> bool {
        // Check if the intervals are adjacent on left side of other
        let cond1_adjacent = match (&self.left, &other.right) {
            (Bound::Excluded(_), Bound::Excluded(_)) => false,
            (Bound::Included(_), Bound::Included(_)) => false,
            (_, _) => self.left.value() == other.right.value(),
        };
        // Check if the intervals are adjacent on right side of other
        let cond2_adjacent = match (&self.right, &other.left) {
            (Bound::Excluded(_), Bound::Excluded(_)) => false,
            (Bound::Included(_), Bound::Included(_)) => false,
            (_, _) => self.right.value() == other.left.value(),
        };

        return cond1_adjacent || cond2_adjacent;
    }

    /// Checks if the interval is disjoint from another interval.
    /// Two intervals are disjoint if they do not share any common points.
    /// 
    /// # Arguments
    /// * `other` - The other interval to check if it is disjoint from the current interval
    /// 
    /// # Returns
    /// `true` if the current interval is disjoint from the other interval, `false` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(6, 10);
    /// assert!(interval1.is_disjoint(&interval2));
    /// ```
    /// 
    pub fn is_disjoint(&self, other: &AtomicInterval<T>) -> bool {
        // Check if the intervals are disjoint on one side
        let cond1_disjoint = match (&self.left, &other.right) {
            (Bound::Included(l1), Bound::Included(r2)) => l1 > r2,
            (_, _) => return self.right.value() <= other.left.value(),
        };

        // Check if the intervals are disjoint on the other side
        let cond2_disjoint = match (&self.right, &other.left) {
            (Bound::Included(r1), Bound::Included(l2)) => r1 < l2,
            (_, _) => return self.left.value() >= other.right.value(),
        };

        return cond1_disjoint || cond2_disjoint;
    }
}

impl <T: PartialOrd + Clone> AtomicInterval<T> {
    /// Computes the union of two overlapping or adjacent intervals.
    /// The union of two intervals is the smallest interval that contains both intervals.
    /// 
    /// # Arguments
    /// * `a` - The first interval to union
    /// * `b` - The second interval to union
    /// 
    /// # Returns
    /// A `Vec` containing the union of the two intervals if they are overlapping or adjacent, an empty `Vec` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(4, 7);
    /// let merged = AtomicInterval::union(&interval1, &interval2);
    /// 
    /// assert_eq!(merged.len(), 1);
    /// assert_eq!(merged.first().unwrap(), &AtomicInterval::closed(1, 7));
    /// ```
    /// 
    pub fn union(a: &AtomicInterval<T>, b: &AtomicInterval<T>) -> Vec<AtomicInterval<T>> {
        if a.is_overlapping(b) || a.is_adjacent(b) {
            let left = if a.left.value() <= b.left.value() {
                a.left.clone()
            } else {
                b.left.clone()
            };
            let right = if a.right.value() >= b.right.value() {
                a.right.clone()
            } else {
                b.right.clone()
            };
            vec![AtomicInterval { left, right }]
        } else {
            vec![]
        }
    }

    /// Computes the intersection of two overlapping intervals.
    /// The intersection of two intervals is the largest interval that is contained within both intervals.
    /// 
    /// # Arguments
    /// * `other` - The other interval to intersect with the current interval
    /// 
    /// # Returns
    /// A `Vec` containing the intersection of the two intervals if they are overlapping, an empty `Vec` otherwise
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(3, 7);
    /// let intersection = interval1.intersection(&interval2);
    /// 
    /// assert_eq!(intersection.len(), 1);
    /// assert_eq!(intersection.first().unwrap(), &AtomicInterval::closed(3, 5));
    /// ```
    /// 
    pub fn intersection(&self, other: &Self) -> Vec<Self> {
        // If they're disjoint, there's no intersection.
        if self.is_disjoint(other) {
            return vec![];
        }

        // Determine the left boundary of the intersection.
        let left = if self.left.value() > other.left.value() {
            self.left.clone()
        } else {
            other.left.clone()
        };

        // Determine the right boundary of the intersection.
        let right = if self.right.value() < other.right.value() {
            self.right.clone()
        } else {
            other.right.clone()
        };

        // If they meet at a single point, ensure it's included on both sides.
        if left.value() == right.value() {
            return match (left, right) {
                (Bound::Included(val), Bound::Included(_)) => {
                    vec![ AtomicInterval { left: Bound::Included(val.clone()), right: Bound::Included(val) } ]
                }
                _ => vec![],
            };
        }

        // Otherwise, we have a valid overlapping range.
        vec![ AtomicInterval { left, right } ]
    }

    /// Computes the difference between two intervals.
    /// The difference between two intervals is the set of intervals that are in the first interval but not in the second interval.
    /// 
    /// # Arguments
    /// * `other` - The other interval to compute the difference with the current interval
    /// 
    /// # Returns
    /// A `Vec` of `AtomicInterval` representing the difference between the two intervals
    /// 
    /// # Examples
    /// ```
    /// use timekeep_rs::AtomicInterval;
    /// 
    /// let interval1 = AtomicInterval::closed(1, 5);
    /// let interval2 = AtomicInterval::closed(3, 7);
    /// let difference = interval1.difference(&interval2);
    /// assert_eq!(difference.len(), 1);
    /// assert_eq!(difference[0], AtomicInterval::closed_open(1, 3));
    /// ```
    /// 
    pub fn difference(&self, other: &Self) -> Vec<Self> {
        // If disjoint, difference is just self.
        if self.is_disjoint(other) {
            return vec![self.clone()];
        } else if self.is_subset(other) {
            return vec![];
        }

        // If there's no intersection, difference is self.
        let intersection_vec = self.intersection(other);
        let intersection = intersection_vec.first().expect("No intersection found!");

        let mut result = Vec::new();

        // Left remainder: from self.left up to intersection.left (if any).
        if intersection.left.value() > self.left.value() {
            let left_interval = AtomicInterval {
                left: self.left.clone(),
                right: match &intersection.left {
                    Bound::Included(val) => Bound::Excluded(val.clone()),
                    Bound::Excluded(val) => Bound::Excluded(val.clone()),
                },
            };
            // Only add if valid (left <= right).
            if left_interval.left.value() < left_interval.right.value() {
                result.push(left_interval);
            }
        }

        // Right remainder: from intersection.right up to self.right (if any).
        if intersection.right.value() < self.right.value() {
            let right_interval = AtomicInterval {
                left: match &intersection.right {
                    Bound::Included(val) => Bound::Excluded(val.clone()),
                    Bound::Excluded(val) => Bound::Excluded(val.clone()),
                },
                right: self.right.clone(),
            };
            // Only add if valid (left <= right).
            if right_interval.left.value() < right_interval.right.value() {
                result.push(right_interval);
            }
        }

        result
    }

}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_open_interval() {
        let interval = AtomicInterval::open(1, 5);
        assert_eq!(interval.left, Bound::Excluded(1));
        assert_eq!(interval.right, Bound::Excluded(5));
    }

    #[test]
    fn test_closed_interval() {
        let interval = AtomicInterval::closed(1, 5);
        assert_eq!(interval.left, Bound::Included(1));
        assert_eq!(interval.right, Bound::Included(5));
    }

    #[test]
    fn test_open_closed_interval() {
        let interval = AtomicInterval::open_closed(1, 5);
        assert_eq!(interval.left, Bound::Excluded(1));
        assert_eq!(interval.right, Bound::Included(5));
    }

    #[test]
    fn test_closed_open_interval() {
        let interval = AtomicInterval::closed_open(1, 5);
        assert_eq!(interval.left, Bound::Included(1));
        assert_eq!(interval.right, Bound::Excluded(5));
    }

    #[test]
    fn test_point_interval() {
        let interval = AtomicInterval::point(1);
        assert_eq!(interval.left, Bound::Included(1));
        assert_eq!(interval.right, Bound::Included(1));
    }

    #[test]
    fn test_is_overlapping() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(4, 6);
        assert!(interval1.is_overlapping(&interval2));
    }

    #[test]
    fn test_is_adjacent() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::open_closed(5, 10);
        assert!(interval1.is_adjacent(&interval2));
    }

    #[test]
    fn test_is_disjoint() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(6, 10);
        assert!(interval1.is_disjoint(&interval2));
    }

    #[test]
    fn test_is_subset() {
        let interval1 = AtomicInterval::closed(2, 4);
        let interval2 = AtomicInterval::closed(1, 5);
        assert!(interval1.is_subset(&interval2));
    }

    #[test]
    fn test_is_superset() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(2, 4);
        assert!(interval1.is_superset(&interval2));
    }

    #[test]
    fn test_union_overlapping_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(4, 7);
        let merged = AtomicInterval::union(&interval1, &interval2);
        assert_eq!(merged.len(), 1);
        assert_eq!(merged.first().unwrap(), &AtomicInterval::closed(1, 7));
    }

    #[test]
    fn test_union_adjacent_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(5, 7);
        let merged = AtomicInterval::union(&interval1, &interval2);
        assert_eq!(merged.len(), 1);
        assert_eq!(merged.first().unwrap(), &AtomicInterval::closed(1, 7));
    }

    #[test]
    fn test_union_disjoint_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(6, 7);
        let merged = AtomicInterval::union(&interval1, &interval2);
        assert_eq!(merged.len(), 0);
    }

    #[test]
    fn test_intersection_between_two_overlapping_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(3, 7);
        let intersection = interval1.intersection(&interval2);
        assert_eq!(intersection.len(), 1);
        assert_eq!(intersection.first().unwrap(), &AtomicInterval::closed(3, 5));
    }

    #[test]
    fn test_intersection_between_two_disjoint_intervals() {
        let interval1 = AtomicInterval::closed(1, 3);
        let interval2 = AtomicInterval::closed(4, 7);
        let intersection = interval1.intersection(&interval2);
        assert_eq!(intersection.len(), 0);
    }

    #[test]
    fn test_intersection_between_two_adjacent_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::open(5, 7);
        let intersection = interval1.intersection(&interval2);
        assert_eq!(intersection.len(), 0);
    }

    #[test]
    fn test_difference_between_two_overlapping_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(3, 7);
        let difference = interval1.difference(&interval2);
        assert_eq!(difference.len(), 1);
        assert_eq!(difference[0], AtomicInterval::closed_open(1, 3));
    }

    #[test]
    fn test_difference_between_subset_and_superset_interval() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::closed(2, 4);
        let difference = interval1.difference(&interval2);
        assert_eq!(difference.len(), 2);
        assert_eq!(difference[0], AtomicInterval::closed_open(1, 2));
        assert_eq!(difference[1], AtomicInterval::open_closed(4, 5));
    }

    #[test]
    fn test_difference_between_two_disjoint_intervals() {
        let interval1 = AtomicInterval::closed(1, 3);
        let interval2 = AtomicInterval::closed(4, 7);
        let difference = interval1.difference(&interval2);
        assert_eq!(difference.len(), 1);
        assert_eq!(difference[0], AtomicInterval::closed(1, 3));
    }

    #[test]
    fn test_difference_between_two_adjacent_intervals() {
        let interval1 = AtomicInterval::closed(1, 5);
        let interval2 = AtomicInterval::open(5, 7);
        let difference = interval1.difference(&interval2);
        assert_eq!(difference.len(), 1);
        assert_eq!(difference[0], AtomicInterval::closed(1, 5));
    }
}