Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Test / Mono.CodeContracts.Static / RationalTests.cs
1 // 
2 // RationalTests.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]
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);
41
42                 static readonly Rational plusInf = Rational.PlusInfinity;
43                 static readonly Rational minusInf = Rational.MinusInfinity;
44
45                 [Test]
46                 public void ShouldBeEqualByModuloOfDenominator ()
47                 {
48                         Assert.IsTrue (threeFourth == sixEighth);
49                 }
50
51                 [Test]
52                 public void ShouldHaveAddOperation ()
53                 {
54                         Rational seven20 = Rational.For (7, 20);
55                         Rational eleven15 = Rational.For (11, 15);
56
57                         Assert.That (seven20 + eleven15, Is.EqualTo (Rational.For (325, 300)));
58                 }
59
60                 [Test]
61                 public void ShouldHaveDivOperation ()
62                 {
63                         Rational seven22 = Rational.For (7, 22);
64                         Rational eleven21 = Rational.For (21, 11);
65
66                         Assert.That (seven22 / eleven21, Is.EqualTo (Rational.For (1, 6)));
67
68                         Rational result;
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");
71                 }
72
73                 [Test]
74                 public void ShouldHaveGreaterThanOperatorsWithLongs ()
75                 {
76                         Assert.That (threeFourth < 1L);
77                         Assert.That (1L > threeFourth);
78
79                         Assert.That (0L < threeFourth);
80                         Assert.That (threeFourth > 0L);
81                 }
82
83                 [Test]
84                 public void ShouldHaveInfinitiesAndNormalValuesWhichArentEqual ()
85                 {
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);
91                 }
92
93                 [Test]
94                 public void ShouldHaveLessEqualOperator ()
95                 {
96                         minusInf.ShouldBeLessEqualThan (minusInf);
97                         minusInf.ShouldBeLessEqualThan (plusInf);
98                         minusInf.ShouldBeLessEqualThan (one);
99
100                         one.ShouldNotBeLessEqualThan (minusInf);
101                         one.ShouldNotBeLessEqualThan (zero);
102                         one.ShouldBeLessEqualThan (one);
103                         one.ShouldBeLessEqualThan (plusInf);
104
105                         plusInf.ShouldNotBeLessEqualThan (minusInf);
106                         plusInf.ShouldBeLessEqualThan (plusInf);
107                         plusInf.ShouldNotBeLessEqualThan (one);
108                 }
109
110                 [Test]
111                 public void ShouldHaveLessThanOperator ()
112                 {
113                         minusInf.ShouldNotBeLessThan (minusInf);
114                         minusInf.ShouldBeLessThan (plusInf);
115                         minusInf.ShouldBeLessThan (one);
116
117                         zero.ShouldNotBeLessThan (minusInf);
118                         zero.ShouldBeLessThan (plusInf);
119                         zero.ShouldBeLessThan (one);
120
121                         plusInf.ShouldNotBeLessThan (minusInf);
122                         plusInf.ShouldNotBeLessThan (plusInf);
123                         plusInf.ShouldNotBeLessThan (one);
124                 }
125
126                 [Test]
127                 public void ShouldHaveMulOperation ()
128                 {
129                         Rational seven22 = Rational.For (7, 22);
130                         Rational eleven21 = Rational.For (11, 21);
131
132                         Assert.That (seven22 * eleven21, Is.EqualTo (Rational.For (1, 6)));
133                 }
134
135                 [Test]
136                 public void ShouldHaveNextInt32 ()
137                 {
138                         Assert.That (threeFourth.NextInt32, Is.EqualTo (Rational.One));
139                 }
140
141                 [Test]
142                 public void ShouldHaveSubOperation ()
143                 {
144                         Rational seven20 = Rational.For (7, 20);
145                         Rational eleven15 = Rational.For (11, 15);
146
147                         Assert.That (seven20 - eleven15, Is.EqualTo (Rational.For (-23, 60)));
148                 }
149
150                 [Test]
151                 public void ShouldInterpretMinusMinValueAsMaxValue ()
152                 {
153                         Rational maxValue = Rational.For (long.MinValue, -1L);
154                         Assert.IsTrue ((long) maxValue == long.MaxValue);
155                 }
156         }
157 }