fixed_wide_vectors/fixed_wide/src/tests.rs

110 lines
2.4 KiB
Rust
Raw Normal View History

2024-08-29 20:16:02 +00:00
use crate::types::I32F32;
2024-09-03 00:42:01 +00:00
use crate::types::I256F256;
#[test]
fn you_can_add_numbers(){
let a=I256F256::from((3i128*2).pow(4));
assert_eq!(a+a,I256F256::from((3i128*2).pow(4)*2))
}
#[test]
fn you_can_shr_numbers(){
let a=I32F32::from(4);
assert_eq!(a>>1,I32F32::from(2))
}
2024-08-28 19:17:00 +00:00
#[test]
fn test_wide_mul(){
2024-08-29 20:16:02 +00:00
let a=I32F32::ONE;
2024-09-03 00:03:01 +00:00
let aa=a.wide_mul_1_1(a);
2024-08-28 19:17:00 +00:00
assert_eq!(aa,crate::types::I64F64::ONE);
}
2024-09-09 21:45:47 +00:00
#[test]
fn test_wide_div(){
let a=I32F32::ONE*4;
let b=I32F32::ONE*2;
let wide_a=a.wide_mul_1_1(I32F32::ONE);
let wide_b=b.wide_mul_1_1(I32F32::ONE);
let ab=a.wide_div_1_1(b);
assert_eq!(ab,crate::types::I64F64::ONE*2);
let wab=wide_a.wide_div_2_1(b);
assert_eq!(wab,crate::fixed::Fixed::<3,96>::ONE*2);
let awb=a.wide_div_1_2(wide_b);
assert_eq!(awb,crate::fixed::Fixed::<3,96>::ONE*2);
}
2024-09-03 00:42:01 +00:00
#[test]
fn test_wide_mul_repeated() {
let a=I32F32::from(2);
let b=I32F32::from(3);
let w1=a.wide_mul_1_1(b);
let w2=w1.wide_mul_2_2(w1);
let w3=w2.wide_mul_4_4(w2);
assert_eq!(w3,I256F256::from((3i128*2).pow(4)));
}
2024-08-28 19:17:00 +00:00
#[test]
fn test_bint(){
2024-08-29 20:16:02 +00:00
let a=I32F32::ONE;
assert_eq!(a*2,I32F32::from(2));
2024-08-28 19:17:00 +00:00
}
2024-08-29 17:12:08 +00:00
#[test]
fn test_fix(){
let a=I32F32::ONE;
assert_eq!(a.fix_8(),I256F256::ONE);
}
2024-08-29 17:12:08 +00:00
#[test]
fn test_sqrt(){
2024-08-29 20:16:02 +00:00
let a=I32F32::ONE*4;
assert_eq!(a.sqrt(),I32F32::from(2));
2024-08-29 17:12:08 +00:00
}
2024-08-29 19:13:42 +00:00
#[test]
2024-08-29 23:20:10 +00:00
fn test_sqrt_zero(){
let a=I32F32::ZERO;
assert_eq!(a.sqrt(),I32F32::ZERO);
}
#[test]
2024-08-29 19:13:42 +00:00
fn test_sqrt_low(){
2024-08-29 20:16:02 +00:00
let a=I32F32::HALF;
2024-09-06 19:49:10 +00:00
let b=a.fixed_mul(a);
2024-08-29 19:13:42 +00:00
assert_eq!(b.sqrt(),a);
}
2024-08-29 20:16:09 +00:00
fn find_equiv_sqrt_via_f64(n:I32F32)->I32F32{
//GIMME THEM BITS BOY
let &[bits]=n.to_bits().to_bits().digits();
let ibits=bits as i64;
let f=(ibits as f64)/((1u64<<32) as f64);
let f_ans=f.sqrt();
2024-08-29 20:30:48 +00:00
let i=(f_ans*((1u64<<32) as f64)) as i64;
let r=I32F32::from_bits(bnum::BInt::<1>::from(i));
//mimic the behaviour of the algorithm,
//return the result if it truncates to the exact answer
2024-09-03 00:03:01 +00:00
if (r+I32F32::EPSILON).wide_mul_1_1(r+I32F32::EPSILON)==n.wide_mul_1_1(I32F32::ONE){
2024-08-29 20:30:48 +00:00
return r+I32F32::EPSILON;
2024-08-29 20:16:09 +00:00
}
2024-09-03 00:03:01 +00:00
if (r-I32F32::EPSILON).wide_mul_1_1(r-I32F32::EPSILON)==n.wide_mul_1_1(I32F32::ONE){
2024-08-29 20:30:48 +00:00
return r-I32F32::EPSILON;
}
return r;
}
fn test_exact(n:I32F32){
assert_eq!(n.sqrt(),find_equiv_sqrt_via_f64(n));
2024-08-29 20:16:09 +00:00
}
#[test]
fn test_sqrt_exact(){
2024-08-29 20:30:48 +00:00
//43
for i in 0..((i64::MAX as f32).ln() as u32){
let n=I32F32::from_bits(bnum::BInt::<1>::from((i as f32).exp() as i64));
test_exact(n);
}
2024-08-29 20:16:09 +00:00
}
2024-08-29 22:27:48 +00:00
#[test]
fn test_sqrt_max(){
let a=I32F32::MAX;
test_exact(a);
}