diff --git a/Cargo.toml b/Cargo.toml index 09306ab..6bf9e8d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,8 +30,15 @@ serde = { version = "1.0", features = ["derive"], optional = true } rand_core = { version = "0.6", features = ["std"] } rand_chacha = { version = "0.3", optional = true } +# This cfg cannot be enabled, but it forces Cargo to keep bevy_prng's +# version in lockstep with bevy_rand, so that even minor versions +# cannot be out of step with bevy_rand due to dependencies on traits +# and implementations between the two crates. +[target.'cfg(any())'.dependencies] +bevy_prng = { path = "bevy_prng", version = "=0.2" } + [dev-dependencies] -bevy_prng = { path = "bevy_prng", version = "0.2", features = ["rand_chacha"] } +bevy_prng = { path = "bevy_prng", version = "0.2", features = ["rand_chacha", "wyrand"] } rand = "0.8" ron = { version = "0.8.0", features = ["integer128"] } diff --git a/src/resource.rs b/src/resource.rs index a841daf..658bdb4 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -162,7 +162,7 @@ where #[cfg(test)] mod tests { use bevy::reflect::TypePath; - use bevy_prng::{ChaCha8Rng, ChaCha12Rng}; + use bevy_prng::{ChaCha8Rng, ChaCha12Rng, WyRand}; use super::*; @@ -196,7 +196,7 @@ mod tests { fn forking_as() { let mut rng1 = GlobalEntropy::::default(); - let rng2 = rng1.fork_as::(); + let rng2 = rng1.fork_as::(); let rng1 = format!("{:?}", rng1); let rng2 = format!("{:?}", rng2); diff --git a/tests/determinism.rs b/tests/determinism.rs index aa56a2d..76554d4 100644 --- a/tests/determinism.rs +++ b/tests/determinism.rs @@ -1,10 +1,11 @@ #![allow(clippy::type_complexity)] use bevy::prelude::*; -use bevy_prng::ChaCha8Rng; +use bevy_prng::{ChaCha8Rng, WyRand, ChaCha12Rng}; use bevy_rand::prelude::*; use rand::prelude::Rng; +use rand_core::RngCore; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -23,6 +24,9 @@ struct SourceC; #[derive(Component)] struct SourceD; +#[derive(Component)] +struct SourceE; + fn random_output_a(mut q_source: Query<&mut EntropyComponent, With>) { let mut rng = q_source.single_mut(); @@ -49,24 +53,40 @@ fn random_output_c(mut q_source: Query<&mut EntropyComponent, With, With>) { +fn random_output_d(mut q_source: Query<&mut EntropyComponent, With>) { let mut rng = q_source.single_mut(); assert_eq!( rng.gen::<(u16, u16)>(), - (61569, 26940), + (41421, 7891), "SourceD does not match expected output" ); } +fn random_output_e(mut q_source: Query<&mut EntropyComponent, With>) { + let mut rng = q_source.single_mut(); + + let mut bytes = [0u8; 8]; + + rng.fill_bytes(bytes.as_mut()); + + assert_eq!( + &bytes, + &[195, 159, 73, 157, 39, 99, 104, 111], + "SourceE does not match expected output" + ); +} + fn setup_sources(mut commands: Commands, mut rng: ResMut>) { - commands.spawn((SourceA, rng.fork_as::())); + commands.spawn((SourceA, rng.fork_rng())); commands.spawn((SourceB, rng.fork_rng())); commands.spawn((SourceC, rng.fork_rng())); - commands.spawn((SourceD, rng.fork_rng())); + commands.spawn((SourceD, rng.fork_as::())); + + commands.spawn((SourceE, rng.fork_as::())); } /// Entities having their own sources side-steps issues with parallel execution and scheduling @@ -91,6 +111,7 @@ fn test_parallel_determinism() { random_output_b, random_output_c, random_output_d, + random_output_e, ), ) .run();