Merge remote-tracking branch 'joncham/sgen-msvc2'
[mono.git] / mcs / class / Mono.CodeContracts / Test / Mono.CodeContracts.Static / IntervalTests.cs
1 // 
2 // IntervalTests.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
31 using NUnit.Framework;
32
33 namespace MonoTests.Mono.CodeContracts {
34         [TestFixture (typeof (Interval))]
35         class IntervalTests : DomainTestBase<Interval> {
36                 readonly Interval _1__2 = Interval.For (1, 2);
37                 readonly Interval _2__4 = Interval.For (2, 4);
38                 readonly Interval _3__4 = Interval.For (3, 4);
39                 readonly Interval _1__4 = Interval.For (1, 4);
40
41                 readonly Interval zero_to_one = Interval.For (Rational.Zero, Rational.One);
42                 readonly Interval minus_one_to_zero = Interval.For (Rational.MinusOne, Rational.Zero);
43
44                 protected override Interval Top { get { return Interval.TopValue; } }
45                 protected override Interval Bottom { get { return Interval.BottomValue; } }
46                 protected override Interval Normal { get { return this._1__2; } }
47
48                 [Test]
49                 public void ConsecutiveIntegers ()
50                 {
51                         Assert.That (Interval.AreConsecutiveIntegers (this._1__2, this._3__4), Is.True);
52                         Assert.That (Interval.AreConsecutiveIntegers (this._1__2, this._1__2), Is.False);
53                         Assert.That (Interval.AreConsecutiveIntegers (this._3__4, this._1__2), Is.False);
54                         Assert.That (Interval.AreConsecutiveIntegers (Interval.For (Rational.For (1, 3)), this._1__2),
55                                      Is.False);
56                 }
57
58                 [Test]
59                 public void OnTheLeftOf ()
60                 {
61                         Assert.IsTrue (this._1__2.OnTheLeftOf (this._3__4));
62                         Assert.IsTrue (this._1__2.OnTheLeftOf (this._2__4));
63
64                         Assert.IsFalse (this._2__4.OnTheLeftOf (this._1__2));
65                         Assert.IsFalse (this._1__4.OnTheLeftOf (this._1__2));
66                         Assert.IsFalse (this._1__2.OnTheLeftOf (this._1__4));
67                 }
68
69                 [Test]
70                 public void ShouldAddIntervalsByEachBound ()
71                 {
72                         Assert.That (this.Bottom + this._1__2, Is.EqualTo (this.Bottom), "bottom + normal = bottom");
73                         Assert.That (this.Bottom + this.Top, Is.EqualTo (this.Bottom), "bottom + top = bottom");
74                         Assert.That (this.Bottom + this.Bottom, Is.EqualTo (this.Bottom), "bottom + bottom = bottom");
75
76                         Assert.That (this.Top + this.Top, Is.EqualTo (this.Top), "top + top = top");
77                         Assert.That (this.Top + this._1__2, Is.EqualTo (this.Top), "top + normal = top");
78                         Assert.That (this.Top + this.Bottom, Is.EqualTo (this.Bottom), "top + bottom = bottom");
79
80                         Assert.That (this._1__2 + this.Bottom, Is.EqualTo (this.Bottom), "normal + bottom = bottom");
81                         Assert.That (this._1__2 + this.Top, Is.EqualTo (this.Top), "normal + top = top");
82                         Assert.That (this._1__2 + this._3__4, Is.EqualTo (Interval.For (1 + 3, 2 + 4)));
83                 }
84
85                 [Test]
86                 public void ShouldDivIntervalsByMaxMin ()
87                 {
88                         Assert.That (this.Bottom / this._1__2, Is.EqualTo (this.Bottom), "bottom / normal = bottom");
89                         Assert.That (this.Bottom / this.Top, Is.EqualTo (this.Bottom), "bottom / top = bottom");
90                         Assert.That (this.Bottom / this.Bottom, Is.EqualTo (this.Bottom), "bottom / bottom = bottom");
91
92                         Assert.That (this.Top / this.Top, Is.EqualTo (this.Top), "top / top = top");
93                         Assert.That (this.Top / this._1__2, Is.EqualTo (this.Top), "top / normal = top");
94                         Assert.That (this.Top / this.Bottom, Is.EqualTo (this.Bottom), "top / bottom = bottom");
95
96                         Assert.That (this._1__2 / this.Bottom, Is.EqualTo (this.Bottom), "normal / bottom = bottom");
97                         Assert.That (this._1__2 / this.Top, Is.EqualTo (this.Top), "normal / top = top");
98
99                         Assert.That (this._1__2 / this.zero_to_one, Is.EqualTo (this.Top), "normal / zeroToOne = top");
100                         Assert.That (this._1__2 / this.minus_one_to_zero, Is.EqualTo (this.Top),
101                                      "normal / minusOneToZero = top");
102                         Assert.That (
103                                 this._1__2 / this._3__4,
104                                 Is.EqualTo (Interval.For (Rational.For (1, 4), Rational.For (2, 3))),
105                                 "normal / normal = normal");
106                 }
107
108                 [Test]
109                 public void ShouldJoinByInclusion ()
110                 {
111                         Assert.That (this._1__2.Join (this._3__4), Is.EqualTo (Interval.For (1, 4)));
112                         Assert.That (this._3__4.Join (this._1__2), Is.EqualTo (Interval.For (1, 4)));
113                 }
114
115                 [Test]
116                 public void ShouldLessEqualByInclusion ()
117                 {
118                         Assert.That (this._1__2.LessEqual (this._1__2), Is.True);
119
120                         Assert.That (this._1__2.LessEqual (this._1__4), Is.True);
121                         Assert.That (this._3__4.LessEqual (this._1__4), Is.True);
122
123                         Assert.That (this._1__2.LessEqual (this._3__4), Is.False);
124                         Assert.That (this._3__4.LessEqual (this._1__2), Is.False);
125
126                         Assert.That (this._1__4.LessEqual (this._1__2), Is.False);
127                         Assert.That (this._1__4.LessEqual (this._3__4), Is.False);
128                 }
129
130                 [Test]
131                 public void ShouldMultIntervalsByMaxMin ()
132                 {
133                         Assert.That (this.Bottom * this._1__2, Is.EqualTo (this.Bottom), "bottom * normal = bottom");
134                         Assert.That (this.Bottom * this.Top, Is.EqualTo (this.Bottom), "bottom * top = bottom");
135                         Assert.That (this.Bottom * this.Bottom, Is.EqualTo (this.Bottom), "bottom * bottom = bottom");
136
137                         Assert.That (this.Top * this.Top, Is.EqualTo (this.Top), "top * top = top");
138                         Assert.That (this.Top * this._1__2, Is.EqualTo (this.Top), "top * normal = top");
139                         Assert.That (this.Top * this.Bottom, Is.EqualTo (this.Bottom), "top * bottom = bottom");
140
141                         Assert.That (this._1__2 * this.Bottom, Is.EqualTo (this.Bottom), "normal * bottom = bottom");
142                         Assert.That (this._1__2 * this.Top, Is.EqualTo (this.Top), "normal * top = top");
143
144                         Assert.That (this._1__2 * this._3__4, Is.EqualTo (Interval.For (3, 8)),
145                                      "normal * normal = normal");
146                 }
147
148                 [Test]
149                 public void ShouldSubIntervalsByMaxMin ()
150                 {
151                         Assert.That (this.Bottom - this._1__2, Is.EqualTo (this.Bottom), "bottom - normal = bottom");
152                         Assert.That (this.Bottom - this.Top, Is.EqualTo (this.Bottom), "bottom - top = bottom");
153                         Assert.That (this.Bottom - this.Bottom, Is.EqualTo (this.Bottom), "bottom - bottom = bottom");
154
155                         Assert.That (this.Top - this.Top, Is.EqualTo (this.Top), "top - top = top");
156                         Assert.That (this.Top - this._1__2, Is.EqualTo (this.Top), "top - normal = top");
157                         Assert.That (this.Top - this.Bottom, Is.EqualTo (this.Bottom), "top - bottom = bottom");
158
159                         Assert.That (this._1__2 - this.Bottom, Is.EqualTo (this.Bottom), "normal - bottom = bottom");
160                         Assert.That (this._1__2 - this.Top, Is.EqualTo (this.Top), "normal - top = top");
161                         Assert.That (this._1__2 - this._3__4, Is.EqualTo (Interval.For (1 - 4, 2 - 3)),
162                                      "normal - normal = normal");
163                 }
164
165                 [Test]
166                 public void ShouldUnaryMinusIntervals ()
167                 {
168                         Assert.That (-this.Bottom, Is.EqualTo (this.Bottom), "-bottom = bottom");
169                         Assert.That (-this.Top, Is.EqualTo (this.Top), "-top = top");
170                         Assert.That (-this._1__2, Is.EqualTo (Interval.For (-2, -1)), "normal: -[l,r] = [-r,-l]");
171                 }
172         }
173 }