libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 // Include boost test framework required headers 00029 #include <boost/test/unit_test.hpp> 00030 #include <boost/test/test_tools.hpp> 00031 00032 // Include CRT/STL required header(s) 00033 #include <stdexcept> 00034 #include <set> 00035 00036 #include <boost/cstdint.hpp> 00037 00038 #include "../../include/moost/testing/test_directory_creator.hpp" 00039 00040 // Include thrift headers 00041 00042 // Include application required header(s) 00043 #include "../../include/moost/kvds/kvds_tch.hpp" 00044 #include "../../include/moost/kvds/kvds_mem.hpp" 00045 #include "../../include/moost/kvds/kvds.hpp" 00046 #include "../../include/moost/kvds/kvds_key_iterator.hpp" 00047 00048 // Imported required namespace(s) 00049 using boost::uint32_t; 00050 using namespace moost::kvds; 00051 using namespace moost::testing; 00052 00053 // Name the test suite 00054 BOOST_AUTO_TEST_SUITE( kvdsIteratorTest ) 00055 00056 // Define the test fixture 00057 struct Fixture 00058 { 00059 test_directory_creator tdc; 00060 00061 // C_tor 00062 Fixture() 00063 { 00064 } 00065 00066 // D_tor 00067 ~Fixture() 00068 { 00069 } 00070 }; 00071 00072 template <typename kvdsT> 00073 class KvdsKeyIteratorTester 00074 { 00075 // NB. We use BOOST_REQUIRE since each test is dependent on the previous one working. 00076 // This is to avoid having to re-populate the datastore for each test (save time). 00077 00078 typedef kvdsT kvds_type; 00079 00080 static uint32_t const val0_mask = 0xF1E2D3C4; 00081 static uint32_t const val1_mask = 0x12345678; 00082 00083 void test_create(kvds_type & kvds) 00084 { 00085 for(uint32_t key = 1 ; key != 0 ; key <<= 1) 00086 { 00087 typename kvds_type::kvds_values_t put_vals; 00088 put_vals.push_back(key ^ val0_mask); 00089 put_vals.push_back(key ^ val1_mask); 00090 00091 typename kvds_type::kvds_values_t get_vals; 00092 00093 BOOST_REQUIRE(!kvds.get(key, get_vals)); 00094 BOOST_REQUIRE(get_vals.empty()); 00095 00096 BOOST_REQUIRE(kvds.put(key, put_vals)); 00097 00098 BOOST_REQUIRE(kvds.get(key, get_vals)); 00099 BOOST_REQUIRE(!get_vals.empty()); 00100 BOOST_REQUIRE(2 == get_vals.size()); 00101 BOOST_REQUIRE(get_vals[0] == put_vals[0]); 00102 BOOST_REQUIRE(get_vals[1] == put_vals[1]); 00103 } 00104 } 00105 00106 void test_iteration_prefix(kvds_type & kvds) 00107 { 00108 // Keys come out in no particular order, so we need 00109 // to build a set of keys we expect to see 00110 std::set<uint32_t> keySet; 00111 00112 // Build a set of all they keys we expect to see 00113 for(uint32_t key = 1;key != 0 ; key <<= 1) 00114 { 00115 keySet.insert(key); 00116 } 00117 00118 KvdsKeyIterator<kvds_type> const itrKvdsEnd; 00119 KvdsKeyIterator<kvds_type> itrAssignKvds; 00120 00121 for(KvdsKeyIterator<kvds_type> itrKvds(kvds) ; itrKvds != itrKvdsEnd ; ++itrKvds) 00122 { 00123 BOOST_REQUIRE(itrKvds != itrKvdsEnd); 00124 00125 BOOST_REQUIRE(keySet.find(*itrKvds) != keySet.end()); 00126 00127 KvdsKeyIterator<kvds_type> itrCopyKvds = itrKvds; 00128 BOOST_REQUIRE(itrKvds == itrCopyKvds); 00129 00130 itrAssignKvds = itrKvds; 00131 BOOST_REQUIRE(itrKvds == itrAssignKvds); 00132 00133 // Found it now remove it, we shouldn't see it again 00134 keySet.erase(*itrKvds); 00135 } 00136 00137 // We populated out keySet with all the keys we expected 00138 // to see if there are any left something when wrong 00139 BOOST_REQUIRE(keySet.empty()); 00140 } 00141 00142 00143 void test_iteration_postfix(kvds_type & kvds) 00144 { 00145 // Keys come out in no particular order, so we need 00146 // to build a set of keys we expect to see 00147 std::set<uint32_t> keySet; 00148 00149 // Build a set of all they keys we expect to see 00150 for(uint32_t key = 1;key != 0 ; key <<= 1) 00151 { 00152 keySet.insert(key); 00153 } 00154 00155 KvdsKeyIterator<kvds_type> const itrKvdsEnd; 00156 KvdsKeyIterator<kvds_type> itrKvds(kvds); 00157 KvdsKeyIterator<kvds_type> itrAssignKvds; 00158 00159 while(itrKvds != itrKvdsEnd) 00160 { 00161 BOOST_REQUIRE(itrKvds != itrKvdsEnd); 00162 00163 uint32_t key = *itrKvds++; 00164 00165 BOOST_REQUIRE(keySet.find(key) != keySet.end()); 00166 00167 KvdsKeyIterator<kvds_type> itrCopyKvds = itrKvds; 00168 BOOST_REQUIRE(itrKvds == itrCopyKvds); 00169 00170 itrAssignKvds = itrKvds; 00171 BOOST_REQUIRE(itrKvds == itrAssignKvds); 00172 00173 // Found it now remove it, we shouldn't see it again 00174 keySet.erase(key); 00175 } 00176 00177 // We populated out keySet with all the keys we expected 00178 // to see if there are any left something when wrong 00179 BOOST_REQUIRE(keySet.empty()); 00180 } 00181 00182 public: 00183 00184 void operator()(kvds_type & kvds) 00185 { 00186 test_create(kvds); 00187 test_iteration_prefix(kvds); 00188 test_iteration_postfix(kvds); 00189 } 00190 }; 00191 00192 namespace { 00193 typedef Kvds<uint32_t, uint32_t> kvds_test_t; 00194 } 00195 00196 BOOST_FIXTURE_TEST_CASE( test_kvds_tch, Fixture ) 00197 { 00198 boost::shared_ptr<KvdsTch> spKvdsTch(new KvdsTch); 00199 spKvdsTch->open(tdc.GetFilePath("KvsaTch").c_str()); 00200 kvds_test_t kvds(spKvdsTch); 00201 KvdsKeyIteratorTester<kvds_test_t>()(kvds); 00202 } 00203 00204 BOOST_FIXTURE_TEST_CASE( test_kvds_mem_map, Fixture ) 00205 { 00206 kvds_test_t kvds(ikvds_ptr_t(new KvdsMemMap)); 00207 KvdsKeyIteratorTester<kvds_test_t>()(kvds); 00208 } 00209 // Define end of test suite 00210 BOOST_AUTO_TEST_SUITE_END()