simple tests

This commit is contained in:
Niko Abeler 2022-11-20 20:39:36 +01:00
parent c3cc7f690e
commit e9f90cbb69
2 changed files with 95 additions and 0 deletions

View File

@ -96,3 +96,54 @@ fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>
}
matrix_ptr
}
#[cfg(test)]
mod test {
use crate::graph::{Node, NodeVector};
use super::*;
struct MockModel {
counter: usize,
}
impl ForceModel for MockModel {
fn init(&mut self, _edges: EdgeMatrix, _size: usize, _iterations: usize) {}
fn prepare(&mut self, _nodes: &NodeVector) {
self.counter += 1;
}
fn step(&self, _nodes: &NodeVector, i_node: usize) -> Node {
Node {
x: i_node as f32,
y: self.counter as f32,
}
}
}
#[test]
fn test_edge_matrix_from_edge_list() {
let edge_list = vec![(0, 1), (1, 2)];
let matrix = edge_matrix_from_edge_list(3, edge_list);
let matrix = matrix.read().unwrap();
assert_eq!(matrix[0][1].weight, 1.0);
assert_eq!(matrix[1][0].weight, 1.0);
assert_eq!(matrix[1][2].weight, 1.0);
assert_eq!(matrix[2][1].weight, 1.0);
assert_eq!(matrix[0][0].weight, 0.0);
assert_eq!(matrix[1][1].weight, 0.0);
assert_eq!(matrix[2][2].weight, 0.0);
assert_eq!(matrix[0][2].weight, 0.0);
assert_eq!(matrix[2][0].weight, 0.0);
}
#[test]
fn test_layout() {
let edge_list = vec![];
let model = Arc::new(RwLock::new(MockModel { counter: 0 }));
let runner = Runner::new(10, 1);
let result = runner.layout(3, edge_list, model);
assert_eq!(result, vec![(0.0, 10.0), (1.0, 10.0), (2.0, 10.0)]);
}
}

View File

@ -89,3 +89,47 @@ impl ForceModel for SimpleSpringModel {
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::graph::{new_edge_matrix, new_node_vector};
#[test]
fn test_simple_spring_model_attraction() {
let mut model = SimpleSpringModel::new(1.0);
let edges = new_edge_matrix(2);
edges.write().unwrap()[0][1].weight = 1.0;
edges.write().unwrap()[1][0].weight = 1.0;
model.init(edges, 2, 1);
let nodes = new_node_vector(2);
nodes[0].write().unwrap().x = 0.0;
nodes[0].write().unwrap().y = 0.0;
nodes[1].write().unwrap().x = 1.0;
nodes[1].write().unwrap().y = 1.0;
model.prepare(&nodes);
let node = model.step(&nodes, 0);
assert!(node.x > 0.0);
assert!(node.y > 0.0);
}
#[test]
fn test_simple_spring_model_repulsion() {
let mut model = SimpleSpringModel::new(1.0);
let edges = new_edge_matrix(2);
model.init(edges, 2, 1);
let nodes = new_node_vector(2);
nodes[0].write().unwrap().x = 0.0;
nodes[0].write().unwrap().y = 0.0;
nodes[1].write().unwrap().x = 1.0;
nodes[1].write().unwrap().y = 1.0;
model.prepare(&nodes);
let node = model.step(&nodes, 0);
assert!(node.x < 0.0);
assert!(node.y < 0.0);
}
}