Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage #199

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/deterministic_randomizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,36 @@ mod test {
let data = [0u8, 1u8, 2u8, 3u8];
let mut randomizer = R::new(seed);

// Test a known sampling
let sample = randomizer.sample(&data, 3).unwrap();
assert_eq!(&sample, &[2u8, 3u8, 1u8]);

// Test sampling from an empty array
let empty: [u8; 0] = [];
assert_eq!(&randomizer.sample(&empty, 0).unwrap(), &empty);
assert!(randomizer.sample(&empty, 1).is_err());

// Test sampling too many items from a non-empty array
assert!(randomizer.sample(&data, data.len() + 1).is_err());

// Test sampling an entire array
let mut full = randomizer.sample(&data, data.len()).unwrap();
full.sort();
assert_eq!(full, data);
}

#[test]
fn test_seeding() {
let seed = [1u8; 32];
let mut randomizer = R::new(seed);

// Assert that resetting yields the same state
let data = randomizer.next_bounded_u64(u64::MAX).unwrap();
randomizer.reset();
assert_eq!(randomizer.next_bounded_u64(u64::MAX).unwrap(), data);

// Assert that reseeding yields a distinct state (with high probability)
randomizer.reseed([2u8; 32]);
assert_ne!(randomizer.next_bounded_u64(u64::MAX).unwrap(), data);
}
}