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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
use std::fmt;
use std::cmp;

use std::str::FromStr;

/// Struct `Interval` containing two values representing the limit of the interval.
///
/// The `Interval` is incluse which means that `Interval(0, 10)` is [0, 10].
/// The value 0 is supposed to be equals or greater than the second value.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Interval(u32, u32);

/// Struct `IntervalSet` representing a set of sorted not overllaping intervals.
/// Be aware that the validity of the interval set is not checked.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IntervalSet {
    intervals: Vec<Interval>,
}

/// Struct to iterate through an `IntervalSet`
pub struct IntervalSetIterator<'a> {
    pos: usize,
    inner: &'a IntervalSet,
}

impl<'a> Iterator for IntervalSetIterator<'a> {
    type Item = &'a Interval;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.inner.intervals.len() {
            None
        } else {
            self.pos += 1;
            self.inner.intervals.get(self.pos - 1)
        }
    }
}

impl Interval {
    pub fn new(begin: u32, end: u32) -> Interval {
        let res = Interval(begin, end);
        if !res.is_valid() {
            panic!("Call constructor of Interval with invalid endpoints: Interval({}, {})",
                   begin,
                   end);
        }
        res
    }

    /// Return the maximum interval possible (with u32 var)
    pub fn whole() -> Interval {
        Interval(u32::min_value(), u32::max_value())
    }

    /// Because the trait Order is needed to sort the IntervalSet I dont what to change the
    /// native order. This function coud be considered as the `len` of the interval.
    pub fn range_size(&self) -> u32 {
        self.1 - self.0 + 1
    }

    /// Simply return an equivalent interval as tuple.
    pub fn as_tuple(&self) -> (u32, u32) {
        (self.0, self.1)
    }

    /// I am not sure about those two function, maybe set the field as public could be a better
    /// idea...
    pub fn get_inf(&self) -> u32 {
        self.0
    }

    pub fn get_sup(&self) -> u32 {
        self.1
    }

    /// Utility function check if the interval is valid.
    ///
    /// # Examples
    /// The following intervals are valids:
    ///
    /// ```
    /// use interval_set::Interval;
    /// Interval::new(0, 0);
    /// Interval::new(10, 100);
    /// ```
    ///
    /// The following intervals ae not valid:
    ///
    /// ```rust,should_panic
    /// use interval_set::Interval;
    /// Interval::new(10, 0);
    /// ```
    pub fn is_valid(&self) -> bool {
        self.0 <= self.1
    }
}

/// Trait `ToIntervalSet` allows to write a function to convert type into an IntervalSet.
pub trait ToIntervalSet {
    /// Consume `self` to create an IntervalSet
    fn to_interval_set(self) -> IntervalSet;
}

impl ToIntervalSet for Interval {
    /// Convert a simple interval into an intervalset.
    /// Note that the validity of the interval is checked.
    fn to_interval_set(self) -> IntervalSet {
        if self.is_valid() {
            IntervalSet { intervals: vec![self] }
        } else {
            panic!("CReate interval set from an unvalid interval");
        }
    }
}

impl ToIntervalSet for String {
    /// Convert a string formatted into an
    /// interval set.
    /// The rules are simple for the string to be
    /// valid.
    /// - Each intervals are separated by a space.
    /// - Each bounds of the interval are separated by
    ///   a dash(-).
    /// - If an interval is of size 1, it is sufficient to
    ///   write only one integer.
    /// # Example
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    /// use interval_set::Interval;
    /// let interval = String::from("3-4 7-19").to_interval_set();
    /// assert_eq!(interval, vec![(3, 4), (7, 19)].to_interval_set());
    ///
    /// let interval = String::from("3-4 6 7-19").to_interval_set();
    /// assert_eq!(interval, vec![(3, 4), (6, 6) ,(7, 19)].to_interval_set());
    ///
    ///
    /// let interval = String::from("3-4 7-19 6").to_interval_set();
    /// assert_eq!(interval, vec![(3, 4), (6, 6), (7, 19)].to_interval_set());
    ///
    ///
    /// let interval = String::from("3-4 7-19 6").to_interval_set();
    /// let interval_bis = String::from("3-3 4 7-7 8 9-19 6").to_interval_set();
    /// assert_eq!(interval, interval_bis);
    ///
    /// ```
    fn to_interval_set(self) -> IntervalSet {
        let mut iter = self.split_whitespace();
        let mut result = IntervalSet::empty();
        for interval in iter {
            // Handles the case where we have two specified bounds.
            if interval.contains("-") {
                // split by - and use map to transform the string into u32
                let bounds: Vec<u32> =
                    interval.split('-').map(|b| u32::from_str(b).unwrap()
                                            ).collect();

                let interval = Interval::new(bounds[0], bounds[1]);
                result = result.union(interval.to_interval_set());
            } else {
                let bound = u32::from_str(interval).unwrap();
                result = result.union(Interval::new(bound, bound).to_interval_set());
            }
        }
        result
    }
}

impl ToIntervalSet for Vec<Interval> {
    /// Convert an array of interval into an intervalset.
    /// Note that the validity of the intervals are checked.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    /// use interval_set::Interval;
    /// vec![Interval::new(5, 10), Interval::new(15, 20)].to_interval_set();
    /// ```
    fn to_interval_set(self) -> IntervalSet {
        let mut res: IntervalSet = IntervalSet::empty();
        for intv in self {
            if !intv.is_valid() {
                panic!("Invalid interval: {}-{}", intv.0, intv.1)
            }
            res.insert(intv);
        }
        res
    }
}

impl ToIntervalSet for Vec<(u32, u32)> {
    /// Convert an array of tuples into an intervalset.
    /// Note that the validity of the intervals are checked.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    /// vec![(5, 10), (15, 20)].to_interval_set();
    /// ```
    fn to_interval_set(self) -> IntervalSet {
        let mut res: IntervalSet = IntervalSet::empty();
        for (begin, end) in self {
            if begin > end {
                panic!("Invalid interval: {}-{}", begin, end)
            }
            res.insert(Interval(begin, end));
        }
        res
    }
}

impl IntervalSet {
    /// Function to create an empty interval set.
    pub fn empty() -> IntervalSet {
        IntervalSet { intervals: vec![] }
    }

    /// Return `true` if the interval is empty.
    pub fn is_empty(&self) -> bool {
        self.intervals.len() == 0
    }

    /// Return the union of two intervals.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10)].to_interval_set();
    /// let b = vec![(15, 20)].to_interval_set();
    /// a.union(b); // [5-10, 15-20]
    /// ```
    pub fn union(self, rhs: IntervalSet) -> IntervalSet {
        self.merge(rhs, &|a, b| -> bool { a | b })
    }

    /// Return the intersection of two intervals.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10)].to_interval_set();
    /// let b = vec![(5, 10), (15, 20)].to_interval_set();
    /// a.intersection(b); //[5-10]
    /// ```
    pub fn intersection(self, rhs: IntervalSet) -> IntervalSet {
        self.merge(rhs, &|a, b| -> bool { a & b })
    }

    /// Return the difference between two intervals.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10), (15, 20)].to_interval_set();
    /// let b = vec![(5, 10)].to_interval_set();
    /// a.difference(b); //[15-20]
    /// ```
    pub fn difference(self, rhs: IntervalSet) -> IntervalSet {
        self.merge(rhs, &|a, b| -> bool { a & !b })
    }

    /// Return the symetric difference of two intervals.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10), (15, 20)].to_interval_set();
    /// let b = vec![(0, 10)].to_interval_set();
    /// a.difference(b); //[0-5, 15-20]
    /// ```
    pub fn symetric_difference(self, rhs: IntervalSet) -> IntervalSet {
        self.merge(rhs, &|a, b| -> bool { a ^ b })
    }

    /// Return the greater interval from the set.
    /// Note that the function return a cloned interval, so I will be easier to manipulate.
    /// Moreover, in the case where many intervals have the same size,
    /// the function will return the first element.
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    /// use interval_set::interval_set::IntervalSet;
    /// use interval_set::interval_set::Interval;
    ///
    /// let a = vec![(5, 10), (15, 25)].to_interval_set();
    /// let b = vec![(5, 10), (15, 20)].to_interval_set();
    /// let c = vec![(5, 10), (15, 20), (100, 1000)].to_interval_set();
    ///
    /// assert_eq!(a.max().unwrap(), Interval::new(15, 25));
    /// assert_eq!(b.max().unwrap(), Interval::new(5, 10));
    /// assert_eq!(c.max().unwrap(), Interval::new(100, 1000));
    /// assert_eq!(IntervalSet::empty().max(), None);
    ///
    /// ```
    pub fn max(&self) -> Option<Interval> {
        let mut max = usize::min_value();
        let mut res = None;

        if self.is_empty() {
            return None;
        }

        for intv in self.iter() {
            let curr_: usize = (intv.1 - intv.0) as usize;
            if curr_ > max {
                max = curr_ as usize;
                res = Some(intv.clone());
            }
        }
        res
    }

    /// Return the size of the interval set. The sie is defined by the sum of the len of each
    /// intervals contained into the set.
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10), (15, 20)].to_interval_set();
    /// let b = vec![(0, 10), (15, 20)].to_interval_set();
    /// assert_eq!(a.size(), 12);
    /// assert_eq!(b.size(), 17);
    /// ```
    pub fn size(&self) -> u32 {
        if self.is_empty() {
            return 0;
        }
        self.iter().fold(0, |acc, ref x| acc + (x.range_size()))
    }

    /// Get an iterator over an IntervalSet
    ///
    /// # Example
    ///
    /// ```
    /// use interval_set::interval_set::ToIntervalSet;
    ///
    /// let a = vec![(5, 10), (15, 20)].to_interval_set();
    /// for intv in a.iter() {
    ///     let tuple = intv.as_tuple();
    ///     println!("{}--{}", tuple.0, tuple.1);
    /// }
    ///
    /// ```
    pub fn iter<'a>(&'a self) -> IntervalSetIterator<'a> {
        IntervalSetIterator {
            inner: self,
            pos: 0,
        }
    }

    /// Generate the (flat) list of interval bounds of the requested merge.
    /// The implementation is inspired by  http://stackoverflow.com/a/20062829.
    fn merge(self, rhs: IntervalSet, keep_operator: &Fn(bool, bool) -> bool) -> IntervalSet {
        if self.is_empty() & rhs.is_empty() {
            return self;
        }

        let mut lflat = self.flatten();
        let mut rflat = rhs.flatten();

        let sentinel: u32 = *cmp::max(lflat.iter().max(), rflat.iter().max()).unwrap() + 1;

        lflat.push(sentinel);
        rflat.push(sentinel);

        let mut ltail = lflat.iter().enumerate();
        let mut rtail = rflat.iter().enumerate();

        let mut res = vec![];

        //Because both vec are supposed to be sorted we could only take the min of vec[0].
        let mut scan: u32 = *cmp::min(lflat.iter().min(), rflat.iter().min()).unwrap();

        let mut lhead = ltail.next().unwrap();
        let mut rhead = rtail.next().unwrap();

        while scan < sentinel {
            let lin = !((scan < *lhead.1) ^ (lhead.0 % 2 != 0));
            let rin = !((scan < *rhead.1) ^ (rhead.0 % 2 != 0));

            let inres = keep_operator(lin, rin);

            if inres ^ (res.len() % 2 != 0) {
                res.push(scan);
            }

            if scan == *lhead.1 {
                lhead = match ltail.next() {
                    Some((lpos, lval)) => (lpos, lval),
                    _ => panic!("Deal with it braw"),
                };
            }
            if scan == *rhead.1 {
                rhead = match rtail.next() {
                    Some(rval) => rval,
                    _ => panic!("Deal with it braw"),
                };
            }
            scan = cmp::min(*lhead.1, *rhead.1);
        }
        IntervalSet::unflatten(res)
    }

    /// Generate a vector of endpoints.
    /// For example with the interval set `[0-5, 9-9,]`
    /// The resulting array would be: [0, 5, 9]
    fn flatten(self) -> Vec<u32> {
        let mut res = vec![];
        for intv in self.intervals {
            res.extend(vec![intv.0, intv.1 + 1]);
        }
        res
    }

    /// From an array of endpoints generate an `IntervalSet`.
    fn unflatten(vec: Vec<u32>) -> IntervalSet {
        let mut res: Vec<Interval> = Vec::new();
        let mut i = 0;
        while i < vec.len() {
            res.push(Interval(vec[i], vec[i + 1] - 1));
            i += 2;
        }
        res.to_interval_set()
    }

    pub fn insert(&mut self, element: Interval) {
        let mut newinf = element.0;
        let mut newsup = element.1;

        // Because we may remove one interval from self while we loop through its clone, we need to
        // adjuste the position.
        let mut idx_shift = 0;
        for (pos, intv) in self.intervals.clone().iter().enumerate() {
            if newinf > intv.1 + 1 {
                continue;
            }
            if newsup + 1 < intv.0 {
                break;
            }

            self.intervals.remove(pos - idx_shift);
            idx_shift += 1;

            newinf = cmp::min(newinf, intv.0);
            newsup = cmp::max(newsup, intv.1);
        }
        self.intervals.push(Interval::new(newinf, newsup));
        self.intervals.sort();
    }
}

impl fmt::Display for Interval {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.0 == self.1 {
            write!(f, "{}", self.0)
        } else {
            write!(f, "{}-{}", self.0, self.1)
        }
    }
}

impl fmt::Display for IntervalSet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (pos, interval) in self.intervals.iter().enumerate() {
            if pos == self.intervals.len() - 1 {
                f.write_fmt(format_args!("{}", interval))?;
            } else {
                f.write_fmt(format_args!("{} ", interval))?;
            }
        }
        write!(f, "")
    }
}

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

    #[test]
    fn test_print() {
        let empty_set = IntervalSet::empty();
        assert_eq!(format!("{}", empty_set), "");
    }

    fn assert_to_interval_set(tes_id: u32, v: Vec<(u32, u32)>, expected: IntervalSet) {
        assert_eq!(v.to_interval_set(), expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_to_interval_set() {
        let sym_cases =
            vec![(1, vec![(5, 10)], IntervalSet { intervals: vec![Interval(5, 10)] }),
                 (2, vec![(0, 100), (5, 10)], IntervalSet { intervals: vec![Interval(0, 100)] }),
                 (3,
                  vec![(1, 1), (2, 2), (3, 3), (4, 10), (10, 20)],
                  IntervalSet { intervals: vec![Interval(1, 20)] })];

        for (id, v, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_to_interval_set(id, v, expected);
        }
    }

    fn assert_insertion(tes_id: u32, a: IntervalSet, element: Interval, expected: IntervalSet) {
        let mut a_ = a.clone();
        a_.insert(element);
        assert_eq!(a_, expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_insertion() {
        let sym_cases: Vec<(u32, IntervalSet, Interval, IntervalSet)> =
            vec![(1,
                  IntervalSet { intervals: vec![Interval(0, 0)] },
                  Interval(1, 1),
                  IntervalSet { intervals: vec![Interval(0, 1)] }),
                 (2,
                  IntervalSet { intervals: vec![Interval(0, 0), Interval(2, 2)] },
                  Interval(1, 1),
                  IntervalSet { intervals: vec![Interval(0, 2)] }),
                 (3,
                  IntervalSet { intervals: vec![Interval(0, 3)] },
                  Interval(1, 1),
                  IntervalSet { intervals: vec![Interval(0, 3)] }),
                 (4,
                  IntervalSet { intervals: vec![Interval(1, 1)] },
                  Interval(0, 3),
                  IntervalSet { intervals: vec![Interval(0, 3)] }),
                 (5,
                  IntervalSet { intervals: vec![Interval(0, 100)] },
                  Interval(1, 3),
                  IntervalSet { intervals: vec![Interval(0, 100)] }),
                 (6,
                  IntervalSet { intervals: vec![Interval(10, 20)] },
                  Interval(40, 80),
                  IntervalSet { intervals: vec![Interval(10, 20), Interval::new(40, 80)] })];

        for (id, a, element, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_insertion(id, a, element, expected);
        }
    }

    #[test]
    fn test_flatten() {
        let simple_range = vec![Interval(0, 10)].to_interval_set();
        let disjoint = vec![Interval(0, 10), Interval(15, 20)].to_interval_set();
        assert_eq!(simple_range.flatten(), vec![0, 11]);
        assert_eq!(disjoint.flatten(), vec![0, 11, 15, 21]);
    }

    #[test]
    fn test_unflatten() {
        let simple_range = vec![0, 11];
        let disjoint = vec![0, 11, 15, 21];
        assert_eq!(IntervalSet::unflatten(disjoint),
                   vec![Interval(0, 10), Interval(15, 20)].to_interval_set());
        assert_eq!(IntervalSet::unflatten(simple_range),
                   vec![Interval(0, 10)].to_interval_set());
    }

    fn assert_difference(tes_id: u32, a: IntervalSet, b: IntervalSet, expected: IntervalSet) {
        assert_eq!(a.difference(b), expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_difference() {
        let sym_cases: Vec<(u32, IntervalSet, IntervalSet, IntervalSet)> =
            vec![(1,
                  vec![Interval(5, 10)].to_interval_set(),
                  vec![Interval(5, 10), Interval(15, 20)].to_interval_set(),
                  IntervalSet::empty()),
                 (2,
                  vec![(5, 10)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty()),
                 (3,
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty()),
                 (4,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (5,
                  vec![(0, 100)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(0, 4), (11, 14), (21, 100)].to_interval_set()),
                 (6,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(0, 100)].to_interval_set(),
                  IntervalSet::empty()),
                 (7, IntervalSet::empty(), IntervalSet::empty(), IntervalSet::empty())];

        for (id, a, b, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_difference(id, a, b, expected);
        }
    }

    fn assert_intersection(tes_id: u32, a: IntervalSet, b: IntervalSet, expected: IntervalSet) {
        assert_eq!(a.intersection(b), expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_intersection() {
        let sym_cases: Vec<(u32, IntervalSet, IntervalSet, IntervalSet)> =
            vec![(1,
                  vec![Interval(5, 10)].to_interval_set(),
                  vec![Interval(5, 10), Interval(15, 20)].to_interval_set(),
                  vec![Interval(5, 10)].to_interval_set()),
                 (2,
                  vec![(5, 10)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(5, 10)].to_interval_set()),
                 (3,
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty()),
                 (4,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty(),
                  IntervalSet::empty()),
                 (5,
                  vec![(0, 100)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (6, IntervalSet::empty(), IntervalSet::empty(), IntervalSet::empty())];

        for (id, a, b, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_intersection(id, a, b, expected);
        }
    }

    fn assert_union(tes_id: u32, a: IntervalSet, b: IntervalSet, expected: IntervalSet) {
        assert_eq!(a.union(b), expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_union() {
        // Note: the first number is the test id, so it should be easy
        // to identify which test has failed.
        // The two first vectors are the operands and the expected result is last.
        let sym_cases: Vec<(u32, IntervalSet, IntervalSet, IntervalSet)> =
            vec![(1,
                  vec![Interval(5, 10)].to_interval_set(),
                  vec![Interval(5, 10), Interval(15, 20)].to_interval_set(),
                  vec![Interval(5, 10), Interval(15, 20)].to_interval_set()),
                 (2,
                  vec![(5, 10)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (3,
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (4,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (5,
                  vec![(0, 100)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(0, 100)].to_interval_set()),
                 (6, IntervalSet::empty(), IntervalSet::empty(), IntervalSet::empty()),
                 (7,
                  vec![(0, 0), (2, 2), (3, 3)].to_interval_set(),
                  vec![(1, 1)].to_interval_set(),
                  vec![(0, 3)].to_interval_set())];

        for (id, a, b, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_union(id, a, b, expected);
        }
    }

    fn assert_symetric_difference(tes_id: u32,
                                  a: IntervalSet,
                                  b: IntervalSet,
                                  expected: IntervalSet) {
        assert_eq!(a.symetric_difference(b), expected, "Test {} failed", tes_id);
    }

    #[test]
    fn test_symetric_difference() {
        let sym_cases: Vec<(u32, IntervalSet, IntervalSet, IntervalSet)> =
            vec![(1,
                  vec![Interval(5, 10)].to_interval_set(),
                  vec![Interval(5, 10), Interval(15, 20)].to_interval_set(),
                  vec![(15, 20)].to_interval_set()),
                 (2,
                  vec![(5, 10)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(15, 20)].to_interval_set()),
                 (3,
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (4,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  IntervalSet::empty(),
                  vec![(5, 10), (15, 20)].to_interval_set()),
                 (5,
                  vec![(0, 100)].to_interval_set(),
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(0, 4), (11, 14), (21, 100)].to_interval_set()),
                 (6,
                  vec![(5, 10), (15, 20)].to_interval_set(),
                  vec![(0, 100)].to_interval_set(),
                  vec![(0, 4), (11, 14), (21, 100)].to_interval_set()),
                 (7, IntervalSet::empty(), IntervalSet::empty(), IntervalSet::empty())];

        for (id, a, b, expected) in sym_cases {
            //assert_eq!(format!("test #{} of union", id), a, b, |x,y| x.union(y), expected);
            assert_symetric_difference(id, a, b, expected);
        }
    }
}