Stxxl  1.4.0
mng/unittest.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  mng/unittest.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2007 Roman Dementiev <dementiev@ira.uka.de>
00007  *  Copyright (C) 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00008  *
00009  *  Distributed under the Boost Software License, Version 1.0.
00010  *  (See accompanying file LICENSE_1_0.txt or copy at
00011  *  http://www.boost.org/LICENSE_1_0.txt)
00012  **************************************************************************/
00013 
00014 #include <iostream>
00015 #include <stxxl.h>
00016 
00017 #include <cppunit/extensions/TestFactoryRegistry.h>
00018 #include <cppunit/extensions/HelperMacros.h>
00019 #include <cppunit/ui/text/TestRunner.h>
00020 
00021 
00022 #define BLOCK_SIZE (1024 * 512)
00023 
00024 struct MyType
00025 {
00026     int integer;
00027     char chars[5];
00028 };
00029 
00030 struct my_handler
00031 {
00032     void operator () (stxxl::request * req)
00033     {
00034         STXXL_MSG(req << " done, type=" << req->io_type());
00035     }
00036 };
00037 
00038 typedef stxxl::typed_block<BLOCK_SIZE, MyType> block_type;
00039 
00040 class BMLayerTest : public CppUnit::TestCase
00041 {
00042     CPPUNIT_TEST_SUITE(BMLayerTest);
00043     CPPUNIT_TEST(testIO);
00044     CPPUNIT_TEST(testIO2);
00045     CPPUNIT_TEST(testPrefetchPool);
00046     CPPUNIT_TEST(testWritePool);
00047     CPPUNIT_TEST(testStreams);
00048     CPPUNIT_TEST_SUITE_END();
00049 
00050 public:
00051     BMLayerTest(std::string name) : CppUnit::TestCase(name) { }
00052     BMLayerTest() { }
00053 
00054     void testIO()
00055     {
00056         const unsigned nblocks = 2;
00057         stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
00058         std::vector<int> disks(nblocks, 2);
00059         stxxl::request_ptr * reqs = new stxxl::request_ptr[nblocks];
00060         stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00061         bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
00062 
00063         block_type * block = new block_type;
00064         STXXL_MSG(std::hex);
00065         STXXL_MSG("Allocated block address    : " << long(block));
00066         STXXL_MSG("Allocated block address + 1: " << long(block + 1));
00067         STXXL_MSG(std::dec);
00068         unsigned i = 0;
00069         for (i = 0; i < block_type::size; ++i)
00070         {
00071             block->elem[i].integer = i;
00072             //memcpy (block->elem[i].chars, "STXXL", 4);
00073         }
00074         for (i = 0; i < nblocks; ++i)
00075             reqs[i] = block->write(bids[i], my_handler());
00076 
00077 
00078         std::cout << "Waiting " << std::endl;
00079         stxxl::wait_all(reqs, nblocks);
00080 
00081         for (i = 0; i < nblocks; ++i)
00082         {
00083             reqs[i] = block->read(bids[i], my_handler());
00084             reqs[i]->wait();
00085             for (int j = 0; j < block_type::size; ++j)
00086             {
00087                 CPPUNIT_ASSERT(j == block->elem[j].integer);
00088             }
00089         }
00090 
00091         bm->delete_blocks(bids.begin(), bids.end());
00092 
00093         delete[] reqs;
00094         delete block;
00095     }
00096 
00097     void testIO2()
00098     {
00099         typedef stxxl::typed_block<128 * 1024, double> block_type;
00100         std::vector<block_type::bid_type> bids;
00101         std::vector<stxxl::request_ptr> requests;
00102         stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00103         bm->new_blocks<block_type>(32, stxxl::striping(), std::back_inserter(bids));
00104         block_type * blocks = new block_type[32];
00105         int vIndex;
00106         for (vIndex = 0; vIndex < 32; ++vIndex) {
00107             for (int vIndex2 = 0; vIndex2 < block_type::size; ++vIndex2) {
00108                 blocks[vIndex][vIndex2] = vIndex2;
00109             }
00110         }
00111         for (vIndex = 0; vIndex < 32; ++vIndex) {
00112             requests.push_back(blocks[vIndex].write(bids[vIndex]));
00113         }
00114         stxxl::wait_all(requests.begin(), requests.end());
00115         bm->delete_blocks(bids.begin(), bids.end());
00116         delete[] blocks;
00117     }
00118 
00119 
00120     void testPrefetchPool()
00121     {
00122         stxxl::prefetch_pool<block_type> pool(2);
00123         pool.resize(10);
00124         pool.resize(5);
00125         block_type * blk = new block_type;
00126         block_type::bid_type bid;
00127         stxxl::block_manager::get_instance()->new_block(stxxl::single_disk(), bid);
00128         pool.hint(bid);
00129         pool.read(blk, bid)->wait();
00130         delete blk;
00131     }
00132 
00133     void testWritePool()
00134     {
00135         stxxl::write_pool<block_type> pool(100);
00136         pool.resize(10);
00137         pool.resize(5);
00138         block_type * blk = new block_type;
00139         block_type::bid_type bid;
00140         stxxl::block_manager::get_instance()->new_block(stxxl::single_disk(), bid);
00141         pool.write(blk, bid);
00142     }
00143 
00144     typedef stxxl::typed_block<BLOCK_SIZE, int> block_type1;
00145     typedef stxxl::buf_ostream<block_type1, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_ostream_type;
00146     typedef stxxl::buf_istream<block_type1, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_istream_type;
00147 
00148     void testStreams()
00149     {
00150         const unsigned nblocks = 64;
00151         const unsigned nelements = nblocks * block_type1::size;
00152         stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
00153 
00154         stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00155         bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
00156         {
00157             buf_ostream_type out(bids.begin(), 2);
00158             for (unsigned i = 0; i < nelements; i++)
00159                 out << i;
00160         }
00161         {
00162             buf_istream_type in(bids.begin(), bids.end(), 2);
00163             for (unsigned i = 0; i < nelements; i++)
00164             {
00165                 int value;
00166                 in >> value;
00167                 CPPUNIT_ASSERT(value == int(i));
00168             }
00169         }
00170         bm->delete_blocks(bids.begin(), bids.end());
00171     }
00172 };
00173 
00174 CPPUNIT_TEST_SUITE_REGISTRATION(BMLayerTest);
00175 
00176 int main()
00177 {
00178     CppUnit::TextUi::TestRunner runner;
00179     CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
00180     runner.addTest(registry.makeTest());
00181     bool wasSuccessful = runner.run("", false);
00182     return wasSuccessful ? 0 : 1;
00183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines