test_sma.rs 1014 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // -*- coding: utf-8 -*-
  2. //
  3. // Copyright 2021 Michael Büsch <m@bues.ch>
  4. //
  5. // Licensed under the Apache License version 2.0
  6. // or the MIT license, at your option.
  7. // SPDX-License-Identifier: Apache-2.0 OR MIT
  8. //
  9. extern crate movavg;
  10. use movavg::MovAvg;
  11. #[test]
  12. fn test_sma() {
  13. // Integers
  14. let mut avg: MovAvg<i32, i32, 3> = MovAvg::new(); // window size = 3
  15. assert_eq!(avg.feed(10), 10);
  16. assert_eq!(avg.feed(20), 15);
  17. assert_eq!(avg.feed(30), 20);
  18. assert_eq!(avg.feed(40), 30);
  19. assert_eq!(avg.get(), 30);
  20. // Floats
  21. let mut avg: MovAvg<f64, f64, 3> = MovAvg::new();
  22. assert_eq!(avg.feed(10.0), 10.0);
  23. assert_eq!(avg.feed(20.0), 15.0);
  24. assert_eq!(avg.feed(30.0), 20.0);
  25. assert_eq!(avg.feed(40.0), 30.0);
  26. assert_eq!(avg.get(), 30.0);
  27. // Bigger accumulator
  28. let mut avg: MovAvg<i8, i32, 3> = MovAvg::new();
  29. assert_eq!(avg.feed(100), 100);
  30. assert_eq!(avg.feed(100), 100); // This would overflow an i8 accumulator
  31. }
  32. // vim: ts=4 sw=4 expandtab