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 {
35 public class RationalTests {
36 static readonly Rational zero = Rational.For (0L);
37 static readonly Rational zero1 = Rational.For (0L);
38 static readonly Rational one = Rational.For (1L);
39 static readonly Rational threeFourth = Rational.For (3L, 4L);
40 static readonly Rational sixEighth = Rational.For (6, 8);
42 static readonly Rational plusInf = Rational.PlusInfinity;
43 static readonly Rational minusInf = Rational.MinusInfinity;
46 public void ShouldBeEqualByModuloOfDenominator ()
48 Assert.IsTrue (threeFourth == sixEighth);
52 public void ShouldHaveAddOperation ()
54 Rational seven20 = Rational.For (7, 20);
55 Rational eleven15 = Rational.For (11, 15);
57 Assert.That (seven20 + eleven15, Is.EqualTo (Rational.For (325, 300)));
61 public void ShouldHaveDivOperation ()
63 Rational seven22 = Rational.For (7, 22);
64 Rational eleven21 = Rational.For (21, 11);
66 Assert.That (seven22 / eleven21, Is.EqualTo (Rational.For (1, 6)));
69 Assert.That (Rational.TryDivide (one, zero, out result), Is.False, "shouldn't div by zero");
70 Assert.That (Rational.TryDivide (seven22, zero, out result), Is.False, "shouldn't div by zero");
74 public void ShouldHaveGreaterThanOperatorsWithLongs ()
76 Assert.That (threeFourth < 1L);
77 Assert.That (1L > threeFourth);
79 Assert.That (0L < threeFourth);
80 Assert.That (threeFourth > 0L);
84 public void ShouldHaveInfinitiesAndNormalValuesWhichArentEqual ()
86 Assert.IsFalse (plusInf == minusInf);
87 Assert.IsFalse (plusInf == zero);
88 Assert.IsFalse (minusInf == zero);
89 Assert.IsTrue (zero == zero1);
90 Assert.IsFalse (one == zero);
94 public void ShouldHaveLessEqualOperator ()
96 minusInf.ShouldBeLessEqualThan (minusInf);
97 minusInf.ShouldBeLessEqualThan (plusInf);
98 minusInf.ShouldBeLessEqualThan (one);
100 one.ShouldNotBeLessEqualThan (minusInf);
101 one.ShouldNotBeLessEqualThan (zero);
102 one.ShouldBeLessEqualThan (one);
103 one.ShouldBeLessEqualThan (plusInf);
105 plusInf.ShouldNotBeLessEqualThan (minusInf);
106 plusInf.ShouldBeLessEqualThan (plusInf);
107 plusInf.ShouldNotBeLessEqualThan (one);
111 public void ShouldHaveLessThanOperator ()
113 minusInf.ShouldNotBeLessThan (minusInf);
114 minusInf.ShouldBeLessThan (plusInf);
115 minusInf.ShouldBeLessThan (one);
117 zero.ShouldNotBeLessThan (minusInf);
118 zero.ShouldBeLessThan (plusInf);
119 zero.ShouldBeLessThan (one);
121 plusInf.ShouldNotBeLessThan (minusInf);
122 plusInf.ShouldNotBeLessThan (plusInf);
123 plusInf.ShouldNotBeLessThan (one);
127 public void ShouldHaveMulOperation ()
129 Rational seven22 = Rational.For (7, 22);
130 Rational eleven21 = Rational.For (11, 21);
132 Assert.That (seven22 * eleven21, Is.EqualTo (Rational.For (1, 6)));
136 public void ShouldHaveNextInt32 ()
138 Assert.That (threeFourth.NextInt32, Is.EqualTo (Rational.One));
142 public void ShouldHaveSubOperation ()
144 Rational seven20 = Rational.For (7, 20);
145 Rational eleven15 = Rational.For (11, 15);
147 Assert.That (seven20 - eleven15, Is.EqualTo (Rational.For (-23, 60)));
151 public void ShouldInterpretMinusMinValueAsMaxValue ()
153 Rational maxValue = Rational.For (long.MinValue, -1L);
154 Assert.IsTrue ((long) maxValue == long.MaxValue);