2005-04-01 16:58:09 +00:00
|
|
|
|
2007-08-24 01:05:36 +00:00
|
|
|
// Copyright 2005-2007 Daniel James.
|
|
|
|
// 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)
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-07 21:57:22 +00:00
|
|
|
#include "./config.hpp"
|
|
|
|
|
|
|
|
#ifdef TEST_STD_INCLUDES
|
|
|
|
# include <functional>
|
|
|
|
#else
|
2006-02-13 18:26:00 +00:00
|
|
|
# include <boost/functional/hash.hpp>
|
2005-04-07 21:57:22 +00:00
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
#include <boost/detail/lightweight_test.hpp>
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <boost/limits.hpp>
|
|
|
|
|
2005-04-11 22:14:26 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2005-04-01 16:58:09 +00:00
|
|
|
template <class T>
|
2005-04-22 06:37:41 +00:00
|
|
|
void float_tests(char const* name, T* = 0)
|
2005-04-01 16:58:09 +00:00
|
|
|
{
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"\n"
|
2005-12-04 20:02:08 +00:00
|
|
|
<<"Testing " BOOST_STRINGIZE(HASH_NAMESPACE) "::hash<"<<name<<">\n"
|
|
|
|
<<"\n"
|
2007-08-24 00:42:19 +00:00
|
|
|
<<"std::numeric_limits<T>::digits = "
|
|
|
|
<<std::numeric_limits<T>::digits<<"\n"
|
|
|
|
<<"std::numeric_limits<int>::digits = "
|
|
|
|
<<std::numeric_limits<int>::digits<<"\n"
|
|
|
|
<<"std::numeric_limits<std::size_t>::digits = "
|
|
|
|
<<std::numeric_limits<std::size_t>::digits<<"\n"
|
2005-12-04 20:02:08 +00:00
|
|
|
<<"\n"
|
|
|
|
;
|
2005-04-21 23:41:12 +00:00
|
|
|
|
2005-04-07 21:57:22 +00:00
|
|
|
HASH_NAMESPACE::hash<T> x1;
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
T zero = 0;
|
|
|
|
T minus_zero = (T) -1 * zero;
|
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(zero == minus_zero);
|
|
|
|
BOOST_TEST(x1(zero) == x1(minus_zero));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(x1(zero) == HASH_NAMESPACE::hash_value(zero));
|
|
|
|
BOOST_TEST(x1(minus_zero) == HASH_NAMESPACE::hash_value(minus_zero));
|
2005-04-16 16:56:27 +00:00
|
|
|
|
2005-04-01 16:58:09 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2005-04-17 09:12:01 +00:00
|
|
|
// Doing anything with infinity causes borland to crash.
|
2005-04-21 23:41:12 +00:00
|
|
|
#if defined(__BORLANDC__)
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"Not running infinity checks on Borland, as it causes it to crash.\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
#else
|
2007-08-24 00:42:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_infinity) {
|
2005-04-17 09:12:01 +00:00
|
|
|
T infinity = -log(zero);
|
|
|
|
T infinity2 = (T) 1. / zero;
|
2005-04-01 16:58:09 +00:00
|
|
|
T infinity3 = (T) -1. / minus_zero;
|
2007-08-24 00:42:19 +00:00
|
|
|
T infinity4 = std::numeric_limits<T>::infinity();
|
2005-04-17 09:12:01 +00:00
|
|
|
|
|
|
|
T minus_infinity = log(zero);
|
|
|
|
T minus_infinity2 = (T) -1. / zero;
|
2005-04-01 16:58:09 +00:00
|
|
|
T minus_infinity3 = (T) 1. / minus_zero;
|
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(x1(infinity) == HASH_NAMESPACE::hash_value(infinity));
|
|
|
|
BOOST_TEST(x1(minus_infinity)
|
2005-04-16 16:56:27 +00:00
|
|
|
== HASH_NAMESPACE::hash_value(minus_infinity));
|
|
|
|
|
2006-02-28 23:15:43 +00:00
|
|
|
if(infinity == infinity2)
|
|
|
|
BOOST_TEST(x1(infinity) == x1(infinity2));
|
2007-10-05 09:43:01 +00:00
|
|
|
if(infinity == infinity3)
|
2006-02-28 23:15:43 +00:00
|
|
|
BOOST_TEST(x1(infinity) == x1(infinity3));
|
|
|
|
if(infinity == infinity4)
|
|
|
|
BOOST_TEST(x1(infinity) == x1(infinity4));
|
|
|
|
|
|
|
|
if(minus_infinity == minus_infinity2)
|
|
|
|
BOOST_TEST(x1(minus_infinity) == x1(minus_infinity2));
|
|
|
|
if(minus_infinity == minus_infinity3)
|
|
|
|
BOOST_TEST(x1(minus_infinity) == x1(minus_infinity3));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(infinity != minus_infinity);
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(infinity) == x1(minus_infinity)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(infinity) == x1(-infinity) == "<<x1(infinity)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-01 22:49:19 +00:00
|
|
|
// This should really be 'has_denorm == denorm_present' but some
|
2005-04-21 23:41:12 +00:00
|
|
|
// compilers don't have 'denorm_present'. See also a later use.
|
2007-08-24 00:42:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_denorm) {
|
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(infinity)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(denorm_min) == x1(infinity) == "<<x1(infinity)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2007-08-24 00:42:19 +00:00
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(minus_infinity)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(denorm_min) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
2007-08-24 00:42:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_quiet_NaN) {
|
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(infinity)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(quiet_NaN) == x1(infinity) == "<<x1(infinity)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2007-08-24 00:42:19 +00:00
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(minus_infinity)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(quiet_NaN) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 09:12:01 +00:00
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
T max = (std::numeric_limits<T>::max)();
|
2005-04-01 16:58:09 +00:00
|
|
|
T half_max = max / 2;
|
2005-04-16 16:56:27 +00:00
|
|
|
T quarter_max = max / 4;
|
|
|
|
T three_quarter_max = max - quarter_max;
|
|
|
|
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(x1(max) == HASH_NAMESPACE::hash_value(max));
|
|
|
|
BOOST_TEST(x1(half_max) == HASH_NAMESPACE::hash_value(half_max));
|
|
|
|
BOOST_TEST(x1(quarter_max) == HASH_NAMESPACE::hash_value(quarter_max));
|
|
|
|
BOOST_TEST(x1(three_quarter_max) == HASH_NAMESPACE::hash_value(three_quarter_max));
|
2005-04-21 23:41:12 +00:00
|
|
|
|
|
|
|
// The '!=' tests could legitimately fail, but with my hash it indicates a bug.
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(x1(max) == x1(max));
|
|
|
|
BOOST_TEST(x1(max) != x1(quarter_max));
|
|
|
|
BOOST_TEST(x1(max) != x1(half_max));
|
|
|
|
BOOST_TEST(x1(max) != x1(three_quarter_max));
|
|
|
|
BOOST_TEST(x1(quarter_max) == x1(quarter_max));
|
|
|
|
BOOST_TEST(x1(quarter_max) != x1(half_max));
|
|
|
|
BOOST_TEST(x1(quarter_max) != x1(three_quarter_max));
|
|
|
|
BOOST_TEST(x1(half_max) == x1(half_max));
|
|
|
|
BOOST_TEST(x1(half_max) != x1(three_quarter_max));
|
|
|
|
BOOST_TEST(x1(three_quarter_max) == x1(three_quarter_max));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2007-04-12 20:37:27 +00:00
|
|
|
// Intel with gcc stdlib sometimes segfaults on calls to asin and acos.
|
|
|
|
#if !((defined(__INTEL_COMPILER) || defined(__ICL) || \
|
|
|
|
defined(__ICC) || defined(__ECC)) && \
|
|
|
|
(defined(__GLIBCPP__) || defined(__GLIBCXX__)))
|
2005-04-01 16:58:09 +00:00
|
|
|
T v1 = asin((T) 1);
|
|
|
|
T v2 = acos((T) 0);
|
2005-05-21 17:15:10 +00:00
|
|
|
if(v1 == v2)
|
2006-02-09 19:16:08 +00:00
|
|
|
BOOST_TEST(x1(v1) == x1(v2));
|
|
|
|
BOOST_TEST(x1(v1) == HASH_NAMESPACE::hash_value(v1));
|
|
|
|
BOOST_TEST(x1(v2) == HASH_NAMESPACE::hash_value(v2));
|
2007-04-12 20:37:27 +00:00
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST(x1(std::numeric_limits<T>::epsilon()) ==
|
|
|
|
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::epsilon()));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST(std::numeric_limits<T>::epsilon() != (T) 0);
|
|
|
|
if(x1(std::numeric_limits<T>::epsilon()) == x1((T) 0))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
2005-12-04 20:02:08 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST(-std::numeric_limits<T>::epsilon() != (T) 0);
|
|
|
|
if(x1(-std::numeric_limits<T>::epsilon()) == x1((T) 0))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(-epsilon) == x1(0) == "<<x1((T) 0)<<"\n";
|
2005-12-15 00:22:00 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST((T) 1 + std::numeric_limits<T>::epsilon() != (T) 1);
|
|
|
|
if(x1((T) 1 + std::numeric_limits<T>::epsilon()) == x1((T) 1))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(1 + epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
2005-12-15 00:22:00 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST((T) 1 - std::numeric_limits<T>::epsilon() != (T) 1);
|
|
|
|
if(x1((T) 1 - std::numeric_limits<T>::epsilon()) == x1((T) 1))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(1 - epsilon) == x1(1) == "<<x1((T) 1)<<"\n";
|
2005-12-15 00:22:00 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST((T) -1 + std::numeric_limits<T>::epsilon() != (T) -1);
|
|
|
|
if(x1((T) -1 + std::numeric_limits<T>::epsilon()) == x1((T) -1))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(-1 + epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
2005-12-15 00:22:00 +00:00
|
|
|
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST((T) -1 - std::numeric_limits<T>::epsilon() != (T) -1);
|
|
|
|
if(x1((T) -1 - std::numeric_limits<T>::epsilon()) == x1((T) -1))
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(-1 - epsilon) == x1(-1) == "<<x1((T) -1)<<"\n";
|
2005-12-04 20:02:08 +00:00
|
|
|
|
2005-04-01 22:49:19 +00:00
|
|
|
// As before.
|
2007-08-24 00:42:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_denorm) {
|
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(zero)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(denorm_min) == x1(zero) == "<<x1(zero)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2005-09-19 18:25:30 +00:00
|
|
|
#if !BOOST_WORKAROUND(__DECCXX_VER,<70190006)
|
|
|
|
// The Tru64/CXX standard library prior to 7.1 contains a bug in the
|
2007-08-24 00:42:19 +00:00
|
|
|
// specialization of std::numeric_limits::denorm_min() for long
|
2005-09-19 18:25:30 +00:00
|
|
|
// doubles which causes this test to fail.
|
2007-08-24 00:42:19 +00:00
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) !=
|
|
|
|
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::denorm_min()))
|
2006-02-09 19:16:08 +00:00
|
|
|
{
|
2007-08-24 00:42:19 +00:00
|
|
|
std::cerr<<"x1(std::numeric_limits<T>::denorm_min()) = "
|
|
|
|
<< x1(std::numeric_limits<T>::denorm_min())
|
|
|
|
<< "\nhash_value(std::numeric_limits<T>::denorm_min()) = "
|
2005-05-21 16:46:53 +00:00
|
|
|
<< HASH_NAMESPACE::hash_value(
|
2007-08-24 00:42:19 +00:00
|
|
|
std::numeric_limits<T>::denorm_min())
|
2006-02-09 19:16:08 +00:00
|
|
|
<< "\nx1(0) = "<<x1(0)<<"\n";
|
|
|
|
}
|
2005-09-19 18:25:30 +00:00
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
|
|
|
|
2005-04-17 09:12:01 +00:00
|
|
|
// NaN also causes borland to crash.
|
|
|
|
#if !defined(__BORLANDC__)
|
2007-08-24 00:42:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_quiet_NaN) {
|
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(1.0)) {
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"x1(quiet_NaN) == x1(1.0) == "<<x1(1.0)<<"\n";
|
2005-04-21 23:41:12 +00:00
|
|
|
}
|
2007-08-24 00:42:19 +00:00
|
|
|
BOOST_TEST(x1(std::numeric_limits<T>::quiet_NaN()) ==
|
|
|
|
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::quiet_NaN()));
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
2005-04-17 09:12:01 +00:00
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
|
|
|
|
2007-10-13 17:35:48 +00:00
|
|
|
int main()
|
2005-04-01 16:58:09 +00:00
|
|
|
{
|
2006-02-09 19:16:08 +00:00
|
|
|
std::cerr<<"Compiler: "<<BOOST_COMPILER<<"\n";
|
|
|
|
std::cerr<<"Platform: "<<BOOST_PLATFORM<<"\n";
|
|
|
|
std::cerr<<"Library: "<<BOOST_STDLIB<<"\n\n";
|
2005-04-11 22:14:26 +00:00
|
|
|
|
2005-04-22 06:37:41 +00:00
|
|
|
float_tests("float", (float*) 0);
|
|
|
|
float_tests("double", (double*) 0);
|
|
|
|
float_tests("long double", (long double*) 0);
|
2006-02-09 19:16:08 +00:00
|
|
|
|
|
|
|
return boost::report_errors();
|
|
|
|
}
|
|
|
|
|