Skip to content

Commit 1060f34

Browse files
authored
Merge pull request #310 from alexcrichton/modernize
Modernize the `testcrate` slighty
2 parents f0b4323 + 332220a commit 1060f34

File tree

2 files changed

+83
-51
lines changed

2 files changed

+83
-51
lines changed

testcrate/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
name = "testcrate"
33
version = "0.1.0"
44
authors = ["Alex Crichton <[email protected]>"]
5+
edition = "2018"
56

67
[lib]
78
test = false
89
doctest = false
910

1011
[build-dependencies]
11-
cast = { version = "0.2.2", features = ["x128"] }
12-
rand = { version = "0.4", features = ["i128_support"] }
12+
rand = "0.7"
1313

1414
[dependencies.compiler_builtins]
1515
path = ".."

testcrate/build.rs

+81-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
extern crate cast;
2-
extern crate rand;
3-
1+
use rand::seq::SliceRandom;
2+
use rand::Rng;
43
use std::collections::HashMap;
54
use std::fmt;
65
use std::fmt::Write as FmtWrite;
@@ -10,9 +9,6 @@ use std::io::Write;
109
use std::path::PathBuf;
1110
use std::{env, mem};
1211

13-
use self::cast::{f32, f64, i128, i32, i64, u128, u32, u64};
14-
use self::rand::Rng;
15-
1612
const NTESTS: usize = 1_000;
1713

1814
fn main() {
@@ -397,7 +393,7 @@ fn main() {
397393
if a.0.is_nan() {
398394
return None;
399395
}
400-
Some(f64(a.0))
396+
Some(f64::from(a.0))
401397
},
402398
"builtins::float::extend::__extendsfdf2(a)",
403399
);
@@ -407,100 +403,100 @@ fn main() {
407403
if a.0.is_nan() {
408404
return None;
409405
}
410-
Some(f64(a.0))
406+
Some(f64::from(a.0))
411407
},
412408
"builtins::float::extend::__extendsfdf2vfp(a)",
413409
);
414410
}
415411

416412
// float/conv.rs
417413
gen(
418-
|a: MyF64| i64(a.0).ok(),
414+
|a: MyF64| i64::cast(a.0),
419415
"builtins::float::conv::__fixdfdi(a)",
420416
);
421417
gen(
422-
|a: MyF64| i32(a.0).ok(),
418+
|a: MyF64| i32::cast(a.0),
423419
"builtins::float::conv::__fixdfsi(a)",
424420
);
425421
gen(
426-
|a: MyF32| i64(a.0).ok(),
422+
|a: MyF32| i64::cast(a.0),
427423
"builtins::float::conv::__fixsfdi(a)",
428424
);
429425
gen(
430-
|a: MyF32| i32(a.0).ok(),
426+
|a: MyF32| i32::cast(a.0),
431427
"builtins::float::conv::__fixsfsi(a)",
432428
);
433429
gen(
434-
|a: MyF32| i128(a.0).ok(),
430+
|a: MyF32| i128::cast(a.0),
435431
"builtins::float::conv::__fixsfti(a)",
436432
);
437433
gen(
438-
|a: MyF64| i128(a.0).ok(),
434+
|a: MyF64| i128::cast(a.0),
439435
"builtins::float::conv::__fixdfti(a)",
440436
);
441437
gen(
442-
|a: MyF64| u64(a.0).ok(),
438+
|a: MyF64| u64::cast(a.0),
443439
"builtins::float::conv::__fixunsdfdi(a)",
444440
);
445441
gen(
446-
|a: MyF64| u32(a.0).ok(),
442+
|a: MyF64| u32::cast(a.0),
447443
"builtins::float::conv::__fixunsdfsi(a)",
448444
);
449445
gen(
450-
|a: MyF32| u64(a.0).ok(),
446+
|a: MyF32| u64::cast(a.0),
451447
"builtins::float::conv::__fixunssfdi(a)",
452448
);
453449
gen(
454-
|a: MyF32| u32(a.0).ok(),
450+
|a: MyF32| u32::cast(a.0),
455451
"builtins::float::conv::__fixunssfsi(a)",
456452
);
457453
gen(
458-
|a: MyF32| u128(a.0).ok(),
454+
|a: MyF32| u128::cast(a.0),
459455
"builtins::float::conv::__fixunssfti(a)",
460456
);
461457
gen(
462-
|a: MyF64| u128(a.0).ok(),
458+
|a: MyF64| u128::cast(a.0),
463459
"builtins::float::conv::__fixunsdfti(a)",
464460
);
465461
gen(
466-
|a: MyI64| Some(f64(a.0)),
462+
|a: MyI64| Some(a.0 as f64),
467463
"builtins::float::conv::__floatdidf(a)",
468464
);
469465
gen(
470-
|a: MyI32| Some(f64(a.0)),
466+
|a: MyI32| Some(a.0 as f64),
471467
"builtins::float::conv::__floatsidf(a)",
472468
);
473469
gen(
474-
|a: MyI32| Some(f32(a.0)),
470+
|a: MyI32| Some(a.0 as f32),
475471
"builtins::float::conv::__floatsisf(a)",
476472
);
477473
gen(
478-
|a: MyU64| Some(f64(a.0)),
474+
|a: MyU64| Some(a.0 as f64),
479475
"builtins::float::conv::__floatundidf(a)",
480476
);
481477
gen(
482-
|a: MyU32| Some(f64(a.0)),
478+
|a: MyU32| Some(a.0 as f64),
483479
"builtins::float::conv::__floatunsidf(a)",
484480
);
485481
gen(
486-
|a: MyU32| Some(f32(a.0)),
482+
|a: MyU32| Some(a.0 as f32),
487483
"builtins::float::conv::__floatunsisf(a)",
488484
);
489485
gen(
490-
|a: MyU128| f32(a.0).ok(),
486+
|a: MyU128| Some(a.0 as f32),
491487
"builtins::float::conv::__floatuntisf(a)",
492488
);
493489
if !target_arch_mips {
494490
gen(
495-
|a: MyI128| Some(f32(a.0)),
491+
|a: MyI128| Some(a.0 as f32),
496492
"builtins::float::conv::__floattisf(a)",
497493
);
498494
gen(
499-
|a: MyI128| Some(f64(a.0)),
495+
|a: MyI128| Some(a.0 as f64),
500496
"builtins::float::conv::__floattidf(a)",
501497
);
502498
gen(
503-
|a: MyU128| Some(f64(a.0)),
499+
|a: MyU128| Some(a.0 as f64),
504500
"builtins::float::conv::__floatuntidf(a)",
505501
);
506502
}
@@ -996,7 +992,7 @@ macro_rules! gen_float {
996992
$significand_bits:expr) => {
997993
pub fn $name<R>(rng: &mut R) -> $fty
998994
where
999-
R: Rng,
995+
R: Rng + ?Sized,
1000996
{
1001997
const BITS: u8 = $bits;
1002998
const SIGNIFICAND_BITS: u8 = $significand_bits;
@@ -1015,9 +1011,9 @@ macro_rules! gen_float {
10151011
}
10161012
}
10171013

1018-
if rng.gen_weighted_bool(10) {
1014+
if rng.gen_range(0, 10) == 1 {
10191015
// Special values
1020-
*rng.choose(&[
1016+
*[
10211017
-0.0,
10221018
0.0,
10231019
::std::$fty::MIN,
@@ -1026,9 +1022,10 @@ macro_rules! gen_float {
10261022
::std::$fty::NAN,
10271023
::std::$fty::INFINITY,
10281024
-::std::$fty::INFINITY,
1029-
])
1025+
]
1026+
.choose(rng)
10301027
.unwrap()
1031-
} else if rng.gen_weighted_bool(10) {
1028+
} else if rng.gen_range(0, 10) == 1 {
10321029
// NaN patterns
10331030
mk_f32(rng.gen(), rng.gen(), 0)
10341031
} else if rng.gen() {
@@ -1053,7 +1050,7 @@ macro_rules! gen_large_float {
10531050
$significand_bits:expr) => {
10541051
pub fn $name<R>(rng: &mut R) -> $fty
10551052
where
1056-
R: Rng,
1053+
R: Rng + ?Sized,
10571054
{
10581055
const BITS: u8 = $bits;
10591056
const SIGNIFICAND_BITS: u8 = $significand_bits;
@@ -1072,9 +1069,9 @@ macro_rules! gen_large_float {
10721069
}
10731070
}
10741071

1075-
if rng.gen_weighted_bool(10) {
1072+
if rng.gen_range(0, 10) == 1 {
10761073
// Special values
1077-
*rng.choose(&[
1074+
*[
10781075
-0.0,
10791076
0.0,
10801077
::std::$fty::MIN,
@@ -1083,9 +1080,10 @@ macro_rules! gen_large_float {
10831080
::std::$fty::NAN,
10841081
::std::$fty::INFINITY,
10851082
-::std::$fty::INFINITY,
1086-
])
1083+
]
1084+
.choose(rng)
10871085
.unwrap()
1088-
} else if rng.gen_weighted_bool(10) {
1086+
} else if rng.gen_range(0, 10) == 1 {
10891087
// NaN patterns
10901088
mk_f32(rng.gen(), rng.gen(), 0)
10911089
} else if rng.gen() {
@@ -1102,7 +1100,7 @@ macro_rules! gen_large_float {
11021100
gen_large_float!(gen_large_f32, f32, u32, 32, 23);
11031101
gen_large_float!(gen_large_f64, f64, u64, 64, 52);
11041102

1105-
trait TestInput: rand::Rand + Hash + Eq + fmt::Debug {
1103+
trait TestInput: Hash + Eq + fmt::Debug {
11061104
fn ty_name() -> String;
11071105
fn generate_lets(container: &str, cnt: &mut u8) -> String;
11081106
fn generate_static(&self, dst: &mut String);
@@ -1119,6 +1117,7 @@ where
11191117
F: FnMut(A) -> Option<R>,
11201118
A: TestInput + Copy,
11211119
R: TestOutput,
1120+
rand::distributions::Standard: rand::distributions::Distribution<A>,
11221121
{
11231122
let rng = &mut rand::thread_rng();
11241123
let testname = test.split("::").last().unwrap().split("(").next().unwrap();
@@ -1207,8 +1206,8 @@ macro_rules! my_float {
12071206
}
12081207
}
12091208

1210-
impl rand::Rand for $name {
1211-
fn rand<R: rand::Rng>(r: &mut R) -> $name {
1209+
impl rand::distributions::Distribution<$name> for rand::distributions::Standard {
1210+
fn sample<R: rand::Rng + ?Sized >(&self, r: &mut R) -> $name {
12121211
$name($gen(r))
12131212
}
12141213
}
@@ -1260,18 +1259,18 @@ macro_rules! my_integer {
12601259
}
12611260
}
12621261

1263-
impl rand::Rand for $name {
1264-
fn rand<R: rand::Rng>(rng: &mut R) -> $name {
1262+
impl rand::distributions::Distribution<$name> for rand::distributions::Standard {
1263+
fn sample<R: rand::Rng + ?Sized >(&self, r: &mut R) -> $name {
12651264
let bits = (0 as $inner).count_zeros();
12661265
let mut mk = || {
1267-
if rng.gen_weighted_bool(10) {
1268-
*rng.choose(&[
1266+
if r.gen_range(0, 10) == 1 {
1267+
*[
12691268
::std::$inner::MAX >> (bits / 2),
12701269
0,
12711270
::std::$inner::MIN >> (bits / 2),
1272-
]).unwrap()
1271+
].choose(r).unwrap()
12731272
} else {
1274-
rng.gen::<$inner>()
1273+
r.gen::<$inner>()
12751274
}
12761275
};
12771276
let a = mk();
@@ -1386,3 +1385,36 @@ where
13861385
container.to_string()
13871386
}
13881387
}
1388+
1389+
trait FromFloat<T>: Sized {
1390+
fn cast(src: T) -> Option<Self>;
1391+
}
1392+
1393+
macro_rules! from_float {
1394+
($($src:ident => $($dst:ident),+);+;) => {
1395+
$(
1396+
$(
1397+
impl FromFloat<$src> for $dst {
1398+
fn cast(src: $src) -> Option<$dst> {
1399+
use std::{$dst, $src};
1400+
1401+
if src.is_nan() ||
1402+
src.is_infinite() ||
1403+
src < std::$dst::MIN as $src ||
1404+
src > std::$dst::MAX as $src
1405+
{
1406+
None
1407+
} else {
1408+
Some(src as $dst)
1409+
}
1410+
}
1411+
}
1412+
)+
1413+
)+
1414+
}
1415+
}
1416+
1417+
from_float! {
1418+
f32 => i32, i64, i128, u32, u64, u128;
1419+
f64 => i32, i64, i128, u32, u64, u128;
1420+
}

0 commit comments

Comments
 (0)