11using Microsoft . VisualStudio . TestTools . UnitTesting ;
2+ using System . Numerics ;
23
34namespace 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