Skip to content

Commit a2c5132

Browse files
committed
simplified division tests
1 parent 78afb29 commit a2c5132

File tree

2 files changed

+30
-83
lines changed

2 files changed

+30
-83
lines changed

FInt/FInt.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,10 @@ private FInt(long value, UseScale _)
256256
long rightDecimal = right._value % _SCALE;
257257

258258
/*
259-
260259
old method (so we can compare in tests)
261260
FInt m2 = new FInt((fi1.value * fi2.value)/(SCALE), false);
262261
263262
Debug.Log(m1 + " vs " + m2 + "vs double: " + ((double)fi1.value/SCALE*(double)fi2.value/SCALE));
264-
265263
*/
266264

267265
//TODO: CHECK FOR OVERFLOW

Tests/DivisionTests.cs

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ public void DivisionByZero_ShouldThrowException()
1414
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / (long)0);
1515
}
1616

17+
private void _assertDivision(FInt result, int expectedWhole, int expectedDecimal)
18+
{
19+
FInt shiftedDecimal = expectedDecimal;
20+
21+
while (shiftedDecimal.Decimal != 0)
22+
{
23+
shiftedDecimal *= 10;
24+
}
25+
26+
Assert.AreEqual(expectedWhole.FI(), result.Sign * ((result.Sign * result) - result.Decimal));
27+
Assert.AreEqual(expectedDecimal.FI(), shiftedDecimal);
28+
}
29+
30+
private void _assertDivision<T1, T2>(T1 left, T2 right, Func<T1, T2, FInt> divide, int expectedWhole, int expectedDecimal)
31+
where T1 : INumber<T1>
32+
where T2 : INumber<T2>
33+
{
34+
_assertDivision(divide(left, right), expectedWhole, expectedDecimal);
35+
_assertDivision(divide(-left, right), -expectedWhole, expectedDecimal);
36+
_assertDivision(divide(left, -right), -expectedWhole, expectedDecimal);
37+
_assertDivision(divide(-left, -right), expectedWhole, expectedDecimal);
38+
}
39+
1740
[DataTestMethod]
1841
[DataRow(0, 1, 0, 0)]
1942
[DataRow(1, 1, 1, 0)]
@@ -22,27 +45,15 @@ public void DivisionByZero_ShouldThrowException()
2245
[DataRow(1, 4, 0, 25)]
2346
[DataRow(3, 2, 1, 5)]
2447
[DataRow(7, 3, 2, 333333)]
25-
public void DivisionByFInt_ShouldWorkAsExpected(int left, int right, int expectedWhole, int expectedDecimal)
48+
public void Division_ShouldWorkAsExpected(int left, int right, int expectedWhole, int expectedDecimal)
2649
{
27-
void assertDivision(int l, int r, int ew, int ed)
28-
{
29-
FInt result = l.FI() / r.FI();
30-
31-
FInt shiftedDecimal = ed;
32-
33-
while (shiftedDecimal.Decimal != 0)
34-
{
35-
shiftedDecimal *= 10;
36-
}
50+
Func<int, int, FInt> divisionByFInt = (l, r) => l.FI() / r.FI();
51+
Func<int, int, FInt> divisionByInt = (l, r) => l.FI() / r;
52+
Func<int, long, FInt> divisionByLong = (l, r) => l.FI() / r;
3753

38-
Assert.AreEqual(ew.FI(), result.Sign * ((result.Sign * result) - result.Decimal));
39-
Assert.AreEqual(ed.FI(), shiftedDecimal);
40-
}
41-
42-
assertDivision(left, right, expectedWhole, expectedDecimal);
43-
assertDivision(-left, right, -expectedWhole, expectedDecimal);
44-
assertDivision(left, -right, -expectedWhole, expectedDecimal);
45-
assertDivision(-left, -right, expectedWhole, expectedDecimal);
54+
_assertDivision(left, right, divisionByFInt, expectedWhole, expectedDecimal);
55+
_assertDivision(left, right, divisionByInt, expectedWhole, expectedDecimal);
56+
_assertDivision(left, right, divisionByLong, expectedWhole, expectedDecimal);
4657
}
4758

4859
[DataTestMethod]
@@ -73,67 +84,5 @@ void assertDivision(FInt l, FInt r, int ew, int ed)
7384
assertDivision(numerator, -denominator, -expectedWhole, expectedDecimal);
7485
assertDivision(-numerator, -denominator, expectedWhole, expectedDecimal);
7586
}
76-
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)
86-
{
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);
137-
}
13887
}
13988
}

0 commit comments

Comments
 (0)