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

Add lipsum_words_from_seed helper function #65

Merged
merged 1 commit into from
Jul 6, 2020
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ codecov = { repository = "mgeisler/lipsum" }

[dependencies]
rand = "0.7"
rand_chacha = "0.2"

[dev-dependencies]
version-sync = "0.9"
rand_xorshift = "0.2"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ The build failures makes it infeasible to keep `lipsum` compatible
with any particular version of Rust. We will therefore track the
latest stable version of Rust from now on.

The new `lipsum_words_from_seed` function generates random but
deterministic lorem ipsum text. This is useful in unit tests when you
need fixed inputs.

### Version 0.6.0 — December 9th, 2018

The new `lipsum_words` function can be used to generate random lorem
Expand Down
44 changes: 34 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

use rand::rngs::ThreadRng;
use rand::seq::SliceRandom;
use rand::Rng;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
use std::cell::RefCell;
use std::collections::HashMap;

Expand Down Expand Up @@ -85,17 +86,17 @@ impl<'a, R: Rng> MarkovChain<'a, R> {
/// ```
/// # fn main() {
/// use rand::SeedableRng;
/// use rand_xorshift::XorShiftRng;
/// use rand_chacha::ChaCha20Rng;
/// use lipsum::MarkovChain;
///
/// let rng = XorShiftRng::seed_from_u64(0);
/// let rng = ChaCha20Rng::seed_from_u64(0);
/// let mut chain = MarkovChain::new_with_rng(rng);
/// chain.learn("infra-red red orange yellow green blue indigo x-ray");
///
/// // The chain jumps consistently like this:
/// assert_eq!(chain.generate(1), "Yellow.");
/// assert_eq!(chain.generate(1), "Blue.");
/// assert_eq!(chain.generate(1), "Orange.");
/// assert_eq!(chain.generate(1), "Infra-red.");
/// assert_eq!(chain.generate(1), "Yellow.");
/// # }
/// ```
pub fn new_with_rng(rng: R) -> MarkovChain<'a, R> {
Expand Down Expand Up @@ -392,11 +393,11 @@ pub fn lipsum(n: usize) -> String {
})
}

/// Generate `n` words of random lorem ipsum text.
/// Generate `n` random words of lorem ipsum text.
///
/// The text starts with a random word from [`LOREM_IPSUM`]. Multiple
/// sentences may be generated, depending on the punctuation of the
/// words being random selected.
/// words being selected.
///
/// # Examples
///
Expand All @@ -415,6 +416,29 @@ pub fn lipsum_words(n: usize) -> String {
})
}

/// Generate `n` random words of lorem ipsum text. The seed is used to
/// make the sequence deterministic. This is useful in unit tests
/// where you need random but consistent inputs.
///
/// # 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);
let mut chain = MarkovChain::new_with_rng(rng);
chain.learn(LOREM_IPSUM);
chain.learn(LIBER_PRIMUS);
chain.generate(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 @@ -474,7 +498,7 @@ pub fn lipsum_title() -> String {
mod tests {
use super::*;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use rand_chacha::ChaCha20Rng;

#[test]
fn starts_with_lorem_ipsum() {
Expand Down Expand Up @@ -572,11 +596,11 @@ mod tests {

#[test]
fn new_with_rng() {
let rng = XorShiftRng::seed_from_u64(1234);
let rng = ChaCha20Rng::seed_from_u64(1234);
let mut chain = MarkovChain::new_with_rng(rng);
chain.learn("foo bar x y z");
chain.learn("foo bar a b c");

assert_eq!(chain.generate(15), "Bar x y a b x y y b b a b a b bar.");
assert_eq!(chain.generate(15), "A b bar a b a b bar a b x y b y x.");
}
}