Stxxl  1.4.0
io/unittest.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  io/unittest.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2007 Roman Dementiev <dementiev@ira.uka.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 <cppunit/extensions/TestFactoryRegistry.h>
00014 #include <cppunit/extensions/HelperMacros.h>
00015 #include <cppunit/ui/text/TestRunner.h>
00016 #include <stxxl.h>
00017 
00018 
00019 struct my_handler
00020 {
00021     void operator () (stxxl::request * ptr)
00022     {
00023         STXXL_MSG("Request completed: " << ptr);
00024     }
00025 };
00026 
00027 
00028 class IOLayerTest : public CppUnit::TestCase
00029 {
00030     CPPUNIT_TEST_SUITE(IOLayerTest);
00031     CPPUNIT_TEST(testIO);
00032     CPPUNIT_TEST_EXCEPTION(testIOException, stxxl::io_error);
00033     CPPUNIT_TEST_SUITE_END();
00034 
00035 public:
00036     IOLayerTest(std::string name) : CppUnit::TestCase(name) { }
00037     IOLayerTest() { }
00038 
00039     void testIO()
00040     {
00041         const int size = 1024 * 384;
00042         char * buffer = static_cast<char *>(stxxl::aligned_alloc<BLOCK_ALIGN>(size));
00043         memset(buffer, 0, size);
00044 #ifdef BOOST_MSVC
00045         const char * paths[2] = { "data1", "data2" };
00046 #else
00047         const char * paths[2] = { "/var/tmp/data1", "/var/tmp/data2" };
00048         stxxl::mmap_file file1(paths[0], stxxl::file::CREAT | stxxl::file::RDWR, 0);
00049         file1.set_size(size * 1024);
00050 #endif
00051 
00052         stxxl::syscall_file file2(paths[1], stxxl::file::CREAT | stxxl::file::RDWR, 1);
00053 
00054         stxxl::request_ptr req[16];
00055         unsigned i = 0;
00056         for ( ; i < 16; i++)
00057             req[i] = file2.awrite(buffer, i * size, size, my_handler());
00058 
00059         stxxl::wait_all(req, 16);
00060 
00061         stxxl::aligned_dealloc<BLOCK_ALIGN>(buffer);
00062 
00063         unlink(paths[0]);
00064         unlink(paths[1]);
00065     }
00066 
00067     void testIOException()
00068     {
00069         unlink("TestFile");
00070 #ifdef BOOST_MSVC
00071         stxxl::mmap_file file1("TestFile", stxxl::file::RDWR, 0);
00072 #else
00073         stxxl::mmap_file file1("TestFile", stxxl::file::RDWR, 0);
00074 #endif
00075     }
00076 };
00077 
00078 
00079 CPPUNIT_TEST_SUITE_REGISTRATION(IOLayerTest);
00080 
00081 
00082 int main()
00083 {
00084     CppUnit::TextUi::TestRunner runner;
00085     CppUnit::TestFactoryRegistry & registry = CppUnit::TestFactoryRegistry::getRegistry();
00086     runner.addTest(registry.makeTest());
00087     bool wasSuccessful = runner.run("", false);
00088     return wasSuccessful ? 0 : 1;
00089 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines