Debian Codegraph @

crates/wasm-wave/tests/nan.rs

raw
1 //! Test that NaN bitpatterns are not propagated through Wave values.
2 //!
3 //! The component-model floating-point types only have a single NaN value, to
4 //! make it easier to exchange values with source languages and protocols where
5 //! there is only one NaN value. To help users avoid depending on NaN bits being
6 //! propagated, we canonicalize NaNs.
7
8 use std::{f32, f64};
9
10 use wasm_wave::wasm::WasmValue;
11
12 #[test]
13 fn nan() {
14 for bits in [
15 0,
16 i32::MIN as u32,
17 1.0_f32.to_bits(),
18 (-f32::consts::TAU).to_bits(),
19 0xffffffff,
20 0x7fff0f0f,
21 0x8f800000,
22 f32::NAN.to_bits(),
23 ] {
24 let val = f32::from_bits(bits);
25 let expected = if val.is_nan() { 0x7fc00000 } else { bits };
26
27 {
28 use wasm_wave::value::Value;
29 assert_eq!(Value::make_f32(val).unwrap_f32().to_bits(), expected);
30 }
31 }
32
33 for bits in [
34 0,
35 i64::MIN as u64,
36 1.0_f64.to_bits(),
37 (-f64::consts::TAU).to_bits(),
38 0xffffffffffffffff,
39 0x7fff0f0f0f0f0f0f,
40 0x8ff0000000000000,
41 f64::NAN.to_bits(),
42 ] {
43 let val = f64::from_bits(bits);
44 let expected = if val.is_nan() {
45 0x7ff8000000000000
46 } else {
47 bits
48 };
49
50 {
51 use wasm_wave::value::Value;
52 assert_eq!(Value::make_f64(val).unwrap_f64().to_bits(), expected);
53 }
54 }
55 }