00001 //---------------------------------------------------------------------------- 00002 /** @file GroupsTest.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include <boost/test/auto_unit_test.hpp> 00007 00008 #include "Groups.hpp" 00009 #include "BoardIterator.hpp" 00010 00011 using namespace benzene; 00012 00013 //--------------------------------------------------------------------------- 00014 00015 namespace { 00016 00017 BOOST_AUTO_TEST_CASE(Groups_Captains) 00018 { 00019 BOOST_REQUIRE(MAX_WIDTH >= 5 && MAX_HEIGHT >= 5); 00020 StoneBoard brd(5, 5); 00021 Groups groups; 00022 // On empty board all edges and cells are captains of themselves. 00023 GroupBuilder::Build(brd, groups); 00024 BOOST_CHECK(groups.GetGroup(NORTH).Captain() == NORTH); 00025 BOOST_CHECK(groups.GetGroup(SOUTH).Captain() == SOUTH); 00026 BOOST_CHECK(groups.GetGroup(EAST).Captain() == EAST); 00027 BOOST_CHECK(groups.GetGroup(WEST).Captain() == WEST); 00028 for (BoardIterator p(brd.Const().Interior()); p; ++p) 00029 BOOST_CHECK(groups.GetGroup(*p).Captain() == *p); 00030 // Check that FIRST_CELL is absorbed into the north group; 00031 // and that NORTH is always the captain of its group. 00032 brd.PlayMove(BLACK, FIRST_CELL); 00033 GroupBuilder::Build(brd, groups); 00034 BOOST_CHECK(groups.GetGroup(NORTH).Captain() == NORTH); 00035 BOOST_CHECK(groups.GetGroup(FIRST_CELL).Captain() == NORTH); 00036 } 00037 00038 BOOST_AUTO_TEST_CASE(Groups_Nbs) 00039 { 00040 BOOST_REQUIRE(MAX_WIDTH >= 5 && MAX_HEIGHT >= 5); 00041 std::string s(". . . . ." 00042 "W B . . ." 00043 ". B . . ." 00044 ". . . . ." 00045 ". . . . ."); 00046 StoneBoard brd(5, 5, s); 00047 Groups groups; 00048 GroupBuilder::Build(brd, groups); 00049 00050 bitset_t nbs = groups.GetGroup(HEX_CELL_B2).Nbs(); 00051 BOOST_CHECK_EQUAL(nbs.count(), 8u); 00052 BOOST_CHECK(nbs.test(HEX_CELL_B1)); 00053 BOOST_CHECK(nbs.test(HEX_CELL_C1)); 00054 BOOST_CHECK(nbs.test(groups.GetGroup(HEX_CELL_A2).Captain())); 00055 BOOST_CHECK(nbs.test(HEX_CELL_C2)); 00056 BOOST_CHECK(nbs.test(HEX_CELL_A3)); 00057 BOOST_CHECK(nbs.test(HEX_CELL_C3)); 00058 BOOST_CHECK(nbs.test(HEX_CELL_A4)); 00059 BOOST_CHECK(nbs.test(HEX_CELL_B4)); 00060 00061 nbs = groups.GetGroup(HEX_CELL_C2).Nbs(); 00062 BOOST_CHECK_EQUAL(nbs.count(), 5u); 00063 BOOST_CHECK(nbs.test(groups.GetGroup(HEX_CELL_B2).Captain())); 00064 00065 nbs = groups.GetGroup(HEX_CELL_A2).Nbs(); 00066 BOOST_CHECK_EQUAL(nbs.count(), 8u); 00067 BOOST_CHECK(nbs.test(NORTH)); 00068 BOOST_CHECK(nbs.test(SOUTH)); 00069 BOOST_CHECK(nbs.test(HEX_CELL_A1)); 00070 BOOST_CHECK(nbs.test(HEX_CELL_B1)); 00071 BOOST_CHECK(nbs.test(HEX_CELL_A3)); 00072 BOOST_CHECK(nbs.test(HEX_CELL_A4)); 00073 BOOST_CHECK(nbs.test(HEX_CELL_A5)); 00074 BOOST_CHECK(nbs.test(groups.GetGroup(HEX_CELL_B2).Captain())); 00075 00076 nbs = groups.GetGroup(HEX_CELL_A3).Nbs(); 00077 BOOST_CHECK_EQUAL(nbs.count(), 3u); 00078 BOOST_CHECK(nbs.test(HEX_CELL_A4)); 00079 BOOST_CHECK(nbs.test(groups.GetGroup(HEX_CELL_A2).Captain())); 00080 BOOST_CHECK(nbs.test(groups.GetGroup(HEX_CELL_B2).Captain())); 00081 } 00082 00083 BOOST_AUTO_TEST_CASE(Groups_Members) 00084 { 00085 std::string s(". . W . W" 00086 "W . B B ." 00087 "B B W B ." 00088 ". B B . W" 00089 ". . . . ."); 00090 StoneBoard brd(5, 5, s); 00091 Groups groups; 00092 GroupBuilder::Build(brd, groups); 00093 BOOST_CHECK_EQUAL(groups.NumGroups(), 20u); 00094 Group grp; 00095 00096 // Check all empties are singletons 00097 for (BoardIterator p(brd.Const().Interior()); p; ++p) 00098 if (brd.GetColor(*p) == EMPTY) 00099 { 00100 BOOST_CHECK_EQUAL(groups.GetGroup(*p).Size(), 1u); 00101 BOOST_CHECK(groups.GetGroup(*p).Members().test(*p)); 00102 } 00103 00104 grp = groups.GetGroup(NORTH); 00105 BOOST_CHECK_EQUAL(grp.Size(), 1u); 00106 BOOST_CHECK(grp.Members().test(NORTH)); 00107 BOOST_CHECK_EQUAL(grp.Captain(), NORTH); 00108 00109 grp = groups.GetGroup(HEX_CELL_C1); 00110 BOOST_CHECK_EQUAL(grp.Size(), 1u); 00111 BOOST_CHECK(grp.Members().test(HEX_CELL_C1)); 00112 BOOST_CHECK_EQUAL(grp.Captain(), HEX_CELL_C1); 00113 00114 grp = groups.GetGroup(HEX_CELL_E1); 00115 BOOST_CHECK_EQUAL(grp.Size(), 3u); 00116 BOOST_CHECK(grp.Members().test(EAST)); 00117 BOOST_CHECK(grp.Members().test(HEX_CELL_E1)); 00118 BOOST_CHECK(grp.Members().test(HEX_CELL_E4)); 00119 BOOST_CHECK_EQUAL(grp.Captain(), EAST); 00120 00121 grp = groups.GetGroup(HEX_CELL_A2); 00122 BOOST_CHECK_EQUAL(grp.Size(), 2u); 00123 BOOST_CHECK(grp.Members().test(HEX_CELL_A2)); 00124 BOOST_CHECK(grp.Members().test(WEST)); 00125 BOOST_CHECK_EQUAL(grp.Captain(), WEST); 00126 00127 grp = groups.GetGroup(HEX_CELL_C2); 00128 BOOST_CHECK_EQUAL(grp.Size(), 7u); 00129 BOOST_CHECK(grp.Members().test(HEX_CELL_C2)); 00130 BOOST_CHECK(grp.Members().test(HEX_CELL_D2)); 00131 BOOST_CHECK(grp.Members().test(HEX_CELL_A3)); 00132 BOOST_CHECK(grp.Members().test(HEX_CELL_B3)); 00133 BOOST_CHECK(grp.Members().test(HEX_CELL_D3)); 00134 BOOST_CHECK(grp.Members().test(HEX_CELL_B4)); 00135 BOOST_CHECK(grp.Members().test(HEX_CELL_C4)); 00136 BOOST_CHECK_EQUAL(grp.Captain(), HEX_CELL_C2); 00137 00138 grp = groups.GetGroup(HEX_CELL_C3); 00139 BOOST_CHECK_EQUAL(grp.Size(), 1u); 00140 BOOST_CHECK(grp.Members().test(HEX_CELL_C3)); 00141 BOOST_CHECK_EQUAL(grp.Captain(), HEX_CELL_C3); 00142 00143 grp = groups.GetGroup(SOUTH); 00144 BOOST_CHECK_EQUAL(grp.Size(), 1u); 00145 BOOST_CHECK(grp.Members().test(SOUTH)); 00146 BOOST_CHECK_EQUAL(grp.Captain(), SOUTH); 00147 } 00148 00149 BOOST_AUTO_TEST_CASE(Groups_Iterator) 00150 { 00151 Groups groups; 00152 std::string s(". . W" 00153 "W W B" 00154 "B . W"); 00155 StoneBoard brd(3, 3, s); 00156 GroupBuilder::Build(brd, groups); 00157 00158 GroupIterator g(groups); 00159 BOOST_CHECK(g); 00160 BOOST_CHECK_EQUAL(g->Captain(), NORTH); 00161 ++g; 00162 BOOST_CHECK(g); 00163 BOOST_CHECK_EQUAL(g->Captain(), EAST); 00164 ++g; 00165 BOOST_CHECK(g); 00166 BOOST_CHECK_EQUAL(g->Captain(), SOUTH); 00167 ++g; 00168 BOOST_CHECK(g); 00169 BOOST_CHECK_EQUAL(g->Captain(), HEX_CELL_A1); 00170 ++g; 00171 BOOST_CHECK(g); 00172 BOOST_CHECK_EQUAL(g->Captain(), HEX_CELL_B1); 00173 ++g; 00174 BOOST_CHECK(g); 00175 BOOST_CHECK_EQUAL(g->Captain(), HEX_CELL_C2); 00176 ++g; 00177 BOOST_CHECK(g); 00178 BOOST_CHECK_EQUAL(g->Captain(), HEX_CELL_B3); 00179 ++g; 00180 BOOST_CHECK(!g); 00181 } 00182 00183 //---------------------------------------------------------------------------- 00184 00185 } // namespace 00186 00187 //----------------------------------------------------------------------------