Merge remote-tracking branch 'joncham/sgen-msvc2'
[mono.git] / mcs / class / Mono.CodeContracts / Test / Mono.CodeContracts.Static / DisIntervalTests.cs
1 // 
2 // DisIntervalTests.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2012 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using Mono.CodeContracts.Static.Analysis.Numerical;
30 using Mono.CodeContracts.Static.DataStructures;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.Mono.CodeContracts {
35         [TestFixture]
36         class DisIntervalTests : DomainTestBase<DisInterval> {
37                 protected override DisInterval Top { get { return DisInterval.TopValue; } }
38                 protected override DisInterval Bottom { get { return DisInterval.BottomValue; } }
39                 protected override DisInterval Normal { get { return DisInterval.For (this._1__2); } }
40
41                 readonly Interval _0__1 = Interval.For (0, 1);
42                 readonly Interval _0__4 = Interval.For (0, 4);
43                 readonly Interval _1__2 = Interval.For (1, 2);
44                 readonly Interval _1__3 = Interval.For (1, 3);
45                 readonly Interval _1__4 = Interval.For (1, 4);
46                 readonly Interval _1__5 = Interval.For (1, 5);
47                 readonly Interval _2__4 = Interval.For (2, 4);
48                 readonly Interval _2__5 = Interval.For (2, 5);
49                 readonly Interval _3__4 = Interval.For (3, 4);
50
51                 static Interval JoinAll (params Interval[] intervals)
52                 {
53                         return DisInterval.JoinAll (Sequence<Interval>.From (intervals));
54                 }
55
56                 [Test]
57                 public void ForSingleInterval ()
58                 {
59                         DisInterval disInterval = DisInterval.For (this._1__2);
60
61                         Assert.That (disInterval.AsInterval, Is.EqualTo (this._1__2));
62                 }
63
64                 [Test]
65                 public void JoinAllForIntervals ()
66                 {
67                         Assert.That (JoinAll (this._1__2), Is.EqualTo (this._1__2));
68                         Assert.That (JoinAll (this._1__2, this._3__4), Is.EqualTo (this._1__4));
69                         Assert.That (JoinAll (), Is.EqualTo (Interval.TopValue));
70                 }
71
72                 [Test]
73                 public void NormalizeTests ()
74                 {
75                         bool isBottom;
76                         Assert.AreEqual (
77                                 DisInterval.Normalize (Sequence<Interval>.From (this._1__2, this._3__4), out isBottom),
78                                 Sequence<Interval>.From (this._1__4));
79
80                         Assert.AreEqual (
81                                 DisInterval.Normalize (Sequence<Interval>.From (this._1__4, this._1__2), out isBottom),
82                                 Sequence<Interval>.From (this._1__4));
83
84                         Assert.AreEqual (
85                                 DisInterval.Normalize (Sequence<Interval>.From (this._1__2, this._1__4), out isBottom),
86                                 Sequence<Interval>.From (this._1__4));
87
88                         Assert.AreEqual (
89                                 DisInterval.Normalize (Sequence<Interval>.From (this._1__4, this._2__4), out isBottom),
90                                 Sequence<Interval>.From (this._1__4));
91
92                         Assert.AreEqual (
93                                 DisInterval.Normalize (Sequence<Interval>.From (this._1__3, this._2__5), out isBottom),
94                                 Sequence<Interval>.From (this._1__5));
95
96                         Assert.AreEqual (
97                                 DisInterval.Normalize (Sequence<Interval>.From (Interval.BottomValue, Interval.BottomValue), out isBottom),
98                                 Sequence<Interval>.Empty);
99                         Assert.IsTrue (isBottom);
100
101                         Assert.AreEqual (
102                                 DisInterval.Normalize (Sequence<Interval>.From (Interval.BottomValue), out isBottom),
103                                 Sequence<Interval>.Empty);
104                         Assert.IsTrue (isBottom);
105                 }
106
107                 [Test]
108                 public void ShouldHaveAddOperation ()
109                 {
110                         Assert.That (DisInterval.For (this._1__2) + DisInterval.For (this._3__4),
111                                      Is.EqualTo (DisInterval.For (Interval.For (4, 6))));
112                 }
113
114                 [Test]
115                 public void ShouldHaveJoinOperation ()
116                 {
117                         DisInterval left = DisInterval.For (this._0__1).Join (DisInterval.For (this._3__4));
118                         DisInterval right = DisInterval.For (this._1__2).Join (DisInterval.For (this._1__4));
119                         Assert.That (left.Join (right), Is.EqualTo (DisInterval.For (this._0__4)));
120                 }
121
122                 [Test]
123                 public void ShouldHaveMeetOperation ()
124                 {
125                         Assert.That (DisInterval.For (this._1__4).Meet (DisInterval.For (this._1__2)),
126                                      Is.EqualTo (DisInterval.For (this._1__2)));
127                 }
128
129                 [Test]
130                 public void ShouldHaveSubOperation ()
131                 {
132                         Assert.That (DisInterval.For (this._1__2) - DisInterval.For (this._3__4),
133                                      Is.EqualTo (DisInterval.For (Interval.For (-3, -1))));
134                 }
135         }
136 }