Skip to content

Commit 83eb4c0

Browse files
committed
Add a helper for cached test inputs
Add a type that caches values used to implement `GenerateInput` on a variety of signatures.
1 parent f8ce0b6 commit 83eb4c0

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

libm/crates/libm-test/src/gen.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! Different generators that can create random or systematic bit patterns.
2+
3+
use crate::GenerateInput;
4+
5+
/// Helper type to turn any reusable input into a generator.
6+
#[derive(Clone, Debug, Default)]
7+
pub struct CachedInput {
8+
pub inputs_f32: Vec<(f32, f32, f32)>,
9+
pub inputs_f64: Vec<(f64, f64, f64)>,
10+
pub inputs_i32: Vec<(i32, i32, i32)>,
11+
}
12+
13+
impl GenerateInput<(f32,)> for CachedInput {
14+
fn get_cases(&self) -> impl Iterator<Item = (f32,)> {
15+
self.inputs_f32.iter().map(|f| (f.0,))
16+
}
17+
}
18+
19+
impl GenerateInput<(f32, f32)> for CachedInput {
20+
fn get_cases(&self) -> impl Iterator<Item = (f32, f32)> {
21+
self.inputs_f32.iter().map(|f| (f.0, f.1))
22+
}
23+
}
24+
25+
impl GenerateInput<(i32, f32)> for CachedInput {
26+
fn get_cases(&self) -> impl Iterator<Item = (i32, f32)> {
27+
self.inputs_i32.iter().zip(self.inputs_f32.iter()).map(|(i, f)| (i.0, f.0))
28+
}
29+
}
30+
31+
impl GenerateInput<(f32, i32)> for CachedInput {
32+
fn get_cases(&self) -> impl Iterator<Item = (f32, i32)> {
33+
GenerateInput::<(i32, f32)>::get_cases(self).map(|(i, f)| (f, i))
34+
}
35+
}
36+
37+
impl GenerateInput<(f32, f32, f32)> for CachedInput {
38+
fn get_cases(&self) -> impl Iterator<Item = (f32, f32, f32)> {
39+
self.inputs_f32.iter().copied()
40+
}
41+
}
42+
43+
impl GenerateInput<(f64,)> for CachedInput {
44+
fn get_cases(&self) -> impl Iterator<Item = (f64,)> {
45+
self.inputs_f64.iter().map(|f| (f.0,))
46+
}
47+
}
48+
49+
impl GenerateInput<(f64, f64)> for CachedInput {
50+
fn get_cases(&self) -> impl Iterator<Item = (f64, f64)> {
51+
self.inputs_f64.iter().map(|f| (f.0, f.1))
52+
}
53+
}
54+
55+
impl GenerateInput<(i32, f64)> for CachedInput {
56+
fn get_cases(&self) -> impl Iterator<Item = (i32, f64)> {
57+
self.inputs_i32.iter().zip(self.inputs_f64.iter()).map(|(i, f)| (i.0, f.0))
58+
}
59+
}
60+
61+
impl GenerateInput<(f64, i32)> for CachedInput {
62+
fn get_cases(&self) -> impl Iterator<Item = (f64, i32)> {
63+
GenerateInput::<(i32, f64)>::get_cases(self).map(|(i, f)| (f, i))
64+
}
65+
}
66+
67+
impl GenerateInput<(f64, f64, f64)> for CachedInput {
68+
fn get_cases(&self) -> impl Iterator<Item = (f64, f64, f64)> {
69+
self.inputs_f64.iter().copied()
70+
}
71+
}

libm/crates/libm-test/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod gen;
12
mod num_traits;
23
mod test_traits;
34

0 commit comments

Comments
 (0)