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
// Copyright 2015 Pierre Talbot (IRCAM)

// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use kind::*;
use ops::*;
use num::One;

macro_rules! integer_basic_ops_impl
{
  ( $( $source:ty, $size:ty ),* ) =>
  {$(
    impl GroundType for $source {}

    impl Cardinality for $source
    {
      type Size = $size;
      fn size(&self) -> $size {
        <$size as One>::one()
      }
    }

    impl Singleton for $source {
      fn singleton(value: $source) -> $source {
        value
      }
    }

    impl Collection for $source {
      type Item = $source;
    }

    impl Bounded for $source {
      fn lower(&self) -> $source {
        *self
      }
      fn upper(&self) -> $source {
        *self
      }
    }

    impl Contains for $source
    {
      fn contains(&self, value: &$source) -> bool {
        self == value
      }
    }

    impl Disjoint for $source
    {
      fn is_disjoint(&self, value: &$source) -> bool {
        self != value
      }
    }

    impl Subset for $source
    {
      fn is_subset(&self, value: &$source) -> bool {
        self == value
      }
    }

    impl ProperSubset for $source
    {
      fn is_proper_subset(&self, _value: &$source) -> bool {
        false
      }
    }

    impl Overlap for $source
    {
      fn overlap(&self, value: &$source) -> bool {
        self == value
      }
    }
  )*}
}

integer_basic_ops_impl!(i8,u8,u8,u8,i16,u16,u16,u16,i32,u32,u32,u32,i64,u64,u64,u64,isize,usize,usize,usize);

impl GroundType for bool {}
impl GroundType for char {}
impl GroundType for f32 {}
impl GroundType for f64 {}

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

  #[test]
  fn simple_tests() {
    for ref i in -2i32..10 {
      assert_eq!(i.size(), 1u32);
      assert_eq!(i.is_singleton(), true);
      assert_eq!(i.is_empty(), false);
      let res: i32 = Singleton::singleton(*i);
      assert_eq!(res, *i);
      assert_eq!(i.lower(), *i);
      assert_eq!(i.upper(), *i);
      for ref j in -10..10 {
        assert_eq!(i.contains(j), j.contains(i));
        assert_eq!(i.contains(j), i == j);
        assert_eq!(i.is_subset(j), i.contains(j));
        assert_eq!(i.overlap(j), i.contains(j));
        assert_eq!(i.is_subset(j), j.is_subset(i));

        assert_eq!(i.is_disjoint(j), j.is_disjoint(i));
        assert_eq!(i.is_disjoint(j), i != j);
        assert_eq!(i.is_proper_subset(j), false);
        assert_eq!(j.is_proper_subset(i), false);
      }
    }
  }
}