Skip to content

Commit 687a6a7

Browse files
committed
add dummy test cases
1 parent 2ad340e commit 687a6a7

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

tests/ui/batching/batch_const.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Problem: The user might want to pass either [f64; 4], (f64, f64, f64, f64), or S to the
2+
// function. All of these are valid (modulo we have to force the user to set the right repr).
3+
// Our current design doesn't allow users to specify those, so we will want at least one iteration.
4+
// However, for the sake of similarity to the current autodiff (where we'd also want a change),
5+
// leave it as is.
6+
7+
struct _S {
8+
x1: f64,
9+
x2: f64,
10+
x3: f64,
11+
x4: f64,
12+
}
13+
14+
#[batch(bsquare4, 4, Const, Leaf(8))]
15+
#[batch(vsquare4, 4, Const, Vector)]
16+
fn square(multiplier: f64, x: f64) -> f64 {
17+
x * x * multiplier
18+
}
19+
20+
fn main() {
21+
let vals = [23.1, 10.0, 100.0, 3.14];
22+
let expected = [square(3.14, vals[0]), square(3.14, vals[1]), square(3.14, vals[2]), square(3.14, vals[3])];
23+
let result1 = bsquare4(3.14, vals[0], vals[1], vals[2], vals[3]);
24+
let result2 = vsquare4(3.14, vals);
25+
assert_eq!(result.x1, expected[0]);
26+
assert_eq!(result.x2, expected[1]);
27+
assert_eq!(result.x3, expected[2]);
28+
assert_eq!(result.x4, expected[3]);
29+
assert_eq!(result2.x1, expected[0]);
30+
assert_eq!(result2.x2, expected[1]);
31+
assert_eq!(result2.x3, expected[2]);
32+
assert_eq!(result2.x4, expected[3]);
33+
}

tests/ui/batching/slice.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// We want a batch size of 4.
2+
// The original function processes 2 elements a 64 bit, so for our vfoo we have an offset of 16 bytes.
3+
// Both vfoo and bfoo return [f64; 4].
4+
5+
#[batch(vfoo, 4, Leaf(16))]
6+
#[batch(bfoo, 4, Batch)]
7+
fn foo(x: &[f64]) -> f64 {
8+
assert!(x.len() == 2);
9+
x.iter().map(|&x| x * x).sum()
10+
}
11+
12+
fn main() {
13+
// 8 elements
14+
let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
15+
16+
let x2 = vec![1.0, 2.0];
17+
let x3 = vec![3.0, 4.0];
18+
let x4 = vec![5.0, 6.0];
19+
let x5 = vec![7.0, 8.0];
20+
21+
let mut res1 = [0.0;4];
22+
for i in 0..4 {
23+
res1[i] = foo(&x1[i..i + 1]);
24+
}
25+
26+
let res2: [f64; 4] = bfoo(&x2, &x3, &x4, &x5);
27+
28+
let res3: [f64; 4] = vfoo(&x1);
29+
30+
for i in 0..4 {
31+
assert_eq!(res1[i], res2[i]);
32+
assert_eq!(res1[i], res3[i]);
33+
}
34+
}

tests/ui/batching/vector_char-ptr.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Showcasing a slightly more complex type.
2+
3+
4+
#[repr(C, packed)]
5+
struct Foo {
6+
arr: [i32; 3],
7+
x: f64,
8+
y: f32,
9+
res: f64,
10+
}
11+
12+
//#pragma pack(1)
13+
//struct Foo {
14+
// int arr[3];
15+
// double x;
16+
// float y;
17+
// double res;
18+
//};
19+
20+
#[batch(df, 4, Vector)]
21+
unsafe fn f(foo: *mut i32) {
22+
let xptr = foo.add(3) as *mut f64;
23+
let yptr = foo.add(5) as *mut f32;
24+
let resptr = foo.add(6) as *mut f64;
25+
let x: f64 = *xptr;
26+
let y: f32 = *yptr;
27+
*resptr = x * y;
28+
}
29+
30+
fn main() {
31+
let foo1: Foo = Foo { [0,0,0], 10.0, 9.0, 0.0 };
32+
let foo2: Foo = Foo { [0,0,0], 99.0, 7.0, 0.0 };
33+
let foo3: Foo = Foo { [0,0,0], 1.10, 9.0, 0.0 };
34+
let foo4: Foo = Foo { [0,0,0], 3.14, 0.1, 0.0 };
35+
36+
let expected = [90.0, 693.0, 9.9, 0.314};
37+
38+
df(&foo1.as_ptr() as *mut i32,
39+
&foo2.as_ptr() as *mut i32,
40+
&foo3.as_ptr() as *mut i32,
41+
&foo4.as_ptr() as *mut i32);
42+
43+
assert_eq!(foo1.res, expected[0]);
44+
assert_eq!(foo2.res, expected[1]);
45+
assert_eq!(foo3.res, expected[2]);
46+
assert_eq!(foo4.res, expected[3]);
47+
}

0 commit comments

Comments
 (0)