summaryrefslogtreecommitdiff
path: root/libs/random/test/test_generate_canonical.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libs/random/test/test_generate_canonical.cpp')
-rw-r--r--libs/random/test/test_generate_canonical.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/libs/random/test/test_generate_canonical.cpp b/libs/random/test/test_generate_canonical.cpp
new file mode 100644
index 0000000000..ecf7463aa8
--- /dev/null
+++ b/libs/random/test/test_generate_canonical.cpp
@@ -0,0 +1,103 @@
+/* test_generate_canonical.cpp
+ *
+ * Copyright Steven Watanabe 2011
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * $Id: test_generate_canonical.cpp 71018 2011-04-05 21:27:52Z steven_watanabe $
+ *
+ */
+
+#include <boost/random/generate_canonical.hpp>
+
+#include <boost/random/linear_congruential.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/lagged_fibonacci.hpp>
+#include <boost/cstdint.hpp>
+
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
+
+typedef boost::mpl::vector<
+ boost::random::minstd_rand,
+ boost::random::mt19937,
+ boost::random::lagged_fibonacci607
+> engines;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(test_float, Engine, engines)
+{
+ Engine eng;
+ Engine expected;
+ for(int i = 0; i < 1000; ++i) {
+ float val = boost::random::generate_canonical<float, 64>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(1000);
+ BOOST_CHECK_EQUAL(eng, expected);
+ for(int i = 0; i < 1000; ++i) {
+ float val = boost::random::generate_canonical<float, 12>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(1000);
+ BOOST_CHECK_EQUAL(eng, expected);
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(test_double, Engine, engines)
+{
+ Engine eng;
+ Engine expected;
+ for(int i = 0; i < 1000; ++i) {
+ double val = boost::random::generate_canonical<double, 64>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(2000);
+ BOOST_CHECK_EQUAL(eng, expected);
+ for(int i = 0; i < 1000; ++i) {
+ double val = boost::random::generate_canonical<double, 12>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(1000);
+ BOOST_CHECK_EQUAL(eng, expected);
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(test_long_double, Engine, engines)
+{
+ Engine eng;
+ Engine expected;
+ for(int i = 0; i < 1000; ++i) {
+ long double val = boost::random::generate_canonical<long double, 60>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(2000);
+ BOOST_CHECK_EQUAL(eng, expected);
+ for(int i = 0; i < 1000; ++i) {
+ long double val = boost::random::generate_canonical<long double, 12>(eng);
+ BOOST_CHECK_GE(val, 0);
+ BOOST_CHECK_LT(val, 1);
+ }
+ expected.discard(1000);
+ BOOST_CHECK_EQUAL(eng, expected);
+}
+
+struct max_engine
+{
+ typedef boost::uint32_t result_type;
+ static result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }
+ static result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
+ { return ~boost::uint32_t(0); }
+ result_type operator()() { return (max)(); }
+};
+
+BOOST_AUTO_TEST_CASE(test_max)
+{
+ max_engine eng;
+ BOOST_CHECK_LT((boost::random::generate_canonical<float, 64>(eng)), 1);
+ BOOST_CHECK_LT((boost::random::generate_canonical<double, 64>(eng)), 1);
+ BOOST_CHECK_LT((boost::random::generate_canonical<long double, 64>(eng)), 1);
+}