Skip to content

Commit 7debc56

Browse files
committed
-fixed broken test
-added more tests
1 parent f222c7c commit 7debc56

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

FInt/FInt.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,15 @@ old method (so we can compare in tests)
300300

301301
public static FInt operator / (FInt left, FInt right)
302302
{
303-
//TODO: overflow check on right being < 0?
303+
if(right._value == 0)
304+
{
305+
throw new DivideByZeroException();
306+
}
307+
308+
//TODO: overflow check +/- values that approach min/max values (like +right < 1)?
304309

305310
long whole = left._value / right._value;
306-
long remainder = _SCALE * (left._value - whole * right._value);
311+
long remainder = _SCALE * (left._value - (whole * right._value)); //TODO: if we wanted to round the last digit, we could * 10, round based on the last digit, then truncate the last digit (e.g. 2/3 -> 6666666 -> 6666670 -> 666667)
307312
remainder /= right._value;
308313

309314
return new FInt(whole * _SCALE + remainder, UseScale.None);

Tests/ConstructorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void LongConstructor_ShouldEqualLong()
3636
[TestMethod]
3737
public void LongConstructor_ThrowsOnOverflow()
3838
{
39-
Assert.ThrowsException<ArgumentException>(() => new FInt(long.MaxValue));
39+
Assert.ThrowsException<OverflowException>(() => new FInt(long.MaxValue));
4040
}
4141
}
4242
}

Tests/DivisionTests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace Tests
66
public class DivisionTests
77
{
88
[DataTestMethod]
9-
[DataRow(0, 1, 0, 0)]
109
[DataRow(1, 2, 0, 5)]
1110
[DataRow(1, 4, 0, 25)]
1211
[DataRow(3, 2, 1, 5)]
@@ -53,5 +52,40 @@ void assertDivision(int l, int r, int e)
5352
assertDivision(left, -right, -expectedResult);
5453
assertDivision(-left, -right, expectedResult);
5554
}
55+
56+
[DataTestMethod]
57+
[DataRow(1, 2, 1, 4, 2, 0)]
58+
[DataRow(2, 10, 3, 10, 0, 666666)]
59+
public void DivisionWithFractions_ShouldWorkAsExpected(int left, int right, int left2, int right2, int expectedWhole, int expectedDecimal)
60+
{
61+
void assertDivision(FInt l, FInt r, int ew, int ed)
62+
{
63+
FInt result = l / r;
64+
65+
FInt shiftedDecimal = ed;
66+
67+
while (shiftedDecimal.Decimal != 0)
68+
{
69+
shiftedDecimal *= 10;
70+
}
71+
72+
Assert.AreEqual(ew.FI(), result.Sign * ((result.Sign * result) - result.Decimal));
73+
Assert.AreEqual(ed.FI(), shiftedDecimal);
74+
}
75+
76+
FInt numerator = left.FI() / right.FI();
77+
FInt denominator = left2.FI() / right2.FI();
78+
79+
assertDivision(numerator, denominator, expectedWhole, expectedDecimal);
80+
assertDivision(-numerator, denominator, -expectedWhole, expectedDecimal);
81+
assertDivision(numerator, -denominator, -expectedWhole, expectedDecimal);
82+
assertDivision(-numerator, -denominator, expectedWhole, expectedDecimal);
83+
}
84+
85+
[TestMethod]
86+
public void DivisionByZero_ShouldThrowException()
87+
{
88+
Assert.ThrowsException<DivideByZeroException>(() => 1.FI() / 0.FI());
89+
}
5690
}
5791
}

0 commit comments

Comments
 (0)