1 use std::f64::consts::PI;
2 use std::f64::{NAN, NEG_INFINITY};
3 
4 use js_sys::*;
5 use wasm_bindgen_test::*;
6 
7 macro_rules! assert_eq {
8     ($a:expr, $b:expr) => {{
9         let (a, b) = (&$a, &$b);
10         if f64::is_infinite(*a) && f64::is_infinite(*b) {
11             assert!(a == b);
12         } else {
13             assert!(
14                 (*a - *b).abs() < 1.0e-6,
15                 "not approximately equal {:?} ?= {:?}",
16                 a,
17                 b
18             );
19         }
20     }};
21 }
22 
23 #[wasm_bindgen_test]
abs()24 fn abs() {
25     assert_eq!(Math::abs(-32.), 32.);
26     assert_eq!(Math::abs(-32.), 32.);
27     assert_eq!(Math::abs(-4.7), 4.7);
28 }
29 
30 #[wasm_bindgen_test]
acos()31 fn acos() {
32     assert_eq!(Math::acos(-1.), PI);
33     assert_eq!(Math::acos(0.5), 1.0471975511965979);
34     assert!(Math::acos(2.).is_nan());
35 }
36 
37 #[wasm_bindgen_test]
acosh()38 fn acosh() {
39     assert_eq!(Math::acosh(1.), 0.);
40     assert_eq!(Math::acosh(2.), 2.0f64.acosh());
41     assert!(Math::acosh(0.5).is_nan());
42 }
43 
44 #[wasm_bindgen_test]
asin()45 fn asin() {
46     assert_eq!(Math::asin(1.), 1.0f64.asin());
47     assert_eq!(Math::asin(0.5), 0.5f64.asin());
48     assert!(Math::asin(2.).is_nan());
49 }
50 
51 #[wasm_bindgen_test]
asinh()52 fn asinh() {
53     assert_eq!(Math::asinh(1.0), 1f64.asinh());
54     assert_eq!(Math::asinh(0.5), 0.5f64.asinh());
55 }
56 
57 #[wasm_bindgen_test]
atan()58 fn atan() {
59     assert_eq!(Math::atan(1.0), 1f64.atan());
60     assert_eq!(Math::atan(0.5), 0.5f64.atan());
61 }
62 
63 #[wasm_bindgen_test]
atan2()64 fn atan2() {
65     assert_eq!(Math::atan2(1.0, 2.0), 1f64.atan2(2.));
66     assert_eq!(Math::atan2(0.7, 3.8), 0.7f64.atan2(3.8f64));
67 }
68 
69 #[wasm_bindgen_test]
atanh()70 fn atanh() {
71     assert_eq!(Math::atanh(1.), 1f64.atanh());
72     assert_eq!(Math::atanh(0.5), 0.5f64.atanh());
73     assert!(Math::atanh(2.).is_nan());
74 }
75 
76 #[wasm_bindgen_test]
cbrt()77 fn cbrt() {
78     assert_eq!(Math::cbrt(27.), 3.);
79     assert_eq!(Math::cbrt(12.3), 12.3f64.cbrt());
80 }
81 
82 #[wasm_bindgen_test]
ceil()83 fn ceil() {
84     assert_eq!(Math::ceil(1.1), 2.);
85     assert_eq!(Math::ceil(-1.1), -1.);
86 }
87 
88 #[wasm_bindgen_test]
clz32()89 fn clz32() {
90     assert!(Math::clz32(1) == 31);
91     assert!(Math::clz32(1000) == 22);
92 }
93 
94 #[wasm_bindgen_test]
cos()95 fn cos() {
96     assert_eq!(Math::cos(0.0), 1.);
97     assert_eq!(Math::cos(1.5), 1.5f64.cos());
98 }
99 
100 #[wasm_bindgen_test]
cosh()101 fn cosh() {
102     assert_eq!(Math::cosh(0.), 1.);
103     assert_eq!(Math::cosh(2.), 3.7621956910836314);
104 }
105 
106 #[wasm_bindgen_test]
exp()107 fn exp() {
108     assert_eq!(Math::exp(0.), 1.);
109     assert_eq!(Math::exp(-1.), 0.36787944117144233);
110     assert_eq!(Math::exp(2.), 7.38905609893065);
111 }
112 
113 #[wasm_bindgen_test]
expm1()114 fn expm1() {
115     assert_eq!(Math::expm1(0.), 0.);
116     assert_eq!(Math::expm1(1.), 1.718281828459045);
117     assert_eq!(Math::expm1(-1.), -0.6321205588285577);
118     assert_eq!(Math::expm1(2.), 6.38905609893065);
119 }
120 
121 #[wasm_bindgen_test]
floor()122 fn floor() {
123     assert_eq!(Math::floor(5.95), 5.);
124     assert_eq!(Math::floor(-5.05), -6.);
125 }
126 
127 #[wasm_bindgen_test]
fround()128 fn fround() {
129     assert!(Math::fround(5.5) == 5.5);
130     assert!(Math::fround(5.05) == 5.050000190734863);
131     assert!(Math::fround(5.) == 5.);
132     assert!(Math::fround(-5.05) == -5.050000190734863);
133 }
134 
135 #[wasm_bindgen_test]
hypot()136 fn hypot() {
137     assert!(Math::hypot(3., 4.) == 5.);
138     assert!(Math::hypot(3.9, 5.2) == 6.5);
139     assert!(Math::hypot(6., 8.) == 10.);
140     assert!(Math::hypot(7., 24.) == 25.);
141 }
142 
143 #[wasm_bindgen_test]
imul()144 fn imul() {
145     assert!(Math::imul(3, 4) == 12);
146     assert!(Math::imul(-5, 12) == -60);
147     assert!(Math::imul(0xffffffffu32 as i32, 5) == 0xffffffffu32.wrapping_mul(5) as i32);
148 }
149 
150 #[wasm_bindgen_test]
log()151 fn log() {
152     assert_eq!(Math::log(8.) / Math::log(2.), 3.);
153     assert_eq!(Math::log(625.) / Math::log(5.), 4.);
154 }
155 
156 #[wasm_bindgen_test]
log10()157 fn log10() {
158     assert_eq!(Math::log10(100000.), 5.);
159     assert_eq!(Math::log10(1.), 0.);
160     assert_eq!(Math::log10(2.), 0.3010299956639812);
161 }
162 
163 #[wasm_bindgen_test]
log1p()164 fn log1p() {
165     assert_eq!(Math::log1p(1.), 0.6931471805599453);
166     assert_eq!(Math::log1p(0.), 0.);
167     assert_eq!(Math::log1p(-1.), NEG_INFINITY);
168     assert!(Math::log1p(-2.).is_nan());
169 }
170 
171 #[wasm_bindgen_test]
log2()172 fn log2() {
173     assert_eq!(Math::log2(3.), 1.584962500721156);
174     assert_eq!(Math::log2(2.), 1.);
175     assert_eq!(Math::log2(1.), 0.);
176     assert_eq!(Math::log2(0.), NEG_INFINITY);
177 }
178 
179 #[wasm_bindgen_test]
max()180 fn max() {
181     assert_eq!(Math::max(3., 1.), 3.);
182     assert_eq!(Math::max(-3., 1.), 1.);
183     assert_eq!(Math::max(9913., 43.4), 9913.);
184     assert_eq!(Math::max(-27., -43.), -27.);
185     assert_eq!(Math::max(-423.27, -43.1), -43.1);
186 }
187 
188 #[wasm_bindgen_test]
min()189 fn min() {
190     assert_eq!(Math::min(3., 1.), 1.);
191     assert_eq!(Math::min(-3., 1.), -3.);
192     assert_eq!(Math::min(9913., 43.4), 43.4);
193     assert_eq!(Math::min(-27., -43.), -43.);
194     assert_eq!(Math::min(-423.27, -43.1), -423.27);
195 }
196 
197 #[wasm_bindgen_test]
pow()198 fn pow() {
199     assert_eq!(Math::pow(7., 2.), 49.);
200     assert_eq!(Math::pow(3.8, 0.5), 3.8f64.powf(0.5f64));
201     assert!(Math::pow(-2., 0.5).is_nan());
202 }
203 
204 #[wasm_bindgen_test]
random()205 fn random() {
206     assert!(Math::random() < 1.);
207     assert!(Math::random() >= 0.);
208 }
209 
210 #[wasm_bindgen_test]
round()211 fn round() {
212     assert_eq!(Math::round(20.49), 20.);
213     assert_eq!(Math::round(20.5), 21.);
214     assert_eq!(Math::round(42.), 42.);
215     assert_eq!(Math::round(-20.5), -20.);
216     assert_eq!(Math::round(-20.51), -21.);
217 }
218 
219 #[wasm_bindgen_test]
sign()220 fn sign() {
221     assert_eq!(Math::sign(3.), 1.);
222     assert_eq!(Math::sign(-3.), -1.);
223     assert_eq!(Math::sign(2.3), 1.);
224     assert_eq!(Math::sign(0.), 0.);
225     assert!(Math::sign(NAN).is_nan());
226 }
227 
228 #[wasm_bindgen_test]
sin()229 fn sin() {
230     assert_eq!(Math::sin(0.), 0.);
231     assert_eq!(Math::sin(1.), 1f64.sin());
232     assert_eq!(Math::sin(PI / 2.), 1.);
233 }
234 
235 #[wasm_bindgen_test]
sinh()236 fn sinh() {
237     assert_eq!(Math::sinh(0.), 0.);
238     assert_eq!(Math::sinh(1.), 1f64.sinh());
239     assert_eq!(Math::sinh(2.3), 2.3f64.sinh());
240 }
241 
242 #[wasm_bindgen_test]
sqrt()243 fn sqrt() {
244     assert_eq!(Math::sqrt(9.), 3.);
245     assert_eq!(Math::sqrt(2.), 2f64.sqrt());
246     assert_eq!(Math::sqrt(42.42), 42.42f64.sqrt());
247     assert_eq!(Math::sqrt(1.), 1.);
248     assert!(Math::sqrt(-1.).is_nan());
249 }
250 
251 #[wasm_bindgen_test]
tan()252 fn tan() {
253     assert_eq!(Math::tan(0.), 0.);
254     assert_eq!(Math::tan(1.), 1f64.tan());
255     assert_eq!(Math::tan(0.5), 0.5f64.tan());
256 }
257 
258 #[wasm_bindgen_test]
tanh()259 fn tanh() {
260     assert_eq!(Math::tanh(0.), 0.);
261     assert_eq!(Math::tanh(1.), 1f64.tanh());
262     assert_eq!(Math::tanh(0.5), 0.5f64.tanh());
263 }
264 
265 #[wasm_bindgen_test]
trunc()266 fn trunc() {
267     assert_eq!(Math::trunc(13.37), 13.);
268     assert_eq!(Math::trunc(42.84), 42.);
269     assert_eq!(Math::trunc(0.123), 0.);
270     assert_eq!(Math::trunc(-0.123), 0.);
271 }
272