1pub struct Interp;
2
3pub struct Clamp;
4
5pub trait Bound<Indep> {
6 fn upper_bound(indep: Indep, upper_bound: Indep) -> Indep;
7 fn lower_bound(indep: Indep, lower_bound: Indep) -> Indep;
8}
9
10impl<Indep> Bound<Indep> for Clamp
11where
12 Indep: std::cmp::PartialOrd,
13{
14 fn upper_bound(indep: Indep, upper_bound: Indep) -> Indep {
15 if indep > upper_bound {
17 upper_bound
18 } else {
19 indep
20 }
21 }
22
23 fn lower_bound(indep: Indep, lower_bound: Indep) -> Indep {
24 if indep < lower_bound {
26 lower_bound
27 } else {
28 indep
29 }
30 }
31}
32
33impl<Indep> Bound<Indep> for Interp {
34 fn upper_bound(indep: Indep, _upper_bound: Indep) -> Indep {
35 indep
36 }
37
38 fn lower_bound(indep: Indep, _lower_bound: Indep) -> Indep {
39 indep
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
52 fn clamp_inbounds() {
53 let x = 2.5;
54 let lower = 0.0;
55 let higher = 5.0;
56 let output_high = Clamp::upper_bound(x, higher);
57 let output_low = Clamp::lower_bound(x, lower);
58
59 assert!(output_high == x);
60 assert!(output_low == x);
61 }
62
63 #[test]
64 fn clamp_high() {
65 let x = 10.0;
66 let lower = 0.0;
67 let higher = 5.0;
68 let output_high = Clamp::upper_bound(x, higher);
69 let output_low = Clamp::lower_bound(x, lower);
70
71 assert!(output_high == higher);
72 assert!(output_low == x);
73 }
74
75 #[test]
76 fn clamp_low() {
77 let x = -2.0;
78 let lower = 0.0;
79 let higher = 5.0;
80 let output_high = Clamp::upper_bound(x, higher);
81 let output_low = Clamp::lower_bound(x, lower);
82
83 assert!(output_high == x);
84 assert!(output_low == lower);
85 }
86
87 #[test]
92 fn interp_inbounds() {
93 let x = 2.5;
94 let lower = 0.0;
95 let higher = 5.0;
96 let output_high = Interp::upper_bound(x, higher);
97 let output_low = Interp::lower_bound(x, lower);
98
99 assert!(output_high == x);
100 assert!(output_low == x);
101 }
102
103 #[test]
104 fn interp_high() {
105 let x = 10.0;
106 let lower = 0.0;
107 let higher = 5.0;
108 let output_high = Interp::upper_bound(x, higher);
109 let output_low = Interp::lower_bound(x, lower);
110
111 assert!(output_high == x);
112 assert!(output_low == x);
113 }
114
115 #[test]
116 fn interp_low() {
117 let x = -2.0;
118 let lower = 0.0;
119 let higher = 5.0;
120 let output_high = Interp::upper_bound(x, higher);
121 let output_low = Interp::lower_bound(x, lower);
122
123 assert!(output_high == x);
124 assert!(output_low == x);
125 }
126}