Commit 0fc1058b authored by SirBlueRabbit's avatar SirBlueRabbit
Browse files

add random test

parent 007bf628
use std::{
fmt::Debug,
fmt,
ops::{Index, IndexMut},
};
use super::collections::{Label, Pool};
#[derive(Debug, PartialEq)]
enum Parent<V> {
Path(Label<Node<V>>),
BinTree(Label<Node<V>>),
......@@ -20,6 +19,44 @@ impl<V> Clone for Parent<V> {
}
}
impl<V> fmt::Debug for Parent<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Parent::Path(label) => f
.debug_struct("Parent::Path")
.field("label", label)
.finish(),
Parent::BinTree(label) => f
.debug_struct("Parent::BinTree")
.field("label", label)
.finish(),
Parent::Root => f.debug_struct("Parent::Root").finish(),
}
}
}
impl<V> PartialEq for Parent<V> {
fn eq(&self, other: &Self) -> bool {
match self {
Parent::Path(label1) => {
if let Parent::Path(label2) = other {
label1 == label2
} else {
false
}
}
Parent::BinTree(label1) => {
if let Parent::BinTree(label2) = other {
label1 == label2
} else {
false
}
}
Parent::Root => matches!(other, Parent::Root),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct Children<V>(Option<Label<Node<V>>>, Option<Label<Node<V>>>);
......@@ -553,4 +590,54 @@ mod tests {
let seq2 = lct.sequence(o);
assert_eq!(seq1, seq2);
}
#[test]
fn random_scan() {
// generate a basic lct
let size = 10;
let mut lct = Pool::with_capacity(size);
for i in 0..size {
let _ = lct.add_node(i);
}
for i in 0..(size - 1) {
let parent = Label::from(i);
let child = Label::from(i + 1);
lct.link(parent, child);
}
// randomly mutate lct and check integrity
for _ in 0..(size * size * size) {
let label = Label::from(fastrand::usize(0..size));
let step = fastrand::usize(0..4);
if step == 0 {
lct.evert(label);
for i in 0..size {
let u = Label::from(i);
assert_eq!(lct.find_root(u), label);
}
}
assert_clean(&lct);
}
}
fn assert_clean<V>(lct: &Pool<Node<V>>) {
for label in lct.labels() {
// check parent reciprocrity
match lct[label].parent {
Parent::Root => (),
Parent::Path(_) => (),
Parent::BinTree(p) => {
assert!(lct[p].children.0 == Some(label) || lct[p].children.1 == Some(label))
}
}
// check child reciprocrity
if let Some(c) = lct[label].children.0 {
assert_eq!(lct[c].parent, Parent::BinTree(label));
}
if let Some(c) = lct[label].children.1 {
assert_eq!(lct[c].parent, Parent::BinTree(label));
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment