31
|
1 |
// Boost GCD & LCM common_factor.hpp test program --------------------------//
|
|
2 |
|
|
3 |
// (C) Copyright Daryle Walker 2001, 2006.
|
|
4 |
// Distributed under the Boost Software License, Version 1.0. (See
|
|
5 |
// accompanying file LICENSE_1_0.txt or copy at
|
|
6 |
// http://www.boost.org/LICENSE_1_0.txt)
|
|
7 |
|
|
8 |
// See http://www.boost.org for most recent version including documentation.
|
|
9 |
|
|
10 |
// Revision History
|
|
11 |
// 01 Dec 2006 Various fixes for old compilers (Joaquín M López Muñoz)
|
|
12 |
// 10 Nov 2006 Make long long and __int64 mutually exclusive (Daryle Walker)
|
|
13 |
// 04 Nov 2006 Use more built-in numeric types, binary-GCD (Daryle Walker)
|
|
14 |
// 03 Nov 2006 Use custom numeric types (Daryle Walker)
|
|
15 |
// 02 Nov 2006 Change to Boost.Test's unit test system (Daryle Walker)
|
|
16 |
// 07 Nov 2001 Initial version (Daryle Walker)
|
|
17 |
|
|
18 |
#define BOOST_TEST_MAIN "Boost.Math GCD & LCM unit tests"
|
|
19 |
|
|
20 |
#include <boost/config.hpp> // for BOOST_MSVC, etc.
|
|
21 |
#include <boost/detail/workaround.hpp>
|
|
22 |
#include <boost/math/common_factor.hpp> // for boost::math::gcd, etc.
|
|
23 |
#include <boost/mpl/list.hpp> // for boost::mpl::list
|
|
24 |
#include <boost/operators.hpp>
|
|
25 |
#include <boost/test/unit_test.hpp>
|
|
26 |
#include <boost/test/test_case_template.hpp>
|
|
27 |
|
|
28 |
#include <istream> // for std::basic_istream
|
|
29 |
#include <limits> // for std::numeric_limits
|
|
30 |
#include <ostream> // for std::basic_ostream
|
|
31 |
|
|
32 |
|
|
33 |
namespace {
|
|
34 |
|
|
35 |
// TODO: add polynominal/non-real type; especially after any switch to the
|
|
36 |
// binary-GCD algorithm for built-in types
|
|
37 |
|
|
38 |
// Custom integer class (template)
|
|
39 |
template < typename IntType, int ID = 0 >
|
|
40 |
class my_wrapped_integer
|
|
41 |
: private ::boost::shiftable1<my_wrapped_integer<IntType, ID>,
|
|
42 |
::boost::operators<my_wrapped_integer<IntType, ID> > >
|
|
43 |
{
|
|
44 |
// Helper type-aliases
|
|
45 |
typedef my_wrapped_integer self_type;
|
|
46 |
typedef IntType self_type::* bool_type;
|
|
47 |
|
|
48 |
// Member data
|
|
49 |
IntType v_;
|
|
50 |
|
|
51 |
public:
|
|
52 |
// Template parameters
|
|
53 |
typedef IntType int_type;
|
|
54 |
|
|
55 |
BOOST_STATIC_CONSTANT(int,id = ID);
|
|
56 |
|
|
57 |
// Lifetime management (use automatic destructor and copy constructor)
|
|
58 |
my_wrapped_integer( int_type const &v = int_type() ) : v_( v ) {}
|
|
59 |
|
|
60 |
#ifdef __ARMCC__
|
|
61 |
#pragma diag_suppress 177
|
|
62 |
#endif
|
|
63 |
// Accessors
|
|
64 |
int_type value() const { return this->v_; }
|
|
65 |
|
|
66 |
// Operators (use automatic copy assignment)
|
|
67 |
operator bool_type() const { return this->v_ ? &self_type::v_ : 0; }
|
|
68 |
|
|
69 |
self_type & operator ++() { ++this->v_; return *this; }
|
|
70 |
self_type & operator --() { --this->v_; return *this; }
|
|
71 |
|
|
72 |
self_type operator ~() const { return self_type( ~this->v_ ); }
|
|
73 |
self_type operator !() const { return self_type( !this->v_ ); }
|
|
74 |
self_type operator +() const { return self_type( +this->v_ ); }
|
|
75 |
self_type operator -() const { return self_type( -this->v_ ); }
|
|
76 |
|
|
77 |
bool operator <( self_type const &r ) const { return this->v_ < r.v_; }
|
|
78 |
bool operator ==( self_type const &r ) const { return this->v_ == r.v_; }
|
|
79 |
|
|
80 |
self_type &operator *=(self_type const &r) {this->v_ *= r.v_; return *this;}
|
|
81 |
self_type &operator /=(self_type const &r) {this->v_ /= r.v_; return *this;}
|
|
82 |
self_type &operator %=(self_type const &r) {this->v_ %= r.v_; return *this;}
|
|
83 |
self_type &operator +=(self_type const &r) {this->v_ += r.v_; return *this;}
|
|
84 |
self_type &operator -=(self_type const &r) {this->v_ -= r.v_; return *this;}
|
|
85 |
self_type &operator<<=(self_type const &r){this->v_ <<= r.v_; return *this;}
|
|
86 |
self_type &operator>>=(self_type const &r){this->v_ >>= r.v_; return *this;}
|
|
87 |
self_type &operator &=(self_type const &r) {this->v_ &= r.v_; return *this;}
|
|
88 |
self_type &operator |=(self_type const &r) {this->v_ |= r.v_; return *this;}
|
|
89 |
self_type &operator ^=(self_type const &r) {this->v_ ^= r.v_; return *this;}
|
|
90 |
|
|
91 |
|
|
92 |
// Input & output
|
|
93 |
friend std::istream & operator >>( std::istream &i, self_type &x )
|
|
94 |
{ return i >> x.v_; }
|
|
95 |
|
|
96 |
friend std::ostream & operator <<( std::ostream &o, self_type const &x )
|
|
97 |
{ return o << x.v_; }
|
|
98 |
|
|
99 |
}; // my_wrapped_integer
|
|
100 |
|
|
101 |
template < typename IntType, int ID >
|
|
102 |
my_wrapped_integer<IntType, ID> abs( my_wrapped_integer<IntType, ID> const &x )
|
|
103 |
{ return ( x < my_wrapped_integer<IntType, ID>(0) ) ? -x : +x; }
|
|
104 |
|
|
105 |
typedef my_wrapped_integer<int> MyInt1;
|
|
106 |
typedef my_wrapped_integer<unsigned> MyUnsigned1;
|
|
107 |
typedef my_wrapped_integer<int, 1> MyInt2;
|
|
108 |
typedef my_wrapped_integer<unsigned, 1> MyUnsigned2;
|
|
109 |
|
|
110 |
// Without these explicit instantiations, MSVC++ 6.5/7.0 does not find
|
|
111 |
// some friend operators in certain contexts.
|
|
112 |
MyInt1 dummy1;
|
|
113 |
MyUnsigned1 dummy2;
|
|
114 |
MyInt2 dummy3;
|
|
115 |
MyUnsigned2 dummy4;
|
|
116 |
|
|
117 |
// Various types to test with each GCD/LCM
|
|
118 |
typedef ::boost::mpl::list<signed char, short, int, long,
|
|
119 |
#ifdef BOOST_HAS_LONG_LONG
|
|
120 |
long long,
|
|
121 |
#elif defined(BOOST_HAS_MS_INT64)
|
|
122 |
__int64,
|
|
123 |
#endif
|
|
124 |
MyInt1> signed_test_types;
|
|
125 |
typedef ::boost::mpl::list<unsigned char, unsigned short, unsigned,
|
|
126 |
unsigned long,
|
|
127 |
#ifdef BOOST_HAS_LONG_LONG
|
|
128 |
unsigned long long,
|
|
129 |
#elif defined(BOOST_HAS_MS_INT64)
|
|
130 |
unsigned __int64,
|
|
131 |
#endif
|
|
132 |
MyUnsigned1, MyUnsigned2> unsigned_test_types;
|
|
133 |
|
|
134 |
} // namespace
|
|
135 |
|
|
136 |
|
|
137 |
// Specialize numeric_limits for _some_ of our types
|
|
138 |
namespace std
|
|
139 |
{
|
|
140 |
|
|
141 |
template < >
|
|
142 |
class numeric_limits< MyInt1 >
|
|
143 |
{
|
|
144 |
typedef MyInt1::int_type int_type;
|
|
145 |
typedef numeric_limits<int_type> limits_type;
|
|
146 |
|
|
147 |
public:
|
|
148 |
BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized);
|
|
149 |
|
|
150 |
static MyInt1 min() throw() { return limits_type::min(); }
|
|
151 |
static MyInt1 max() throw() { return limits_type::max(); }
|
|
152 |
|
|
153 |
BOOST_STATIC_CONSTANT(int, digits = limits_type::digits);
|
|
154 |
BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10);
|
|
155 |
BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed);
|
|
156 |
BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer);
|
|
157 |
BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact);
|
|
158 |
BOOST_STATIC_CONSTANT(int, radix = limits_type::radix);
|
|
159 |
static MyInt1 epsilon() throw() { return limits_type::epsilon(); }
|
|
160 |
static MyInt1 round_error() throw() { return limits_type::round_error(); }
|
|
161 |
|
|
162 |
BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent);
|
|
163 |
BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10);
|
|
164 |
BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent);
|
|
165 |
BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10);
|
|
166 |
|
|
167 |
BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity);
|
|
168 |
BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN);
|
|
169 |
BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN);
|
|
170 |
BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = static_cast<std::float_denorm_style>(limits_type::has_denorm));
|
|
171 |
BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss);
|
|
172 |
|
|
173 |
static MyInt1 infinity() throw() { return limits_type::infinity(); }
|
|
174 |
static MyInt1 quiet_NaN() throw() { return limits_type::quiet_NaN(); }
|
|
175 |
static MyInt1 signaling_NaN() throw() {return limits_type::signaling_NaN();}
|
|
176 |
static MyInt1 denorm_min() throw() { return limits_type::denorm_min(); }
|
|
177 |
|
|
178 |
BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559);
|
|
179 |
BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded);
|
|
180 |
BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo);
|
|
181 |
|
|
182 |
BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps);
|
|
183 |
BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before);
|
|
184 |
BOOST_STATIC_CONSTANT(float_round_style, round_style = static_cast<std::float_round_style>(limits_type::round_style));
|
|
185 |
|
|
186 |
}; // std::numeric_limits<MyInt1>
|
|
187 |
|
|
188 |
template < >
|
|
189 |
class numeric_limits< MyUnsigned1 >
|
|
190 |
{
|
|
191 |
typedef MyUnsigned1::int_type int_type;
|
|
192 |
typedef numeric_limits<int_type> limits_type;
|
|
193 |
|
|
194 |
public:
|
|
195 |
BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized);
|
|
196 |
|
|
197 |
static MyUnsigned1 min() throw() { return limits_type::min(); }
|
|
198 |
static MyUnsigned1 max() throw() { return limits_type::max(); }
|
|
199 |
|
|
200 |
BOOST_STATIC_CONSTANT(int, digits = limits_type::digits);
|
|
201 |
BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10);
|
|
202 |
BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed);
|
|
203 |
BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer);
|
|
204 |
BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact);
|
|
205 |
BOOST_STATIC_CONSTANT(int, radix = limits_type::radix);
|
|
206 |
static MyUnsigned1 epsilon() throw() { return limits_type::epsilon(); }
|
|
207 |
static MyUnsigned1 round_error() throw(){return limits_type::round_error();}
|
|
208 |
|
|
209 |
BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent);
|
|
210 |
BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10);
|
|
211 |
BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent);
|
|
212 |
BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10);
|
|
213 |
|
|
214 |
BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity);
|
|
215 |
BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN);
|
|
216 |
BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN);
|
|
217 |
BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = static_cast<std::float_denorm_style>(limits_type::has_denorm));
|
|
218 |
BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss);
|
|
219 |
|
|
220 |
static MyUnsigned1 infinity() throw() { return limits_type::infinity(); }
|
|
221 |
static MyUnsigned1 quiet_NaN() throw() { return limits_type::quiet_NaN(); }
|
|
222 |
static MyUnsigned1 signaling_NaN() throw()
|
|
223 |
{ return limits_type::signaling_NaN(); }
|
|
224 |
static MyUnsigned1 denorm_min() throw(){ return limits_type::denorm_min(); }
|
|
225 |
|
|
226 |
BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559);
|
|
227 |
BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded);
|
|
228 |
BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo);
|
|
229 |
|
|
230 |
BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps);
|
|
231 |
BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before);
|
|
232 |
BOOST_STATIC_CONSTANT(float_round_style, round_style = static_cast<std::float_round_style>(limits_type::round_style));
|
|
233 |
|
|
234 |
}; // std::numeric_limits<MyUnsigned1>
|
|
235 |
|
|
236 |
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
|
|
237 |
// MSVC 6.0 lacks operator<< for __int64, see
|
|
238 |
// http://support.microsoft.com/default.aspx?scid=kb;en-us;168440
|
|
239 |
|
|
240 |
inline ostream& operator<<(ostream& os, __int64 i)
|
|
241 |
{
|
|
242 |
char buf[20];
|
|
243 |
sprintf(buf,"%I64d", i);
|
|
244 |
os << buf;
|
|
245 |
return os;
|
|
246 |
}
|
|
247 |
|
|
248 |
inline ostream& operator<<(ostream& os, unsigned __int64 i)
|
|
249 |
{
|
|
250 |
char buf[20];
|
|
251 |
sprintf(buf,"%I64u", i);
|
|
252 |
os << buf;
|
|
253 |
return os;
|
|
254 |
}
|
|
255 |
#endif
|
|
256 |
|
|
257 |
} // namespace std
|
|
258 |
|
|
259 |
// GCD tests
|
|
260 |
BOOST_AUTO_TEST_SUITE( gcd_test_suite )
|
|
261 |
|
|
262 |
// GCD on signed integer types
|
|
263 |
BOOST_AUTO_TEST_CASE_TEMPLATE( gcd_int_test, T, signed_test_types )
|
|
264 |
{
|
|
265 |
#ifndef BOOST_MSVC
|
|
266 |
using boost::math::gcd;
|
|
267 |
#else
|
|
268 |
using namespace boost::math;
|
|
269 |
#endif
|
|
270 |
|
|
271 |
// Originally from Boost.Rational tests
|
|
272 |
BOOST_CHECK_EQUAL( gcd<T>( 1, -1), static_cast<T>( 1) );
|
|
273 |
BOOST_CHECK_EQUAL( gcd<T>( -1, 1), static_cast<T>( 1) );
|
|
274 |
BOOST_CHECK_EQUAL( gcd<T>( 1, 1), static_cast<T>( 1) );
|
|
275 |
BOOST_CHECK_EQUAL( gcd<T>( -1, -1), static_cast<T>( 1) );
|
|
276 |
BOOST_CHECK_EQUAL( gcd<T>( 0, 0), static_cast<T>( 0) );
|
|
277 |
BOOST_CHECK_EQUAL( gcd<T>( 7, 0), static_cast<T>( 7) );
|
|
278 |
BOOST_CHECK_EQUAL( gcd<T>( 0, 9), static_cast<T>( 9) );
|
|
279 |
BOOST_CHECK_EQUAL( gcd<T>( -7, 0), static_cast<T>( 7) );
|
|
280 |
BOOST_CHECK_EQUAL( gcd<T>( 0, -9), static_cast<T>( 9) );
|
|
281 |
BOOST_CHECK_EQUAL( gcd<T>( 42, 30), static_cast<T>( 6) );
|
|
282 |
BOOST_CHECK_EQUAL( gcd<T>( 6, -9), static_cast<T>( 3) );
|
|
283 |
BOOST_CHECK_EQUAL( gcd<T>(-10, -10), static_cast<T>(10) );
|
|
284 |
BOOST_CHECK_EQUAL( gcd<T>(-25, -10), static_cast<T>( 5) );
|
|
285 |
BOOST_CHECK_EQUAL( gcd<T>( 3, 7), static_cast<T>( 1) );
|
|
286 |
BOOST_CHECK_EQUAL( gcd<T>( 8, 9), static_cast<T>( 1) );
|
|
287 |
BOOST_CHECK_EQUAL( gcd<T>( 7, 49), static_cast<T>( 7) );
|
|
288 |
}
|
|
289 |
|
|
290 |
// GCD on unmarked signed integer type
|
|
291 |
BOOST_AUTO_TEST_CASE( gcd_unmarked_int_test )
|
|
292 |
{
|
|
293 |
#ifndef BOOST_MSVC
|
|
294 |
using boost::math::gcd;
|
|
295 |
#else
|
|
296 |
using namespace boost::math;
|
|
297 |
#endif
|
|
298 |
|
|
299 |
// The regular signed-integer GCD function performs the unsigned version,
|
|
300 |
// then does an absolute-value on the result. Signed types that are not
|
|
301 |
// marked as such (due to no std::numeric_limits specialization) may be off
|
|
302 |
// by a sign.
|
|
303 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 1, -1 )), MyInt2( 1) );
|
|
304 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( -1, 1 )), MyInt2( 1) );
|
|
305 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 1, 1 )), MyInt2( 1) );
|
|
306 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( -1, -1 )), MyInt2( 1) );
|
|
307 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 0, 0 )), MyInt2( 0) );
|
|
308 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 7, 0 )), MyInt2( 7) );
|
|
309 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 0, 9 )), MyInt2( 9) );
|
|
310 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( -7, 0 )), MyInt2( 7) );
|
|
311 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 0, -9 )), MyInt2( 9) );
|
|
312 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 42, 30 )), MyInt2( 6) );
|
|
313 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 6, -9 )), MyInt2( 3) );
|
|
314 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( -10, -10 )), MyInt2(10) );
|
|
315 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( -25, -10 )), MyInt2( 5) );
|
|
316 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 3, 7 )), MyInt2( 1) );
|
|
317 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 8, 9 )), MyInt2( 1) );
|
|
318 |
BOOST_CHECK_EQUAL( abs(gcd<MyInt2>( 7, 49 )), MyInt2( 7) );
|
|
319 |
}
|
|
320 |
|
|
321 |
// GCD on unsigned integer types
|
|
322 |
BOOST_AUTO_TEST_CASE_TEMPLATE( gcd_unsigned_test, T, unsigned_test_types )
|
|
323 |
{
|
|
324 |
#ifndef BOOST_MSVC
|
|
325 |
using boost::math::gcd;
|
|
326 |
#else
|
|
327 |
using namespace boost::math;
|
|
328 |
#endif
|
|
329 |
|
|
330 |
// Note that unmarked types (i.e. have no std::numeric_limits
|
|
331 |
// specialization) are treated like non/unsigned types
|
|
332 |
BOOST_CHECK_EQUAL( gcd<T>( 1u, 1u), static_cast<T>( 1u) );
|
|
333 |
BOOST_CHECK_EQUAL( gcd<T>( 0u, 0u), static_cast<T>( 0u) );
|
|
334 |
BOOST_CHECK_EQUAL( gcd<T>( 7u, 0u), static_cast<T>( 7u) );
|
|
335 |
BOOST_CHECK_EQUAL( gcd<T>( 0u, 9u), static_cast<T>( 9u) );
|
|
336 |
BOOST_CHECK_EQUAL( gcd<T>(42u, 30u), static_cast<T>( 6u) );
|
|
337 |
BOOST_CHECK_EQUAL( gcd<T>( 3u, 7u), static_cast<T>( 1u) );
|
|
338 |
BOOST_CHECK_EQUAL( gcd<T>( 8u, 9u), static_cast<T>( 1u) );
|
|
339 |
BOOST_CHECK_EQUAL( gcd<T>( 7u, 49u), static_cast<T>( 7u) );
|
|
340 |
}
|
|
341 |
|
|
342 |
// GCD at compile-time
|
|
343 |
BOOST_AUTO_TEST_CASE( gcd_static_test )
|
|
344 |
{
|
|
345 |
#ifndef BOOST_MSVC
|
|
346 |
using boost::math::static_gcd;
|
|
347 |
#else
|
|
348 |
using namespace boost::math;
|
|
349 |
#endif
|
|
350 |
|
|
351 |
// Can't use "BOOST_CHECK_EQUAL", otherwise the "value" member will be
|
|
352 |
// disqualified as compile-time-only constant, needing explicit definition
|
|
353 |
BOOST_CHECK( (static_gcd< 1, 1>::value) == 1 );
|
|
354 |
BOOST_CHECK( (static_gcd< 0, 0>::value) == 0 );
|
|
355 |
BOOST_CHECK( (static_gcd< 7, 0>::value) == 7 );
|
|
356 |
BOOST_CHECK( (static_gcd< 0, 9>::value) == 9 );
|
|
357 |
BOOST_CHECK( (static_gcd<42, 30>::value) == 6 );
|
|
358 |
BOOST_CHECK( (static_gcd< 3, 7>::value) == 1 );
|
|
359 |
BOOST_CHECK( (static_gcd< 8, 9>::value) == 1 );
|
|
360 |
BOOST_CHECK( (static_gcd< 7, 49>::value) == 7 );
|
|
361 |
}
|
|
362 |
|
|
363 |
// TODO: non-built-in signed and unsigned integer tests, with and without
|
|
364 |
// numeric_limits specialization; polynominal tests; note any changes if
|
|
365 |
// built-ins switch to binary-GCD algorithm
|
|
366 |
|
|
367 |
BOOST_AUTO_TEST_SUITE_END()
|
|
368 |
|
|
369 |
|
|
370 |
// LCM tests
|
|
371 |
BOOST_AUTO_TEST_SUITE( lcm_test_suite )
|
|
372 |
|
|
373 |
// LCM on signed integer types
|
|
374 |
BOOST_AUTO_TEST_CASE_TEMPLATE( lcm_int_test, T, signed_test_types )
|
|
375 |
{
|
|
376 |
#ifndef BOOST_MSVC
|
|
377 |
using boost::math::lcm;
|
|
378 |
#else
|
|
379 |
using namespace boost::math;
|
|
380 |
#endif
|
|
381 |
|
|
382 |
// Originally from Boost.Rational tests
|
|
383 |
BOOST_CHECK_EQUAL( lcm<T>( 1, -1), static_cast<T>( 1) );
|
|
384 |
BOOST_CHECK_EQUAL( lcm<T>( -1, 1), static_cast<T>( 1) );
|
|
385 |
BOOST_CHECK_EQUAL( lcm<T>( 1, 1), static_cast<T>( 1) );
|
|
386 |
BOOST_CHECK_EQUAL( lcm<T>( -1, -1), static_cast<T>( 1) );
|
|
387 |
BOOST_CHECK_EQUAL( lcm<T>( 0, 0), static_cast<T>( 0) );
|
|
388 |
BOOST_CHECK_EQUAL( lcm<T>( 6, 0), static_cast<T>( 0) );
|
|
389 |
BOOST_CHECK_EQUAL( lcm<T>( 0, 7), static_cast<T>( 0) );
|
|
390 |
BOOST_CHECK_EQUAL( lcm<T>( -5, 0), static_cast<T>( 0) );
|
|
391 |
BOOST_CHECK_EQUAL( lcm<T>( 0, -4), static_cast<T>( 0) );
|
|
392 |
BOOST_CHECK_EQUAL( lcm<T>( 18, 30), static_cast<T>(90) );
|
|
393 |
BOOST_CHECK_EQUAL( lcm<T>( -6, 9), static_cast<T>(18) );
|
|
394 |
BOOST_CHECK_EQUAL( lcm<T>(-10, -10), static_cast<T>(10) );
|
|
395 |
BOOST_CHECK_EQUAL( lcm<T>( 25, -10), static_cast<T>(50) );
|
|
396 |
BOOST_CHECK_EQUAL( lcm<T>( 3, 7), static_cast<T>(21) );
|
|
397 |
BOOST_CHECK_EQUAL( lcm<T>( 8, 9), static_cast<T>(72) );
|
|
398 |
BOOST_CHECK_EQUAL( lcm<T>( 7, 49), static_cast<T>(49) );
|
|
399 |
}
|
|
400 |
|
|
401 |
// LCM on unmarked signed integer type
|
|
402 |
BOOST_AUTO_TEST_CASE( lcm_unmarked_int_test )
|
|
403 |
{
|
|
404 |
#ifndef BOOST_MSVC
|
|
405 |
using boost::math::lcm;
|
|
406 |
#else
|
|
407 |
using namespace boost::math;
|
|
408 |
#endif
|
|
409 |
|
|
410 |
// The regular signed-integer LCM function performs the unsigned version,
|
|
411 |
// then does an absolute-value on the result. Signed types that are not
|
|
412 |
// marked as such (due to no std::numeric_limits specialization) may be off
|
|
413 |
// by a sign.
|
|
414 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 1, -1 )), MyInt2( 1) );
|
|
415 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( -1, 1 )), MyInt2( 1) );
|
|
416 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 1, 1 )), MyInt2( 1) );
|
|
417 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( -1, -1 )), MyInt2( 1) );
|
|
418 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 0, 0 )), MyInt2( 0) );
|
|
419 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 6, 0 )), MyInt2( 0) );
|
|
420 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 0, 7 )), MyInt2( 0) );
|
|
421 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( -5, 0 )), MyInt2( 0) );
|
|
422 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 0, -4 )), MyInt2( 0) );
|
|
423 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 18, 30 )), MyInt2(90) );
|
|
424 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( -6, 9 )), MyInt2(18) );
|
|
425 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( -10, -10 )), MyInt2(10) );
|
|
426 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 25, -10 )), MyInt2(50) );
|
|
427 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 3, 7 )), MyInt2(21) );
|
|
428 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 8, 9 )), MyInt2(72) );
|
|
429 |
BOOST_CHECK_EQUAL( abs(lcm<MyInt2>( 7, 49 )), MyInt2(49) );
|
|
430 |
}
|
|
431 |
|
|
432 |
// LCM on unsigned integer types
|
|
433 |
BOOST_AUTO_TEST_CASE_TEMPLATE( lcm_unsigned_test, T, unsigned_test_types )
|
|
434 |
{
|
|
435 |
#ifndef BOOST_MSVC
|
|
436 |
using boost::math::lcm;
|
|
437 |
#else
|
|
438 |
using namespace boost::math;
|
|
439 |
#endif
|
|
440 |
|
|
441 |
// Note that unmarked types (i.e. have no std::numeric_limits
|
|
442 |
// specialization) are treated like non/unsigned types
|
|
443 |
BOOST_CHECK_EQUAL( lcm<T>( 1u, 1u), static_cast<T>( 1u) );
|
|
444 |
BOOST_CHECK_EQUAL( lcm<T>( 0u, 0u), static_cast<T>( 0u) );
|
|
445 |
BOOST_CHECK_EQUAL( lcm<T>( 6u, 0u), static_cast<T>( 0u) );
|
|
446 |
BOOST_CHECK_EQUAL( lcm<T>( 0u, 7u), static_cast<T>( 0u) );
|
|
447 |
BOOST_CHECK_EQUAL( lcm<T>(18u, 30u), static_cast<T>(90u) );
|
|
448 |
BOOST_CHECK_EQUAL( lcm<T>( 3u, 7u), static_cast<T>(21u) );
|
|
449 |
BOOST_CHECK_EQUAL( lcm<T>( 8u, 9u), static_cast<T>(72u) );
|
|
450 |
BOOST_CHECK_EQUAL( lcm<T>( 7u, 49u), static_cast<T>(49u) );
|
|
451 |
}
|
|
452 |
|
|
453 |
// LCM at compile-time
|
|
454 |
BOOST_AUTO_TEST_CASE( lcm_static_test )
|
|
455 |
{
|
|
456 |
#ifndef BOOST_MSVC
|
|
457 |
using boost::math::static_lcm;
|
|
458 |
#else
|
|
459 |
using namespace boost::math;
|
|
460 |
#endif
|
|
461 |
|
|
462 |
// Can't use "BOOST_CHECK_EQUAL", otherwise the "value" member will be
|
|
463 |
// disqualified as compile-time-only constant, needing explicit definition
|
|
464 |
BOOST_CHECK( (static_lcm< 1, 1>::value) == 1 );
|
|
465 |
BOOST_CHECK( (static_lcm< 0, 0>::value) == 0 );
|
|
466 |
BOOST_CHECK( (static_lcm< 6, 0>::value) == 0 );
|
|
467 |
BOOST_CHECK( (static_lcm< 0, 7>::value) == 0 );
|
|
468 |
BOOST_CHECK( (static_lcm<18, 30>::value) == 90 );
|
|
469 |
BOOST_CHECK( (static_lcm< 3, 7>::value) == 21 );
|
|
470 |
BOOST_CHECK( (static_lcm< 8, 9>::value) == 72 );
|
|
471 |
BOOST_CHECK( (static_lcm< 7, 49>::value) == 49 );
|
|
472 |
}
|
|
473 |
|
|
474 |
// TODO: see GCD to-do
|
|
475 |
|
|
476 |
BOOST_AUTO_TEST_SUITE_END()
|