external_weights.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // (C) Copyright Eric Niebler 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/test/unit_test.hpp>
  6. #include <boost/test/floating_point_comparison.hpp>
  7. #include <boost/accumulators/accumulators.hpp>
  8. #include <boost/accumulators/statistics/stats.hpp>
  9. #include <boost/accumulators/statistics/weighted_mean.hpp>
  10. using namespace boost;
  11. using namespace unit_test;
  12. using namespace accumulators;
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // test_stat
  15. //
  16. void test_stat()
  17. {
  18. accumulator_set<int, stats<tag::weighted_mean>, external<int> > acc;
  19. accumulator_set<void, stats<tag::sum_of_weights>, int> weight_acc;
  20. acc(10, weight = 2); // 20
  21. weight_acc(weight = 2); //
  22. BOOST_CHECK_EQUAL(2, sum_of_weights(weight_acc)); //
  23. //
  24. acc(6, weight = 3); // 18
  25. weight_acc(weight = 3); //
  26. BOOST_CHECK_EQUAL(5, sum_of_weights(weight_acc)); //
  27. //
  28. acc(4, weight = 4); // 16
  29. weight_acc(weight = 4); //
  30. BOOST_CHECK_EQUAL(9, sum_of_weights(weight_acc)); //
  31. //
  32. acc(6, weight = 5); //+ 30
  33. weight_acc(weight = 5); //
  34. BOOST_CHECK_EQUAL(14, sum_of_weights(weight_acc)); //
  35. //= 84 / 14 = 6
  36. BOOST_CHECK_CLOSE(6., weighted_mean(acc, weights = weight_acc), 1e-5);
  37. }
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // init_unit_test_suite
  40. //
  41. test_suite* init_unit_test_suite( int argc, char* argv[] )
  42. {
  43. test_suite *test = BOOST_TEST_SUITE("external_weights test");
  44. test->add(BOOST_TEST_CASE(&test_stat));
  45. return test;
  46. }