use pyrucast::aggregate::Aggregate;
use pyrucast::archive::Portable;
use pyrucast::atoms::element_type::ElementType;
use pyrucast::atoms::node::Node;
use pyrucast::atoms::NodeId;
use pyrucast::containers::mesh::{Mesh, SubMesh};
use pyrucast::coords::Coords;
use pyrucast::handle::Handle;
use pyrucast::{PyrucastError, Result};
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Debug)]
struct Point {
x: f64,
y: f64,
}
#[test]
fn persist_roundtrip_via_public_api() -> Result<()> {
let a = Point { x: 1.5, y: -2.0 };
let bytes = a.to_bytes()?;
let b = Point::from_bytes(&bytes)?;
assert_eq!(a, b);
Ok(())
}
#[test]
fn human_display_for_error() {
let e = PyrucastError::Message("test".into());
assert_eq!(e.to_string(), "test");
let e = PyrucastError::MeshSealed;
assert!(e.to_string().contains("sealed"));
}
#[test]
fn version_exposed() {
assert_eq!(pyrucast::VERSION, env!("CARGO_PKG_VERSION"));
}
#[derive(Debug)]
struct IntegrationSample {
values: Vec<f64>,
name: String,
}
#[test]
fn full_handle_cycle_via_public_api() {
let h = Handle::new(IntegrationSample {
values: vec![1.0, 2.0, 3.0],
name: "alpha".into(),
});
{
let e = h.read();
assert_eq!(e.values, vec![1.0, 2.0, 3.0]);
assert_eq!(e.name, "alpha");
}
h.write().values.push(4.0);
assert_eq!(h.read().values.len(), 4);
let g = h.clone();
assert!(g.same_object(&h));
drop(h);
assert_eq!(g.read().name, "alpha");
}
#[test]
fn coords_cycle_via_handle() -> Result<()> {
let h = Handle::new(Coords::new(2)?);
let a: NodeId = h.write().add_node(&[0.0, 0.0])?;
let b: NodeId = h.write().add_node(&[1.0, 0.0])?;
{
let c = h.read();
assert_eq!(c.node_count(), 2);
assert_eq!((c.refcount(a), c.refcount(b)), (1, 1));
}
{
let mut c = h.write();
c.decref(a).unwrap();
c.decref(b).unwrap();
}
{
let c = h.read();
assert_eq!((c.refcount(a), c.refcount(b)), (0, 0));
assert_eq!(c.node_count(), 2);
}
Ok(())
}
#[test]
fn node_holds_a_unit_of_its_count() -> Result<()> {
let h = Handle::new(Coords::new(2)?);
let n = Node::create_in(h.clone(), &[3.0, 4.0])?;
let id = n.id();
assert_eq!(n.position()?, vec![3.0, 4.0]);
let m = n.clone();
assert_eq!(h.read().refcount(id), 2);
drop(n);
assert_eq!(h.read().refcount(id), 1);
drop(m);
assert_eq!(h.read().refcount(id), 0);
Ok(())
}
#[test]
fn submesh_counts_its_nodes() -> Result<()> {
let coords = Handle::new(Coords::new(2)?);
let a = Node::create_in(coords.clone(), &[0.0, 0.0])?;
let b = Node::create_in(coords.clone(), &[1.0, 0.0])?;
let cc = Node::create_in(coords.clone(), &[0.5, 1.0])?;
let sm_handle = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), cc.id()])?;
Handle::new(sm)
};
{
let c = coords.read();
assert_eq!(c.refcount(a.id()), 2);
assert_eq!(c.refcount(b.id()), 2);
assert_eq!(c.refcount(cc.id()), 2);
}
let (ida, idb, idc) = (a.id(), b.id(), cc.id());
drop(a);
drop(b);
drop(cc);
{
let c = coords.read();
assert_eq!(c.refcount(ida), 1);
assert_eq!(c.refcount(idb), 1);
assert_eq!(c.refcount(idc), 1);
}
drop(sm_handle);
{
let c = coords.read();
assert_eq!(
(c.refcount(ida), c.refcount(idb), c.refcount(idc)),
(0, 0, 0)
);
}
Ok(())
}
#[test]
fn triangulate_surface_from_circle_contour() -> Result<()> {
let coords = Handle::new(Coords::new(2)?);
let center = Node::create_in(coords.clone(), &[0.0, 0.0])?;
let circle = pyrucast::ops::mesh::circle(¢er, &[0.0, 0.0, 1.0], 1.0, 8, ElementType::SEG2)?;
let tri = pyrucast::ops::mesh::triangulate_surface(&circle, ElementType::TRI3, Some(0.25))?;
assert_eq!(tri.element_types()?, vec![ElementType::TRI3]);
assert!(tri.cell_count() > 6, "expected interior nodes to be added");
let mut total = 0.0;
for ci in 0..tri.cell_count() {
let p0 = tri.node(0, ci, 0)?.position()?;
let p1 = tri.node(0, ci, 1)?.position()?;
let p2 = tri.node(0, ci, 2)?.position()?;
let area = 0.5 * ((p1[0] - p0[0]) * (p2[1] - p0[1]) - (p1[1] - p0[1]) * (p2[0] - p0[0]));
assert!(area > 0.0, "triangle {} not CCW", ci);
total += area;
}
let expected = 2.0 * std::f64::consts::SQRT_2;
assert!(
(total - expected).abs() < 1e-9,
"total area {} ≠ inscribed octagon area {}",
total,
expected
);
Ok(())
}
#[test]
fn mesh_composed_of_multiple_submeshes() -> Result<()> {
let coords = Handle::new(Coords::new(2)?);
let a = Node::create_in(coords.clone(), &[0.0, 0.0])?;
let b = Node::create_in(coords.clone(), &[1.0, 0.0])?;
let cc = Node::create_in(coords.clone(), &[0.5, 1.0])?;
let sm_pts = {
let mut sm = SubMesh::new(coords.clone(), ElementType::POI1);
sm.add_cell(&[a.id()])?;
sm.add_cell(&[b.id()])?;
sm.add_cell(&[cc.id()])?;
Handle::new(sm)
};
let sm_tri = {
let mut sm = SubMesh::new(coords.clone(), ElementType::TRI3);
sm.add_cell(&[a.id(), b.id(), cc.id()])?;
Handle::new(sm)
};
let mesh = {
let mut mesh = Mesh::empty();
mesh.add_sub(sm_pts)?;
mesh.add_sub(sm_tri)?;
mesh
};
assert_eq!(mesh.len(), 2);
let total = mesh.cell_count();
assert_eq!(total, 4); Ok(())
}