00001 //--------------------------------------------------------------------------- 00002 /** @file ZobristHashTest.cpp 00003 */ 00004 //--------------------------------------------------------------------------- 00005 00006 #include <boost/test/auto_unit_test.hpp> 00007 00008 #include "ZobristHash.hpp" 00009 00010 using namespace benzene; 00011 00012 //--------------------------------------------------------------------------- 00013 00014 namespace { 00015 00016 BOOST_AUTO_TEST_CASE(ZobristHash_InitializationAndUpdates) 00017 { 00018 // Note: due to the probabilistic nature of Zobrist 00019 // hashing, it is possible that some of these tests 00020 // can fail. However, it should be extremely improbable, 00021 // to the point of never occuring in practice. 00022 // This is true only if USE_PREDEFINED_HASHES is false. 00023 00024 00025 // check that base values do NOT differ 00026 ZobristHash zh1(5, 5); 00027 ZobristHash zh2(5, 5); 00028 BOOST_CHECK(zh1.Hash() == zh2.Hash()); 00029 00030 // check that updates change hash value, reset restores 00031 hash_t h1, h2, h3; 00032 h1 = zh1.Hash(); 00033 zh1.Update(BLACK, FIRST_CELL); 00034 h2 = zh1.Hash(); 00035 BOOST_CHECK(h1 != h2); 00036 zh1.Reset(); 00037 BOOST_CHECK_EQUAL(h1, zh1.Hash()); 00038 zh1.Update(WHITE, FIRST_CELL); 00039 h3 = zh1.Hash(); 00040 BOOST_CHECK(h1 != h3); 00041 BOOST_CHECK(h2 != h3); 00042 zh1.Update(WHITE, FIRST_CELL); 00043 BOOST_CHECK_EQUAL(h1, zh1.Hash()); 00044 zh1.Update(BLACK, FIRST_CELL); 00045 zh1.Update(WHITE, FIRST_CELL); 00046 BOOST_CHECK(h1 != zh1.Hash()); 00047 BOOST_CHECK(h2 != zh1.Hash()); 00048 BOOST_CHECK(h3 != zh1.Hash()); 00049 zh1.Update(BLACK, FIRST_CELL); 00050 BOOST_CHECK_EQUAL(h3, zh1.Hash()); 00051 00052 // check that sequence of updates after reset 00053 // obtains same result as initialization 00054 bitset_t black, white; 00055 BOOST_REQUIRE(FIRST_CELL < FIRST_INVALID - 1); 00056 black.set(FIRST_CELL); 00057 black.set(FIRST_INVALID - 1); 00058 white.set(SWAP_PIECES); 00059 zh1 = ZobristHash(5, 5); 00060 zh1.Compute(black, white); 00061 h1 = zh1.Hash(); 00062 zh1.Reset(); 00063 zh1.Update(BLACK, FIRST_CELL); 00064 BOOST_CHECK(h1 != zh1.Hash()); 00065 zh1.Update(WHITE, SWAP_PIECES); 00066 BOOST_CHECK(h1 != zh1.Hash()); 00067 zh1.Update(BLACK, static_cast<HexPoint>(FIRST_INVALID - 1)); 00068 BOOST_CHECK_EQUAL(h1, zh1.Hash()); 00069 00070 // check that compute gives same result as 00071 // Update and initialization 00072 zh1.Reset(); 00073 zh1.Compute(black, white); 00074 BOOST_CHECK_EQUAL(h1, zh1.Hash()); 00075 zh1.Reset(); 00076 for (int i=0; i<FIRST_INVALID; ++i) { 00077 if (black.test(i)) 00078 zh1.Update(BLACK, static_cast<HexPoint>(i)); 00079 if (white.test(i)) 00080 zh1.Update(WHITE, static_cast<HexPoint>(i)); 00081 } 00082 BOOST_CHECK_EQUAL(h1, zh1.Hash()); 00083 } 00084 00085 } 00086 00087 //---------------------------------------------------------------------------