summaryrefslogtreecommitdiffstats
path: root/vendor/wasm-bindgen/tests/wasm/math.rs
blob: 5462cc2d475f9153ddef70dd31b4a32dcd62ffea (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen(module = "tests/wasm/math.js")]
extern "C" {
    fn js_auto_bind_math();

    // There's an identity function called `roundtrip` in the module and we bind
    // that one function with multiple different signatures here. Note that the
    // return value is always `f64` to faithfully capture what was sent to JS
    // (what we're interested in) because all JS numbers fit in `f64` anyway.
    // This is testing what happens when we pass numbers to JS and what it sees.
    #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
    fn roundtrip_i8(a: i8) -> f64;
    #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
    fn roundtrip_i16(a: i16) -> f64;
    #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
    fn roundtrip_i32(a: i32) -> f64;
    #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
    fn roundtrip_u8(a: u8) -> f64;
    #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
    fn roundtrip_u16(a: u16) -> f64;
    #[wasm_bindgen(js_name = roundtrip)]
    fn roundtrip_u32(a: u32) -> f64;

    fn test_js_roundtrip();
}

#[wasm_bindgen]
pub fn math(a: f32, b: f64) -> f64 {
    b.acos()
        + b.asin()
        + b.atan()
        + b.atan2(b)
        + b.cbrt()
        + b.cosh()
        + b.exp_m1()
        + b.ln_1p()
        + b.sinh()
        + b.tan()
        + b.tanh()
        + b.hypot(b)
        + b.cos()
        + b.exp()
        + b.exp2()
        + b.mul_add(b, b)
        + b.ln()
        + b.log(b)
        + b.log10()
        + b.log2()
        + b.powi(8)
        + b.powf(b)
        + b.round()
        + b.sin()
        + b.abs()
        + b.signum()
        + b.floor()
        + b.ceil()
        + b.trunc()
        + b.sqrt()
        + (b % (a as f64))
        + ((a.cos()
            + a.exp()
            + a.exp2()
            + a.mul_add(a, a)
            + a.ln()
            + a.log(a)
            + a.log10()
            + a.log2()
            + a.powi(8)
            + a.powf(a)
            + a.round()
            + a.sin()
            + a.abs()
            + a.signum()
            + a.floor()
            + a.ceil()
            + a.trunc()
            + a.sqrt()
            + (a % (b as f32))) as f64)
        + (b + 2.0f64.powf(a as f64))
}

#[wasm_bindgen_test]
fn auto_bind_math() {
    js_auto_bind_math();
}

macro_rules! t_roundtrip {
    ($f:ident($e:expr)) => {
        assert_eq!($f($e), $e as f64)
    };
}

#[wasm_bindgen_test]
fn limits_correct() {
    t_roundtrip!(roundtrip_i8(i8::min_value()));
    t_roundtrip!(roundtrip_i8(0));
    t_roundtrip!(roundtrip_i8(i8::max_value()));
    t_roundtrip!(roundtrip_i16(i16::min_value()));
    t_roundtrip!(roundtrip_i16(0));
    t_roundtrip!(roundtrip_i16(i16::max_value()));
    t_roundtrip!(roundtrip_i32(i32::min_value()));
    t_roundtrip!(roundtrip_i32(0));
    t_roundtrip!(roundtrip_i32(i32::max_value()));
    t_roundtrip!(roundtrip_u8(u8::min_value()));
    t_roundtrip!(roundtrip_u8(0));
    t_roundtrip!(roundtrip_u8(u8::max_value()));
    t_roundtrip!(roundtrip_u16(u16::min_value()));
    t_roundtrip!(roundtrip_u16(0));
    t_roundtrip!(roundtrip_u16(u16::max_value()));
    t_roundtrip!(roundtrip_u32(u32::min_value()));
    t_roundtrip!(roundtrip_u32(0));
    t_roundtrip!(roundtrip_u32(u32::max_value()));

    test_js_roundtrip();

    #[wasm_bindgen]
    pub fn rust_roundtrip_i8(a: i8) -> i8 {
        a
    }
    #[wasm_bindgen]
    pub fn rust_roundtrip_i16(a: i16) -> i16 {
        a
    }
    #[wasm_bindgen]
    pub fn rust_roundtrip_i32(a: i32) -> i32 {
        a
    }
    #[wasm_bindgen]
    pub fn rust_roundtrip_u8(a: u8) -> u8 {
        a
    }
    #[wasm_bindgen]
    pub fn rust_roundtrip_u16(a: u16) -> u16 {
        a
    }
    #[wasm_bindgen]
    pub fn rust_roundtrip_u32(a: u32) -> u32 {
        a
    }
}