correct tests

This commit is contained in:
Sebastian Hugentobler 2023-03-14 13:11:19 +01:00
parent 14ab9bf469
commit 95161692f4
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0

View File

@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter};
use crdts::{CmRDT, List};
use crdts::list::Op;
use crdts::{CmRDT, List};
use std::fmt::{Display, Formatter};
#[derive(Clone)]
pub struct Text {
@ -9,9 +9,7 @@ pub struct Text {
impl Default for Text {
fn default() -> Self {
Self {
inner: List::new()
}
Self { inner: List::new() }
}
}
@ -25,14 +23,26 @@ impl Display for Text {
impl Text {
pub fn apply_ops(&mut self, ops: Vec<Op<u16, String>>) {
ops.iter().for_each(move |op| self.inner.apply(op.to_owned()));
ops.iter()
.for_each(move |op| self.inner.apply(op.to_owned()));
}
pub fn insert_linebreak(&mut self, start: usize, end: usize, src: &str) -> Vec<Op<u16, String>> {
pub fn insert_linebreak(
&mut self,
start: usize,
end: usize,
src: &str,
) -> Vec<Op<u16, String>> {
self.insert(start, end, "\n", src)
}
pub fn insert(&mut self, start: usize, end: usize, data: &str, src: &str) -> Vec<Op<u16, String>> {
pub fn insert(
&mut self,
start: usize,
end: usize,
data: &str,
src: &str,
) -> Vec<Op<u16, String>> {
let mut ops: Vec<Op<u16, String>> = Vec::new();
if start < end {
@ -55,14 +65,22 @@ impl Text {
}
pub fn delete_backward(&mut self, start: usize, end: usize, src: &str) -> Vec<Op<u16, String>> {
if (start == 0 && start == end) || end > self.inner.len() { return Vec::new(); }
if (start == 0 && start == end) || end > self.inner.len() {
return Vec::new();
}
let (end, start) = if start == end { (start, start - 1) } else { (end, start) };
let (end, start) = if start == end {
(start, start - 1)
} else {
(end, start)
};
self.delete(start, end, src)
}
pub fn delete_forward(&mut self, start: usize, end: usize, src: &str) -> Vec<Op<u16, String>> {
if start >= self.inner.len() { return Vec::new(); }
if start >= self.inner.len() {
return Vec::new();
}
let end = if start == end { start + 1 } else { end };
self.delete(start, end, src)
@ -104,7 +122,7 @@ mod tests {
#[test]
fn insert_delete() {
let mut t = Text::new();
let mut t = Text::default();
t.insert(0, 0, "Hello", "A");
t.insert(5, 5, "world!", "A");
t.insert(5, 5, ", ", "B");
@ -121,7 +139,7 @@ mod tests {
#[test]
fn backspace() {
let mut t = Text::new();
let mut t = Text::default();
t.insert(0, 0, "Hello", "A");
t.delete_backward(5, 5, "A");
@ -133,7 +151,7 @@ mod tests {
#[test]
fn delete() {
let mut t = Text::new();
let mut t = Text::default();
t.insert(0, 0, "Hello", "A");
t.delete_forward(4, 4, "A");
@ -145,7 +163,7 @@ mod tests {
#[test]
fn linebreak() {
let mut t = Text::new();
let mut t = Text::default();
t.insert(0, 0, "Hello, world!", "A");
t.insert_linebreak(6, 6, "A");
@ -159,21 +177,20 @@ mod tests {
#[test]
fn apply_ops() {
let mut t_a = Text::new();
let mut t_a = Text::default();
let ops_a = t_a.insert(0, 0, "Hello, world!", "A");
let mut t_b = Text::new();
let mut t_b = Text::default();
t_b.apply_ops(ops_a);
assert_eq!(t_b.to_string(), "Hello, world!");
let ops_a = t_a.insert(7,7, "distributed ", "A");
let ops_a = t_a.insert(7, 7, "distributed ", "A");
t_b.apply_ops(ops_a);
let ops_b = t_b.insert(7,7, "cruel ", "B");
let ops_b = t_b.insert(7, 7, "cruel ", "B");
t_a.apply_ops(ops_b);
assert_eq!(t_a.to_string(), "Hello, cruel distributed world!");
assert_eq!(t_b.to_string(), "Hello, cruel distributed world!");
}