@@ -2,10 +2,11 @@ use core::cmp::PartialEq;
2
2
use core:: convert:: { TryFrom , TryInto } ;
3
3
use core:: fmt:: Debug ;
4
4
use core:: marker:: Copy ;
5
- use core:: num:: TryFromIntError ;
5
+ use core:: num:: { IntErrorKind , ParseIntError , TryFromIntError } ;
6
6
use core:: ops:: { Add , Div , Mul , Rem , Sub } ;
7
7
use core:: option:: Option ;
8
- use core:: option:: Option :: { None , Some } ;
8
+ use core:: option:: Option :: None ;
9
+ use core:: str:: FromStr ;
9
10
10
11
#[ macro_use]
11
12
mod int_macros;
67
68
assert_eq ! ( ten. rem( two) , ten % two) ;
68
69
}
69
70
71
+ /// Helper function for asserting number parsing returns a specific error
72
+ fn test_parse < T > ( num_str : & str , expected : Result < T , IntErrorKind > )
73
+ where
74
+ T : FromStr < Err = ParseIntError > ,
75
+ Result < T , IntErrorKind > : PartialEq + Debug ,
76
+ {
77
+ assert_eq ! ( num_str. parse:: <T >( ) . map_err( |e| e. kind( ) . clone( ) ) , expected)
78
+ }
79
+
70
80
#[ test]
71
81
fn from_str_issue7588 ( ) {
72
82
let u: Option < u8 > = u8:: from_str_radix ( "1000" , 10 ) . ok ( ) ;
@@ -77,49 +87,52 @@ fn from_str_issue7588() {
77
87
78
88
#[ test]
79
89
fn test_int_from_str_overflow ( ) {
80
- assert_eq ! ( "127" . parse :: <i8 >( ) . ok ( ) , Some ( 127i8 ) ) ;
81
- assert_eq ! ( "128" . parse :: <i8 >( ) . ok ( ) , None ) ;
90
+ test_parse :: < i8 > ( "127" , Ok ( 127 ) ) ;
91
+ test_parse :: < i8 > ( "128" , Err ( IntErrorKind :: PosOverflow ) ) ;
82
92
83
- assert_eq ! ( "-128" . parse :: <i8 >( ) . ok ( ) , Some ( - 128i8 ) ) ;
84
- assert_eq ! ( "-129" . parse :: <i8 >( ) . ok ( ) , None ) ;
93
+ test_parse :: < i8 > ( "-128" , Ok ( - 128 ) ) ;
94
+ test_parse :: < i8 > ( "-129" , Err ( IntErrorKind :: NegOverflow ) ) ;
85
95
86
- assert_eq ! ( "32767" . parse :: <i16 >( ) . ok ( ) , Some ( 32_767i16 ) ) ;
87
- assert_eq ! ( "32768" . parse :: <i16 >( ) . ok ( ) , None ) ;
96
+ test_parse :: < i16 > ( "32767" , Ok ( 32_767 ) ) ;
97
+ test_parse :: < i16 > ( "32768" , Err ( IntErrorKind :: PosOverflow ) ) ;
88
98
89
- assert_eq ! ( "-32768" . parse :: <i16 >( ) . ok ( ) , Some ( - 32_768i16 ) ) ;
90
- assert_eq ! ( "-32769" . parse :: <i16 >( ) . ok ( ) , None ) ;
99
+ test_parse :: < i16 > ( "-32768" , Ok ( - 32_768 ) ) ;
100
+ test_parse :: < i16 > ( "-32769" , Err ( IntErrorKind :: NegOverflow ) ) ;
91
101
92
- assert_eq ! ( "2147483647" . parse :: <i32 >( ) . ok ( ) , Some ( 2_147_483_647i32 ) ) ;
93
- assert_eq ! ( "2147483648" . parse :: <i32 >( ) . ok ( ) , None ) ;
102
+ test_parse :: < i32 > ( "2147483647" , Ok ( 2_147_483_647 ) ) ;
103
+ test_parse :: < i32 > ( "2147483648" , Err ( IntErrorKind :: PosOverflow ) ) ;
94
104
95
- assert_eq ! ( "-2147483648" . parse :: <i32 >( ) . ok ( ) , Some ( - 2_147_483_648i32 ) ) ;
96
- assert_eq ! ( "-2147483649" . parse :: <i32 >( ) . ok ( ) , None ) ;
105
+ test_parse :: < i32 > ( "-2147483648" , Ok ( - 2_147_483_648 ) ) ;
106
+ test_parse :: < i32 > ( "-2147483649" , Err ( IntErrorKind :: NegOverflow ) ) ;
97
107
98
- assert_eq ! ( "9223372036854775807" . parse :: <i64 >( ) . ok ( ) , Some ( 9_223_372_036_854_775_807i64 ) ) ;
99
- assert_eq ! ( "9223372036854775808" . parse :: <i64 >( ) . ok ( ) , None ) ;
108
+ test_parse :: < i64 > ( "9223372036854775807" , Ok ( 9_223_372_036_854_775_807 ) ) ;
109
+ test_parse :: < i64 > ( "9223372036854775808" , Err ( IntErrorKind :: PosOverflow ) ) ;
100
110
101
- assert_eq ! ( "-9223372036854775808" . parse :: <i64 >( ) . ok ( ) , Some ( - 9_223_372_036_854_775_808i64 ) ) ;
102
- assert_eq ! ( "-9223372036854775809" . parse :: <i64 >( ) . ok ( ) , None ) ;
111
+ test_parse :: < i64 > ( "-9223372036854775808" , Ok ( - 9_223_372_036_854_775_808 ) ) ;
112
+ test_parse :: < i64 > ( "-9223372036854775809" , Err ( IntErrorKind :: NegOverflow ) ) ;
103
113
}
104
114
105
115
#[ test]
106
116
fn test_leading_plus ( ) {
107
- assert_eq ! ( "+127" . parse :: <u8 >( ) . ok ( ) , Some ( 127 ) ) ;
108
- assert_eq ! ( "+9223372036854775807" . parse :: <i64 >( ) . ok ( ) , Some ( 9223372036854775807 ) ) ;
117
+ test_parse :: < u8 > ( "+127" , Ok ( 127 ) ) ;
118
+ test_parse :: < i64 > ( "+9223372036854775807" , Ok ( 9223372036854775807 ) ) ;
109
119
}
110
120
111
121
#[ test]
112
122
fn test_invalid ( ) {
113
- assert_eq ! ( "--129" . parse:: <i8 >( ) . ok( ) , None ) ;
114
- assert_eq ! ( "++129" . parse:: <i8 >( ) . ok( ) , None ) ;
115
- assert_eq ! ( "Съешь" . parse:: <u8 >( ) . ok( ) , None ) ;
123
+ test_parse :: < i8 > ( "--129" , Err ( IntErrorKind :: InvalidDigit ) ) ;
124
+ test_parse :: < i8 > ( "++129" , Err ( IntErrorKind :: InvalidDigit ) ) ;
125
+ test_parse :: < u8 > ( "Съешь" , Err ( IntErrorKind :: InvalidDigit ) ) ;
126
+ test_parse :: < u8 > ( "123Hello" , Err ( IntErrorKind :: InvalidDigit ) ) ;
127
+ test_parse :: < i8 > ( "--" , Err ( IntErrorKind :: InvalidDigit ) ) ;
128
+ test_parse :: < i8 > ( "-" , Err ( IntErrorKind :: InvalidDigit ) ) ;
129
+ test_parse :: < i8 > ( "+" , Err ( IntErrorKind :: InvalidDigit ) ) ;
130
+ test_parse :: < u8 > ( "-1" , Err ( IntErrorKind :: InvalidDigit ) ) ;
116
131
}
117
132
118
133
#[ test]
119
134
fn test_empty ( ) {
120
- assert_eq ! ( "-" . parse:: <i8 >( ) . ok( ) , None ) ;
121
- assert_eq ! ( "+" . parse:: <i8 >( ) . ok( ) , None ) ;
122
- assert_eq ! ( "" . parse:: <u8 >( ) . ok( ) , None ) ;
135
+ test_parse :: < u8 > ( "" , Err ( IntErrorKind :: Empty ) ) ;
123
136
}
124
137
125
138
#[ test]
0 commit comments