📄 common_factor_test.cpp
字号:
// Boost GCD & LCM common_factor.hpp test program --------------------------//// (C) Copyright Daryle Walker 2001, 2006.// 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)// See http://www.boost.org for most recent version including documentation.// Revision History// 01 Dec 2006 Various fixes for old compilers (Joaquin M Lopez Munoz)// 10 Nov 2006 Make long long and __int64 mutually exclusive (Daryle Walker)// 04 Nov 2006 Use more built-in numeric types, binary-GCD (Daryle Walker)// 03 Nov 2006 Use custom numeric types (Daryle Walker)// 02 Nov 2006 Change to Boost.Test's unit test system (Daryle Walker)// 07 Nov 2001 Initial version (Daryle Walker)#define BOOST_TEST_MAIN "Boost.Math GCD & LCM unit tests"#include <boost/config.hpp> // for BOOST_MSVC, etc.#include <boost/detail/workaround.hpp>#include <boost/math/common_factor.hpp> // for boost::math::gcd, etc.#include <boost/mpl/list.hpp> // for boost::mpl::list#include <boost/operators.hpp>#include <boost/test/unit_test.hpp>#include <boost/test/test_case_template.hpp>#include <istream> // for std::basic_istream#include <limits> // for std::numeric_limits#include <ostream> // for std::basic_ostreamnamespace {// TODO: add polynominal/non-real type; especially after any switch to the// binary-GCD algorithm for built-in types// Custom integer class (template)template < typename IntType, int ID = 0 >class my_wrapped_integer : private ::boost::shiftable1<my_wrapped_integer<IntType, ID>, ::boost::operators<my_wrapped_integer<IntType, ID> > >{ // Helper type-aliases typedef my_wrapped_integer self_type; typedef IntType self_type::* bool_type; // Member data IntType v_;public: // Template parameters typedef IntType int_type; BOOST_STATIC_CONSTANT(int,id = ID); // Lifetime management (use automatic destructor and copy constructor) my_wrapped_integer( int_type const &v = int_type() ) : v_( v ) {} // Accessors int_type value() const { return this->v_; } // Operators (use automatic copy assignment) operator bool_type() const { return this->v_ ? &self_type::v_ : 0; } self_type & operator ++() { ++this->v_; return *this; } self_type & operator --() { --this->v_; return *this; } self_type operator ~() const { return self_type( ~this->v_ ); } self_type operator !() const { return self_type( !this->v_ ); } self_type operator +() const { return self_type( +this->v_ ); } self_type operator -() const { return self_type( -this->v_ ); } bool operator <( self_type const &r ) const { return this->v_ < r.v_; } bool operator ==( self_type const &r ) const { return this->v_ == r.v_; } self_type &operator *=(self_type const &r) {this->v_ *= r.v_; return *this;} self_type &operator /=(self_type const &r) {this->v_ /= r.v_; return *this;} self_type &operator %=(self_type const &r) {this->v_ %= r.v_; return *this;} self_type &operator +=(self_type const &r) {this->v_ += r.v_; return *this;} self_type &operator -=(self_type const &r) {this->v_ -= r.v_; return *this;} self_type &operator<<=(self_type const &r){this->v_ <<= r.v_; return *this;} self_type &operator>>=(self_type const &r){this->v_ >>= r.v_; return *this;} self_type &operator &=(self_type const &r) {this->v_ &= r.v_; return *this;} self_type &operator |=(self_type const &r) {this->v_ |= r.v_; return *this;} self_type &operator ^=(self_type const &r) {this->v_ ^= r.v_; return *this;} // Input & output friend std::istream & operator >>( std::istream &i, self_type &x ) { return i >> x.v_; } friend std::ostream & operator <<( std::ostream &o, self_type const &x ) { return o << x.v_; }}; // my_wrapped_integertemplate < typename IntType, int ID >my_wrapped_integer<IntType, ID> abs( my_wrapped_integer<IntType, ID> const &x ){ return ( x < my_wrapped_integer<IntType, ID>(0) ) ? -x : +x; }typedef my_wrapped_integer<int> MyInt1;typedef my_wrapped_integer<unsigned> MyUnsigned1;typedef my_wrapped_integer<int, 1> MyInt2;typedef my_wrapped_integer<unsigned, 1> MyUnsigned2;// Without these explicit instantiations, MSVC++ 6.5/7.0 does not find// some friend operators in certain contexts.MyInt1 dummy1;MyUnsigned1 dummy2;MyInt2 dummy3;MyUnsigned2 dummy4;// Various types to test with each GCD/LCMtypedef ::boost::mpl::list<signed char, short, int, long,#ifdef BOOST_HAS_LONG_LONG boost::long_long_type,#elif defined(BOOST_HAS_MS_INT64) __int64,#endif MyInt1> signed_test_types;typedef ::boost::mpl::list<unsigned char, unsigned short, unsigned, unsigned long,#ifdef BOOST_HAS_LONG_LONG boost::ulong_long_type,#elif defined(BOOST_HAS_MS_INT64) unsigned __int64,#endif MyUnsigned1, MyUnsigned2> unsigned_test_types;} // namespace#define BOOST_NO_MACRO_EXPAND /**/// Specialize numeric_limits for _some_ of our typesnamespace std{template < >class numeric_limits< MyInt1 >{ typedef MyInt1::int_type int_type; typedef numeric_limits<int_type> limits_type;public: BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized); static MyInt1 min BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::min)(); } static MyInt1 max BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::max)(); } BOOST_STATIC_CONSTANT(int, digits = limits_type::digits); BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10); BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed); BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer); BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact); BOOST_STATIC_CONSTANT(int, radix = limits_type::radix); static MyInt1 epsilon() throw() { return limits_type::epsilon(); } static MyInt1 round_error() throw() { return limits_type::round_error(); } BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent); BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10); BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent); BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10); BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity); BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN); BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN); BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = limits_type::has_denorm); BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss); static MyInt1 infinity() throw() { return limits_type::infinity(); } static MyInt1 quiet_NaN() throw() { return limits_type::quiet_NaN(); } static MyInt1 signaling_NaN() throw() {return limits_type::signaling_NaN();} static MyInt1 denorm_min() throw() { return limits_type::denorm_min(); } BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559); BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded); BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo); BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps); BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before); BOOST_STATIC_CONSTANT(float_round_style, round_style = limits_type::round_style);}; // std::numeric_limits<MyInt1>template < >class numeric_limits< MyUnsigned1 >{ typedef MyUnsigned1::int_type int_type; typedef numeric_limits<int_type> limits_type;public: BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized); static MyUnsigned1 min BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::min)(); } static MyUnsigned1 max BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::max)(); } BOOST_STATIC_CONSTANT(int, digits = limits_type::digits); BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10); BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed); BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer); BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact); BOOST_STATIC_CONSTANT(int, radix = limits_type::radix); static MyUnsigned1 epsilon() throw() { return limits_type::epsilon(); } static MyUnsigned1 round_error() throw(){return limits_type::round_error();} BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent); BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10); BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent); BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10); BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity); BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN); BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN); BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = limits_type::has_denorm); BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss); static MyUnsigned1 infinity() throw() { return limits_type::infinity(); } static MyUnsigned1 quiet_NaN() throw() { return limits_type::quiet_NaN(); } static MyUnsigned1 signaling_NaN() throw() { return limits_type::signaling_NaN(); } static MyUnsigned1 denorm_min() throw(){ return limits_type::denorm_min(); } BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559); BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded); BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo); BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps); BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before); BOOST_STATIC_CONSTANT(float_round_style, round_style = limits_type::round_style);}; // std::numeric_limits<MyUnsigned1>#if BOOST_WORKAROUND(BOOST_MSVC,<1300)// MSVC 6.0 lacks operator<< for __int64, see// http://support.microsoft.com/default.aspx?scid=kb;en-us;168440inline ostream& operator<<(ostream& os, __int64 i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -