http://stxxl.sourceforge.net
<beckmann@cs.uni-frankfurt.de>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/io>
#include <stxxl/vector>
typedef int my_type;
typedef stxxl::VECTOR_GENERATOR<my_type>::result vector_type;
typedef vector_type::block_type block_type;
void test_write(const char* fn, const char* ft, stxxl::unsigned_type sz, my_type ofs)
{
stxxl::file* f = stxxl::create_file(ft, fn, stxxl::file::CREAT | stxxl::file::DIRECT | stxxl::file::RDWR);
{
vector_type v(f);
v.resize(sz);
STXXL_MSG("writing " << v.size() << " elements");
for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
v[i] = ofs + (int)i;
}
delete f;
}
template <typename Vector>
void test_rdwr(const char* fn, const char* ft, stxxl::unsigned_type sz, my_type ofs)
{
stxxl::file* f = stxxl::create_file(ft, fn, stxxl::file::DIRECT | stxxl::file::RDWR);
{
Vector v(f);
STXXL_MSG("reading " << v.size() << " elements (RDWR)");
STXXL_CHECK(v.size() == sz);
for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
STXXL_CHECK(v[i] == ofs + my_type(i));
}
delete f;
}
template <typename Vector>
void test_rdonly(const char* fn, const char* ft, stxxl::unsigned_type sz, my_type ofs)
{
stxxl::file* f = stxxl::create_file(ft, fn, stxxl::file::DIRECT | stxxl::file::RDONLY);
{
Vector v(f);
STXXL_MSG("reading " << v.size() << " elements (RDONLY)");
STXXL_CHECK(v.size() == sz);
for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
STXXL_CHECK(v[i] == ofs + my_type(i));
}
delete f;
}
void test(const char* fn, const char* ft, stxxl::unsigned_type sz, my_type ofs)
{
test_write(fn, ft, sz, ofs);
test_rdwr<const vector_type>(fn, ft, sz, ofs);
test_rdwr<vector_type>(fn, ft, sz, ofs);
if (strcmp(ft, "mmap") == 0) return;
test_rdonly<const vector_type>(fn, ft, sz, ofs);
}
int main(int argc, char** argv)
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " file [filetype]" << std::endl;
return -1;
}
stxxl::config::get_instance();
const char* fn = argv[1];
const char* ft = (argc >= 3) ? argv[2] : "syscall";
stxxl::unsigned_type start_elements = 42 * block_type::size;
STXXL_MSG("using " << ft << " file");
STXXL_MSG("running test with " << start_elements << " items");
test(fn, ft, start_elements, 100000000);
STXXL_MSG("running test with " << start_elements << " + 4096 items");
test(fn, ft, start_elements + 4096, 200000000);
STXXL_MSG("running test with " << start_elements << " + 4096 + 23 items");
test(fn, ft, start_elements + 4096 + 23, 300000000);
{
stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
STXXL_MSG("file size is " << f.size() << " bytes");
f.set_size(f.size() - 1);
STXXL_MSG("truncated to " << f.size() << " bytes");
}
test_rdwr<vector_type>(fn, ft, start_elements + 4096 + 23 - 1, 300000000);
{
stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
STXXL_MSG("file size is " << f.size() << " bytes");
f.set_size(f.size() - 1);
STXXL_MSG("truncated to " << f.size() << " bytes");
}
{
stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
STXXL_MSG("file size is " << f.size() << " bytes");
STXXL_CHECK(f.size() == (start_elements + 4096 + 23 - 1) * sizeof(my_type) - 1);
}
{
stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
f.close_remove();
}
}