use rand::Rng;
#[allow(dead_code)]
mod common;
use common::{AllowedOps, Client, Update};
const ITERATIONS: usize = 500;
fn random_op<R: Rng>(
client: &mut Client,
allowed: AllowedOps,
rng: &mut R,
) -> Update {
let update = client.random_op(allowed, rng);
client.apply(update);
update
}
fn random_ops<R: Rng>(
client: &mut Client,
allowed: AllowedOps,
rng: &mut R,
) -> Vec<Update> {
(0..ITERATIONS).map(|_| random_op(client, allowed, rng)).collect()
}
fn replay_common(allowed: AllowedOps) -> Client {
let mut rng = common::make_rng();
let mut client1 = Client::new(rng.random_range(0..100) * 4);
let updates1 = random_ops(&mut client1, allowed, &mut rng);
let mut client2 = Client::new(rng.random_range(0..100) * 4 + 1);
for &update in &updates1 {
client2.apply(update);
}
assert!(client1.text() == client2.text());
let updates2 = random_ops(&mut client2, allowed, &mut rng);
for &update in &updates2 {
client1.apply(update);
}
assert!(client1.text() == client2.text());
let mut client3 = Client::new(rng.random_range(0..100) * 4 + 2);
for _ in 0..2 {
for &update in updates1.iter().chain(&updates2) {
client3.apply(update);
}
assert!(client1.text() == client3.text());
}
client2
}
#[test]
fn replay_insert() {
replay_common(AllowedOps::INSERT_ONLY);
}
#[test]
fn replay_all() {
let client1 = replay_common(AllowedOps::ALL);
let mut client2 = Client::new(500);
for update in client1.updates() {
client2.apply(update);
}
assert!(client1.text() == client2.text());
let mut client3 = Client::new(501);
for update in client2.clone(502).updates() {
client3.apply(update);
}
assert!(client1.text() == client3.text());
}