-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRational_should.cs
161 lines (142 loc) · 4.49 KB
/
Rational_should.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using NUnit.Framework;
using System;
namespace Incapsulation.RationalNumbers
{
[TestFixture]
public class Rational_should
{
public void AssertEqual(int expectedNumerator, int expectedDenominator, Rational actual)
{
Assert.False(actual.IsNan);
Assert.AreEqual(expectedNumerator, actual.Numerator);
Assert.AreEqual(expectedDenominator, actual.Denominator);
}
[Test]
public void InitializeSimpleRatioCorrectly()
{
AssertEqual(1, 2, new Rational(1, 2));
}
[Test]
public void InitializeWithoutDenominator()
{
AssertEqual(4, 1, new Rational(4));
}
[Test]
public void InitializeWithZeroDenominator()
{
Assert.True(new Rational(2, 0).IsNan);
}
[Test]
public void BeCorrectWithZeroNumerator()
{
AssertEqual(0, 1, new Rational(0, 5));
}
[TestCase(1, 2, 2, 4)]
[TestCase(-1, 2, -2, 4)]
[TestCase(-1, 2, 2, -4)]
[TestCase(1, 2, -2, -4)]
[TestCase(1, 2, 1, 2)]
[TestCase(1, 2, 8, 16)]
[TestCase(2, 3, 10, 15)]
[TestCase(4, 7, 16, 28)]
[TestCase(3, 256, 12, 1024)]
[TestCase(1, 1, 1, 1)]
public void InitializeAndReduce1(int expectedNum, int expectedDen, int num, int den)
{
AssertEqual(expectedNum, expectedDen, new Rational(num, den));
}
[Test]
public void Sum()
{
AssertEqual(1, 2, new Rational(1, 4) + new Rational(1, 4));
}
[Test]
public void SumWithNan()
{
Assert.True((new Rational(1, 2) + new Rational(1, 0)).IsNan);
Assert.True((new Rational(1, 0) + new Rational(1, 2)).IsNan);
}
[Test]
public void Subtract()
{
AssertEqual(1, 4, new Rational(1, 2) - new Rational(1, 4));
}
[Test]
public void SubtractWithNan()
{
Assert.True((new Rational(1, 2) - new Rational(1, 0)).IsNan);
Assert.True((new Rational(1, 0) - new Rational(1, 2)).IsNan);
}
[Test]
public void Multiply()
{
AssertEqual(-1, 4, new Rational(-1, 2) * new Rational(1, 2));
}
[Test]
public void MultiplyWithNan()
{
Assert.True((new Rational(1, 2) * new Rational(1, 0)).IsNan);
Assert.True((new Rational(1, 0) * new Rational(1, 2)).IsNan);
}
[Test]
public void Divide()
{
AssertEqual(-1, 2, new Rational(1, 4) / new Rational(-1, 2));
}
[Test]
public void DivideWithNan()
{
Assert.True((new Rational(1, 2) / new Rational(1, 0)).IsNan);
Assert.True((new Rational(1, 0) / new Rational(1, 2)).IsNan);
}
[Test]
public void DivideToZero()
{
Assert.True((new Rational(1, 2) / new Rational(0, 5)).IsNan);
}
[TestCase(1, 2, 0.5d)]
[TestCase(10, 5, 2d)]
[TestCase(-1, 5, -0.2d)]
[TestCase(10, 0, double.NaN)]
[TestCase(-10, 0, double.NaN)]
[TestCase(0, 0, double.NaN)]
public void ConvertToDouble(int numerator, int denominator, double expectedValue)
{
double v = new Rational(numerator, denominator);
Assert.AreEqual(expectedValue, v, 1e-7);
}
[Test]
public void ConvertFromInt()
{
Rational r = 5;
AssertEqual(5, 1, r);
}
[TestCase(0, 1, 0)]
[TestCase(1, 1, 1)]
[TestCase(2, 1, 2)]
[TestCase(3, 1, 3)]
[TestCase(2, 2, 1)]
[TestCase(6, 3, 2)]
[TestCase(12, 2, 6)]
[TestCase(12, 3, 4)]
[TestCase(12, 4, 3)]
[TestCase(12, 6, 2)]
[TestCase(12, 12, 1)]
[TestCase(1000, 1, 1000)]
public void ExplicitlyConvertToInt(int numerator, int denominator, int expectedValue)
{
int a = (int)new Rational(numerator, denominator);
Assert.AreEqual(expectedValue, a);
}
[TestCase(1, 2)]
[TestCase(12, 5)]
[TestCase(12, 10)]
[TestCase(25, 8)]
[TestCase(2, 3)]
[TestCase(2, 4)]
public void ExplicitlyConvertToIntAndFailsIfNonConvertible(int numerator, int denominator)
{
Assert.Catch<Exception>(() => { int a = (int)new Rational(numerator, denominator); });
}
}
}