Stxxl  1.4.0
containers/test_vector_sizes.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  containers/test_vector_sizes.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #include <stxxl/io>
00014 #include <stxxl/vector>
00015 
00016 
00017 typedef int my_type;
00018 typedef stxxl::VECTOR_GENERATOR<my_type>::result vector_type;
00019 typedef vector_type::block_type block_type;
00020 
00021 void test_write(const char * fn, const char * ft, stxxl::unsigned_type sz, my_type ofs)
00022 {
00023     stxxl::file * f = stxxl::create_file(ft, fn, stxxl::file::CREAT | stxxl::file::DIRECT | stxxl::file::RDWR);
00024     {
00025         vector_type v(f);
00026         v.resize(sz);
00027         STXXL_MSG("writing " << v.size() << " elements");
00028         for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
00029             v[i] = ofs + i;
00030     }
00031     delete f;
00032 }
00033 
00034 template <typename Vector>
00035 void test_rdwr(const char * fn, const char * ft, stxxl::unsigned_type sz, my_type ofs)
00036 {
00037     stxxl::file * f = stxxl::create_file(ft, fn, stxxl::file::DIRECT | stxxl::file::RDWR);
00038     {
00039         Vector v(f);
00040         STXXL_MSG("reading " << v.size() << " elements (RDWR)");
00041         assert(v.size() == sz);
00042         for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
00043             assert(v[i] == ofs + my_type(i));
00044     }
00045     delete f;
00046 }
00047 
00048 template <typename Vector>
00049 void test_rdonly(const char * fn, const char * ft, stxxl::unsigned_type sz, my_type ofs)
00050 {
00051     stxxl::file * f = stxxl::create_file(ft, fn, stxxl::file::DIRECT | stxxl::file::RDONLY);
00052     {
00053         Vector v(f);
00054         STXXL_MSG("reading " << v.size() << " elements (RDONLY)");
00055         assert(v.size() == sz);
00056         for (stxxl::unsigned_type i = 0; i < v.size(); ++i)
00057             assert(v[i] == ofs + my_type(i));
00058     }
00059     delete f;
00060 }
00061 
00062 void test(const char * fn, const char * ft, stxxl::unsigned_type sz, my_type ofs)
00063 {
00064     test_write(fn, ft, sz, ofs);
00065     test_rdwr<const vector_type>(fn, ft, sz, ofs);
00066     test_rdwr<vector_type>(fn, ft, sz, ofs);
00067     test_rdonly<const vector_type>(fn, ft, sz, ofs);
00068     test_rdonly<vector_type>(fn, ft, sz, ofs);
00069 }
00070 
00071 int main(int argc, char ** argv)
00072 {
00073     if (argc < 2)
00074     {
00075         std::cout << "Usage: " << argv[0] << " file [filetype]" << std::endl;
00076         return -1;
00077     }
00078 
00079     stxxl::config::get_instance();
00080 
00081     const char * fn = argv[1];
00082     const char * ft = "syscall";
00083     if (argc >= 3)
00084         ft = argv[2];
00085 
00086     stxxl::unsigned_type start_elements = 42 * block_type::size;
00087 
00088     STXXL_MSG("using " << ft << " file");
00089 
00090     // multiple of block size
00091     test(fn, ft, start_elements, 100000000);
00092 
00093     // multiple of page size, but not block size
00094     test(fn, ft, start_elements + 4096, 200000000);
00095 
00096     // multiple of neither block size nor page size
00097     test(fn, ft, start_elements + 4096 + 23, 300000000);
00098 
00099     // truncate 1 byte
00100     {
00101         stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
00102         STXXL_MSG("file size is " << f.size() << " bytes");
00103         f.set_size(f.size() - 1);
00104         STXXL_MSG("truncated to " << f.size() << " bytes");
00105     }
00106 
00107     // will truncate after the last complete element
00108     test_rdwr<vector_type>(fn, ft, start_elements + 4096 + 23 - 1, 300000000);
00109 
00110     // truncate 1 more byte
00111     {
00112         stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
00113         STXXL_MSG("file size is " << f.size() << " bytes");
00114         f.set_size(f.size() - 1);
00115         STXXL_MSG("truncated to " << f.size() << " bytes");
00116     }
00117 
00118     // will not truncate
00119     test_rdonly<vector_type>(fn, ft, start_elements + 4096 + 23 - 2, 300000000);
00120 
00121     // check final size
00122     {
00123         stxxl::syscall_file f(fn, stxxl::file::DIRECT | stxxl::file::RDWR);
00124         STXXL_MSG("file size is " << f.size() << " bytes");
00125         assert(f.size() == (start_elements + 4096 + 23 - 1) * sizeof(my_type) - 1);
00126     }
00127 }
00128 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines