1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
use num::BigRational;
use crdts::glist::{GList, Op};
use crdts::{CmRDT, CvRDT, Identifier};
use quickcheck_macros::quickcheck;
#[test]
fn test_concurrent_inserts_with_same_identifier_can_be_split() {
let mut list = GList::new();
let op_a = list.insert_after(None, 'a');
let op_b = list.insert_after(None, 'b');
list.apply(op_a);
list.apply(op_b);
assert_eq!(list.read::<String>(), "ab");
let a_entry = list.get(0);
assert_eq!(a_entry.map(|e| e.value()), Some(&'a'));
let op_c = list.insert_after(a_entry, 'c');
list.apply(op_c);
println!("{}", list);
assert_eq!(list.read::<String>(), "acb");
}
#[test]
fn test_append_increments_entry() {
let mut glist: GList<char> = Default::default();
glist.apply(glist.insert_after(glist.last(), 'a'));
glist.apply(glist.insert_after(glist.last(), 'b'));
glist.apply(glist.insert_after(glist.last(), 'c'));
assert_eq!(
vec![
Identifier::from((BigRational::from_integer(0.into()), 'a')),
Identifier::from((BigRational::from_integer(1.into()), 'b')),
Identifier::from((BigRational::from_integer(2.into()), 'c')),
],
glist.iter().cloned().collect::<Vec<_>>()
);
println!("{:?}", glist);
assert_eq!("abc", glist.read::<String>());
}
#[test]
fn test_insert_at_front() {
let mut glist: GList<u8> = Default::default();
let op = glist.insert_before(glist.first(), 0);
glist.apply(op);
let op = glist.insert_before(glist.first(), 1);
glist.apply(op);
println!("{:?}", glist);
assert_eq!(vec![&1, &0], glist.read::<Vec<_>>());
}
#[quickcheck]
fn prop_ops_commute(ops_a: Vec<Op<u8>>, ops_b: Vec<Op<u8>>) {
let mut glist_a = GList::new();
let mut glist_b = GList::new();
for op in ops_a.clone() {
assert!(glist_a.validate_op(&op).is_ok());
glist_a.apply(op)
}
for op in ops_b.clone() {
assert!(glist_b.validate_op(&op).is_ok());
glist_b.apply(op)
}
// Deliver the ops to each other
for op in ops_a {
assert!(glist_b.validate_op(&op).is_ok());
glist_b.apply(op)
}
for op in ops_b {
assert!(glist_a.validate_op(&op).is_ok());
glist_a.apply(op)
}
assert_eq!(glist_a, glist_b);
}
#[quickcheck]
fn prop_ops_are_associative(ops_a: Vec<Op<u8>>, ops_b: Vec<Op<u8>>, ops_c: Vec<Op<u8>>) {
let mut glist_a = GList::new();
let mut glist_b = GList::new();
let mut glist_c = GList::new();
for op in ops_a.clone() {
assert!(glist_a.validate_op(&op).is_ok());
glist_a.apply(op);
}
for op in ops_b.clone() {
assert!(glist_b.validate_op(&op).is_ok());
glist_b.apply(op);
}
for op in ops_c.clone() {
assert!(glist_c.validate_op(&op).is_ok());
glist_c.apply(op);
}
// a * b
let mut glist_ab = glist_a;
for op in ops_b {
assert!(glist_ab.validate_op(&op).is_ok());
glist_ab.apply(op);
}
// b * c
let mut glist_bc = glist_b;
for op in ops_c.clone() {
assert!(glist_bc.validate_op(&op).is_ok());
glist_bc.apply(op);
}
// (a * b) * c
for op in ops_c {
assert!(glist_ab.validate_op(&op).is_ok());
glist_ab.apply(op)
}
// a * (b * c)
for op in ops_a {
assert!(glist_bc.validate_op(&op).is_ok());
glist_bc.apply(op)
}
assert_eq!(glist_ab, glist_bc);
}
#[quickcheck]
fn prop_merge_commute(ops_a: Vec<Op<u8>>, ops_b: Vec<Op<u8>>) {
let mut glist_a = GList::new();
let mut glist_b = GList::new();
for op in ops_a {
assert!(glist_a.validate_op(&op).is_ok());
glist_a.apply(op)
}
for op in ops_b {
assert!(glist_b.validate_op(&op).is_ok());
glist_b.apply(op)
}
let glist_a_snapshot = glist_a.clone();
glist_a.merge(glist_b.clone());
glist_b.merge(glist_a_snapshot);
assert_eq!(glist_a, glist_b);
}
#[quickcheck]
fn prop_merge_associative(ops_a: Vec<Op<u8>>, ops_b: Vec<Op<u8>>, ops_c: Vec<Op<u8>>) {
let mut glist_a = GList::new();
let mut glist_b = GList::new();
let mut glist_c = GList::new();
for op in ops_a {
assert!(glist_a.validate_op(&op).is_ok());
glist_a.apply(op)
}
for op in ops_b {
assert!(glist_b.validate_op(&op).is_ok());
glist_b.apply(op)
}
for op in ops_c {
assert!(glist_c.validate_op(&op).is_ok());
glist_c.apply(op)
}
// (a * b) * c
let mut glist_ab_first = glist_a.clone();
glist_ab_first.merge(glist_b.clone());
glist_ab_first.merge(glist_c.clone());
// a * (b * c)
let mut glist_bc_first = glist_b;
glist_bc_first.merge(glist_c);
glist_bc_first.merge(glist_a);
assert_eq!(glist_ab_first, glist_bc_first);
}
#[quickcheck]
fn prop_validate_against_vec_model(plan: Vec<(usize, u8, bool)>) {
let mut model: Vec<u8> = Default::default();
let mut glist: GList<u8> = Default::default();
for mut instruction in plan {
instruction.0 = if !glist.is_empty() {
instruction.0 % glist.len()
} else {
0
};
match instruction {
(index, elem, true) => {
// insert before
model.insert(index, elem);
let op = glist.insert_before(glist.get(index), elem);
glist.apply(op);
}
(index, elem, false) => {
// insert after
if index + 1 == model.len() || model.is_empty() {
model.push(elem)
} else {
model.insert(index + 1, elem);
}
let op = glist.insert_after(glist.get(index), elem);
glist.apply(op);
}
}
}
assert_eq!(model, glist.read_into::<Vec<u8>>());
}
|