5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2012 Alexander Chebaturkin
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using Mono.CodeContracts.Static.Analysis.Numerical;
31 using NUnit.Framework;
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);
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);
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; } }
49 public void ConsecutiveIntegers ()
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),
59 public void OnTheLeftOf ()
61 Assert.IsTrue (this._1__2.OnTheLeftOf (this._3__4));
62 Assert.IsTrue (this._1__2.OnTheLeftOf (this._2__4));
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));
70 public void ShouldAddIntervalsByEachBound ()
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");
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");
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)));
86 public void ShouldDivIntervalsByMaxMin ()
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");
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");
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");
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");
103 this._1__2 / this._3__4,
104 Is.EqualTo (Interval.For (Rational.For (1, 4), Rational.For (2, 3))),
105 "normal / normal = normal");
109 public void ShouldJoinByInclusion ()
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)));
116 public void ShouldLessEqualByInclusion ()
118 Assert.That (this._1__2.LessEqual (this._1__2), Is.True);
120 Assert.That (this._1__2.LessEqual (this._1__4), Is.True);
121 Assert.That (this._3__4.LessEqual (this._1__4), Is.True);
123 Assert.That (this._1__2.LessEqual (this._3__4), Is.False);
124 Assert.That (this._3__4.LessEqual (this._1__2), Is.False);
126 Assert.That (this._1__4.LessEqual (this._1__2), Is.False);
127 Assert.That (this._1__4.LessEqual (this._3__4), Is.False);
131 public void ShouldMultIntervalsByMaxMin ()
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");
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");
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");
144 Assert.That (this._1__2 * this._3__4, Is.EqualTo (Interval.For (3, 8)),
145 "normal * normal = normal");
149 public void ShouldSubIntervalsByMaxMin ()
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");
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");
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");
166 public void ShouldUnaryMinusIntervals ()
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]");