|
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
这是问题
write rust language unit test pub fn calculation_ema(array: &Vec<f64>, average: usize) -> Vec<f64> {
let multiplier = 2.0 / (average as f64 + 1.0);
let mut exponential_moving_average = array[0];
let mut result = vec![];
result.push(exponential_moving_average);
for i in 1..array.len() {
exponential_moving_average =
array[i] * multiplier + exponential_moving_average * (1.0 - multiplier);
result.push(exponential_moving_average);
}
result
}
++++++++++++++++++++++++++++++
这是回答
Sure! Here's a unit test for the calculation_ema function in Rust:
rust
Copy code
#[test]
fn test_calculation_ema() {
let array = vec![10.0, 12.0, 15.0, 20.0, 18.0];
let average = 3;
let expected_result = vec![10.0, 11.0, 12.333333333333334, 15.11111111111111, 16.74074074074074];
let result = calculation_ema(&array, average);
assert_eq!(result, expected_result);
}
This test creates an input array [10.0, 12.0, 15.0, 20.0, 18.0] and an average of 3, and expects the output of the calculation_ema function to be [10.0, 11.0, 12.333333333333334, 15.11111111111111, 16.74074074074074]. The test checks that the actual output matches the expected output using the assert_eq macro.
|
|