euclid/
approxord.rs

1// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10/// Utilities for testing approximate ordering - especially true for 
11/// floating point types, where NaN's cannot be ordered. 
12pub fn min<T: PartialOrd>(x: T, y: T) -> T {
13    if x <= y {
14        x
15    } else {
16        y
17    }
18}
19
20pub fn max<T: PartialOrd>(x: T, y: T) -> T {
21    if x >= y {
22        x
23    } else {
24        y
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_min() {
34        assert!(min(0u32, 1u32) == 0u32);
35        assert!(min(-1.0f32, 0.0f32) == -1.0f32);
36    }
37
38    #[test]
39    fn test_max() {
40        assert!(max(0u32, 1u32) == 1u32);
41        assert!(max(-1.0f32, 0.0f32) == 0.0f32);
42    }
43}