Stxxl  1.4.0
containers/btree/test_btree.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/btree/test_btree.cpp
00003  *
00004  *  A very basic test
00005  *
00006  *  Part of the STXXL. See http://stxxl.sourceforge.net
00007  *
00008  *  Copyright (C) 2006 Roman Dementiev <dementiev@ira.uka.de>
00009  *
00010  *  Distributed under the Boost Software License, Version 1.0.
00011  *  (See accompanying file LICENSE_1_0.txt or copy at
00012  *  http://www.boost.org/LICENSE_1_0.txt)
00013  **************************************************************************/
00014 
00015 #include <iostream>
00016 
00017 #include <stxxl/bits/containers/btree/btree.h>
00018 #include <stxxl/stats>
00019 #include <stxxl/timer>
00020 
00021 
00022 struct comp_type : public std::less<int>
00023 {
00024     static int max_value()
00025     {
00026         return (std::numeric_limits<int>::max)();
00027     }
00028 };
00029 
00030 typedef stxxl::btree::btree<int, double, comp_type, 4096, 4096, stxxl::SR> btree_type;
00031 
00032 std::ostream & operator << (std::ostream & o, const std::pair<int, double> & obj)
00033 {
00034     o << obj.first << " " << obj.second;
00035     return o;
00036 }
00037 
00038 #define node_cache_size (25 * 1024 * 1024)
00039 #define leaf_cache_size (25 * 1024 * 1024)
00040 
00041 int main(int argc, char * argv[])
00042 {
00043     if (argc < 2)
00044     {
00045         STXXL_MSG("Usage: " << argv[0] << " #ins");
00046         return -1;
00047     }
00048 
00049     btree_type BTree1(node_cache_size, leaf_cache_size);
00050 
00051     unsigned nins = atoi(argv[1]);
00052     if (nins < 100)
00053         nins = 100;
00054 
00055 
00056     stxxl::random_number32 rnd;
00057 
00058     // .begin() .end() test
00059     BTree1[10] = 100.;
00060     btree_type::iterator begin = BTree1.begin();
00061     btree_type::iterator end = BTree1.end();
00062     assert(begin == BTree1.begin());
00063     BTree1[5] = 50.;
00064     btree_type::iterator nbegin = BTree1.begin();
00065     btree_type::iterator nend = BTree1.end();
00066     assert(nbegin == BTree1.begin());
00067     assert(begin != nbegin);
00068     assert(end == nend);
00069     assert(begin != end);
00070     assert(nbegin != end);
00071     assert(begin->first == 10);
00072     assert(begin->second == 100);
00073     assert(nbegin->first == 5);
00074     assert(nbegin->second == 50);
00075 
00076     BTree1[10] = 200.;
00077     assert(begin->second == 200.);
00078 
00079     btree_type::iterator it = BTree1.find(5);
00080     assert(it != BTree1.end());
00081     assert(it->first == 5);
00082     assert(it->second == 50.);
00083     it = BTree1.find(6);
00084     assert(it == BTree1.end());
00085     it = BTree1.find(1000);
00086     assert(it == BTree1.end());
00087 
00088 
00089     int f = BTree1.erase(5);
00090     assert(f == 1);
00091     f = BTree1.erase(6);
00092     assert(f == 0);
00093     f = BTree1.erase(5);
00094     assert(f == 0);
00095 
00096     assert(BTree1.count(10) == 1);
00097     assert(BTree1.count(5) == 0);
00098 
00099     it = BTree1.insert(BTree1.begin(), std::pair<int, double>(7, 70.));
00100     assert(it->second == 70.);
00101     assert(BTree1.size() == 2);
00102     it = BTree1.insert(BTree1.begin(), std::pair<int, double>(10, 300.));
00103     assert(it->second == 200.);
00104     assert(BTree1.size() == 2);
00105 
00106     // test lower_bound
00107 
00108     it = BTree1.lower_bound(6);
00109     assert(it != BTree1.end());
00110     assert(it->first == 7);
00111 
00112     it = BTree1.lower_bound(7);
00113     assert(it != BTree1.end());
00114     assert(it->first == 7);
00115 
00116     it = BTree1.lower_bound(8);
00117     assert(it != BTree1.end());
00118     assert(it->first == 10);
00119 
00120     it = BTree1.lower_bound(11);
00121     assert(it == BTree1.end());
00122 
00123     // test upper_bound
00124 
00125     it = BTree1.upper_bound(6);
00126     assert(it != BTree1.end());
00127     assert(it->first == 7);
00128 
00129     it = BTree1.upper_bound(7);
00130     assert(it != BTree1.end());
00131     assert(it->first == 10);
00132 
00133     it = BTree1.upper_bound(8);
00134     assert(it != BTree1.end());
00135     assert(it->first == 10);
00136 
00137     it = BTree1.upper_bound(10);
00138     assert(it == BTree1.end());
00139 
00140     it = BTree1.upper_bound(11);
00141     assert(it == BTree1.end());
00142 
00143     // test equal_range
00144 
00145     std::pair<btree_type::iterator, btree_type::iterator> it_pair = BTree1.equal_range(1);
00146     assert(BTree1.find(7) == it_pair.first);
00147     assert(BTree1.find(7) == it_pair.second);
00148 
00149     it_pair = BTree1.equal_range(7);
00150     assert(BTree1.find(7) == it_pair.first);
00151     assert(BTree1.find(10) == it_pair.second);
00152 
00153     it_pair = BTree1.equal_range(8);
00154     assert(BTree1.find(10) == it_pair.first);
00155     assert(BTree1.find(10) == it_pair.second);
00156 
00157     it_pair = BTree1.equal_range(10);
00158     assert(BTree1.find(10) == it_pair.first);
00159     assert(BTree1.end() == it_pair.second);
00160 
00161     it_pair = BTree1.equal_range(11);
00162     assert(BTree1.end() == it_pair.first);
00163     assert(BTree1.end() == it_pair.second);
00164 
00165     //
00166 
00167     it = BTree1.lower_bound(0);
00168     BTree1.erase(it);
00169     assert(BTree1.size() == 1);
00170 
00171     BTree1.clear();
00172     assert(BTree1.size() == 0);
00173 
00174     for (unsigned int i = 0; i < nins / 2; ++i)
00175     {
00176         BTree1[rnd() % nins] = 10.1;
00177     }
00178     STXXL_MSG("Size of map: " << BTree1.size());
00179 
00180 
00181     BTree1.clear();
00182 
00183     for (unsigned int i = 0; i < nins / 2; ++i)
00184     {
00185         BTree1[rnd() % nins] = 10.1;
00186     }
00187 
00188     STXXL_MSG("Size of map: " << BTree1.size());
00189 
00190     btree_type BTree2(comp_type(), node_cache_size, leaf_cache_size);
00191 
00192     STXXL_MSG("Construction of BTree3 from BTree1 that has " << BTree1.size() << " elements");
00193     btree_type BTree3(BTree1.begin(), BTree1.end(), comp_type(), node_cache_size, leaf_cache_size);
00194 
00195     assert(BTree3 == BTree1);
00196 
00197     STXXL_MSG("Bulk construction of BTree4 from BTree1 that has " << BTree1.size() << " elements");
00198     btree_type BTree4(BTree1.begin(), BTree1.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00199 
00200     STXXL_MSG("Size of BTree1: " << BTree1.size());
00201     STXXL_MSG("Size of BTree4: " << BTree4.size());
00202 
00203     assert(BTree4 == BTree1);
00204     assert(BTree3 == BTree4);
00205 
00206     BTree4.begin()->second = 0;
00207     assert(BTree3 != BTree4);
00208     assert(BTree4 < BTree3);
00209 
00210     BTree4.begin()->second = 1000;
00211     assert(BTree4 > BTree3);
00212 
00213     assert(BTree3 != BTree4);
00214 
00215     it = BTree4.begin();
00216     ++it;
00217     STXXL_MSG("Size of Btree4 before erase: " << BTree4.size());
00218     BTree4.erase(it, BTree4.end());
00219     STXXL_MSG("Size of Btree4 after erase: " << BTree4.size());
00220     assert(BTree4.size() == 1);
00221 
00222 
00223     STXXL_MSG("Size of Btree1 before erase: " << BTree1.size());
00224     BTree1.erase(BTree1.begin(), BTree1.end());
00225     STXXL_MSG("Size of Btree1 after erase: " << BTree1.size());
00226     assert(BTree1.empty());
00227 
00228     // a copy of BTree3
00229     btree_type BTree5(BTree3.begin(), BTree3.end(), comp_type(), node_cache_size, leaf_cache_size, true);
00230     assert(BTree5 == BTree3);
00231 
00232     btree_type::iterator b3 = BTree3.begin();
00233     btree_type::iterator b4 = BTree4.begin();
00234     btree_type::iterator e3 = BTree3.end();
00235     btree_type::iterator e4 = BTree4.end();
00236 
00237     STXXL_MSG("Testing swapping operation (std::swap)");
00238     std::swap(BTree4, BTree3);
00239     assert(b3 == BTree4.begin());
00240     assert(b4 == BTree3.begin());
00241     assert(e3 == BTree4.end());
00242     assert(e4 == BTree3.end());
00243 
00244     assert(BTree5 == BTree4);
00245     assert(BTree5 != BTree3);
00246 
00247     btree_type::const_iterator cb = BTree3.begin();
00248     btree_type::const_iterator ce = BTree3.end();
00249     const btree_type & CBTree3 = BTree3;
00250     cb = CBTree3.begin();
00251     b3 == cb;
00252     b3 != cb;
00253     cb == b3;
00254     cb != b3;
00255     ce = CBTree3.end();
00256     btree_type::const_iterator cit = CBTree3.find(0);
00257     cit = CBTree3.lower_bound(0);
00258     cit = CBTree3.upper_bound(0);
00259 
00260     std::pair<btree_type::const_iterator, btree_type::const_iterator> cit_pair = CBTree3.equal_range(1);
00261 
00262     assert(CBTree3.max_size() >= CBTree3.size());
00263 
00264 
00265     CBTree3.key_comp();
00266     CBTree3.value_comp();
00267 
00268     double sum = 0.0;
00269 
00270     STXXL_MSG(*stxxl::stats::get_instance());
00271 
00272     stxxl::timer Timer2;
00273     Timer2.start();
00274     cit = BTree5.begin();
00275     for ( ; cit != BTree5.end(); ++cit)
00276         sum += cit->second;
00277 
00278     Timer2.stop();
00279     STXXL_MSG("Scanning with const iterator: " << Timer2.mseconds() << " msec");
00280 
00281     STXXL_MSG(*stxxl::stats::get_instance());
00282 
00283     stxxl::timer Timer1;
00284     Timer1.start();
00285     it = BTree5.begin();
00286     for ( ; it != BTree5.end(); ++it)
00287         sum += it->second;
00288 
00289     Timer1.stop();
00290     STXXL_MSG("Scanning with non const iterator: " << Timer1.mseconds() << " msec");
00291 
00292     STXXL_MSG(*stxxl::stats::get_instance());
00293 
00294     BTree5.disable_prefetching();
00295     BTree5.enable_prefetching();
00296     BTree5.prefetching_enabled();
00297     assert(BTree5.prefetching_enabled());
00298 
00299     STXXL_MSG("All tests passed successfully");
00300 
00301     return 0;
00302 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines