2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
// Copyright Daniel James 2005. Use, modification, and distribution are
|
|
|
|
// subject to 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-07 21:57:22 +00:00
|
|
|
#include "./config.hpp"
|
|
|
|
|
|
|
|
#ifdef TEST_STD_INCLUDES
|
|
|
|
# include <functional>
|
|
|
|
#else
|
|
|
|
# include <boost/functional/hash/hash.hpp>
|
|
|
|
#endif
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
#define BOOST_AUTO_TEST_MAIN
|
|
|
|
#include <boost/test/auto_unit_test.hpp>
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2005-04-21 23:41:12 +00:00
|
|
|
std::cout<<"\n"
|
|
|
|
"Testing " BOOST_STRINGIZE(HASH_NAMESPACE) "::hash<"<<name<<">\n"
|
|
|
|
"\n";
|
|
|
|
|
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;
|
|
|
|
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(zero == minus_zero);
|
|
|
|
BOOST_CHECK(x1(zero) == x1(minus_zero));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(zero) == HASH_NAMESPACE::hash_value(zero));
|
|
|
|
BOOST_CHECK(x1(minus_zero) == HASH_NAMESPACE::hash_value(minus_zero));
|
|
|
|
|
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__)
|
|
|
|
std::cout<<"Not running infinity checks on Borland, as it causes it to crash.\n";
|
|
|
|
#else
|
2005-04-01 16:58:09 +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;
|
|
|
|
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;
|
|
|
|
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(infinity) == HASH_NAMESPACE::hash_value(infinity));
|
|
|
|
BOOST_CHECK(x1(minus_infinity)
|
|
|
|
== HASH_NAMESPACE::hash_value(minus_infinity));
|
|
|
|
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(infinity == infinity2);
|
|
|
|
BOOST_CHECK(infinity == infinity3);
|
|
|
|
BOOST_CHECK(infinity == infinity4);
|
|
|
|
BOOST_CHECK(x1(infinity) == x1(infinity2));
|
|
|
|
BOOST_CHECK(x1(infinity) == x1(infinity3));
|
|
|
|
BOOST_CHECK(x1(infinity) == x1(infinity4));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(minus_infinity == minus_infinity2);
|
|
|
|
BOOST_CHECK(x1(minus_infinity) == x1(minus_infinity2));
|
|
|
|
BOOST_CHECK(minus_infinity == minus_infinity3);
|
|
|
|
BOOST_CHECK(x1(minus_infinity) == x1(minus_infinity3));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
BOOST_CHECK(infinity != minus_infinity);
|
|
|
|
|
|
|
|
// My hash fails this one, I guess it's not that bad.
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(infinity) == x1(minus_infinity)) {
|
|
|
|
std::cout<<"x1(infinity) == x1(-infinity) == "<<x1(infinity)<<"\n";
|
|
|
|
}
|
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.
|
2005-04-01 22:49:19 +00:00
|
|
|
if(std::numeric_limits<T>::has_denorm) {
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(infinity)) {
|
|
|
|
std::cout<<"x1(denorm_min) == x1(infinity) == "<<x1(infinity)<<"\n";
|
|
|
|
}
|
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(minus_infinity)) {
|
|
|
|
std::cout<<"x1(denorm_min) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
|
|
|
}
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(std::numeric_limits<T>::has_quiet_NaN) {
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(infinity)) {
|
|
|
|
std::cout<<"x1(quiet_NaN) == x1(infinity) == "<<x1(infinity)<<"\n";
|
|
|
|
}
|
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(minus_infinity)) {
|
|
|
|
std::cout<<"x1(quiet_NaN) == x1(-infinity) == "<<x1(minus_infinity)<<"\n";
|
|
|
|
}
|
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
|
|
|
|
|
|
|
T max = (std::numeric_limits<T>::max)();
|
|
|
|
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;
|
|
|
|
|
|
|
|
BOOST_CHECK(x1(max) == HASH_NAMESPACE::hash_value(max));
|
|
|
|
BOOST_CHECK(x1(half_max) == HASH_NAMESPACE::hash_value(half_max));
|
|
|
|
BOOST_CHECK(x1(quarter_max) == HASH_NAMESPACE::hash_value(quarter_max));
|
|
|
|
BOOST_CHECK(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.
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(x1(max) == x1(max));
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(max) != x1(quarter_max));
|
2005-04-01 16:58:09 +00:00
|
|
|
BOOST_CHECK(x1(max) != x1(half_max));
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(max) != x1(three_quarter_max));
|
|
|
|
BOOST_CHECK(x1(quarter_max) == x1(quarter_max));
|
|
|
|
BOOST_CHECK(x1(quarter_max) != x1(half_max));
|
|
|
|
BOOST_CHECK(x1(quarter_max) != x1(three_quarter_max));
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(x1(half_max) == x1(half_max));
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(half_max) != x1(three_quarter_max));
|
|
|
|
BOOST_CHECK(x1(three_quarter_max) == x1(three_quarter_max));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
T v1 = asin((T) 1);
|
|
|
|
T v2 = acos((T) 0);
|
2005-04-07 20:53:20 +00:00
|
|
|
BOOST_CHECK(v1 == v2);
|
|
|
|
BOOST_CHECK(x1(v1) == x1(v2));
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(v1) == HASH_NAMESPACE::hash_value(v1));
|
|
|
|
BOOST_CHECK(x1(v2) == HASH_NAMESPACE::hash_value(v2));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
|
|
|
BOOST_CHECK(x1(std::numeric_limits<T>::epsilon()) != x1((T) 0));
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(std::numeric_limits<T>::epsilon()) ==
|
|
|
|
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::epsilon()));
|
2005-04-01 16:58:09 +00:00
|
|
|
|
2005-04-01 22:49:19 +00:00
|
|
|
// As before.
|
|
|
|
if(std::numeric_limits<T>::has_denorm) {
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(std::numeric_limits<T>::denorm_min()) == x1(zero)) {
|
|
|
|
std::cout<<"x1(denorm_min) == x1(zero) == "<<x1(zero)<<"\n";
|
|
|
|
}
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(x1(std::numeric_limits<T>::denorm_min()) ==
|
|
|
|
HASH_NAMESPACE::hash_value(std::numeric_limits<T>::denorm_min()));
|
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__)
|
2005-04-01 16:58:09 +00:00
|
|
|
if(std::numeric_limits<T>::has_quiet_NaN) {
|
2005-04-21 23:41:12 +00:00
|
|
|
if(x1(std::numeric_limits<T>::quiet_NaN()) == x1(1.0)) {
|
|
|
|
std::cout<<"x1(quiet_NaN) == x1(1.0) == "<<x1(1.0)<<"\n";
|
|
|
|
}
|
2005-04-16 16:56:27 +00:00
|
|
|
BOOST_CHECK(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
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_UNIT_TEST(hash_float_tests)
|
|
|
|
{
|
2005-04-11 22:14:26 +00:00
|
|
|
std::cout<<"Compiler: "<<BOOST_COMPILER<<"\n";
|
|
|
|
std::cout<<"Platform: "<<BOOST_PLATFORM<<"\n";
|
|
|
|
std::cout<<"Library: "<<BOOST_STDLIB<<"\n\n";
|
|
|
|
|
2005-04-22 06:37:41 +00:00
|
|
|
float_tests("float", (float*) 0);
|
2005-04-11 22:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_UNIT_TEST(hash_double_tests)
|
|
|
|
{
|
2005-04-22 06:37:41 +00:00
|
|
|
float_tests("double", (double*) 0);
|
2005-04-11 22:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_UNIT_TEST(hash_long_double_tests)
|
|
|
|
{
|
2005-04-22 06:37:41 +00:00
|
|
|
float_tests("long double", (long double*) 0);
|
2005-04-01 16:58:09 +00:00
|
|
|
}
|