Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 0e1d82b

Browse files
committed
add tests
1 parent 3222e99 commit 0e1d82b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/math/generic/scalbn.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,57 @@ where
8181

8282
x * F::from_bits(F::Int::cast_from(F::EXP_BIAS as i32 + n) << F::SIG_BITS)
8383
}
84+
85+
#[cfg(test)]
86+
mod tests {
87+
use super::super::super::Int;
88+
use super::*;
89+
90+
// Tests against N3220
91+
fn spec_test<F: Float>()
92+
where
93+
u32: CastInto<F::Int>,
94+
F::Int: CastFrom<i32>,
95+
F::Int: CastFrom<u32>,
96+
{
97+
// `scalbn(±0, n)` returns `±0`.
98+
assert_biteq!(scalbn(F::NEG_ZERO, 10), F::NEG_ZERO);
99+
assert_biteq!(scalbn(F::NEG_ZERO, 0), F::NEG_ZERO);
100+
assert_biteq!(scalbn(F::NEG_ZERO, -10), F::NEG_ZERO);
101+
assert_biteq!(scalbn(F::ZERO, 10), F::ZERO);
102+
assert_biteq!(scalbn(F::ZERO, 0), F::ZERO);
103+
assert_biteq!(scalbn(F::ZERO, -10), F::ZERO);
104+
105+
// `scalbn(x, 0)` returns `x`.
106+
assert_biteq!(scalbn(F::MIN, 0), F::MIN);
107+
assert_biteq!(scalbn(F::MAX, 0), F::MAX);
108+
assert_biteq!(scalbn(F::INFINITY, 0), F::INFINITY);
109+
assert_biteq!(scalbn(F::NEG_INFINITY, 0), F::NEG_INFINITY);
110+
assert_biteq!(scalbn(F::ZERO, 0), F::ZERO);
111+
assert_biteq!(scalbn(F::NEG_ZERO, 0), F::NEG_ZERO);
112+
113+
// `scalbn(±∞, n)` returns `±∞`.
114+
assert_biteq!(scalbn(F::INFINITY, 10), F::INFINITY);
115+
assert_biteq!(scalbn(F::INFINITY, -10), F::INFINITY);
116+
assert_biteq!(scalbn(F::NEG_INFINITY, 10), F::NEG_INFINITY);
117+
assert_biteq!(scalbn(F::NEG_INFINITY, -10), F::NEG_INFINITY);
118+
119+
// NaN should return unchanged
120+
assert_biteq!(scalbn(F::NAN, 10), F::NAN);
121+
assert_biteq!(scalbn(F::NAN, 0), F::NAN);
122+
assert_biteq!(scalbn(F::NAN, -10), F::NAN);
123+
assert_biteq!(scalbn(-F::NAN, 10), -F::NAN);
124+
assert_biteq!(scalbn(-F::NAN, 0), -F::NAN);
125+
assert_biteq!(scalbn(-F::NAN, -10), -F::NAN);
126+
}
127+
128+
#[test]
129+
fn spec_test_f32() {
130+
spec_test::<f32>();
131+
}
132+
133+
#[test]
134+
fn spec_test_f64() {
135+
spec_test::<f64>();
136+
}
137+
}

0 commit comments

Comments
 (0)