fail_safe_boundaries/
fail_safe_boundaries.rs1use ph_surfaces::{BilinearSurface, Boundary, BoundaryPolicy, SurfaceError};
14
15static X: [u16; 2] = [100, 200];
16static Y: [u16; 2] = [10, 30];
17static VALUES: [[i32; 2]; 2] = [
18 [0, 100], [40, 180], ];
21
22static STRICT: BilinearSurface<2, 2> = BilinearSurface::new(&X, &Y, &VALUES);
23
24static FAIL_SAFE: BilinearSurface<2, 2> = BilinearSurface::new(&X, &Y, &VALUES).with_policy(
25 BoundaryPolicy::new()
26 .with_x_below(Boundary::Error)
27 .with_x_above(Boundary::Clamp)
28 .with_y_below(Boundary::Error)
29 .with_y_above(Boundary::Clamp),
30);
31
32fn main() {
33 assert_eq!(FAIL_SAFE.policy().x_below(), Boundary::Error);
34 assert_eq!(FAIL_SAFE.policy().x_above(), Boundary::Clamp);
35 assert_eq!(FAIL_SAFE.policy().y_below(), Boundary::Error);
36 assert_eq!(FAIL_SAFE.policy().y_above(), Boundary::Clamp);
37
38 assert_eq!(FAIL_SAFE.evaluate(125, 20), Ok(50));
40 assert_eq!(FAIL_SAFE.evaluate(125, 20), STRICT.evaluate(125, 20));
41
42 assert_eq!(
44 FAIL_SAFE.evaluate(0, 20),
45 Err(SurfaceError::XBelow {
46 coordinate: 0,
47 bound: 100
48 })
49 );
50 assert_eq!(
51 FAIL_SAFE.evaluate(125, 0),
52 Err(SurfaceError::YBelow {
53 coordinate: 0,
54 bound: 10
55 })
56 );
57
58 assert_eq!(FAIL_SAFE.evaluate(4_000, 20), FAIL_SAFE.evaluate(200, 20));
60 assert_eq!(FAIL_SAFE.evaluate(125, 4_000), FAIL_SAFE.evaluate(125, 30));
61 assert_eq!(FAIL_SAFE.evaluate(4_000, 20), Ok(140));
62 assert_eq!(FAIL_SAFE.evaluate(125, 4_000), Ok(75));
63
64 assert_eq!(
66 STRICT.evaluate(0, 0),
67 Err(SurfaceError::XBelow {
68 coordinate: 0,
69 bound: 100
70 })
71 );
72 assert_eq!(
74 FAIL_SAFE.evaluate(4_000, 0),
75 Err(SurfaceError::YBelow {
76 coordinate: 0,
77 bound: 10
78 })
79 );
80}