Skip to content

Commit

Permalink
fix: Lockstep versioning and test different PRNG conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Oct 20, 2023
1 parent d9ba2d8 commit 73ba917
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
4 changes: 2 additions & 2 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
fn forking_as() {
let mut rng1 = GlobalEntropy::<ChaCha12Rng>::default();

let rng2 = rng1.fork_as::<ChaCha8Rng>();
let rng2 = rng1.fork_as::<WyRand>();

let rng1 = format!("{:?}", rng1);
let rng2 = format!("{:?}", rng2);
Expand Down
31 changes: 26 additions & 5 deletions tests/determinism.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -23,6 +24,9 @@ struct SourceC;
#[derive(Component)]
struct SourceD;

#[derive(Component)]
struct SourceE;

fn random_output_a(mut q_source: Query<&mut EntropyComponent<ChaCha8Rng>, With<SourceA>>) {
let mut rng = q_source.single_mut();

Expand All @@ -49,24 +53,40 @@ fn random_output_c(mut q_source: Query<&mut EntropyComponent<ChaCha8Rng>, With<S
);
}

fn random_output_d(mut q_source: Query<&mut EntropyComponent<ChaCha8Rng>, With<SourceD>>) {
fn random_output_d(mut q_source: Query<&mut EntropyComponent<ChaCha12Rng>, With<SourceD>>) {
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<WyRand>, With<SourceE>>) {
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<GlobalEntropy<ChaCha8Rng>>) {
commands.spawn((SourceA, rng.fork_as::<ChaCha8Rng>()));
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::<ChaCha12Rng>()));

commands.spawn((SourceE, rng.fork_as::<WyRand>()));
}

/// Entities having their own sources side-steps issues with parallel execution and scheduling
Expand All @@ -91,6 +111,7 @@ fn test_parallel_determinism() {
random_output_b,
random_output_c,
random_output_d,
random_output_e,
),
)
.run();
Expand Down

0 comments on commit 73ba917

Please sign in to comment.