summaryrefslogtreecommitdiffstats
path: root/src/test/ui/traits/inheritance/self-in-supertype.rs
blob: e8a2bd791a5e62922378bbb3a26fe86c5b4b1dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// run-pass
// Test for issue #4183: use of Self in supertraits.

pub static FUZZY_EPSILON: f64 = 0.1;

pub trait FuzzyEq<Eps> {
    fn fuzzy_eq(&self, other: &Self) -> bool;
    fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
}

trait Float: Sized+FuzzyEq<Self> {
    fn two_pi() -> Self;
}

impl FuzzyEq<f32> for f32 {
    fn fuzzy_eq(&self, other: &f32) -> bool {
        self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32))
    }

    fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
        (*self - *other).abs() < *epsilon
    }
}

impl Float for f32 {
    fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 }
}

impl FuzzyEq<f64> for f64 {
    fn fuzzy_eq(&self, other: &f64) -> bool {
        self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64))
    }

    fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
        (*self - *other).abs() < *epsilon
    }
}

impl Float for f64 {
    fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 }
}

fn compare<F:Float>(f1: F) -> bool {
    let f2 = Float::two_pi();
    f1.fuzzy_eq(&f2)
}

pub fn main() {
    assert!(compare::<f32>(6.28318530717958647692528676655900576));
    assert!(compare::<f32>(6.29));
    assert!(compare::<f32>(6.3));
    assert!(compare::<f32>(6.19));
    assert!(!compare::<f32>(7.28318530717958647692528676655900576));
    assert!(!compare::<f32>(6.18));

    assert!(compare::<f64>(6.28318530717958647692528676655900576));
    assert!(compare::<f64>(6.29));
    assert!(compare::<f64>(6.3));
    assert!(compare::<f64>(6.19));
    assert!(!compare::<f64>(7.28318530717958647692528676655900576));
    assert!(!compare::<f64>(6.18));
}