corelib/tests/hash-functions.cc

89 lines
2.2 KiB
C++
Raw Normal View History

#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 );
2020-09-26 10:37:13 +02:00
CPPUNIT_TEST( testPair );
CPPUNIT_TEST_SUITE_END( );
public:
void testInt8( );
void testInt16( );
void testInt32( );
void testInt64( );
void testStructDefault( );
2020-09-26 10:37:13 +02:00
void testPair( );
};
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 ) );
}
2020-09-26 10:37:13 +02:00
void ComputeHashTest::testPair( )
{
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 ) );
}