Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 5, 2023
1 parent 8937d48 commit 2ddecee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ta_lib/utils/src/change.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub fn change(source: &[f64], length: usize) -> Vec<f64> {
let len = source.len();
let mut change = vec![0.0; len];

if length > len {
return change;
}

for i in length..len {
change[i] = source[i] - source[i - length];
}

change
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_change() {
let source = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let length = 2;
let expected = vec![0.0, 0.0, 2.0, 2.0, 2.0];

let result = change(&source, length);

assert_eq!(result, expected);
}

#[test]
fn test_change_zero_length() {
let source = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let length = 0;
let expected = vec![0.0, 0.0, 0.0, 0.0, 0.0];

let result = change(&source, length);

assert_eq!(result, expected);
}
}
1 change: 1 addition & 0 deletions ta_lib/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod change;
pub mod highest;
pub mod lowest;
pub mod nz;
Expand Down

0 comments on commit 2ddecee

Please sign in to comment.