5
5
using System . Diagnostics ;
6
6
using System . Globalization ;
7
7
using System . Tests ;
8
+ using System . Text . RegularExpressions ;
8
9
using System . Threading ;
9
10
using Microsoft . DotNet . RemoteExecutor ;
10
11
using Xunit ;
12
+ using static System . Net . Mime . MediaTypeNames ;
11
13
12
14
namespace System . Numerics . Tests
13
15
{
@@ -36,57 +38,51 @@ public static IEnumerable<object[]> Cultures
36
38
[ OuterLoop ]
37
39
public static void RunParseToStringTests ( CultureInfo culture )
38
40
{
39
- Test ( ) ;
40
- BigIntTools . Utils . RunWithFakeThreshold ( Number . s_naiveThreshold , 0 , Test ) ;
41
-
42
- void Test ( )
43
- {
44
- byte [ ] tempByteArray1 = new byte [ 0 ] ;
45
- using ( new ThreadCultureChange ( culture ) )
41
+ byte [ ] tempByteArray1 = new byte [ 0 ] ;
42
+ using ( new ThreadCultureChange ( culture ) )
43
+ {
44
+ //default style
45
+ VerifyDefaultParse ( s_random ) ;
46
+
47
+ //single NumberStyles
48
+ VerifyNumberStyles ( NumberStyles . None , s_random ) ;
49
+ VerifyNumberStyles ( NumberStyles . AllowLeadingWhite , s_random ) ;
50
+ VerifyNumberStyles ( NumberStyles . AllowTrailingWhite , s_random ) ;
51
+ VerifyNumberStyles ( NumberStyles . AllowLeadingSign , s_random ) ;
52
+ VerifyNumberStyles ( NumberStyles . AllowTrailingSign , s_random ) ;
53
+ VerifyNumberStyles ( NumberStyles . AllowParentheses , s_random ) ;
54
+ VerifyNumberStyles ( NumberStyles . AllowDecimalPoint , s_random ) ;
55
+ VerifyNumberStyles ( NumberStyles . AllowThousands , s_random ) ;
56
+ VerifyNumberStyles ( NumberStyles . AllowExponent , s_random ) ;
57
+ VerifyNumberStyles ( NumberStyles . AllowCurrencySymbol , s_random ) ;
58
+ VerifyNumberStyles ( NumberStyles . AllowHexSpecifier , s_random ) ;
59
+ VerifyBinaryNumberStyles ( NumberStyles . AllowBinarySpecifier , s_random ) ;
60
+
61
+ //composite NumberStyles
62
+ VerifyNumberStyles ( NumberStyles . Integer , s_random ) ;
63
+ VerifyNumberStyles ( NumberStyles . HexNumber , s_random ) ;
64
+ VerifyBinaryNumberStyles ( NumberStyles . BinaryNumber , s_random ) ;
65
+ VerifyNumberStyles ( NumberStyles . Number , s_random ) ;
66
+ VerifyNumberStyles ( NumberStyles . Float , s_random ) ;
67
+ VerifyNumberStyles ( NumberStyles . Currency , s_random ) ;
68
+ VerifyNumberStyles ( NumberStyles . Any , s_random ) ;
69
+
70
+ //invalid number style
71
+ // ******InvalidNumberStyles
72
+ NumberStyles invalid = ( NumberStyles ) 0x7c00 ;
73
+ AssertExtensions . Throws < ArgumentException > ( "style" , ( ) =>
46
74
{
47
- //default style
48
- VerifyDefaultParse ( s_random ) ;
49
-
50
- //single NumberStyles
51
- VerifyNumberStyles ( NumberStyles . None , s_random ) ;
52
- VerifyNumberStyles ( NumberStyles . AllowLeadingWhite , s_random ) ;
53
- VerifyNumberStyles ( NumberStyles . AllowTrailingWhite , s_random ) ;
54
- VerifyNumberStyles ( NumberStyles . AllowLeadingSign , s_random ) ;
55
- VerifyNumberStyles ( NumberStyles . AllowTrailingSign , s_random ) ;
56
- VerifyNumberStyles ( NumberStyles . AllowParentheses , s_random ) ;
57
- VerifyNumberStyles ( NumberStyles . AllowDecimalPoint , s_random ) ;
58
- VerifyNumberStyles ( NumberStyles . AllowThousands , s_random ) ;
59
- VerifyNumberStyles ( NumberStyles . AllowExponent , s_random ) ;
60
- VerifyNumberStyles ( NumberStyles . AllowCurrencySymbol , s_random ) ;
61
- VerifyNumberStyles ( NumberStyles . AllowHexSpecifier , s_random ) ;
62
- VerifyBinaryNumberStyles ( NumberStyles . AllowBinarySpecifier , s_random ) ;
63
-
64
- //composite NumberStyles
65
- VerifyNumberStyles ( NumberStyles . Integer , s_random ) ;
66
- VerifyNumberStyles ( NumberStyles . HexNumber , s_random ) ;
67
- VerifyBinaryNumberStyles ( NumberStyles . BinaryNumber , s_random ) ;
68
- VerifyNumberStyles ( NumberStyles . Number , s_random ) ;
69
- VerifyNumberStyles ( NumberStyles . Float , s_random ) ;
70
- VerifyNumberStyles ( NumberStyles . Currency , s_random ) ;
71
- VerifyNumberStyles ( NumberStyles . Any , s_random ) ;
72
-
73
- //invalid number style
74
- // ******InvalidNumberStyles
75
- NumberStyles invalid = ( NumberStyles ) 0x7c00 ;
76
- AssertExtensions . Throws < ArgumentException > ( "style" , ( ) =>
77
- {
78
- BigInteger . Parse ( "1" , invalid ) . ToString ( "d" ) ;
79
- } ) ;
80
- AssertExtensions . Throws < ArgumentException > ( "style" , ( ) =>
81
- {
82
- BigInteger junk ;
83
- BigInteger . TryParse ( "1" , invalid , null , out junk ) ;
84
- Assert . Equal ( "1" , junk . ToString ( "d" ) ) ;
85
- } ) ;
75
+ BigInteger . Parse ( "1" , invalid ) . ToString ( "d" ) ;
76
+ } ) ;
77
+ AssertExtensions . Throws < ArgumentException > ( "style" , ( ) =>
78
+ {
79
+ BigInteger junk ;
80
+ BigInteger . TryParse ( "1" , invalid , null , out junk ) ;
81
+ Assert . Equal ( "1" , junk . ToString ( "d" ) ) ;
82
+ } ) ;
86
83
87
- //FormatProvider tests
88
- RunFormatProviderParseStrings ( ) ;
89
- }
84
+ //FormatProvider tests
85
+ RunFormatProviderParseStrings ( ) ;
90
86
}
91
87
}
92
88
@@ -99,36 +95,23 @@ void Test()
99
95
[ InlineData ( "1\0 3456789" , 0 , 1 , "1" ) ]
100
96
[ InlineData ( "1\0 3456789" , 0 , 2 , "1" ) ]
101
97
[ InlineData ( "123456789\0 " , 0 , 10 , "123456789" ) ]
102
- public void Parse_Subspan_Success ( string input , int offset , int length , string expected )
98
+ public static void Parse_Subspan_Success ( string input , int offset , int length , string expected )
103
99
{
104
- Test ( ) ;
105
-
106
- BigIntTools . Utils . RunWithFakeThreshold ( Number . s_naiveThreshold , 0 , Test ) ;
107
-
108
- void Test ( )
109
- {
110
- Eval ( BigInteger . Parse ( input . AsSpan ( offset , length ) ) , expected ) ;
111
- Assert . True ( BigInteger . TryParse ( input . AsSpan ( offset , length ) , out BigInteger test ) ) ;
112
- Eval ( test , expected ) ;
113
- }
100
+ Eval ( BigInteger . Parse ( input . AsSpan ( offset , length ) ) , expected ) ;
101
+ Assert . True ( BigInteger . TryParse ( input . AsSpan ( offset , length ) , out BigInteger test ) ) ;
102
+ Eval ( test , expected ) ;
114
103
}
115
104
116
105
[ Fact ]
117
- public void Parse_EmptySubspan_Fails ( )
106
+ public static void Parse_EmptySubspan_Fails ( )
118
107
{
119
- Test ( ) ;
120
- BigIntTools . Utils . RunWithFakeThreshold ( Number . s_naiveThreshold , 0 , Test ) ;
121
-
122
- void Test ( )
123
- {
124
- BigInteger result ;
108
+ BigInteger result ;
125
109
126
- Assert . False ( BigInteger . TryParse ( "12345" . AsSpan ( 0 , 0 ) , out result ) ) ;
127
- Assert . Equal ( 0 , result ) ;
110
+ Assert . False ( BigInteger . TryParse ( "12345" . AsSpan ( 0 , 0 ) , out result ) ) ;
111
+ Assert . Equal ( 0 , result ) ;
128
112
129
- Assert . False ( BigInteger . TryParse ( [ ] , out result ) ) ;
130
- Assert . Equal ( 0 , result ) ;
131
- }
113
+ Assert . False ( BigInteger . TryParse ( [ ] , out result ) ) ;
114
+ Assert . Equal ( 0 , result ) ;
132
115
}
133
116
134
117
[ Fact ]
@@ -148,7 +131,7 @@ public void Parse_Hex32Bits()
148
131
149
132
Assert . True ( BigInteger . TryParse ( "0F0000001" , NumberStyles . HexNumber , null , out result ) ) ;
150
133
Assert . Equal ( 0xF0000001u , result ) ;
151
-
134
+
152
135
Assert . True ( BigInteger . TryParse ( "F00000001" , NumberStyles . HexNumber , null , out result ) ) ;
153
136
Assert . Equal ( - 0xFFFFFFFFL , result ) ;
154
137
@@ -210,16 +193,10 @@ public static IEnumerable<object[]> RegressionIssueRuntime94610_TestData()
210
193
211
194
[ Theory ]
212
195
[ MemberData ( nameof ( RegressionIssueRuntime94610_TestData ) ) ]
213
- public void RegressionIssueRuntime94610 ( string text )
196
+ public static void RegressionIssueRuntime94610 ( string text )
214
197
{
215
198
// Regression test for: https://github.com/dotnet/runtime/issues/94610
216
- Test ( ) ;
217
- BigIntTools . Utils . RunWithFakeThreshold ( Number . s_naiveThreshold , 0 , Test ) ;
218
-
219
- void Test ( )
220
- {
221
- VerifyParseToString ( text , NumberStyles . Integer , true ) ;
222
- }
199
+ VerifyParseToString ( text , NumberStyles . Integer , true ) ;
223
200
}
224
201
225
202
private static void RunFormatProviderParseStrings ( )
@@ -268,6 +245,19 @@ private static void VerifyDefaultParse(Random random)
268
245
VerifyParseToString ( GetDigitSequence ( 100 , 1000 , random ) ) ;
269
246
}
270
247
248
+ // Trailing Zero - Small
249
+ VerifyParseToString ( "99000000000" ) ;
250
+ for ( int i = 0 ; i < s_samples ; i ++ )
251
+ {
252
+ VerifyParseToString ( GetDigitSequence ( 1 , 10 , random ) + new string ( '0' , random . Next ( 10 , 50 ) ) ) ;
253
+ }
254
+
255
+ // Trailing Zero - Large
256
+ for ( int i = 0 ; i < s_samples ; i ++ )
257
+ {
258
+ VerifyParseToString ( GetDigitSequence ( 10 , 100 , random ) + new string ( '0' , random . Next ( 100 , 1000 ) ) ) ;
259
+ }
260
+
271
261
// Leading White
272
262
for ( int i = 0 ; i < s_samples ; i ++ )
273
263
{
@@ -531,6 +521,13 @@ private static void VerifyNumberStyles(NumberStyles ns, Random random)
531
521
VerifyParseToString ( GetDigitSequence ( 100 , 1000 , random ) , ns , true ) ;
532
522
}
533
523
524
+ // Trailing Zero
525
+ VerifyParseToString ( "99000000000" , ns , true ) ;
526
+ for ( int i = 0 ; i < s_samples ; i ++ )
527
+ {
528
+ VerifyParseToString ( GetDigitSequence ( 1 , 10 , random ) + "1000000000" , ns , true ) ;
529
+ }
530
+
534
531
// Leading White
535
532
for ( int i = 0 ; i < s_samples ; i ++ )
536
533
{
@@ -1169,4 +1166,59 @@ private static void Eval(BigInteger x, string expected)
1169
1166
Assert . Equal ( expected , actual ) ;
1170
1167
}
1171
1168
}
1169
+
1170
+ [ Collection ( nameof ( DisableParallelization ) ) ]
1171
+ public class parseTestThreshold
1172
+ {
1173
+ public static IEnumerable < object [ ] > Cultures => parseTest . Cultures ;
1174
+ [ Theory ]
1175
+ [ MemberData ( nameof ( Cultures ) ) ]
1176
+ [ OuterLoop ]
1177
+ public static void RunParseToStringTests ( CultureInfo culture )
1178
+ {
1179
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThreshold , 0 , ( ) =>
1180
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThresholdInRecursive , 10 , ( ) =>
1181
+ {
1182
+ parseTest . RunParseToStringTests ( culture ) ;
1183
+ } ) ) ;
1184
+ }
1185
+
1186
+ [ Theory ]
1187
+ [ InlineData ( "123456789" , 0 , 9 , "123456789" ) ]
1188
+ [ InlineData ( "123456789" , 0 , 1 , "1" ) ]
1189
+ [ InlineData ( "123456789" , 1 , 3 , "234" ) ]
1190
+ [ InlineData ( "123456789" , 8 , 1 , "9" ) ]
1191
+ [ InlineData ( "123456789abc" , 8 , 1 , "9" ) ]
1192
+ [ InlineData ( "1\0 3456789" , 0 , 1 , "1" ) ]
1193
+ [ InlineData ( "1\0 3456789" , 0 , 2 , "1" ) ]
1194
+ [ InlineData ( "123456789\0 " , 0 , 10 , "123456789" ) ]
1195
+ public static void Parse_Subspan_Success ( string input , int offset , int length , string expected )
1196
+ {
1197
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThreshold , 0 , ( ) =>
1198
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThresholdInRecursive , 10 , ( ) =>
1199
+ {
1200
+ parseTest . Parse_Subspan_Success ( input , offset , length , expected ) ;
1201
+ } ) ) ;
1202
+ }
1203
+
1204
+ [ Fact ]
1205
+ public static void Parse_EmptySubspan_Fails ( )
1206
+ {
1207
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThreshold , 0 , ( ) =>
1208
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThresholdInRecursive , 10 , parseTest . Parse_EmptySubspan_Fails ) ) ;
1209
+ }
1210
+
1211
+ public static IEnumerable < object [ ] > RegressionIssueRuntime94610_TestData ( ) => parseTest . RegressionIssueRuntime94610_TestData ( ) ;
1212
+
1213
+ [ Theory ]
1214
+ [ MemberData ( nameof ( RegressionIssueRuntime94610_TestData ) ) ]
1215
+ public static void RegressionIssueRuntime94610 ( string text )
1216
+ {
1217
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThreshold , 0 , ( ) =>
1218
+ BigIntTools . Utils . RunWithFakeThreshold ( Number . BigIntegerParseNaiveThresholdInRecursive , 10 , ( ) =>
1219
+ {
1220
+ parseTest . RegressionIssueRuntime94610 ( text ) ;
1221
+ } ) ) ;
1222
+ }
1223
+ }
1172
1224
}
0 commit comments