Skip to content

Commit 359fcbb

Browse files
committed
itest: rust: src: Plane integration tests implementation
1 parent 9e6a8a0 commit 359fcbb

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

itest/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod node_test;
2727
mod object_test;
2828
mod option_ffi_test;
2929
mod packed_array_test;
30+
mod plane_test;
3031
mod projection_test;
3132
mod quaternion_test;
3233
mod rect2i_test;

itest/rust/src/plane_test.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
use std::fmt::Debug;
8+
9+
use crate::itest;
10+
use godot::prelude::{inner::InnerPlane, real, Plane, Vector3};
11+
12+
use godot::builtin::VariantConversionError::BadType;
13+
14+
#[itest]
15+
fn plane_equiv_unary() {
16+
let test_planes = [
17+
Plane::new(Vector3::UP.normalized(), 0.0),
18+
Plane::new(Vector3::BACK.normalized(), 0.0),
19+
Plane::new(Vector3::new(1.5, 3.0, 0.0).normalized(), 2.0),
20+
Plane::new(Vector3::new(0.5, 2.0, 2.5).normalized(), 1.0),
21+
Plane::new(Vector3::new(-3.0, 5.0, 0.1).normalized(), -0.2),
22+
Plane::new(Vector3::new(1.82, 5.32, -6.1).normalized(), 6.0),
23+
Plane::new(Vector3::new(1.82, 5.32, -6.000001).normalized(), 6.0),
24+
];
25+
let test_vectors = [
26+
Vector3::ZERO,
27+
Vector3::new(0.0, 3.0, 2.0),
28+
Vector3::new(6.1, 8.0, 9.0),
29+
Vector3::new(2.0, 1.0, -2.0),
30+
Vector3::new(5.2, 2.0, -1.0),
31+
Vector3::new(7.0, -3.0, -0.15),
32+
Vector3::new(0.0, 0.0, 0.0001),
33+
Vector3::new(0.0, 0.0, 0.000001),
34+
Vector3::new(real::INFINITY, 0.0, -9.0),
35+
];
36+
let test_reals = [1.0, 0.0005, 0.000005, 0.0000001, 0.0];
37+
let test_inf_planes = [
38+
Plane {
39+
normal: Vector3::new(real::INFINITY, 0.0, 0.0),
40+
d: 10.0,
41+
},
42+
Plane {
43+
normal: Vector3::new(real::NAN, real::INFINITY, real::NEG_INFINITY),
44+
d: real::NAN,
45+
},
46+
Plane {
47+
normal: Vector3::new(0.8, real::INFINITY, -1.2),
48+
d: 3.5,
49+
},
50+
];
51+
52+
fn check_mapping_eq<T>(context: &str, outer: T, inner: T)
53+
where
54+
T: PartialEq + Debug,
55+
{
56+
assert_eq!(
57+
outer, inner,
58+
"{context}: outer != inner ({outer:?} != {inner:?})"
59+
);
60+
}
61+
62+
for a in test_planes {
63+
let inner_a = InnerPlane::from_outer(&a);
64+
65+
check_mapping_eq("center", a.center(), inner_a.get_center());
66+
check_mapping_eq("is_finite", a.is_finite(), inner_a.is_finite());
67+
68+
for b in test_inf_planes {
69+
let inner_b = InnerPlane::from_outer(&b);
70+
check_mapping_eq("is_finite", b.is_finite(), inner_b.is_finite());
71+
}
72+
73+
check_mapping_eq("normalized", a.normalized(), inner_a.normalized());
74+
75+
for b in test_vectors {
76+
check_mapping_eq(
77+
"distance_to",
78+
a.distance_to(b) as f64,
79+
inner_a.distance_to(b),
80+
);
81+
check_mapping_eq(
82+
"is_point_over",
83+
a.is_point_over(b),
84+
inner_a.is_point_over(b),
85+
);
86+
check_mapping_eq("project", a.project(b), inner_a.project(b));
87+
for c in test_vectors {
88+
check_mapping_eq(
89+
"intersect_segment",
90+
a.intersect_segment(b, c).ok_or(BadType),
91+
inner_a.intersects_segment(b, c).try_to(),
92+
);
93+
check_mapping_eq(
94+
"intersect_ray",
95+
a.intersect_ray(b, c).ok_or(BadType),
96+
inner_a.intersects_ray(b, c).try_to(),
97+
);
98+
}
99+
for c in test_reals {
100+
check_mapping_eq(
101+
"contains_point",
102+
a.contains_point(b, Some(c)),
103+
inner_a.has_point(b, c.into()),
104+
);
105+
}
106+
}
107+
108+
for b in test_planes {
109+
check_mapping_eq(
110+
"is_equal_approx",
111+
a.is_equal_approx(&b),
112+
inner_a.is_equal_approx(b),
113+
);
114+
for c in test_planes {
115+
check_mapping_eq(
116+
"intersect_3",
117+
a.intersect_3(&b, &c).ok_or(BadType),
118+
inner_a.intersect_3(b, c).try_to(),
119+
);
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)