11using System . Numerics ;
2+ using System . Globalization ;
23
34namespace DualNumbers . Test . Interfaces ;
45
@@ -9,6 +10,20 @@ public class DualINumberTests
910
1011 private static bool LessThan < T > ( T left , T right ) where T : IComparisonOperators < T , T , bool > => left < right ;
1112
13+ private const double Tolerance = 1e-12 ;
14+
15+ private static void AssertDualMatchesComplex ( Dual actual , Complex expected )
16+ {
17+ Assert . That ( actual . real , Is . EqualTo ( expected . Real ) . Within ( Tolerance ) ) ;
18+ Assert . That ( actual . dual , Is . EqualTo ( expected . Imaginary ) . Within ( Tolerance ) ) ;
19+ }
20+
21+ private static bool GreaterThan ( Dual left , Dual right ) => left > right ;
22+
23+ private static bool LessThanOrEqual ( Dual left , Dual right ) => left <= right ;
24+
25+ private static bool GreaterThanOrEqual ( Dual left , Dual right ) => left >= right ;
26+
1227 [ Test ]
1328 public void INumber_UnsupportedOperators_ThrowNotSupportedException ( )
1429 {
@@ -17,5 +32,78 @@ public void INumber_UnsupportedOperators_ThrowNotSupportedException()
1732
1833 Assert . Throws < NotSupportedException > ( ( ) => _ = Modulus ( left , right ) ) ;
1934 Assert . Throws < NotSupportedException > ( ( ) => _ = LessThan ( left , right ) ) ;
35+ Assert . Throws < NotSupportedException > ( ( ) => _ = GreaterThan ( left , right ) ) ;
36+ Assert . Throws < NotSupportedException > ( ( ) => _ = LessThanOrEqual ( left , right ) ) ;
37+ Assert . Throws < NotSupportedException > ( ( ) => _ = GreaterThanOrEqual ( left , right ) ) ;
38+ }
39+
40+ [ Test ]
41+ public void INumber_PredicateMembers_MatchComplexImplementations ( )
42+ {
43+ Dual value = new ( 2.0 , 3.0 ) ;
44+ Complex expected = new ( 2.0 , 3.0 ) ;
45+
46+ Assert . That ( Dual . IsComplexNumber ( value ) , Is . EqualTo ( Complex . IsComplexNumber ( expected ) ) ) ;
47+ Assert . That ( Dual . IsEvenInteger ( value ) , Is . EqualTo ( Complex . IsEvenInteger ( expected ) ) ) ;
48+ Assert . That ( Dual . IsFinite ( value ) , Is . EqualTo ( Complex . IsFinite ( expected ) ) ) ;
49+ Assert . That ( Dual . IsImaginaryNumber ( value ) , Is . EqualTo ( Complex . IsImaginaryNumber ( expected ) ) ) ;
50+ Assert . That ( Dual . IsInfinity ( value ) , Is . EqualTo ( Complex . IsInfinity ( expected ) ) ) ;
51+ Assert . That ( Dual . IsInteger ( value ) , Is . EqualTo ( Complex . IsInteger ( expected ) ) ) ;
52+ Assert . That ( Dual . IsNaN ( value ) , Is . EqualTo ( Complex . IsNaN ( expected ) ) ) ;
53+ Assert . That ( Dual . IsNegative ( value ) , Is . EqualTo ( Complex . IsNegative ( expected ) ) ) ;
54+ Assert . That ( Dual . IsNegativeInfinity ( value ) , Is . EqualTo ( Complex . IsNegativeInfinity ( expected ) ) ) ;
55+ Assert . That ( Dual . IsNormal ( value ) , Is . EqualTo ( Complex . IsNormal ( expected ) ) ) ;
56+ Assert . That ( Dual . IsOddInteger ( value ) , Is . EqualTo ( Complex . IsOddInteger ( expected ) ) ) ;
57+ Assert . That ( Dual . IsPositive ( value ) , Is . EqualTo ( Complex . IsPositive ( expected ) ) ) ;
58+ Assert . That ( Dual . IsPositiveInfinity ( value ) , Is . EqualTo ( Complex . IsPositiveInfinity ( expected ) ) ) ;
59+ Assert . That ( Dual . IsRealNumber ( value ) , Is . EqualTo ( Complex . IsRealNumber ( expected ) ) ) ;
60+ Assert . That ( Dual . IsSubnormal ( value ) , Is . EqualTo ( Complex . IsSubnormal ( expected ) ) ) ;
61+ Assert . That ( Dual . IsZero ( Dual . Zero ) , Is . True ) ;
62+ }
63+
64+ [ Test ]
65+ public void INumber_AbsAndMagnitudeMethods_MatchComplex ( )
66+ {
67+ Dual x = new ( 3.0 , 4.0 ) ;
68+ Dual y = new ( 2.0 , - 10.0 ) ;
69+ Complex cx = new ( 3.0 , 4.0 ) ;
70+ Complex cy = new ( 2.0 , - 10.0 ) ;
71+
72+ Assert . That ( Dual . Abs ( x ) , Is . EqualTo ( Complex . Abs ( cx ) ) . Within ( Tolerance ) ) ;
73+ AssertDualMatchesComplex ( Dual . MaxMagnitude ( x , y ) , Complex . MaxMagnitude ( cx , cy ) ) ;
74+ AssertDualMatchesComplex ( Dual . MinMagnitude ( x , y ) , Complex . MinMagnitude ( cx , cy ) ) ;
75+ }
76+
77+ [ Test ]
78+ public void INumber_ParseAndTryParse_WithNumberStyles_MatchComplex ( )
79+ {
80+ Complex expected = new ( 3.5 , - 2.25 ) ;
81+ string text = expected . ToString ( CultureInfo . InvariantCulture ) ;
82+ NumberStyles style = NumberStyles . Float | NumberStyles . AllowParentheses ;
83+
84+ Dual parsedFromString = Dual . Parse ( text , style , CultureInfo . InvariantCulture ) ;
85+ Dual parsedFromSpan = Dual . Parse ( text . AsSpan ( ) , style , CultureInfo . InvariantCulture ) ;
86+ bool tryStringOk = Dual . TryParse ( text , style , CultureInfo . InvariantCulture , out Dual tryParsedString ) ;
87+ bool trySpanOk = Dual . TryParse ( text . AsSpan ( ) , style , CultureInfo . InvariantCulture , out Dual tryParsedSpan ) ;
88+
89+ Assert . That ( tryStringOk , Is . True ) ;
90+ Assert . That ( trySpanOk , Is . True ) ;
91+ AssertDualMatchesComplex ( parsedFromString , expected ) ;
92+ AssertDualMatchesComplex ( parsedFromSpan , expected ) ;
93+ AssertDualMatchesComplex ( tryParsedString , expected ) ;
94+ AssertDualMatchesComplex ( tryParsedSpan , expected ) ;
95+ }
96+
97+ [ Test ]
98+ public void INumber_UnsupportedMembers_ThrowNotSupportedException ( )
99+ {
100+ Dual left = new ( 2.0 , 1.0 ) ;
101+ Dual right = new ( 1.0 , 0.0 ) ;
102+
103+ Assert . Throws < NotSupportedException > ( ( ) => _ = Dual . IsCanonical ( left ) ) ;
104+ Assert . Throws < NotSupportedException > ( ( ) => _ = Dual . MaxMagnitudeNumber ( left , right ) ) ;
105+ Assert . Throws < NotSupportedException > ( ( ) => _ = Dual . MinMagnitudeNumber ( left , right ) ) ;
106+ Assert . Throws < NotSupportedException > ( ( ) => _ = left . CompareTo ( ( object ? ) right ) ) ;
107+ Assert . Throws < NotSupportedException > ( ( ) => _ = left . CompareTo ( right ) ) ;
20108 }
21109}
0 commit comments