Debian Codegraph @

.cargo/vendor/wasm-bindgen/tests/wasm/math.rs

raw
1 use wasm_bindgen::prelude::*;
2 use wasm_bindgen_test::*;
3
4 #[wasm_bindgen(module = "tests/wasm/math.js")]
5 extern "C" {
6 fn js_auto_bind_math();
7
8 // There's an identity function called `roundtrip` in the module and we bind
9 // that one function with multiple different signatures here. Note that the
10 // return value is always `f64` to faithfully capture what was sent to JS
11 // (what we're interested in) because all JS numbers fit in `f64` anyway.
12 // This is testing what happens when we pass numbers to JS and what it sees.
13 #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
14 fn roundtrip_i8(a: i8) -> f64;
15 #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
16 fn roundtrip_i16(a: i16) -> f64;
17 #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
18 fn roundtrip_i32(a: i32) -> f64;
19 #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
20 fn roundtrip_u8(a: u8) -> f64;
21 #[wasm_bindgen(assert_no_shim, js_name = roundtrip)]
22 fn roundtrip_u16(a: u16) -> f64;
23 #[wasm_bindgen(js_name = roundtrip)]
24 fn roundtrip_u32(a: u32) -> f64;
25
26 fn test_js_roundtrip();
27 }
28
29 #[wasm_bindgen]
30 pub fn math(a: f32, b: f64) -> f64 {
31 b.acos()
32 + b.asin()
33 + b.atan()
34 + b.atan2(b)
35 + b.cbrt()
36 + b.cosh()
37 + b.exp_m1()
38 + b.ln_1p()
39 + b.sinh()
40 + b.tan()
41 + b.tanh()
42 + b.hypot(b)
43 + b.cos()
44 + b.exp()
45 + b.exp2()
46 + b.mul_add(b, b)
47 + b.ln()
48 + b.log(b)
49 + b.log10()
50 + b.log2()
51 + b.powi(8)
52 + b.powf(b)
53 + b.round()
54 + b.sin()
55 + b.abs()
56 + b.signum()
57 + b.floor()
58 + b.ceil()
59 + b.trunc()
60 + b.sqrt()
61 + (b % (a as f64))
62 + ((a.cos()
63 + a.exp()
64 + a.exp2()
65 + a.mul_add(a, a)
66 + a.ln()
67 + a.log(a)
68 + a.log10()
69 + a.log2()
70 + a.powi(8)
71 + a.powf(a)
72 + a.round()
73 + a.sin()
74 + a.abs()
75 + a.signum()
76 + a.floor()
77 + a.ceil()
78 + a.trunc()
79 + a.sqrt()
80 + (a % (b as f32))) as f64)
81 + (b + 2.0f64.powf(a as f64))
82 }
83
84 #[wasm_bindgen_test]
85 fn auto_bind_math() {
86 js_auto_bind_math();
87 }
88
89 macro_rules! t_roundtrip {
90 ($f:ident($e:expr)) => {
91 assert_eq!($f($e), $e as f64)
92 };
93 }
94
95 #[wasm_bindgen_test]
96 fn limits_correct() {
97 t_roundtrip!(roundtrip_i8(i8::min_value()));
98 t_roundtrip!(roundtrip_i8(0));
99 t_roundtrip!(roundtrip_i8(i8::max_value()));
100 t_roundtrip!(roundtrip_i16(i16::min_value()));
101 t_roundtrip!(roundtrip_i16(0));
102 t_roundtrip!(roundtrip_i16(i16::max_value()));
103 t_roundtrip!(roundtrip_i32(i32::min_value()));
104 t_roundtrip!(roundtrip_i32(0));
105 t_roundtrip!(roundtrip_i32(i32::max_value()));
106 t_roundtrip!(roundtrip_u8(u8::min_value()));
107 t_roundtrip!(roundtrip_u8(0));
108 t_roundtrip!(roundtrip_u8(u8::max_value()));
109 t_roundtrip!(roundtrip_u16(u16::min_value()));
110 t_roundtrip!(roundtrip_u16(0));
111 t_roundtrip!(roundtrip_u16(u16::max_value()));
112 t_roundtrip!(roundtrip_u32(u32::min_value()));
113 t_roundtrip!(roundtrip_u32(0));
114 t_roundtrip!(roundtrip_u32(u32::max_value()));
115
116 test_js_roundtrip();
117
118 #[wasm_bindgen]
119 pub fn rust_roundtrip_i8(a: i8) -> i8 {
120 a
121 }
122 #[wasm_bindgen]
123 pub fn rust_roundtrip_i16(a: i16) -> i16 {
124 a
125 }
126 #[wasm_bindgen]
127 pub fn rust_roundtrip_i32(a: i32) -> i32 {
128 a
129 }
130 #[wasm_bindgen]
131 pub fn rust_roundtrip_u8(a: u8) -> u8 {
132 a
133 }
134 #[wasm_bindgen]
135 pub fn rust_roundtrip_u16(a: u16) -> u16 {
136 a
137 }
138 #[wasm_bindgen]
139 pub fn rust_roundtrip_u32(a: u32) -> u32 {
140 a
141 }
142 }