@@ -122,3 +122,115 @@ fn checked_div() {
122122 assert_eq ! ( Duration :: new( 1 , 0 ) . checked_div( 2 ) , Some ( Duration :: new( 0 , 500_000_000 ) ) ) ;
123123 assert_eq ! ( Duration :: new( 2 , 0 ) . checked_div( 0 ) , None ) ;
124124}
125+
126+ #[ test]
127+ fn debug_formatting_extreme_values ( ) {
128+ assert_eq ! (
129+ format!( "{:?}" , Duration :: new( 18_446_744_073_709_551_615 , 123_456_789 ) ) ,
130+ "18446744073709551615.123456789s"
131+ ) ;
132+ }
133+
134+ #[ test]
135+ fn debug_formatting_secs ( ) {
136+ assert_eq ! ( format!( "{:?}" , Duration :: new( 7 , 000_000_000 ) ) , "7s" ) ;
137+ assert_eq ! ( format!( "{:?}" , Duration :: new( 7 , 100_000_000 ) ) , "7.1s" ) ;
138+ assert_eq ! ( format!( "{:?}" , Duration :: new( 7 , 000_010_000 ) ) , "7.00001s" ) ;
139+ assert_eq ! ( format!( "{:?}" , Duration :: new( 7 , 000_000_001 ) ) , "7.000000001s" ) ;
140+ assert_eq ! ( format!( "{:?}" , Duration :: new( 7 , 123_456_789 ) ) , "7.123456789s" ) ;
141+
142+ assert_eq ! ( format!( "{:?}" , Duration :: new( 88 , 000_000_000 ) ) , "88s" ) ;
143+ assert_eq ! ( format!( "{:?}" , Duration :: new( 88 , 100_000_000 ) ) , "88.1s" ) ;
144+ assert_eq ! ( format!( "{:?}" , Duration :: new( 88 , 000_010_000 ) ) , "88.00001s" ) ;
145+ assert_eq ! ( format!( "{:?}" , Duration :: new( 88 , 000_000_001 ) ) , "88.000000001s" ) ;
146+ assert_eq ! ( format!( "{:?}" , Duration :: new( 88 , 123_456_789 ) ) , "88.123456789s" ) ;
147+
148+ assert_eq ! ( format!( "{:?}" , Duration :: new( 999 , 000_000_000 ) ) , "999s" ) ;
149+ assert_eq ! ( format!( "{:?}" , Duration :: new( 999 , 100_000_000 ) ) , "999.1s" ) ;
150+ assert_eq ! ( format!( "{:?}" , Duration :: new( 999 , 000_010_000 ) ) , "999.00001s" ) ;
151+ assert_eq ! ( format!( "{:?}" , Duration :: new( 999 , 000_000_001 ) ) , "999.000000001s" ) ;
152+ assert_eq ! ( format!( "{:?}" , Duration :: new( 999 , 123_456_789 ) ) , "999.123456789s" ) ;
153+ }
154+
155+ #[ test]
156+ fn debug_formatting_millis ( ) {
157+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_000_000 ) ) , "7ms" ) ;
158+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_100_000 ) ) , "7.1ms" ) ;
159+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_000_001 ) ) , "7.000001ms" ) ;
160+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_123_456 ) ) , "7.123456ms" ) ;
161+
162+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_000_000 ) ) , "88ms" ) ;
163+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_100_000 ) ) , "88.1ms" ) ;
164+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_000_001 ) ) , "88.000001ms" ) ;
165+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_123_456 ) ) , "88.123456ms" ) ;
166+
167+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_000_000 ) ) , "999ms" ) ;
168+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_100_000 ) ) , "999.1ms" ) ;
169+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_000_001 ) ) , "999.000001ms" ) ;
170+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_123_456 ) ) , "999.123456ms" ) ;
171+ }
172+
173+ #[ test]
174+ fn debug_formatting_micros ( ) {
175+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_000 ) ) , "7µs" ) ;
176+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_100 ) ) , "7.1µs" ) ;
177+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_001 ) ) , "7.001µs" ) ;
178+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 7_123 ) ) , "7.123µs" ) ;
179+
180+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_000 ) ) , "88µs" ) ;
181+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_100 ) ) , "88.1µs" ) ;
182+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_001 ) ) , "88.001µs" ) ;
183+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88_123 ) ) , "88.123µs" ) ;
184+
185+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_000 ) ) , "999µs" ) ;
186+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_100 ) ) , "999.1µs" ) ;
187+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_001 ) ) , "999.001µs" ) ;
188+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999_123 ) ) , "999.123µs" ) ;
189+ }
190+
191+ #[ test]
192+ fn debug_formatting_nanos ( ) {
193+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 0 ) ) , "0ns" ) ;
194+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 1 ) ) , "1ns" ) ;
195+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 88 ) ) , "88ns" ) ;
196+ assert_eq ! ( format!( "{:?}" , Duration :: new( 0 , 999 ) ) , "999ns" ) ;
197+ }
198+
199+ #[ test]
200+ fn debug_formatting_precision_zero ( ) {
201+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 0 ) ) , "0ns" ) ;
202+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 123 ) ) , "123ns" ) ;
203+
204+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 1_001 ) ) , "1µs" ) ;
205+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 1_999 ) ) , "1µs" ) ;
206+
207+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 1_000_001 ) ) , "1ms" ) ;
208+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 0 , 1_999_999 ) ) , "1ms" ) ;
209+
210+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 1 , 000_000_001 ) ) , "1s" ) ;
211+ assert_eq ! ( format!( "{:.0?}" , Duration :: new( 1 , 999_999_999 ) ) , "1s" ) ;
212+ }
213+
214+ #[ test]
215+ fn debug_formatting_precision_two ( ) {
216+ // This might seem inconsistent with the other units, but printing
217+ // fractional digits for nano seconds would imply more precision than is
218+ // actually stored.
219+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 0 ) ) , "0ns" ) ;
220+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 123 ) ) , "123ns" ) ;
221+
222+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 1_000 ) ) , "1.00µs" ) ;
223+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 7_001 ) ) , "7.00µs" ) ;
224+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 7_100 ) ) , "7.10µs" ) ;
225+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 1_999 ) ) , "1.99µs" ) ;
226+
227+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 1_000_000 ) ) , "1.00ms" ) ;
228+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 3_001_000 ) ) , "3.00ms" ) ;
229+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 3_100_000 ) ) , "3.10ms" ) ;
230+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 0 , 1_999_999 ) ) , "1.99ms" ) ;
231+
232+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 1 , 000_000_000 ) ) , "1.00s" ) ;
233+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 4 , 001_000_000 ) ) , "4.00s" ) ;
234+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 2 , 100_000_000 ) ) , "2.10s" ) ;
235+ assert_eq ! ( format!( "{:.2?}" , Duration :: new( 8 , 999_999_999 ) ) , "8.99s" ) ;
236+ }
0 commit comments