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

change: Restore lipsum_from_seed & lipsum_words_from_seed (#98) #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ rand_chacha = "0.3.1"
[dev-dependencies]
version-sync = "0.9.4"
rand = "0.8.5"

[features]
default = ["std"]
std_rng = ["rand/std", "rand/std_rng"]
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,30 @@ pub fn lipsum_with_rng(rng: impl Rng, n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng_from(rng, n, ("Lorem", "ipsum")))
}

/// Generate `n` words of lorem ipsum text. The output will always start with
/// "Lorem ipsum". The seed makes the sequence deterministic.
///
/// Deterministic sequences are useful for unit tests where you need random but
/// consistent inputs or when users expect an infinitely extendable blind text
/// string that does not change.
///
/// # Examples
///
/// ```
/// use lipsum::lipsum_from_seed;
///
/// assert_eq!(lipsum_from_seed(23, 16),
/// "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.");
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
/// [`lipsum`]: fn.lipsum.html
pub fn lipsum_from_seed(n: usize, seed: u64) -> String {
let rng = ChaCha20Rng::seed_from_u64(seed);
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng_from(rng, n, ("Lorem", "ipsum")))
}


/// Generate `n` words of lorem ipsum text.
///
/// The text is deterministically sampled from a Markov chain based on
Expand Down Expand Up @@ -499,6 +523,30 @@ pub fn lipsum_words_with_rng(rng: impl Rng, n: usize) -> String {
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng(rng, n))
}

/// Generate `n` random words of lorem ipsum text. The seed makes the sequence
/// deterministic.
///
/// Deterministic sequences are useful for unit tests where you need random but
/// consistent inputs or when users expect an infinitely extendable blind text
/// string that does not change.
///
/// # Examples
///
/// ```
/// use lipsum::lipsum_words_from_seed;
///
/// assert_eq!(lipsum_words_from_seed(7, 1234),
/// "Anteponant iis, quae recordamur. Stulti autem malorum.");
/// ```
///
/// [`LOREM_IPSUM`]: constant.LOREM_IPSUM.html
/// [`lipsum_words`]: fn.lipsum_words.html
pub fn lipsum_words_from_seed(n: usize, seed: u64) -> String {
let rng = ChaCha20Rng::seed_from_u64(seed);
LOREM_IPSUM_CHAIN.with(|chain| chain.generate_with_rng(rng, n))
}


/// Minimum number of words to include in a title.
const TITLE_MIN_WORDS: usize = 3;
/// Maximum number of words to include in a title.
Expand Down Expand Up @@ -681,4 +729,18 @@ mod tests {
"A b bar a b a b bar a b x y b y x."
);
}

#[test]
fn seed_works() {
assert_eq!(
lipsum_words_from_seed(10, 100_000),
lipsum_words_from_seed(10, 100_000)
);
assert_eq!(lipsum_from_seed(30, 100_000), lipsum_from_seed(30, 100_000));
assert_ne!(
lipsum_words_from_seed(10, 100_000),
lipsum_words_from_seed(10, 100_001)
);
assert_ne!(lipsum_from_seed(30, 100_000), lipsum_from_seed(30, 100_001));
}
}