Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move rnd_chars to utils * Remove whitespace * Update CHANGELOG * Fix clippy warnings * Remove unwanted changes
- Loading branch information
Showing
11 changed files
with
70 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use rand::distributions::{Distribution, Uniform}; | ||
|
||
pub fn rnd_chars(len: usize, src: &[char]) -> String { | ||
let rng = rand::thread_rng(); | ||
let distribution = Uniform::<usize>::from(0..src.len()); | ||
distribution | ||
.sample_iter(rng) | ||
.take(len) | ||
.map(|i| src[i]) | ||
.collect::<String>() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn same_char() { | ||
let chars = vec!['a']; | ||
|
||
assert_eq!(rnd_chars(0, &chars), ""); | ||
assert_eq!(rnd_chars(1, &chars), "a"); | ||
assert_eq!(rnd_chars(4, &chars), "aaaa"); | ||
} | ||
|
||
#[test] | ||
fn different_chars() { | ||
let chars = vec!['a', 'b', 'c']; | ||
|
||
assert_eq!(rnd_chars(0, &chars), ""); | ||
|
||
let s = rnd_chars(1, &chars); | ||
for ch in s.chars() { | ||
assert!(chars.contains(&ch)); | ||
} | ||
|
||
let s = rnd_chars(5, &chars); | ||
for ch in s.chars() { | ||
assert!(chars.contains(&ch)); | ||
} | ||
} | ||
} |