[go: up one dir, main page]

Menu

[8e227d]: / io / unittest.cpp  Maximize  Restore  History

Download this file

89 lines (71 with data), 2.3 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
/***************************************************************************
* io/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 <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include <stxxl.h>
using namespace stxxl;
struct my_handler
{
void operator () (request * ptr)
{
STXXL_MSG("Request completed: " << ptr);
}
};
class IOLayerTest : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(IOLayerTest);
CPPUNIT_TEST(testIO);
CPPUNIT_TEST_EXCEPTION(testIOException, stxxl::io_error);
CPPUNIT_TEST_SUITE_END();
public:
IOLayerTest(std::string name) : CppUnit::TestCase(name) { }
IOLayerTest() { }
void testIO()
{
const int size = 1024 * 384;
char * buffer = static_cast<char *>(stxxl::aligned_alloc<BLOCK_ALIGN>(size));
memset(buffer, 0, size);
#ifdef BOOST_MSVC
const char * paths[2] = { "data1", "data2" };
#else
const char * paths[2] = { "/var/tmp/data1", "/var/tmp/data2" };
mmap_file file1(paths[0], file::CREAT | file::RDWR, 0);
file1.set_size(size * 1024);
#endif
syscall_file file2(paths[1], file::CREAT | file::RDWR, 1);
request_ptr req[16];
unsigned i = 0;
for ( ; i < 16; i++)
req[i] = file2.awrite(buffer, i * size, size, my_handler());
wait_all(req, 16);
stxxl::aligned_dealloc<BLOCK_ALIGN>(buffer);
}
void testIOException()
{
unlink("TestFile");
#ifdef BOOST_MSVC
mmap_file file1("TestFile", file::RDWR, 0);
#else
mmap_file file1("TestFile", file::RDWR, 0);
#endif
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(IOLayerTest);
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;
}