|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: MIT |
| 3 | + * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +use deep_causality_haft::{Foldable, OptionWitness, ResultWitness, VecWitness}; |
| 7 | + |
| 8 | +// --- OptionWitness Foldable Tests --- |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn test_foldable_option_some() { |
| 12 | + let opt = Some(5); |
| 13 | + let result = OptionWitness::fold(opt, 0, |acc, x| acc + x); |
| 14 | + assert_eq!(result, 5); |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn test_foldable_option_none() { |
| 19 | + let opt: Option<i32> = None; |
| 20 | + let result = OptionWitness::fold(opt, 0, |acc, x| acc + x); |
| 21 | + assert_eq!(result, 0); |
| 22 | +} |
| 23 | + |
| 24 | +#[test] |
| 25 | +fn test_foldable_option_string_concat() { |
| 26 | + let opt = Some("hello".to_string()); |
| 27 | + let result = OptionWitness::fold(opt, String::new(), |mut acc, x| { |
| 28 | + acc.push_str(&x); |
| 29 | + acc |
| 30 | + }); |
| 31 | + assert_eq!(result, "hello"); |
| 32 | +} |
| 33 | + |
| 34 | +// --- ResultWitness Foldable Tests --- |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_foldable_result_ok() { |
| 38 | + let res: Result<i32, String> = Ok(5); |
| 39 | + let result = ResultWitness::fold(res, 0, |acc, x| acc + x); |
| 40 | + assert_eq!(result, 5); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_foldable_result_err() { |
| 45 | + let res: Result<i32, String> = Err("error".to_string()); |
| 46 | + let result = ResultWitness::fold(res, 0, |acc, x| acc + x); |
| 47 | + assert_eq!(result, 0); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn test_foldable_result_string_concat() { |
| 52 | + let res: Result<String, String> = Ok("world".to_string()); |
| 53 | + let result = ResultWitness::fold(res, String::new(), |mut acc, x| { |
| 54 | + acc.push_str(&x); |
| 55 | + acc |
| 56 | + }); |
| 57 | + assert_eq!(result, "world"); |
| 58 | +} |
| 59 | + |
| 60 | +// --- VecWitness Foldable Tests --- |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn test_foldable_vec_non_empty() { |
| 64 | + let vec = vec![1, 2, 3]; |
| 65 | + let result = VecWitness::fold(vec, 0, |acc, x| acc + x); |
| 66 | + assert_eq!(result, 6); |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn test_foldable_vec_empty() { |
| 71 | + let vec: Vec<i32> = Vec::new(); |
| 72 | + let result = VecWitness::fold(vec, 0, |acc, x| acc + x); |
| 73 | + assert_eq!(result, 0); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn test_foldable_vec_string_concat() { |
| 78 | + let vec = vec!["hello".to_string(), " ".to_string(), "world".to_string()]; |
| 79 | + let result = VecWitness::fold(vec, String::new(), |mut acc, x| { |
| 80 | + acc.push_str(&x); |
| 81 | + acc |
| 82 | + }); |
| 83 | + assert_eq!(result, "hello world"); |
| 84 | +} |
0 commit comments