]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-inheritance-self-in-supertype.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[rust.git] / src / test / run-pass / trait-inheritance-self-in-supertype.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test for issue #4183: use of Self in supertraits.
12
13 // pretty-expanded FIXME #23616
14
15 use std::num::Float as StdFloat;
16
17 pub static FUZZY_EPSILON: f64 = 0.1;
18
19 pub trait FuzzyEq<Eps> {
20     fn fuzzy_eq(&self, other: &Self) -> bool;
21     fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
22 }
23
24 trait Float: FuzzyEq<Self> {
25     fn two_pi() -> Self;
26 }
27
28 impl FuzzyEq<f32> for f32 {
29     fn fuzzy_eq(&self, other: &f32) -> bool {
30         self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32))
31     }
32
33     fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
34         (*self - *other).abs() < *epsilon
35     }
36 }
37
38 impl Float for f32 {
39     fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 }
40 }
41
42 impl FuzzyEq<f64> for f64 {
43     fn fuzzy_eq(&self, other: &f64) -> bool {
44         self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64))
45     }
46
47     fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
48         (*self - *other).abs() < *epsilon
49     }
50 }
51
52 impl Float for f64 {
53     fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 }
54 }
55
56 fn compare<F:Float>(f1: F) -> bool {
57     let f2 = Float::two_pi();
58     f1.fuzzy_eq(&f2)
59 }
60
61 pub fn main() {
62     assert!(compare::<f32>(6.28318530717958647692528676655900576));
63     assert!(compare::<f32>(6.29));
64     assert!(compare::<f32>(6.3));
65     assert!(compare::<f32>(6.19));
66     assert!(!compare::<f32>(7.28318530717958647692528676655900576));
67     assert!(!compare::<f32>(6.18));
68
69     assert!(compare::<f64>(6.28318530717958647692528676655900576));
70     assert!(compare::<f64>(6.29));
71     assert!(compare::<f64>(6.3));
72     assert!(compare::<f64>(6.19));
73     assert!(!compare::<f64>(7.28318530717958647692528676655900576));
74     assert!(!compare::<f64>(6.18));
75 }