corelib/tests/hash-functions.cc

126 lines
3.3 KiB
C++

#include <ebcl/Utilities.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace ebcl;
/* - ComputeHashTest -------------------------------------------------------*/
class ComputeHashTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ComputeHashTest );
CPPUNIT_TEST( testInt8 );
CPPUNIT_TEST( testInt16 );
CPPUNIT_TEST( testInt32 );
CPPUNIT_TEST( testInt64 );
CPPUNIT_TEST( testStructDefault );
CPPUNIT_TEST( testPair );
CPPUNIT_TEST( testTupleEmpty );
CPPUNIT_TEST( testTupleOne );
CPPUNIT_TEST( testTuplePair );
CPPUNIT_TEST( testTupleMore );
CPPUNIT_TEST_SUITE_END( );
public:
void testInt8( );
void testInt16( );
void testInt32( );
void testInt64( );
void testStructDefault( );
void testPair( );
void testTupleEmpty( );
void testTupleOne( );
void testTuplePair( );
void testTupleMore( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( ComputeHashTest );
void ComputeHashTest::testInt8( )
{
const uint8_t t8u = 1;
const int8_t t8su = 1 , t8ss = -1;
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t8u ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t8su ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0xff ) , ComputeHash( t8ss ) );
}
void ComputeHashTest::testInt16( )
{
const uint16_t t16u = 1;
const int16_t t16su = 1, t16ss = -1;
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t16u ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t16su ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0xffff ) , ComputeHash( t16ss ) );
}
void ComputeHashTest::testInt32( )
{
uint32_t t32u = 1;
int32_t t32su = 1, t32ss = -1;
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t32u ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , ComputeHash( t32su ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0xffffffff ) , ComputeHash( t32ss ) );
}
void ComputeHashTest::testInt64( )
{
uint64_t t64u = 0x100000010L;
int64_t t64s = -1;
CPPUNIT_ASSERT_EQUAL( uint32_t( 0x11 ) , ComputeHash( t64u ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , ComputeHash( t64s ) );
}
void ComputeHashTest::testStructDefault( )
{
struct T_Test_ {
uint8_t meh[ 16 ];
};
T_Test_ test;
for ( auto i = 0 ; i < 16 ; i ++ ) {
test.meh[ i ] = i;
}
CPPUNIT_ASSERT_EQUAL( uint32_t( HashData( test.meh , 16 ) ) ,
ComputeHash( test ) );
}
void ComputeHashTest::testPair( )
{
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
uint32_t hv = 0x9e3779b9 + 12;
hv = 128 + 0x9e3779b9 + ( hv << 6 ) + ( hv >> 2 );
CPPUNIT_ASSERT_EQUAL( hv , ComputeHash( p ) );
}
/*----------------------------------------------------------------------------*/
void ComputeHashTest::testTupleEmpty( )
{
const std::tuple< > t{};
CPPUNIT_ASSERT_EQUAL( 0u , ComputeHash( t ) );
}
void ComputeHashTest::testTupleOne( )
{
const std::tuple< uint8_t > t{ 12 };
CPPUNIT_ASSERT_EQUAL( 0x9e3779b9 + 12u , ComputeHash( t ) );
}
void ComputeHashTest::testTuplePair( )
{
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
const auto t{ std::make_tuple< uint8_t , uint32_t >( 12 , 128 ) };
CPPUNIT_ASSERT_EQUAL( ComputeHash( p ) , ComputeHash( t ) );
}
void ComputeHashTest::testTupleMore( )
{
const auto p{ std::make_pair< uint8_t , uint32_t >( 12 , 128 ) };
const auto t{ std::make_tuple< uint8_t , uint32_t , uint8_t >( 12 , 128 , 92 ) };
uint32_t hv = ComputeHash( p );
CombineHash( hv , uint8_t( 92 ) );
CPPUNIT_ASSERT_EQUAL( hv , ComputeHash( t ) );
}