cross: list: more tests for fold operation

This commit is contained in:
2022-08-15 03:39:22 -07:00
parent 663d657969
commit a3f2b0b33f

View File

@@ -142,16 +142,48 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
impl FoldOp<i32> for i32 {
type Output = i32;
fn feed(self, a: i32) -> Self::Output {
self + a
}
}
#[test] #[test]
fn fold_prim() { fn fold_prim() {
impl FoldOp<i32> for i32 {
type Output = i32;
fn feed(self, a: i32) -> Self::Output {
self + a
}
}
let list = (3, 4, 5i32).into_list(); let list = (3, 4, 5i32).into_list();
assert_eq!(fold(list, 2i32), 2+3+4+5); assert_eq!(fold(list, 2i32), 2+3+4+5);
} }
impl FoldOp<f32> for i32 {
type Output = f32;
fn feed(self, a: f32) -> Self::Output {
self as f32 + a
}
}
impl FoldOp<f32> for f32 {
type Output = f32;
fn feed(self, a: f32) -> Self::Output {
self + a
}
}
impl FoldOp<i32> for f32 {
type Output = f32;
fn feed(self, a: i32) -> Self::Output {
self + a as f32
}
}
#[test]
fn fold_mixed() {
// we fold:
// 2i32 + 3i32
// 5i32 + 4i32
// 9i32 + 5.5f32
// 14.5f32 + 6.5f32
// 21f32 + 7i32
let list = (3i32, 4i32, 5.5f32, 6.5f32, 7i32).into_list();
assert_eq!(fold(list, 2i32), 28f32);
}
} }