|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r67653 - sandbox/guild/pool/libs/pool/test
From: chankwanting_at_[hidden]
Date: 2011-01-04 12:46:13
Author: ktchan
Date: 2011-01-04 12:46:12 EST (Tue, 04 Jan 2011)
New Revision: 67653
URL: http://svn.boost.org/trac/boost/changeset/67653
Log:
Test for GCD & LCM functions. Though should probably refactor code to use Boost.Math (which was branched out from Boost.Pool itself & Boost.Rational) instead.
Added:
sandbox/guild/pool/libs/pool/test/test_gcd_lcm.cpp (contents, props changed)
Text files modified:
sandbox/guild/pool/libs/pool/test/Jamfile.v2 | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
Modified: sandbox/guild/pool/libs/pool/test/Jamfile.v2
==============================================================================
--- sandbox/guild/pool/libs/pool/test/Jamfile.v2 (original)
+++ sandbox/guild/pool/libs/pool/test/Jamfile.v2 2011-01-04 12:46:12 EST (Tue, 04 Jan 2011)
@@ -13,7 +13,8 @@
import testing ;
test-suite pool :
+ [ run test_gcd_lcm.cpp ]
[ run test_pool_alloc.cpp ]
[ run pool_msvc_compiler_bug_test.cpp ]
- [ run test_msvc_mem_leak_detection.cpp ]
+ [ run test_msvc_mem_leak_detection.cpp ]
;
Added: sandbox/guild/pool/libs/pool/test/test_gcd_lcm.cpp
==============================================================================
--- (empty file)
+++ sandbox/guild/pool/libs/pool/test/test_gcd_lcm.cpp 2011-01-04 12:46:12 EST (Tue, 04 Jan 2011)
@@ -0,0 +1,122 @@
+/* Copyright (C) 2011 Kwan Ting Chan
+ *
+ * Use, modification and distribution is subject to the
+ * Boost Software License, Version 1.0. (See accompanying
+ * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <boost/pool/detail/gcd_lcm.hpp>
+#include <boost/pool/detail/ct_gcd_lcm.hpp>
+#include <boost/limits.hpp>
+#include <boost/math/common_factor.hpp>
+
+#include <boost/detail/lightweight_test.hpp>
+
+#include <climits>
+#include <cstdlib>
+#include <ctime>
+
+int main()
+{
+ std::srand(static_cast<unsigned>(std::time(0)));
+
+ {
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(1, 1), 1);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(1, 1), 1);
+ }
+
+ {
+ int r;
+ while((r = std::rand()) < 2) {}
+
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(1, r), 1);
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r, 1), 1);
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r, r), r);
+
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(1, r), r);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(r, 1), r);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(r, r), r);
+ }
+
+ {
+ int m = (std::numeric_limits<int>::max)();
+
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(1, m), 1);
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(m, 1), 1);
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(m, m), m);
+
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(1, m), m);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(m, 1), m);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(m, m), m);
+ }
+
+ {
+ int r1, r2;
+ while((r1 = std::rand()) < 2 || (r2 = std::rand()) < 2) {}
+ int m = (std::numeric_limits<int>::max)();
+
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r1, r2),
+ boost::details::pool::gcd<int>(r2, r1));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r1, r2),
+ boost::math::gcd(r1, r2));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r2, r1),
+ boost::math::gcd(r2, r1));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r1, m),
+ boost::math::gcd(r1, m));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(m, r1),
+ boost::math::gcd(m, r1));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(r2, m),
+ boost::math::gcd(r1, m));
+ BOOST_TEST_EQ(boost::details::pool::gcd<int>(m, r2),
+ boost::math::gcd(m, r1));
+ }
+
+ {
+ int m = (std::numeric_limits<int>::max)();
+ int r1, r2;
+ while((r1 = std::rand()) < 2) {}
+ do { r2 = std::rand(); } while((m/r1) < r2);
+
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(r1, r2),
+ boost::details::pool::lcm<int>(r2, r1));
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(r1, r2),
+ boost::math::lcm(r1, r2));
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(r2, r1),
+ boost::math::lcm(r2, r1));
+
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(46340, 46341), 2147441940);
+ BOOST_TEST_EQ(boost::details::pool::lcm<int>(46341, 46340), 2147441940);
+ }
+
+ {
+ unsigned r;
+ r = boost::details::pool::ct_gcd<1U, 1U>::value;
+ BOOST_TEST_EQ(r, 1U);
+ r = boost::details::pool::ct_lcm<1U, 1U>::value;
+ BOOST_TEST_EQ(r, 1U);
+
+ r = boost::details::pool::ct_gcd<1U, UINT_MAX>::value;
+ BOOST_TEST_EQ(r, 1U);
+ r = boost::details::pool::ct_gcd<UINT_MAX, 1U>::value;
+ BOOST_TEST_EQ(r, 1U);
+ r = boost::details::pool::ct_gcd<UINT_MAX, UINT_MAX>::value;
+ BOOST_TEST_EQ(r, UINT_MAX);
+
+ r = boost::details::pool::ct_lcm<1U, UINT_MAX>::value;
+ BOOST_TEST_EQ(r, UINT_MAX);
+ r = boost::details::pool::ct_lcm<UINT_MAX, 1U>::value;
+ BOOST_TEST_EQ(r, UINT_MAX);
+ r = boost::details::pool::ct_lcm<UINT_MAX, UINT_MAX>::value;
+ BOOST_TEST_EQ(r, UINT_MAX);
+
+ r = boost::details::pool::ct_gcd<3458U, 6916U>::value;
+ BOOST_TEST_EQ(r, 3458U);
+ r = boost::details::pool::ct_gcd<6916U, 3458U>::value;
+ BOOST_TEST_EQ(r, 3458U);
+
+ r = boost::details::pool::ct_lcm<46340U, 46341U>::value;
+ BOOST_TEST_EQ(r, 2147441940U);
+ r = boost::details::pool::ct_lcm<46341U, 46340U>::value;
+ BOOST_TEST_EQ(r, 2147441940U);
+ }
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk