Skip to content

Commit 78afb29

Browse files
committed
updated division tests
1 parent 7debc56 commit 78afb29

File tree

2 files changed

+77
-29
lines changed

2 files changed

+77
-29
lines changed

FInt/FInt.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public struct FInt
33
#region Constants
44

55
public const int PRICISION = 6;
6-
private const long _SCALE = 1000000;
6+
private const long _SCALE = 1_000_000;
77

88
#endregion
99

@@ -314,14 +314,14 @@ old method (so we can compare in tests)
314314
return new FInt(whole * _SCALE + remainder, UseScale.None);
315315
}
316316

317-
public static FInt operator / (FInt fi1, int i)
317+
public static FInt operator / (FInt left, int right)
318318
{
319-
return new FInt(fi1._value / i, UseScale.None);
319+
return new FInt(left._value / right, UseScale.None);
320320
}
321321

322-
public static FInt operator / (FInt fi1, long lng)
322+
public static FInt operator / (FInt left, long right)
323323
{
324-
return new FInt(fi1._value / lng, UseScale.None);
324+
return new FInt(left._value / right, UseScale.None);
325325
}
326326

327327
#endregion

Tests/DivisionTests.cs

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System.Numerics;
23

34
namespace Tests
45
{
56
[TestClass]
67
public class DivisionTests
78
{
9+
[TestMethod]
10+
public void DivisionByZero_ShouldThrowException()
11+
{
12+
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / 0.FI());
13+
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / 0);
14+
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / (long)0);
15+
}
16+
817
[DataTestMethod]
18+
[DataRow(0, 1, 0, 0)]
19+
[DataRow(1, 1, 1, 0)]
20+
[DataRow(4, 2, 2, 0)]
921
[DataRow(1, 2, 0, 5)]
1022
[DataRow(1, 4, 0, 25)]
1123
[DataRow(3, 2, 1, 5)]
1224
[DataRow(7, 3, 2, 333333)]
13-
public void DivisionWithNonIntegerQuotient_ShouldWorkAsExpected(int left, int right, int expectedWhole, int expectedDecimal)
25+
public void DivisionByFInt_ShouldWorkAsExpected(int left, int right, int expectedWhole, int expectedDecimal)
1426
{
1527
void assertDivision(int l, int r, int ew, int ed)
1628
{
@@ -33,26 +45,6 @@ void assertDivision(int l, int r, int ew, int ed)
3345
assertDivision(-left, -right, expectedWhole, expectedDecimal);
3446
}
3547

36-
[DataTestMethod]
37-
[DataRow(0, 1, 0)]
38-
[DataRow(1, 1, 1)]
39-
[DataRow(4, 2, 2)]
40-
public void DivisionWithIntegerQuotient_ShouldWorkAsExpected(int left, int right, int expectedResult)
41-
{
42-
void assertDivision(int l, int r, int e)
43-
{
44-
FInt result = l.FI() / r.FI();
45-
FInt expected = e;
46-
47-
Assert.AreEqual(expected, result);
48-
}
49-
50-
assertDivision(left, right, expectedResult);
51-
assertDivision(-left, right, -expectedResult);
52-
assertDivision(left, -right, -expectedResult);
53-
assertDivision(-left, -right, expectedResult);
54-
}
55-
5648
[DataTestMethod]
5749
[DataRow(1, 2, 1, 4, 2, 0)]
5850
[DataRow(2, 10, 3, 10, 0, 666666)]
@@ -82,10 +74,66 @@ void assertDivision(FInt l, FInt r, int ew, int ed)
8274
assertDivision(-numerator, -denominator, expectedWhole, expectedDecimal);
8375
}
8476

85-
[TestMethod]
86-
public void DivisionByZero_ShouldThrowException()
77+
[DataTestMethod]
78+
[DataRow(0, 1, 0, 0)]
79+
[DataRow(1, 1, 1, 0)]
80+
[DataRow(4, 2, 2, 0)]
81+
[DataRow(1, 2, 0, 5)]
82+
[DataRow(1, 4, 0, 25)]
83+
[DataRow(3, 2, 1, 5)]
84+
[DataRow(7, 3, 2, 333333)]
85+
public void DivisionByInt_ShouldWorkAsExpected(int left, int right, int expectedWhole, int expectedDecimal)
8786
{
88-
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / 0.FI());
87+
void assertDivision(int l, int r, int ew, int ed)
88+
{
89+
FInt result = l.FI() / r;
90+
91+
FInt shiftedDecimal = ed;
92+
93+
while (shiftedDecimal.Decimal != 0)
94+
{
95+
shiftedDecimal *= 10;
96+
}
97+
98+
Assert.AreEqual(ew.FI(), result.Sign * ((result.Sign * result) - result.Decimal));
99+
Assert.AreEqual(ed.FI(), shiftedDecimal);
100+
}
101+
102+
assertDivision(left, right, expectedWhole, expectedDecimal);
103+
assertDivision(-left, right, -expectedWhole, expectedDecimal);
104+
assertDivision(left, -right, -expectedWhole, expectedDecimal);
105+
assertDivision(-left, -right, expectedWhole, expectedDecimal);
106+
}
107+
108+
[DataTestMethod]
109+
[DataRow(0, 1, 0, 0)]
110+
[DataRow(1, 1, 1, 0)]
111+
[DataRow(4, 2, 2, 0)]
112+
[DataRow(1, 2, 0, 5)]
113+
[DataRow(1, 4, 0, 25)]
114+
[DataRow(3, 2, 1, 5)]
115+
[DataRow(7, 3, 2, 333333)]
116+
public void DivisionByLong_ShouldWorkAsExpected(int left, long right, int expectedWhole, int expectedDecimal)
117+
{
118+
void assertDivision(int l, long r, int ew, int ed)
119+
{
120+
FInt result = l.FI() / r;
121+
122+
FInt shiftedDecimal = ed;
123+
124+
while (shiftedDecimal.Decimal != 0)
125+
{
126+
shiftedDecimal *= 10;
127+
}
128+
129+
Assert.AreEqual(ew.FI(), result.Sign * ((result.Sign * result) - result.Decimal));
130+
Assert.AreEqual(ed.FI(), shiftedDecimal);
131+
}
132+
133+
assertDivision(left, right, expectedWhole, expectedDecimal);
134+
assertDivision(-left, right, -expectedWhole, expectedDecimal);
135+
assertDivision(left, -right, -expectedWhole, expectedDecimal);
136+
assertDivision(-left, -right, expectedWhole, expectedDecimal);
89137
}
90138
}
91139
}

0 commit comments

Comments
 (0)