File: test_vector_sizes.cpp

package info (click to toggle)
libstxxl 1.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 45,101; ansic: 4,071; perl: 610; sh: 555; xml: 174; makefile: 18
file content (143 lines) | stat: -rw-r--r-- 4,904 bytes parent folder | download | duplicates (4)
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
/***************************************************************************
 *  tests/containers/test_vector_sizes.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.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 <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);

    // 2013-tb: there is a bug with read-only vectors on mmap backed files:
    // copying from mmapped area will fail for invalid ranges at the end,
    // whereas a usual read() will just stop short at the end. The read-only
    // vector however will always read the last block in full, thus causing a
    // segfault with mmap files. FIXME
    if (strcmp(ft, "mmap") == 0) return;

    test_rdonly<const vector_type>(fn, ft, sz, ofs);
    //-tb: vector always writes data! FIXME
    //test_rdonly<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");

    // multiple of block size
    STXXL_MSG("running test with " << start_elements << " items");
    test(fn, ft, start_elements, 100000000);

    // multiple of page size, but not block size
    STXXL_MSG("running test with " << start_elements << " + 4096 items");
    test(fn, ft, start_elements + 4096, 200000000);

    // multiple of neither block size nor page size
    STXXL_MSG("running test with " << start_elements << " + 4096 + 23 items");
    test(fn, ft, start_elements + 4096 + 23, 300000000);

    // truncate 1 byte
    {
        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");
    }

    // will truncate after the last complete element
    test_rdwr<vector_type>(fn, ft, start_elements + 4096 + 23 - 1, 300000000);

    // truncate 1 more byte
    {
        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");
    }

    // will not truncate
    //-tb: vector already writes data! TODO
    //test_rdonly<vector_type>(fn, ft, start_elements + 4096 + 23 - 2, 300000000);

    // check final size
    {
        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();
    }
}
// vim: et:ts=4:sw=4