ProteoWizard
Functions | Variables
BinaryIndexStreamTest.cpp File Reference
#include "BinaryIndexStream.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include "boost/thread/thread.hpp"
#include "boost/thread/barrier.hpp"

Go to the source code of this file.

Functions

void test ()
void testThreadSafetyWorker (boost::barrier *testBarrier, BinaryIndexStream *testIndex)
void testThreadSafety ()
int main (int argc, char *argv[])

Variables

ostream * os_ = 0

Function Documentation

void test ( )

Definition at line 35 of file BinaryIndexStreamTest.cpp.

References pwiz::data::BinaryIndexStream::create(), pwiz::data::BinaryIndexStream::find(), pwiz::data::Index::Entry::id, pwiz::data::Index::Entry::index, boost::lexical_cast(), pwiz::data::Index::Entry::offset, os_, pwiz::data::BinaryIndexStream::size(), and unit_assert.

{
if (os_) cout << "Testing BinaryIndexStream (single thread)" << endl;
shared_ptr<stringstream> indexStreamPtr(new stringstream);
// test initial creation and usage of the index stream
{
vector<Index::Entry> entries;
for (size_t i=0; i < 10; ++i)
{
Index::Entry entry;
entry.id = lexical_cast<string>(i);
entry.index = i;
entry.offset = i*100;
entries.push_back(entry);
}
BinaryIndexStream index(indexStreamPtr);
unit_assert(index.size() == 0);
unit_assert(!index.find("42").get());
unit_assert(!index.find(42).get());
index.create(entries);
unit_assert(index.size() == 10);
for (size_t i=0; i < 10; ++i)
{
Index::EntryPtr entryPtr = index.find(i);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
entryPtr = index.find(entryPtr->id);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
}
unit_assert(!index.find("42").get());
unit_assert(!index.find(42).get());
}
// test re-use of an existing index stream
{
BinaryIndexStream index(indexStreamPtr);
unit_assert(index.size() == 10);
unit_assert(!index.find("42").get());
unit_assert(!index.find(42).get());
for (size_t i=0; i < 10; ++i)
{
Index::EntryPtr entryPtr = index.find(i);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
entryPtr = index.find(entryPtr->id);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
}
unit_assert(!index.find("42").get());
unit_assert(!index.find(42).get());
}
// test creating a new, smaller index in an existing index stream
{
vector<Index::Entry> entries;
for (size_t i=0; i < 5; ++i)
{
Index::Entry entry;
entry.id = lexical_cast<string>(i);
entry.index = i;
entry.offset = i*100;
entries.push_back(entry);
}
BinaryIndexStream index(indexStreamPtr);
unit_assert(index.size() == 10);
index.create(entries);
unit_assert(index.size() == 5);
for (size_t i=0; i < 5; ++i)
{
Index::EntryPtr entryPtr = index.find(i);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
entryPtr = index.find(entryPtr->id);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
}
unit_assert(!index.find("5").get());
unit_assert(!index.find(5).get());
}
}
void testThreadSafetyWorker ( boost::barrier *  testBarrier,
BinaryIndexStream testIndex 
)

Definition at line 145 of file BinaryIndexStreamTest.cpp.

References e(), pwiz::data::BinaryIndexStream::find(), and unit_assert.

Referenced by testThreadSafety().

{
testBarrier->wait(); // wait until all threads have started
BinaryIndexStream& index = *testIndex;
try
{
for (size_t i=0; i < 10; ++i)
{
Index::EntryPtr entryPtr = index.find(i);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
entryPtr = index.find(entryPtr->id);
unit_assert(entryPtr.get());
unit_assert(entryPtr->id == lexical_cast<string>(i));
unit_assert(entryPtr->index == i);
unit_assert(entryPtr->offset == Index::stream_offset(i*100));
}
unit_assert(!index.find("42").get());
unit_assert(!index.find(42).get());
}
catch (exception& e)
{
cerr << e.what() << endl;
}
catch (...)
{
cerr << "Caught unknown exception." << endl;
}
}
void testThreadSafety ( )

Definition at line 180 of file BinaryIndexStreamTest.cpp.

References pwiz::data::BinaryIndexStream::create(), pwiz::data::Index::Entry::id, pwiz::data::Index::Entry::index, boost::lexical_cast(), pwiz::data::Index::Entry::offset, os_, pwiz::data::BinaryIndexStream::size(), testThreadSafetyWorker(), and unit_assert.

Referenced by main().

{
if (os_) cout << "Testing BinaryIndexStream (multithreaded)" << endl;
shared_ptr<stringstream> indexStreamPtr(new stringstream);
// create a shared index stream
vector<Index::Entry> entries;
for (size_t i=0; i < 10; ++i)
{
Index::Entry entry;
entry.id = lexical_cast<string>(i);
entry.index = i;
entry.offset = i*100;
entries.push_back(entry);
}
BinaryIndexStream index(indexStreamPtr);
index.create(entries);
unit_assert(index.size() == 10);
// create workers to test using the stream
const int testThreadCount = 100;
boost::barrier testBarrier(testThreadCount);
boost::thread_group testThreadGroup;
for (int i=0; i < testThreadCount; ++i)
testThreadGroup.add_thread(new boost::thread(&testThreadSafetyWorker, &testBarrier, &index));
testThreadGroup.join_all();
}
int main ( int  argc,
char *  argv[] 
)

Definition at line 211 of file BinaryIndexStreamTest.cpp.

References e(), os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, and testThreadSafety().

{
TEST_PROLOG(argc, argv)
try
{
if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
test();
}
catch (exception& e)
{
TEST_FAILED(e.what())
}
catch (...)
{
TEST_FAILED("Caught unknown exception.")
}
}

Variable Documentation

ostream* os_ = 0

Definition at line 32 of file BinaryIndexStreamTest.cpp.