thesis-code/src/test.rs

177 lines
4.1 KiB
Rust
Raw Normal View History

use super::tree::*;
use super::parser;
2025-09-23 18:23:20 -04:00
use assert_approx_eq::assert_approx_eq;
2025-09-23 18:14:10 -04:00
// helper methods for testing parser, return constant heap-allocated value
2025-09-23 18:23:20 -04:00
fn simple_symmetric_tree() -> Tree<String, f64> {
let a = Alternative {
name: "A".to_owned()
};
let b = Alternative {
name: "B".to_owned()
};
let edge_a = Edge {
weight: 1.0,
destination: Box::new(Tree {root: Vertex::Terminal(a)})
2025-09-23 18:23:20 -04:00
};
let edge_b = Edge {
weight: 1.0,
destination: Box::new(Tree {root: Vertex::Terminal(b)})
2025-09-23 18:23:20 -04:00
};
let root = Vertex::NonTerminal(edge_a, edge_b);
2025-09-23 18:23:20 -04:00
Tree {root: root}
}
2025-09-23 18:14:10 -04:00
// helper methods for testing parser, return constant heap-allocated value
2025-09-23 18:23:20 -04:00
fn simple_asymmetric_tree() -> Tree<String, f64> {
let a = Alternative {
name: "A".to_owned()
};
let b = Alternative {
name: "B".to_owned()
};
let edge_a = Edge {
weight: 3.0,
destination: Box::new(Tree {root: Vertex::Terminal(a)})
2025-09-23 18:23:20 -04:00
};
let edge_b = Edge {
weight: 1.0,
destination: Box::new(Tree {root: Vertex::Terminal(b)})
2025-09-23 18:23:20 -04:00
};
let root = Vertex::NonTerminal(edge_a, edge_b);
2025-09-23 18:23:20 -04:00
Tree {root: root}
}
2025-09-23 18:14:10 -04:00
// helper methods for testing parser, return constant heap-allocated value
2025-09-23 18:23:20 -04:00
fn complex_tree() -> Tree<String, f64> {
let a = Alternative {
name: "A".to_owned()
};
let b = Alternative {
name: "B".to_owned()
};
let c = Alternative {
name: "C".to_owned()
};
let edge_a = Edge {
weight: 2.5,
destination: Box::new(Tree {root: Vertex::Terminal(a)})
2025-09-23 18:23:20 -04:00
};
let edge_b = Edge {
weight: 1.0,
destination: Box::new(Tree {root: Vertex::Terminal(b)})
2025-09-23 18:23:20 -04:00
};
let edge_ab = Edge{
weight: 0.5,
destination: Box::new(Tree {
root: Vertex::NonTerminal(edge_a, edge_b)
})
2025-09-23 18:23:20 -04:00
};
let edge_c = Edge{
weight: 1.0,
destination: Box::new(Tree {root: Vertex::Terminal(c)})
2025-09-23 18:23:20 -04:00
};
let root = Vertex::NonTerminal(edge_ab, edge_c);
2025-09-23 18:23:20 -04:00
Tree {root: root}
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
// A test for the simplest symmetric case
#[test]
fn choice_probability_test_1() {
assert_eq!(simple_symmetric_tree().choice_probability(&("A".to_owned())), 0.5);
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
// A test for the simplest asymmetric case
#[test]
fn choice_probability_test_2() {
assert_eq!(simple_asymmetric_tree().choice_probability(&("A".to_owned())), 0.75);
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
// A test for depth higher than 1
#[test]
fn choice_probability_test_3() {
assert_approx_eq!(
complex_tree().choice_probability(&("A".to_owned())),
(2.5/(2.5+1.0))*((2.5+1.0+0.5)/(2.5+1.0+0.5+1.0))
);
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
// A test for parsing the simplest symmetric case
#[test]
fn parser_test_1() {
assert_eq!(
{
let (_, b) = parser::tree("([1.0]A[1.0]B)").unwrap();
2025-09-23 18:23:20 -04:00
b
},
simple_symmetric_tree()
)
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
#[test]
fn parser_test_2() {
assert_eq!(
{
let (_, b) = parser::tree("([3.0]A[1.0]B)").unwrap();
2025-09-23 18:23:20 -04:00
b
},
simple_asymmetric_tree()
)
}
2025-09-23 18:14:10 -04:00
2025-09-23 18:23:20 -04:00
#[test]
fn parser_test_3() {
assert_eq!(
{
let (_, b) = parser::tree("([0.5]([2.5]A[1.0]B)[1.0]C)").unwrap();
2025-09-23 18:23:20 -04:00
b
},
complex_tree()
)
}
#[test]
fn test_symmetry_positive() {
let (_, a) = parser::tree("([3.0]A[1.0]B)").unwrap();
let (_, b) = parser::tree("([1.0]B[3.0]A)").unwrap();
assert_eq!(a, b)
}
#[test]
fn test_symmetry_negative() {
let (_, a) = parser::tree("([3.0]A[1.0]B)").unwrap();
let (_, b) = parser::tree("([1.0]A[3.0]B)").unwrap();
assert_ne!(a, b)
}
2025-09-24 13:31:16 -04:00
#[test]
fn test_symmetry_complex() {
let (_, a) = parser::tree("([1.0]C[0.5]([1.0]B[2.5]A))").unwrap();
let (_, b) = parser::tree("([0.5]([2.5]A[1.0]B)[1.0]C)").unwrap();
2025-09-24 13:31:16 -04:00
assert_eq!(a, b)
}
#[test]
fn test_menu_simple() {
let (_, a) = parser::tree("([3.0]A[1.0]B)").unwrap();
let (_, b) = parser::tree("A").unwrap();
let Some((a_menu_tree, _)) = a.menu_tree(&["A".to_owned()]) else {
panic!("Failed to get tree from menu_tree output")
};
assert_eq!(a_menu_tree, b)
}
#[test]
fn test_menu_complex() {
let (_, a) = parser::tree("([1.0]C[0.5]([1.0]B[2.5]A))").unwrap();
let (_, b) = parser::tree("([1.0]C[1.5]B)").unwrap();
let Some((a_menu_tree, _)) = a.menu_tree(&[
"B".to_owned(), "C".to_owned()
]) else {
panic!("Failed to get tree from menu_tree output")
};
assert_eq!(a_menu_tree, b)
}