math_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file math_test.cpp
  3. /// \brief Unit tests functionality in guessmath.h
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2013-10-10 10:20:33 +0200 (Thu, 10 Oct 2013) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "catch.hpp"
  11. #include "guessmath.h"
  12. TEST_CASE("Historic/add", "Some basic tests of adding values to a Historic") {
  13. Historic<double, 3> history;
  14. REQUIRE(history.size() == 0);
  15. history.add(1);
  16. REQUIRE(history.size() == 1);
  17. REQUIRE(history.sum() == Approx(1));
  18. REQUIRE(history[0] == 1);
  19. REQUIRE(history.mean() == Approx(1));
  20. history.add(2);
  21. REQUIRE(history.size() == 2);
  22. REQUIRE(history.sum() == Approx(3));
  23. REQUIRE(history[0] == 1);
  24. REQUIRE(history[1] == 2);
  25. REQUIRE(history.mean() == Approx(1.5));
  26. history.add(3);
  27. REQUIRE(history.size() == 3);
  28. REQUIRE(history.sum() == Approx(6));
  29. REQUIRE(history[0] == 1);
  30. REQUIRE(history[1] == 2);
  31. REQUIRE(history[2] == 3);
  32. REQUIRE(history.mean() == Approx(2));
  33. history.add(4);
  34. REQUIRE(history.size() == 3);
  35. REQUIRE(history.sum() == Approx(9));
  36. REQUIRE(history[0] == 2);
  37. REQUIRE(history[1] == 3);
  38. REQUIRE(history[2] == 4);
  39. REQUIRE(history.mean() == Approx(3));
  40. }
  41. TEST_CASE("variation_coefficient", "Tests of variation coefficient") {
  42. double single_value[] = { 7 };
  43. REQUIRE(variation_coefficient(single_value, 1) == Approx(-1));
  44. double two_values[] = { 7 , 7};
  45. REQUIRE(variation_coefficient(two_values, 2) == Approx(0));
  46. double values[] = { 2, 4, 4, 4, 5, 5, 7, 9 };
  47. REQUIRE(variation_coefficient(values, 8) == Approx(0.427618));
  48. }
  49. TEST_CASE("Historic/periodic", "Some basic tests of the period functions in Historic") {
  50. Historic<double, 3> history;
  51. REQUIRE(history.size() == 0);
  52. history.add(1);
  53. history.add(2);
  54. history.add(3);
  55. REQUIRE(history.periodicsum(2) == Approx(5));
  56. REQUIRE(history.periodicmean(2) == Approx(2.5));
  57. REQUIRE(history.min() == Approx(1));
  58. REQUIRE(history.max() == Approx(3));
  59. history.add(4);
  60. REQUIRE(history.periodicsum(2) == Approx(7));
  61. REQUIRE(history.periodicmean(2) == Approx(3.5));
  62. REQUIRE(history.lastadd() == Approx(4));
  63. REQUIRE(history.min() == Approx(2));
  64. REQUIRE(history.max() == Approx(4));
  65. }