@@ -330,120 +330,4 @@ impl Rational {
330
330
}
331
331
332
332
#[ cfg( test) ]
333
- mod tests {
334
- use num:: ToPrimitive ;
335
-
336
- use super :: * ;
337
-
338
- #[ test]
339
- fn test_parse_rational ( ) {
340
- assert_eq ! (
341
- Rational :: parse( "1234" ) . expect_finite( ) ,
342
- BigRational :: new( 1234 . into( ) , 1 . into( ) )
343
- ) ;
344
- assert_eq ! (
345
- Rational :: parse( "-1234" ) . expect_finite( ) ,
346
- BigRational :: new( ( -1234 ) . into( ) , 1 . into( ) )
347
- ) ;
348
- assert_eq ! (
349
- Rational :: parse( "1e+6" ) . expect_finite( ) ,
350
- BigRational :: new( 1000000 . into( ) , 1 . into( ) )
351
- ) ;
352
- assert_eq ! (
353
- Rational :: parse( "1e-6" ) . expect_finite( ) ,
354
- BigRational :: new( 1 . into( ) , 1000000 . into( ) )
355
- ) ;
356
- assert_eq ! (
357
- Rational :: parse( "10.4e6" ) . expect_finite( ) ,
358
- BigRational :: new( 10400000 . into( ) , 1 . into( ) )
359
- ) ;
360
- assert_eq ! (
361
- Rational :: parse( "10.4e+6" ) . expect_finite( ) ,
362
- BigRational :: new( 10400000 . into( ) , 1 . into( ) )
363
- ) ;
364
- assert_eq ! (
365
- Rational :: parse( "10.4e-6" ) . expect_finite( ) ,
366
- BigRational :: new( 13 . into( ) , 1250000 . into( ) )
367
- ) ;
368
- assert_eq ! (
369
- Rational :: parse( "10.4243566462342456234124" ) . expect_finite( ) ,
370
- BigRational :: new(
371
- 104243566462342456234124_i128 . into( ) ,
372
- 10000000000000000000000_i128 . into( )
373
- )
374
- ) ;
375
- assert_eq ! ( Rational :: parse( "inf" ) , Rational :: Inf ) ;
376
- assert_eq ! ( Rational :: parse( "+inf" ) , Rational :: Inf ) ;
377
- assert_eq ! ( Rational :: parse( "-inf" ) , Rational :: NegInf ) ;
378
- assert_eq ! ( Rational :: parse( "NaN" ) , Rational :: Nan ) ;
379
- }
380
-
381
- #[ test]
382
- fn test_decode ( ) {
383
- assert_eq ! ( decode( 0f32 , false ) , FloatRes :: Zero ) ;
384
- assert_eq ! ( decode( f32 :: INFINITY , false ) , FloatRes :: Inf ) ;
385
- assert_eq ! ( decode( f32 :: NEG_INFINITY , false ) , FloatRes :: NegInf ) ;
386
- assert_eq ! ( decode( 1.0f32 , false ) . normalize( ) , FloatRes :: Real { sig: 1 , exp: 0 } ) ;
387
- assert_eq ! ( decode( -1.0f32 , false ) . normalize( ) , FloatRes :: Real { sig: -1 , exp: 0 } ) ;
388
- assert_eq ! ( decode( 100.0f32 , false ) . normalize( ) , FloatRes :: Real { sig: 100 , exp: 0 } ) ;
389
- assert_eq ! ( decode( 100.5f32 , false ) . normalize( ) , FloatRes :: Real { sig: 201 , exp: -1 } ) ;
390
- assert_eq ! (
391
- decode( -4.004f32 , false ) . normalize( ) ,
392
- FloatRes :: Real { sig: -8396997 , exp: -21 }
393
- ) ;
394
- assert_eq ! (
395
- decode( 0.0004f32 , false ) . normalize( ) ,
396
- FloatRes :: Real { sig: 13743895 , exp: -35 }
397
- ) ;
398
- assert_eq ! (
399
- decode( f32 :: from_bits( 0x1 ) , false ) . normalize( ) ,
400
- FloatRes :: Real { sig: 1 , exp: -149 }
401
- ) ;
402
- }
403
-
404
- #[ test]
405
- fn test_validate ( ) {
406
- validate :: < f32 > ( "0" , false ) . unwrap ( ) ;
407
- validate :: < f32 > ( "-0" , false ) . unwrap ( ) ;
408
- validate :: < f32 > ( "1" , false ) . unwrap ( ) ;
409
- validate :: < f32 > ( "-1" , false ) . unwrap ( ) ;
410
- validate :: < f32 > ( "1.1" , false ) . unwrap ( ) ;
411
- validate :: < f32 > ( "-1.1" , false ) . unwrap ( ) ;
412
- validate :: < f32 > ( "1e10" , false ) . unwrap ( ) ;
413
- validate :: < f32 > ( "1e1000" , false ) . unwrap ( ) ;
414
- validate :: < f32 > ( "-1e1000" , false ) . unwrap ( ) ;
415
- validate :: < f32 > ( "1e-1000" , false ) . unwrap ( ) ;
416
- validate :: < f32 > ( "-1e-1000" , false ) . unwrap ( ) ;
417
- }
418
-
419
- #[ test]
420
- fn test_check ( ) {
421
- // Most of the arbitrary values come from checking against <http://weitz.de/ieee/>.
422
- let r = & BigRational :: from_float ( 10.0 ) . unwrap ( ) ;
423
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 10 , 0 ) . unwrap ( ) ;
424
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 10 , -1 ) . unwrap_err ( ) ;
425
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 10 , 1 ) . unwrap_err ( ) ;
426
-
427
- let r = & BigRational :: from_float ( 0.25 ) . unwrap ( ) ;
428
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 1 , -2 ) . unwrap ( ) ;
429
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 2 , -2 ) . unwrap_err ( ) ;
430
-
431
- let r = & BigRational :: from_float ( 1234.5678 ) . unwrap ( ) ;
432
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 0b100110100101001000101011 , -13 ) . unwrap ( ) ;
433
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 0b100110100101001000101010 , -13 ) . unwrap_err ( ) ;
434
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , 0b100110100101001000101100 , -13 ) . unwrap_err ( ) ;
435
-
436
- let r = & BigRational :: from_float ( -1234.5678 ) . unwrap ( ) ;
437
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , -0b100110100101001000101011 , -13 ) . unwrap ( ) ;
438
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , -0b100110100101001000101010 , -13 ) . unwrap_err ( ) ;
439
- FloatRes :: < f32 > :: validate_real ( r. clone ( ) , -0b100110100101001000101100 , -13 ) . unwrap_err ( ) ;
440
- }
441
-
442
- #[ test]
443
- fn check_constants ( ) {
444
- assert_eq ! ( f32 :: constants( ) . max. to_f32( ) . unwrap( ) , f32 :: MAX ) ;
445
- assert_eq ! ( f32 :: constants( ) . min_subnormal. to_f32( ) . unwrap( ) , f32 :: from_bits( 0x1 ) ) ;
446
- assert_eq ! ( f64 :: constants( ) . max. to_f64( ) . unwrap( ) , f64 :: MAX ) ;
447
- assert_eq ! ( f64 :: constants( ) . min_subnormal. to_f64( ) . unwrap( ) , f64 :: from_bits( 0x1 ) ) ;
448
- }
449
- }
333
+ mod tests;
0 commit comments