Skip to content

Commit 82dbc2e

Browse files
author
Robin Kruppe
committed
Add optional, external tests for floating point parsing.
Running these tests takes hours, so they are not run by @bors.
1 parent ba792a4 commit 82dbc2e

11 files changed

+664
-0
lines changed

src/etc/test-float-parse/_common.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::io;
12+
use std::io::prelude::*;
13+
use std::mem::transmute;
14+
15+
// Nothing up my sleeve: Just (PI - 3) in base 16.
16+
#[allow(dead_code)]
17+
pub const SEED: [u32; 3] = [0x243f_6a88, 0x85a3_08d3, 0x1319_8a2e];
18+
19+
pub fn validate(text: String) {
20+
let mut out = io::stdout();
21+
let x: f64 = text.parse().unwrap();
22+
let f64_bytes: u64 = unsafe { transmute(x) };
23+
let x: f32 = text.parse().unwrap();
24+
let f32_bytes: u32 = unsafe { transmute(x) };
25+
writeln!(&mut out, "{:016x} {:08x} {}", f64_bytes, f32_bytes, text).unwrap();
26+
}

src/etc/test-float-parse/few-ones.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod _common;
12+
13+
use _common::validate;
14+
15+
fn main() {
16+
let mut pow = vec![];
17+
for i in 0..63 {
18+
pow.push(1u64 << i);
19+
}
20+
for a in &pow {
21+
for b in &pow {
22+
for c in &pow {
23+
validate((a | b | c).to_string());
24+
}
25+
}
26+
}
27+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod _common;
12+
13+
use _common::validate;
14+
15+
fn main() {
16+
for e in 300..310 {
17+
for i in 0..100000 {
18+
validate(format!("{}e{}", i, e));
19+
}
20+
}
21+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rand)]
12+
13+
extern crate rand;
14+
15+
mod _common;
16+
17+
use std::char;
18+
use rand::{IsaacRng, Rng, SeedableRng};
19+
use rand::distributions::{Range, Sample};
20+
use _common::{validate, SEED};
21+
22+
fn main() {
23+
let mut rnd = IsaacRng::from_seed(&SEED);
24+
let mut range = Range::new(0, 10);
25+
for _ in 0..5_000_000u64 {
26+
let num_digits = rnd.gen_range(100, 300);
27+
let digits = gen_digits(num_digits, &mut range, &mut rnd);
28+
validate(digits);
29+
}
30+
}
31+
32+
fn gen_digits<R: Rng>(n: u32, range: &mut Range<u32>, rnd: &mut R) -> String {
33+
let mut s = String::new();
34+
for _ in 0..n {
35+
let digit = char::from_digit(range.sample(rnd), 10).unwrap();
36+
s.push(digit);
37+
}
38+
s
39+
}

src/etc/test-float-parse/rand-f64.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rand)]
12+
13+
extern crate rand;
14+
15+
mod _common;
16+
17+
use _common::{validate, SEED};
18+
use rand::{IsaacRng, Rng, SeedableRng};
19+
use std::mem::transmute;
20+
21+
fn main() {
22+
let mut rnd = IsaacRng::from_seed(&SEED);
23+
let mut i = 0;
24+
while i < 10_000_000 {
25+
let bits = rnd.next_u64();
26+
let x: f64 = unsafe { transmute(bits) };
27+
if x.is_finite() {
28+
validate(format!("{:e}", x));
29+
i += 1;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)