Skip to content

Commit d1004e6

Browse files
authored
added unit tests for functions in geometry.rs (#15)
* added unit tests for functions in geometry.rs * formatting * some fixes
1 parent 4cb85b3 commit d1004e6

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

src/geometry.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,61 @@ pub fn are_lines_intersecting(
9494
// p2, q2 and q1 are collinear and q1 lies on segment p2q2
9595
|| ((o4 == 0) && is_point_in_box(p2, q1, q2))
9696
}
97+
98+
#[cfg(test)]
99+
mod tests {
100+
use super::*;
101+
102+
#[test]
103+
fn test_are_polygons_intersecting() {
104+
let mut poly_a: Vec<(f32, f32)> = vec![(0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0)];
105+
let mut poly_b: Vec<(f32, f32)> =
106+
vec![(25.0, 25.0), (25.0, 75.0), (75.0, 75.0), (75.0, 25.0)];
107+
let mut result = are_polygons_intersecting(poly_a, poly_b);
108+
assert!(result == true);
109+
110+
poly_a = vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)];
111+
poly_b = vec![(5.0, 5.0), (6.0, 5.0), (6.0, 6.0), (5.0, 6.0)];
112+
result = are_polygons_intersecting(poly_a, poly_b);
113+
assert!(result == false);
114+
}
115+
116+
#[test]
117+
fn test_is_point_in_box() {
118+
// point insde
119+
let mut result = is_point_in_box((0.0, 0.0), (50.0, 50.0), (100.0, 100.0));
120+
assert!(result == true);
121+
122+
//point outside
123+
result = is_point_in_box((0.0, 0.0), (-1.0, -1.0), (100.0, 100.0));
124+
assert!(result == false);
125+
}
126+
127+
#[test]
128+
fn test_get_triangle_orientation() {
129+
// collinear
130+
let mut result = get_triangle_orientation((0.0, 0.0), (0.0, 1.0), (0.0, 2.0));
131+
assert!(result == 0);
132+
133+
// clockwise
134+
result = get_triangle_orientation((0.0, 0.0), (0.0, 1.0), (1.0, 1.0));
135+
assert!(result == 1);
136+
137+
// anticlockwise
138+
result = get_triangle_orientation((1.0, 1.0), (0.0, 1.0), (0.0, 0.0));
139+
assert!(result == 2);
140+
}
141+
142+
#[test]
143+
fn test_are_lines_intersecting() {
144+
let mut result = are_lines_intersecting((0.0, 0.0), (1.0, 1.0), (0.0, 0.0), (1.0, 1.0));
145+
assert!(result == true);
146+
147+
result = are_lines_intersecting((0.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 1.0));
148+
assert!(result == true);
149+
150+
// parallel lines
151+
result = are_lines_intersecting((0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0));
152+
assert!(result == false);
153+
}
154+
}

0 commit comments

Comments
 (0)