cross: list: more tests for fold
operation
This commit is contained in:
@@ -142,16 +142,48 @@ where
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
impl FoldOp<i32> for i32 {
|
||||
type Output = i32;
|
||||
fn feed(self, a: i32) -> Self::Output {
|
||||
self + a
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user