ProteoWizard
MemoryIndexTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6//
7// Copyright 2009 Vanderbilt University - Nashville, TN 37232
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22#include "MemoryIndex.hpp"
25
26
27using namespace pwiz::util;
28using namespace pwiz::data;
29
30ostream* os_ = 0;
31
32
33void test()
34{
35 if (os_) cout << "Testing MemoryIndex" << endl;
36
37 vector<Index::Entry> entries;
38 for (size_t i=0; i < 10; ++i)
39 {
40 Index::Entry entry;
41 entry.id = lexical_cast<string>(i);
42 entry.index = i;
43 entry.offset = i*100;
44 entries.push_back(entry);
45 }
46
47 MemoryIndex index;
48 unit_assert(index.size() == 0);
49 unit_assert(!index.find("42").get());
50 unit_assert(!index.find(42).get());
51
52 index.create(entries);
53 unit_assert(index.size() == 10);
54
55 for (size_t i=0; i < 10; ++i)
56 {
57 Index::EntryPtr entryPtr = index.find(i);
58 unit_assert(entryPtr.get());
59 unit_assert(entryPtr->id == lexical_cast<string>(i));
60 unit_assert(entryPtr->index == i);
61 unit_assert(entryPtr->offset == Index::stream_offset(i*100));
62
63 entryPtr = index.find(entryPtr->id);
64 unit_assert(entryPtr.get());
65 unit_assert(entryPtr->id == lexical_cast<string>(i));
66 unit_assert(entryPtr->index == i);
67 unit_assert(entryPtr->offset == Index::stream_offset(i*100));
68 }
69
70 unit_assert(!index.find("42").get());
71 unit_assert(!index.find(42).get());
72}
73
74int main(int argc, char* argv[])
75{
76 TEST_PROLOG(argc, argv)
77
78 try
79 {
80 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
81 test();
82 }
83 catch (exception& e)
84 {
85 TEST_FAILED(e.what())
86 }
87 catch (...)
88 {
89 TEST_FAILED("Caught unknown exception.")
90 }
91
93}
int main(int argc, char *argv[])
ostream * os_
void test()
boost::shared_ptr< Entry > EntryPtr
Definition Index.hpp:53
boost::iostreams::stream_offset stream_offset
Definition Index.hpp:43
index implementation in memory; find(string id) is O(logN); find(ordinal index) is O(1); memory footp...
virtual void create(std::vector< Entry > &entries)
create the index from specified list of entries; the list is non-const because the index implementati...
virtual size_t size() const
returns the number of entries in the index
virtual EntryPtr find(const std::string &id) const
returns the entry for the specified string id, or null if the id is not in the index
generic type identifying an indexed item by string id, ordinal index, and stream offset
Definition Index.hpp:47
std::string id
Definition Index.hpp:48
boost::uint64_t index
Definition Index.hpp:49
stream_offset offset
Definition Index.hpp:50
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175