Files
craft-libs/crates/mesh/src/tests.rs
T
aiongg 577055bc34 Merge storytold main 3c59a2aa5 into craft-framework (sync 13 Nov)
202 upstream commits since 6cb00467a. Resolutions:
- craft-window, craft-platform: ours. Upstream's frame policy v2, restore
  store and GNOME light/wait_first are the versions carried on 28 Oct;
  ours moved place() to craft-behavior and kept LayoutWatch and the
  appearance/portal.rs split.
- craft-timeline: ours. craft-time (crates/time) lands as upstream wrote
  it and is not yet used by craft-timeline.
- Old crates/ui modules stay removed. Upstream's frosted backdrops, shape
  gallery, dialog tabs and the iconic flyout top-edge rendering are not
  carried. The #1094 model half is in craft-behavior's dock:
  Group::flyout_offset and Action::MoveFlyoutTop.
- craft-update: #586 Save and Update, the install probe on the check
  thread and quiet per-user MSI self-updates, carried into updater.rs,
  stack_dialogs.rs, install/windows.rs and self_update/helper.rs.
- craft-jpeg: progressive Huffman decoding, carried into huffman.rs,
  parse.rs and scan.rs.
- craft-print: the driver settings dialog's new tests and the bounded
  XML parse's tests, as trials.
- Test-only conflicts: upstream tests from before the base that our side
  cut stay cut; tests new upstream are kept.
2026-11-13 17:11:48 +00:00

123 lines
4.2 KiB
Rust

//! Tests. Every sample is generated here; the crate ships no asset files.
use super::*;
#[cfg(feature = "fbx")]
mod fbx;
#[cfg(feature = "gltf")]
mod gltf;
#[cfg(feature = "obj")]
mod obj;
#[cfg(feature = "ply")]
mod ply;
#[cfg(feature = "stl")]
mod stl;
#[cfg(feature = "threemf")]
mod threemf;
/// Every truncation of `good` and two mutations of every byte go through `read`: it may refuse
/// them, it must not panic, and what it accepts must be a valid scene.
pub(crate) fn hammer(good: &[u8], read: &dyn Fn(&[u8]) -> Result<Scene>) {
let check = |bytes: &[u8]| {
if let Ok(scene) = read(bytes) {
scene.validate().unwrap();
}
};
for len in 0..good.len() {
check(&good[..len]);
}
for i in 0..good.len() {
let mut b = good.to_vec();
b[i] ^= 0xA5;
check(&b);
b[i] = 0xFF;
check(&b);
}
check(b"\x00\x01garbage\xff\xfe");
}
pub(crate) fn tri_mesh() -> Mesh {
Mesh {
name: "Tri".into(),
positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
parts: vec![Part { corners: [0, 1, 2, 0, 3, 1].map(Corner::at).to_vec(), ..Part::default() }],
..Mesh::default()
}
}
#[test]
fn validate_catches_what_writers_cannot_take() {
let mut m = tri_mesh();
m.validate().unwrap();
m.parts[0].corners[0].position = 4;
assert!(m.validate().is_err());
let mut m = tri_mesh();
m.parts[0].corners[0].normal = 0;
assert!(m.validate().is_err(), "a normal index without normals");
let mut m = tri_mesh();
m.parts[0].face_sizes = vec![4, 3];
assert!(m.validate().is_err());
let mut m = tri_mesh();
m.colors = vec![[1.0; 4]];
assert!(m.validate().is_err());
let mut m = tri_mesh();
m.parts[0].has_tangents = true;
assert!(m.validate().is_err(), "a part names tangents without a per-position array");
let mut s = Scene::from_meshes(vec![tri_mesh()], vec![]);
s.validate().unwrap();
s.meshes[0].parts[0].material = Some(0);
assert!(s.validate().is_err());
let mut s = Scene::default();
s.texture_variants.push(TextureRef::default());
assert!(s.validate().is_err(), "a texture references a missing image");
s.images.push(Image::default());
s.validate().unwrap();
let mut s = Scene::from_meshes(vec![tri_mesh()], vec![]);
s.nodes[0].children.push(7);
assert!(s.validate().is_err());
}
#[test]
fn unified_gives_one_vertex_per_distinct_corner() {
// Two triangles sharing an edge; the shared positions carry different normals.
let mut m = tri_mesh();
m.normals = vec![[0.0, 0.0, 1.0], [0.0, -1.0, 0.0]];
m.colors = vec![[1.0, 0.0, 0.0, 1.0]; 4];
for (i, c) in m.parts[0].corners.iter_mut().enumerate() {
c.normal = (i / 3) as u32;
}
let u = m.unified();
assert_eq!(u.positions.len(), 6);
assert_eq!(u.normals.len(), 6);
assert_eq!(u.colors.len(), 6);
assert!(u.uvs.is_empty());
assert_eq!(u.corners.iter().map(|c| c.position).collect::<Vec<_>>(), [0, 1, 2, 0, 3, 1]);
assert_eq!(u.parts, [vec![[0, 1, 2], [3, 4, 5]]]);
// Without normals the shared positions are shared vertices.
let u = tri_mesh().unified();
assert_eq!((u.positions.len(), u.normals.len()), (4, 0));
assert_eq!(u.parts, [vec![[0, 1, 2], [0, 3, 1]]]);
// A corner out of range drops its triangle, not the process.
let mut bad = tri_mesh();
bad.parts[0].corners[4].position = 99;
assert_eq!(bad.unified().parts, [vec![[0, 1, 2]]]);
}
#[test]
fn transforms() {
let mut t = IDENTITY;
t[12] = 5.0;
let mut s = IDENTITY;
s[0] = 2.0;
// Scale first, then move.
assert_eq!(transform_point(&mat_mul(&t, &s), [1.0, 1.0, 1.0]), [7.0, 1.0, 1.0]);
// A non-uniform scale tilts a normal the other way.
let n = transform_normal(&s, [1.0, 1.0, 0.0]);
assert!((n[0] * 2.0 - n[1]).abs() < 1e-12 && n[1] > 0.0, "{n:?}");
// A mirror keeps the normal pointing out of the mirrored surface.
let mut mirror = IDENTITY;
mirror[0] = -1.0;
assert_eq!(transform_normal(&mirror, [1.0, 0.0, 0.0]).map(|x| x.round()), [-1.0, 0.0, 0.0]);
assert_eq!(transform_normal(&[0.0; 16], [0.0, 0.0, 1.0]), [0.0, 0.0, 1.0]);
}