3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
- using System . Diagnostics . CodeAnalysis ;
7
6
using Microsoft . Toolkit . Diagnostics ;
8
7
using Microsoft . VisualStudio . TestTools . UnitTesting ;
9
8
@@ -13,88 +12,76 @@ public partial class Test_Guard
13
12
{
14
13
[ TestCategory ( "Guard" ) ]
15
14
[ TestMethod ]
16
- public void Test_Guard_IsCloseToInt_Ok ( )
15
+ [ DataRow ( 0 , 20 , 10u , false ) ]
16
+ [ DataRow ( 0 , 6 , 5u , false ) ]
17
+ [ DataRow ( 0 , int . MaxValue , 500u , false ) ]
18
+ [ DataRow ( - 500 , - 530 , 10u , false ) ]
19
+ [ DataRow ( 1000 , 800 , 100u , false ) ]
20
+ [ DataRow ( int . MaxValue , int . MaxValue - 10 , 7u , false ) ]
21
+ [ DataRow ( int . MinValue , int . MaxValue , ( uint ) int . MaxValue , false ) ]
22
+ [ DataRow ( 0 , 5 , 10u , true ) ]
23
+ [ DataRow ( 0 , 5 , 5u , true ) ]
24
+ [ DataRow ( 0 , int . MaxValue , ( uint ) int . MaxValue , true ) ]
25
+ [ DataRow ( - 500 , - 530 , 50u , true ) ]
26
+ [ DataRow ( 1000 , 800 , 200u , true ) ]
27
+ [ DataRow ( int . MaxValue , int . MaxValue - 10 , 10u , true ) ]
28
+ public void Test_Guard_IsCloseToInt ( int value , int target , uint delta , bool isClose )
17
29
{
18
- Guard . IsCloseTo ( 0 , 5 , 10 , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
19
- Guard . IsCloseTo ( 0 , 5 , 5 , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
20
- Guard . IsCloseTo ( 0 , int . MaxValue , int . MaxValue , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
21
- Guard . IsCloseTo ( - 500 , - 530 , 50 , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
22
- Guard . IsCloseTo ( 1000 , 800 , 200 , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
23
- Guard . IsCloseTo ( int . MaxValue , int . MaxValue - 10 , 10 , nameof ( Test_Guard_IsCloseToInt_Ok ) ) ;
24
- }
25
-
26
- [ TestCategory ( "Guard" ) ]
27
- [ TestMethod ]
28
- [ SuppressMessage ( "StyleCop.CSharp.SpacingRules" , "SA1000" , Justification = "Value tuple" ) ]
29
- public void Test_Guard_IsCloseToInt_Fail ( )
30
- {
31
- foreach ( var item in new ( int Value , int Target , uint Delta ) [ ]
32
- {
33
- ( 0 , 20 , 10 ) ,
34
- ( 0 , 6 , 5 ) ,
35
- ( 0 , int . MaxValue , 500 ) ,
36
- ( - 500 , - 530 , 10 ) ,
37
- ( 1000 , 800 , 100 ) ,
38
- ( int . MaxValue , int . MaxValue - 10 , 7 ) ,
39
- ( int . MinValue , int . MaxValue , int . MaxValue )
40
- } )
30
+ void Test ( int value , int target )
41
31
{
42
- bool fail = false ;
32
+ bool isFailed = false ;
43
33
44
34
try
45
35
{
46
- Guard . IsCloseTo ( item . Value , item . Target , item . Delta , nameof ( Test_Guard_IsCloseToInt_Fail ) ) ;
36
+ Guard . IsCloseTo ( value , target , delta , nameof ( Test_Guard_IsCloseToInt ) ) ;
47
37
}
48
38
catch ( ArgumentException )
49
39
{
50
- fail = true ;
40
+ isFailed = true ;
51
41
}
52
42
53
- Assert . IsTrue ( fail , $ "IsCloseTo didn't fail with { item } " ) ;
43
+ Assert . AreEqual ( isClose , ! isFailed ) ;
54
44
}
55
- }
56
45
57
- [ TestCategory ( "Guard" ) ]
58
- [ TestMethod ]
59
- public void Test_Guard_IsCloseToFloat_Ok ( )
60
- {
61
- Guard . IsCloseTo ( 0f , 5 , 10 , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
62
- Guard . IsCloseTo ( 0f , 5 , 5 , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
63
- Guard . IsCloseTo ( 0f , float . MaxValue , float . MaxValue , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
64
- Guard . IsCloseTo ( - 500f , - 530 , 50 , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
65
- Guard . IsCloseTo ( 1000f , 800 , 200 , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
66
- Guard . IsCloseTo ( float . MaxValue , float . MaxValue - 10 , 10 , nameof ( Test_Guard_IsCloseToFloat_Ok ) ) ;
46
+ Test ( value , target ) ;
47
+ Test ( target , value ) ;
67
48
}
68
49
69
50
[ TestCategory ( "Guard" ) ]
70
51
[ TestMethod ]
71
- [ SuppressMessage ( "StyleCop.CSharp.SpacingRules" , "SA1000" , Justification = "Value tuple" ) ]
72
- public void Test_Guard_IsCloseToFloat_Fail ( )
52
+ [ DataRow ( 0f , 20f , 10f , false ) ]
53
+ [ DataRow ( 0f , 6f , 5f , false ) ]
54
+ [ DataRow ( 0f , float . MaxValue , 500f , false ) ]
55
+ [ DataRow ( - 500f , - 530f , 10f , false ) ]
56
+ [ DataRow ( 1000f , 800f , 100f , false ) ]
57
+ [ DataRow ( float . MaxValue , float . MaxValue / 2 , 7f , false ) ]
58
+ [ DataRow ( float . MinValue , float . MaxValue , float . MaxValue , false ) ]
59
+ [ DataRow ( 0f , 5f , 10f , true ) ]
60
+ [ DataRow ( 0f , 5f , 5f , true ) ]
61
+ [ DataRow ( 0f , float . MaxValue , float . MaxValue , true ) ]
62
+ [ DataRow ( - 500f , - 530f , 50f , true ) ]
63
+ [ DataRow ( 1000f , 800f , 200f , true ) ]
64
+ [ DataRow ( float . MaxValue , float . MaxValue - 10 , 10f , true ) ]
65
+ public void Test_Guard_IsCloseToFloat ( float value , float target , float delta , bool isClose )
73
66
{
74
- foreach ( var item in new ( float Value , float Target , float Delta ) [ ]
75
- {
76
- ( 0 , 20 , 10 ) ,
77
- ( 0 , 6 , 5 ) ,
78
- ( 0 , float . MaxValue , 500 ) ,
79
- ( - 500 , - 530 , 10 ) ,
80
- ( 1000 , 800 , 100 ) ,
81
- ( float . MaxValue , float . MaxValue / 2 , 7 ) ,
82
- ( float . MinValue , float . MaxValue , float . MaxValue )
83
- } )
67
+ void Test ( float value , float target )
84
68
{
85
- bool fail = false ;
69
+ bool isFailed = false ;
86
70
87
71
try
88
72
{
89
- Guard . IsCloseTo ( item . Value , item . Target , item . Delta , nameof ( Test_Guard_IsCloseToFloat_Fail ) ) ;
73
+ Guard . IsCloseTo ( value , target , delta , nameof ( Test_Guard_IsCloseToInt ) ) ;
90
74
}
91
75
catch ( ArgumentException )
92
76
{
93
- fail = true ;
77
+ isFailed = true ;
94
78
}
95
79
96
- Assert . IsTrue ( fail , $ "IsCloseTo didn't fail with { item } " ) ;
80
+ Assert . AreEqual ( isClose , ! isFailed ) ;
97
81
}
82
+
83
+ Test ( value , target ) ;
84
+ Test ( target , value ) ;
98
85
}
99
86
}
100
87
}
0 commit comments