Files
functional/hash/test/hash_number_test.cpp
Daniel James 60e877acc5 Merge hash and unordered changes.
Remove deprecated headers, move hash_fwd.hpp into hash subdirectory. And
several minor internal changes.
Mostly minor internal details.

Merged revisions 51262-51263,51407-51409,51504-51505,51644-51646,51667 via svnmerge from 
https://svn.boost.org/svn/boost/trunk

........
  r51262 | danieljames | 2009-02-15 19:32:04 +0000 (Sun, 15 Feb 2009) | 1 line
  
  Use the new 'boost:' links for the hash, unordered and quickbook documentation.
........
  r51263 | danieljames | 2009-02-15 19:32:19 +0000 (Sun, 15 Feb 2009) | 2 lines
  
  Don't copy images for the standalone hash and unordered documentation, was only
  really required before the libraries were integrated into boost.
........
  r51407 | danieljames | 2009-02-22 23:49:51 +0000 (Sun, 22 Feb 2009) | 1 line
  
  Fix the hash dirname.
........
  r51408 | danieljames | 2009-02-22 23:50:04 +0000 (Sun, 22 Feb 2009) | 1 line
  
  Make copy_buckets and move_buckets member functions - so that calling them is a bit simpler.
........
  r51409 | danieljames | 2009-02-22 23:50:20 +0000 (Sun, 22 Feb 2009) | 1 line
  
  Move some of the data structure classes out of hash table data.
........
  r51504 | danieljames | 2009-03-01 14:15:09 +0000 (Sun, 01 Mar 2009) | 1 line
  
  Add missing return for operator=.
........
  r51505 | danieljames | 2009-03-01 14:15:39 +0000 (Sun, 01 Mar 2009) | 3 lines
  
  Make the sort stable.
  
  Doesn't really matter, but it might as well be.
........
  r51644 | danieljames | 2009-03-08 09:44:51 +0000 (Sun, 08 Mar 2009) | 1 line
  
  Detab.
........
  r51645 | danieljames | 2009-03-08 09:45:11 +0000 (Sun, 08 Mar 2009) | 4 lines
  
  Move hash_fwd into the hash subdirectory.
  
  I should have done this in the last release. But now all of the hash
  implementation is in the hash subdirectory.
........
  r51646 | danieljames | 2009-03-08 09:45:30 +0000 (Sun, 08 Mar 2009) | 3 lines
  
  Remove deprecated headers.
  
  Fixes #2412.
........
  r51667 | danieljames | 2009-03-09 20:56:23 +0000 (Mon, 09 Mar 2009) | 1 line
  
  Update copyright dates in hash and unordered.
........


[SVN r51729]
2009-03-11 22:51:09 +00:00

168 lines
4.7 KiB
C++

// Copyright 2005-2009 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)
#include "./config.hpp"
#ifdef TEST_STD_INCLUDES
# include <functional>
#else
# include <boost/functional/hash.hpp>
#endif
#include <iostream>
#include <boost/detail/lightweight_test.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/limits.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include "./compile_time.hpp"
#if defined(BOOST_MSVC)
#pragma warning(push)
#pragma warning(disable:4127) // conditional expression is constant
#pragma warning(disable:4309) // truncation of constant value
#pragma warning(disable:4310) // cast truncates constant value
#endif
template <class T>
void numeric_test(T*)
{
typedef boost::hash_detail::limits<T> limits;
compile_time_tests((T*) 0);
HASH_NAMESPACE::hash<T> x1;
HASH_NAMESPACE::hash<T> x2;
T v1 = (T) -5;
BOOST_TEST(x1(v1) == x2(v1));
BOOST_TEST(x1(T(-5)) == x2(T(-5)));
BOOST_TEST(x1(T(0)) == x2(T(0)));
BOOST_TEST(x1(T(10)) == x2(T(10)));
BOOST_TEST(x1(T(25)) == x2(T(25)));
BOOST_TEST(x1(T(5) - T(5)) == x2(T(0)));
BOOST_TEST(x1(T(6) + T(4)) == x2(T(10)));
#if defined(TEST_EXTENSIONS)
BOOST_TEST(x1(T(-5)) == HASH_NAMESPACE::hash_value(T(-5)));
BOOST_TEST(x1(T(0)) == HASH_NAMESPACE::hash_value(T(0)));
BOOST_TEST(x1(T(10)) == HASH_NAMESPACE::hash_value(T(10)));
BOOST_TEST(x1(T(25)) == HASH_NAMESPACE::hash_value(T(25)));
if (limits::is_integer)
{
if(limits::is_signed || limits::digits <= boost::hash_detail::limits<std::size_t>::digits)
BOOST_TEST(HASH_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5));
BOOST_TEST(HASH_NAMESPACE::hash_value(T(0)) == (std::size_t)T(0u));
BOOST_TEST(HASH_NAMESPACE::hash_value(T(10)) == (std::size_t)T(10u));
BOOST_TEST(HASH_NAMESPACE::hash_value(T(25)) == (std::size_t)T(25u));
}
#endif
}
template <class T>
void limits_test(T*)
{
typedef boost::hash_detail::limits<T> limits;
if(limits::is_specialized)
{
HASH_NAMESPACE::hash<T> x1;
HASH_NAMESPACE::hash<T> x2;
T min_value = (limits::min)();
T max_value = (limits::max)();
BOOST_TEST(x1(min_value) == x2((limits::min)()));
BOOST_TEST(x1(max_value) == x2((limits::max)()));
#if defined(TEST_EXTENSIONS)
BOOST_TEST(x1(min_value) == HASH_NAMESPACE::hash_value(min_value));
BOOST_TEST(x1(max_value) == HASH_NAMESPACE::hash_value(max_value));
if (limits::is_integer)
{
BOOST_TEST(HASH_NAMESPACE::hash_value(min_value)
== std::size_t(min_value));
BOOST_TEST(HASH_NAMESPACE::hash_value(max_value)
== std::size_t(max_value));
}
#endif
}
}
template <class T>
void poor_quality_tests(T*)
{
typedef boost::hash_detail::limits<T> limits;
HASH_NAMESPACE::hash<T> x1;
HASH_NAMESPACE::hash<T> x2;
// A hash function can legally fail these tests, but it'll not be a good
// sign.
if(T(1) != T(-1))
BOOST_TEST(x1(T(1)) != x2(T(-1)));
if(T(1) != T(2))
BOOST_TEST(x1(T(1)) != x2(T(2)));
if((limits::max)() != (limits::max)() - 1)
BOOST_TEST(x1((limits::max)()) != x2((limits::max)() - 1));
}
void bool_test()
{
HASH_NAMESPACE::hash<bool> x1;
HASH_NAMESPACE::hash<bool> x2;
BOOST_TEST(x1(true) == x2(true));
BOOST_TEST(x1(false) == x2(false));
BOOST_TEST(x1(true) != x2(false));
BOOST_TEST(x1(false) != x2(true));
}
#define NUMERIC_TEST(type, name) \
std::cerr<<"Testing: " BOOST_STRINGIZE(name) "\n"; \
numeric_test((type*) 0); \
limits_test((type*) 0); \
poor_quality_tests((type*) 0);
#define NUMERIC_TEST_NO_LIMITS(type, name) \
std::cerr<<"Testing: " BOOST_STRINGIZE(name) "\n"; \
numeric_test((type*) 0); \
poor_quality_tests((type*) 0);
int main()
{
NUMERIC_TEST(char, char)
NUMERIC_TEST(signed char, schar)
NUMERIC_TEST(unsigned char, uchar)
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
NUMERIC_TEST(wchar_t, wchar)
#endif
NUMERIC_TEST(short, short)
NUMERIC_TEST(unsigned short, ushort)
NUMERIC_TEST(int, int)
NUMERIC_TEST(unsigned int, uint)
NUMERIC_TEST(long, hash_long)
NUMERIC_TEST(unsigned long, ulong)
#if defined(BOOST_HAS_LONG_LONG)
NUMERIC_TEST_NO_LIMITS(boost::long_long_type, long_long)
NUMERIC_TEST_NO_LIMITS(boost::ulong_long_type, ulong_long)
#endif
NUMERIC_TEST(float, float)
NUMERIC_TEST(double, double)
bool_test();
return boost::report_errors();
}
#if defined(BOOST_MSVC)
#pragma warning(pop)
#endif