[go: up one dir, main page]

randomize 3.0.1

randomization routines
Documentation
use randomize::RandRangeU32;

// this "tests" that the method is `const`
const _D10: RandRangeU32 = RandRangeU32::new(1, 10);

#[test]
fn test_from_ops_ranges() {
  let m = core::u32::MAX;
  assert_eq!(RandRangeU32::new(1, 6), RandRangeU32::from(1..7));
  assert_eq!(RandRangeU32::new(3, m - 1), RandRangeU32::from(3..));
  assert_eq!(RandRangeU32::new(0, m - 1), RandRangeU32::from(..));
  assert_eq!(RandRangeU32::new(1, 6), RandRangeU32::from(1..=6));
  assert_eq!(RandRangeU32::new(0, 6), RandRangeU32::from(..7));
  assert_eq!(RandRangeU32::new(0, 6), RandRangeU32::from(..=6));
}

#[test]
#[allow(non_snake_case)]
fn test_RandRangeU32_try_new() {
  let r = RandRangeU32::try_new(6, 1).unwrap();
  assert_eq!(r.low(), 1);
  assert_eq!(r.high(), 6);

  assert_eq!(RandRangeU32::try_new(0, core::u32::MAX), None);
}

#[test]
#[should_panic]
#[allow(non_snake_case)]
fn test_RandRangeU32_new_panic() {
  let _r = RandRangeU32::new(0, core::u32::MAX);
}

// Note(Lokathor): Each of these range tests is separate so the test suite runs
// them in parallel, since they're each relatively long.
#[test]
#[ignore]
fn test_place_in_range_1_6() {
  let r = RandRangeU32::new(1, 6);
  let mut outputs = [0u32; 7];
  for u in 0..=core::u32::MAX {
    match r.place_in_range(u) {
      Some(val) => outputs[val as usize] += 1,
      None => outputs[0] += 1,
    }
  }
  // we placed an even number in all "good" positions
  let count = outputs[1];
  for &output_value in outputs[1..].iter() {
    assert_eq!(count, output_value, "outputs:{:?}", outputs);
  }
  // and had some number less than the range width left over
  let range_width = r.high() + 1 - r.low();
  assert!(outputs[0] < range_width, "outputs:{:?}", outputs);
}

#[test]
#[ignore]
fn test_place_in_range_1_8() {
  let r = RandRangeU32::new(1, 8);
  let mut outputs = [0u32; 9];
  for u in 0..=core::u32::MAX {
    match r.place_in_range(u) {
      Some(val) => outputs[val as usize] += 1,
      None => outputs[0] += 1,
    }
  }
  // we placed an even number in all "good" positions
  let count = outputs[1];
  for &output_value in outputs[1..].iter() {
    assert_eq!(count, output_value, "outputs:{:?}", outputs);
  }
  // and had some number less than the range width left over
  let range_width = r.high() + 1 - r.low();
  assert!(outputs[0] < range_width, "outputs:{:?}", outputs);
}

#[test]
#[ignore]
fn test_place_in_range_1_10() {
  let r = RandRangeU32::new(1, 10);
  let mut outputs = [0u32; 11];
  for u in 0..=core::u32::MAX {
    match r.place_in_range(u) {
      Some(val) => outputs[val as usize] += 1,
      None => outputs[0] += 1,
    }
  }
  // we placed an even number in all "good" positions
  let count = outputs[1];
  for &output_value in outputs[1..].iter() {
    assert_eq!(count, output_value, "outputs:{:?}", outputs);
  }
  // and had some number less than the range width left over
  let range_width = r.high() + 1 - r.low();
  assert!(outputs[0] < range_width, "outputs:{:?}", outputs);
}