correct tests
This commit is contained in:
parent
14ab9bf469
commit
95161692f4
@ -1,6 +1,6 @@
|
|||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use crdts::{CmRDT, List};
|
|
||||||
use crdts::list::Op;
|
use crdts::list::Op;
|
||||||
|
use crdts::{CmRDT, List};
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Text {
|
pub struct Text {
|
||||||
@ -9,9 +9,7 @@ pub struct Text {
|
|||||||
|
|
||||||
impl Default for Text {
|
impl Default for Text {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self { inner: List::new() }
|
||||||
inner: List::new()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,14 +23,26 @@ impl Display for Text {
|
|||||||
|
|
||||||
impl Text {
|
impl Text {
|
||||||
pub fn apply_ops(&mut self, ops: Vec<Op<u16, String>>) {
|
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)
|
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();
|
let mut ops: Vec<Op<u16, String>> = Vec::new();
|
||||||
|
|
||||||
if start < end {
|
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>> {
|
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)
|
self.delete(start, end, src)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_forward(&mut self, start: usize, end: usize, src: &str) -> Vec<Op<u16, String>> {
|
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 };
|
let end = if start == end { start + 1 } else { end };
|
||||||
self.delete(start, end, src)
|
self.delete(start, end, src)
|
||||||
@ -104,7 +122,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn insert_delete() {
|
fn insert_delete() {
|
||||||
let mut t = Text::new();
|
let mut t = Text::default();
|
||||||
t.insert(0, 0, "Hello", "A");
|
t.insert(0, 0, "Hello", "A");
|
||||||
t.insert(5, 5, "world!", "A");
|
t.insert(5, 5, "world!", "A");
|
||||||
t.insert(5, 5, ", ", "B");
|
t.insert(5, 5, ", ", "B");
|
||||||
@ -121,7 +139,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn backspace() {
|
fn backspace() {
|
||||||
let mut t = Text::new();
|
let mut t = Text::default();
|
||||||
t.insert(0, 0, "Hello", "A");
|
t.insert(0, 0, "Hello", "A");
|
||||||
|
|
||||||
t.delete_backward(5, 5, "A");
|
t.delete_backward(5, 5, "A");
|
||||||
@ -133,7 +151,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delete() {
|
fn delete() {
|
||||||
let mut t = Text::new();
|
let mut t = Text::default();
|
||||||
t.insert(0, 0, "Hello", "A");
|
t.insert(0, 0, "Hello", "A");
|
||||||
|
|
||||||
t.delete_forward(4, 4, "A");
|
t.delete_forward(4, 4, "A");
|
||||||
@ -145,7 +163,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn linebreak() {
|
fn linebreak() {
|
||||||
let mut t = Text::new();
|
let mut t = Text::default();
|
||||||
t.insert(0, 0, "Hello, world!", "A");
|
t.insert(0, 0, "Hello, world!", "A");
|
||||||
t.insert_linebreak(6, 6, "A");
|
t.insert_linebreak(6, 6, "A");
|
||||||
|
|
||||||
@ -159,21 +177,20 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn apply_ops() {
|
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 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);
|
t_b.apply_ops(ops_a);
|
||||||
|
|
||||||
assert_eq!(t_b.to_string(), "Hello, world!");
|
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);
|
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);
|
t_a.apply_ops(ops_b);
|
||||||
|
|
||||||
|
|
||||||
assert_eq!(t_a.to_string(), "Hello, cruel distributed world!");
|
assert_eq!(t_a.to_string(), "Hello, cruel distributed world!");
|
||||||
assert_eq!(t_b.to_string(), "Hello, cruel distributed world!");
|
assert_eq!(t_b.to_string(), "Hello, cruel distributed world!");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user