[go: up one dir, main page]

Menu

[b40542]: / mng / unittest.cpp  Maximize  Restore  History

Download this file

183 lines (157 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/***************************************************************************
* mng/unittest.cpp
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2007 Roman Dementiev <dementiev@ira.uka.de>
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
#include <iostream>
#include <stxxl.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#define BLOCK_SIZE (1024 * 512)
struct MyType
{
int integer;
char chars[5];
};
struct my_handler
{
void operator () (stxxl::request * req)
{
STXXL_MSG(req << " done, type=" << req->io_type());
}
};
typedef stxxl::typed_block<BLOCK_SIZE, MyType> block_type;
class BMLayerTest : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(BMLayerTest);
CPPUNIT_TEST(testIO);
CPPUNIT_TEST(testIO2);
CPPUNIT_TEST(testPrefetchPool);
CPPUNIT_TEST(testWritePool);
CPPUNIT_TEST(testStreams);
CPPUNIT_TEST_SUITE_END();
public:
BMLayerTest(std::string name) : CppUnit::TestCase(name) { }
BMLayerTest() { }
void testIO()
{
const unsigned nblocks = 2;
stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
std::vector<int> disks(nblocks, 2);
stxxl::request_ptr * reqs = new stxxl::request_ptr[nblocks];
stxxl::block_manager * bm = stxxl::block_manager::get_instance();
bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
block_type * block = new block_type;
STXXL_MSG(std::hex);
STXXL_MSG("Allocated block address : " << long(block));
STXXL_MSG("Allocated block address + 1: " << long(block + 1));
STXXL_MSG(std::dec);
unsigned i = 0;
for (i = 0; i < block_type::size; ++i)
{
block->elem[i].integer = i;
//memcpy (block->elem[i].chars, "STXXL", 4);
}
for (i = 0; i < nblocks; ++i)
reqs[i] = block->write(bids[i], my_handler());
std::cout << "Waiting " << std::endl;
stxxl::wait_all(reqs, nblocks);
for (i = 0; i < nblocks; ++i)
{
reqs[i] = block->read(bids[i], my_handler());
reqs[i]->wait();
for (int j = 0; j < block_type::size; ++j)
{
CPPUNIT_ASSERT(j == block->elem[j].integer);
}
}
bm->delete_blocks(bids.begin(), bids.end());
delete[] reqs;
delete block;
}
void testIO2()
{
typedef stxxl::typed_block<128 * 1024, double> block_type;
std::vector<block_type::bid_type> bids;
std::vector<stxxl::request_ptr> requests;
stxxl::block_manager * bm = stxxl::block_manager::get_instance();
bm->new_blocks<block_type>(32, stxxl::striping(), std::back_inserter(bids));
block_type * blocks = new block_type[32];
int vIndex;
for (vIndex = 0; vIndex < 32; ++vIndex) {
for (int vIndex2 = 0; vIndex2 < block_type::size; ++vIndex2) {
blocks[vIndex][vIndex2] = vIndex2;
}
}
for (vIndex = 0; vIndex < 32; ++vIndex) {
requests.push_back(blocks[vIndex].write(bids[vIndex]));
}
stxxl::wait_all(requests.begin(), requests.end());
bm->delete_blocks(bids.begin(), bids.end());
delete[] blocks;
}
void testPrefetchPool()
{
stxxl::prefetch_pool<block_type> pool(2);
pool.resize(10);
pool.resize(5);
block_type * blk = new block_type;
block_type::bid_type bid;
stxxl::block_manager::get_instance()->new_block(stxxl::single_disk(), bid);
pool.hint(bid);
pool.read(blk, bid)->wait();
delete blk;
}
void testWritePool()
{
stxxl::write_pool<block_type> pool(100);
pool.resize(10);
pool.resize(5);
block_type * blk = new block_type;
block_type::bid_type bid;
stxxl::block_manager::get_instance()->new_block(stxxl::single_disk(), bid);
pool.write(blk, bid);
}
typedef stxxl::typed_block<BLOCK_SIZE, int> block_type1;
typedef stxxl::buf_ostream<block_type1, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_ostream_type;
typedef stxxl::buf_istream<block_type1, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_istream_type;
void testStreams()
{
const unsigned nblocks = 64;
const unsigned nelements = nblocks * block_type1::size;
stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
stxxl::block_manager * bm = stxxl::block_manager::get_instance();
bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
{
buf_ostream_type out(bids.begin(), 2);
for (unsigned i = 0; i < nelements; i++)
out << i;
}
{
buf_istream_type in(bids.begin(), bids.end(), 2);
for (unsigned i = 0; i < nelements; i++)
{
int value;
in >> value;
CPPUNIT_ASSERT(value == int(i));
}
}
bm->delete_blocks(bids.begin(), bids.end());
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(BMLayerTest);
int main()
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest(registry.makeTest());
bool wasSuccessful = runner.run("", false);
return wasSuccessful ? 0 : 1;
}