fix: test and some flags

This commit is contained in:
dignifiedquire 2018-12-19 22:00:11 +01:00
parent 2656a03c5a
commit ad076a8cd4
3 changed files with 200 additions and 100 deletions

View File

@ -28,8 +28,9 @@ name = "gcd"
harness = false
name = "shootout-pidigits"
[dependencies]
smallvec = "0.6.7"
[dependencies.smallvec]
version = "0.6.7"
default-features = false
[dependencies.num-integer]
version = "0.1.39"
@ -57,5 +58,5 @@ version = "1.0"
[features]
default = ["std", "i128", "u64_digit"]
i128 = ["num-integer/i128", "num-traits/i128"]
std = ["num-integer/std", "num-traits/std"]
std = ["num-integer/std", "num-traits/std", "smallvec/std"]
u64_digit = []

View File

@ -1682,7 +1682,7 @@ impl FromPrimitive for BigUint {
}
}
#[cfg(not(u64_digit))]
#[cfg(not(feature = "u64_digit"))]
impl From<u64> for BigUint {
#[inline]
fn from(mut n: u64) -> Self {
@ -1698,10 +1698,10 @@ impl From<u64> for BigUint {
}
}
#[cfg(u64_digit)]
#[cfg(feature = "u64_digit")]
impl From<u64> for BigUint {
#[inline]
fn from(mut n: u64) -> Self {
fn from(n: u64) -> Self {
BigUint::new_native(smallvec![n])
}
}
@ -2384,6 +2384,7 @@ impl IntDigits for BigUint {
/// Combine four `u32`s into a single `u128`.
#[cfg(has_i128)]
#[inline]
#[allow(dead_code)]
fn u32_to_u128(a: u32, b: u32, c: u32, d: u32) -> u128 {
u128::from(d) | (u128::from(c) << 32) | (u128::from(b) << 64) | (u128::from(a) << 96)
}
@ -2391,6 +2392,7 @@ fn u32_to_u128(a: u32, b: u32, c: u32, d: u32) -> u128 {
/// Split a single `u128` into four `u32`.
#[cfg(has_i128)]
#[inline]
#[allow(dead_code)]
fn u32_from_u128(n: u128) -> (u32, u32, u32, u32) {
(
(n >> 96) as u32,
@ -3021,7 +3023,7 @@ fn get_radix_base(radix: u32) -> (BigDigit, usize) {
#[cfg(not(feature = "u64_digit"))]
#[test]
fn test_from_slice() {
fn check(slice: &[BigDigit], data: &[BigDigit]) {
fn check(slice: &[u32], data: &[BigDigit]) {
assert_eq!(&BigUint::from_slice(slice).data[..], data);
}
check(&[1], &[1]);
@ -3050,7 +3052,7 @@ fn test_from_slice() {
check(&[1, 2, 0, 0], &[8_589_934_593]);
check(&[0, 0, 1, 2], &[0, 8_589_934_593]);
check(&[0, 0, 1, 2, 0, 0], &[0, 8_589_934_593]);
check(&[(-1i32 as u32) as BigDigit], &[(-1i32 as u32) as BigDigit]);
check(&[-1i32 as u32], &[(-1i32 as u32) as BigDigit]);
}
#[test]

View File

@ -99,65 +99,113 @@ mod biguint {
for (i, &s) in expected.iter().enumerate() {
let n: BigUint = s.parse().unwrap();
let r = rng.gen_biguint((1 << i) + i);
assert_eq!(n, r);
assert_eq!(n, r, "expected {}, got {}", n, r);
}
}
#[test]
fn test_chacha_value_stability() {
const EXPECTED: &[&str] = &[
"0",
"0",
"52",
"84",
"23780",
"86502865016",
"187057847319509867386",
"34045731223080904464438757488196244981910",
"23813754422987836414755953516143692594193066497413249270287126597896871975915808",
"57401636903146945411652549098818446911814352529449356393690984105383482703074355\
67088360974672291353736011718191813678720755501317478656550386324355699624671",
];
use rand::prng::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"0",
"52",
"84",
"23780",
"86502865016",
"187057847319509867386",
"34045731223080904464438757488196244981910",
"23813754422987836414755953516143692594193066497413249270287126597896871975915808",
"57401636903146945411652549098818446911814352529449356393690984105383482703074355\
67088360974672291353736011718191813678720755501317478656550386324355699624671",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"0",
"8",
"1861",
"172076",
"5194801951",
"259202797457072892019",
"2806086822955830608275100562233284760859",
"28771276448190704455825316568337256462972770861366848469339788407170414346460023",
"501572804396703231264118826164515310701005506447150057229826006447721882571235378\
4765127362270091441643338804096337494157874113908470083557122824480944132407",
];
#[test]
fn test_isaac_value_stability() {
const EXPECTED: &[&str] = &[
"1",
"4",
"3",
"649",
"89116",
"7730042024",
"20773149082453254949",
"35999009049239918667571895439206839620281",
"10191757312714088681302309313551624007714035309632506837271600807524767413673006",
"37805949268912387809989378008822038725134260145886913321084097194957861133272558\
43458183365174899239251448892645546322463253898288141861183340823194379722556",
];
use rand::prng::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED);
fn test_chacha_value_stability() {
use rand::prng::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"4",
"3",
"649",
"89116",
"7730042024",
"20773149082453254949",
"35999009049239918667571895439206839620281",
"10191757312714088681302309313551624007714035309632506837271600807524767413673006",
"37805949268912387809989378008822038725134260145886913321084097194957861133272558\
43458183365174899239251448892645546322463253898288141861183340823194379722556",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"2",
"51",
"1198",
"29707",
"35688018574",
"365090200586541225112",
"14051533166427520604648370582738617763816",
"26319846996091585801307964353534339679417889504909644767909019559631059772127122",
"14567336733062747693583250833667292276083519237160662196899060257293814346680656\
30951609693408423310563908301065751714778956255122249041917698392245727713420",
];
#[test]
fn test_isaac_value_stability() {
use rand::prng::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_XOR: &[&str] = &[
"1",
"0",
"37",
"395",
"181116",
"122718231117",
"1068467172329355695001",
"28246925743544411614293300167064395633287",
"12750053187017853048648861493745244146555950255549630854523304068318587267293038",
"53041498719137109355568081064978196049094604705283682101683207799515709404788873\
53417136457745727045473194367732849819278740266658219147356315674940229288531",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_XOR: &[&str] = &[
"0",
"1",
"36",
"970",
"940965",
"61158366130",
"590484965100191554896",
"34050066418951688801044382442803594076612",
"29147581645599998811521651062569705291155276949983132826461704326818089074318948",
"4990842894093964353439376569956547459232523176881032246435842690389845516810345611554402412893818283310117202233021355634125020654279500443420515862554775828",
];
#[test]
fn test_xorshift_value_stability() {
const EXPECTED: &[&str] = &[
"1",
"0",
"37",
"395",
"181116",
"122718231117",
"1068467172329355695001",
"28246925743544411614293300167064395633287",
"12750053187017853048648861493745244146555950255549630854523304068318587267293038",
"53041498719137109355568081064978196049094604705283682101683207799515709404788873\
53417136457745727045473194367732849819278740266658219147356315674940229288531",
];
use rand::prng::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED);
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}
@ -261,64 +309,113 @@ mod bigint {
for (i, &s) in expected.iter().enumerate() {
let n: BigInt = s.parse().unwrap();
let r = rng.gen_bigint((1 << i) + i);
assert_eq!(n, r);
assert_eq!(n, r, "expected {}, got {}", n, r);
}
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"-6",
"-1",
"1321",
"-147247",
"8486373526",
"-272736656290199720696",
"2731152629387534140535423510744221288522",
"-28820024790651190394679732038637785320661450462089347915910979466834461433196572",
"501454570554170484799723603981439288209930393334472085317977614690773821680884844\
8530978478667288338327570972869032358120588620346111979053742269317702532328",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_CHACHA: &[&str] = &[
"0",
"-7",
"-62",
"105",
"13025",
"-33857814162",
"768483926407291599143",
"-42356168828789885585553598574661841382586",
"28813250216034838684899917677182169473483558650956121225920149068989083656174824",
"27056553770481404639717657695702187062015359344716548489861498121037858109133467\
99640556108506718020020878739044048067894089601665199172215093468287730555599",
];
#[test]
fn test_chacha_value_stability() {
const EXPECTED: &[&str] = &[
"0",
"-6",
"-1",
"1321",
"-147247",
"8486373526",
"-272736656290199720696",
"2731152629387534140535423510744221288522",
"-28820024790651190394679732038637785320661450462089347915910979466834461433196572",
"501454570554170484799723603981439288209930393334472085317977614690773821680884844\
8530978478667288338327570972869032358120588620346111979053742269317702532328",
];
use rand::prng::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED);
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_ISAAC: &[&str] = &[
"1",
"0",
"5",
"113",
"-132240",
"-36348760761",
"-365690596708430705434",
"-14090753008246284277803606722552430292432",
"-26313941628626248579319341019368550803676255307056857978955881718727601479436059",
"-14563174552421101848999036239003801073335703811160945137332228646111920972691151\
88341090358094331641182310792892459091016794928947242043358702692294695845817",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_ISAAC: &[&str] = &[
"-1",
"-4",
"-29",
"1621",
"23872",
"-40371956434",
"-350815272425024298187",
"-38554888817044546636456097200348998322457",
"7474426176220721712055446211154990065592106428397966654956172383998793852911545",
"6168955541726830487961394166772329653532583907235825721475483003506842180688827\
391385624898257369023912466314791483731902392667906094226608113824795883754631",
];
#[test]
fn test_isaac_value_stability() {
const EXPECTED: &[&str] = &[
"1",
"0",
"5",
"113",
"-132240",
"-36348760761",
"-365690596708430705434",
"-14090753008246284277803606722552430292432",
"-26313941628626248579319341019368550803676255307056857978955881718727601479436059",
"-14563174552421101848999036239003801073335703811160945137332228646111920972691151\
88341090358094331641182310792892459091016794928947242043358702692294695845817",
];
use rand::prng::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED);
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}
#[cfg(not(feature = "u64_digit"))]
const EXPECTED_XOR: &[&str] = &[
"-1",
"-4",
"11",
"-1802",
"966495",
"-62592045703",
"-602281783447192077116",
"-34335811410223060575607987996861632509125",
"29156580925282215857325937227200350542000244609280383263289720243118706105351199",
"49920038676141573457451407325930326489996232208489690499754573826911037849083623\
24546142615325187412887314466195222441945661833644117700809693098722026764846",
];
#[cfg(feature = "u64_digit")]
const EXPECTED_XOR: &[&str] = &[
"-1",
"-3",
"4",
"-228",
"377276",
"32032893086",
"885120221048601050706",
"33404877924318663206979407569537287223622",
"-15253093455306269007559295940333933266263385975865952571271093251749752787075084",
"4502641950394305250103130679458759592222756470562408577296380915684757985604969904\
381774527626485128207406911227296090734227576935034372181808818486328078978",
];
#[test]
fn test_xorshift_value_stability() {
const EXPECTED: &[&str] = &[
"-1",
"-4",
"11",
"-1802",
"966495",
"-62592045703",
"-602281783447192077116",
"-34335811410223060575607987996861632509125",
"29156580925282215857325937227200350542000244609280383263289720243118706105351199",
"49920038676141573457451407325930326489996232208489690499754573826911037849083623\
24546142615325187412887314466195222441945661833644117700809693098722026764846",
];
use rand::prng::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED);
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}