File: bigfloat_tests.rs

package info (click to toggle)
rust-lexical-parse-float 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,012 kB
  • sloc: makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,653 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#![cfg(feature = "radix")]

mod stackvec;

use lexical_parse_float::bigint::{Bigfloat, Limb};
use lexical_parse_float::float::ExtendedFloat80;
use stackvec::vec_from_u32;

#[test]
fn simple_test() {
    let x = Bigfloat::new();
    assert_eq!(x.exp, 0);

    let y = Bigfloat::from_float(ExtendedFloat80 {
        mant: 1 << 63,
        exp: -63,
    });
    assert_eq!(y.exp, -63);

    let x = Bigfloat::from_u32(1);
    assert_eq!(&*x.data, &[1]);

    let mut x = Bigfloat::from_u64(1);
    assert_eq!(&*x.data, &[1]);

    x.pow(10, 10);
    assert_eq!(&*x.data, &[9765625]);
    assert_eq!(x.exp, 10);

    x.shl_bits(1);
    assert_eq!(&*x.data, &[19531250]);
    assert_eq!(x.exp, 10);

    x.shl_limbs(1);
    assert_eq!(&*x.data, &[0, 19531250]);
    assert_eq!(x.exp, 10);

    assert_eq!(x.leading_zeros(), Limb::BITS - 25);

    // y has a 0 for 32-bit limbs, no 0s for 64-bit limbs.
    x *= &y;
    let expected = if Limb::BITS == 32 {
        vec_from_u32(&[0, 0, 0, 9765625])
    } else {
        vec_from_u32(&[0, 0, 0, 0, 9765625])
    };
    assert!(x.data == expected, "failed");
    assert_eq!(x.exp, -53);
}

#[test]
fn leading_zeros_test() {
    assert_eq!(Bigfloat::new().leading_zeros(), 0);

    assert_eq!(Bigfloat::from_u32(0xFF).leading_zeros(), Limb::BITS - 8);
    assert_eq!(Bigfloat::from_u64(0xFF00000000).leading_zeros(), 24);

    assert_eq!(Bigfloat::from_u32(0xF).leading_zeros(), Limb::BITS - 4);
    assert_eq!(Bigfloat::from_u64(0xF00000000).leading_zeros(), 28);

    assert_eq!(Bigfloat::from_u32(0xF0).leading_zeros(), Limb::BITS - 8);
    assert_eq!(Bigfloat::from_u64(0xF000000000).leading_zeros(), 24);
}