ProteoWizard
pwiz
data
common
MemoryIndexTest.cpp
Go to the documentation of this file.
1
//
2
// $Id: MemoryIndexTest.cpp 4129 2012-11-20 00:05:37Z chambm $
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
"
23
#include "
pwiz/utility/misc/unit.hpp
"
24
#include "
pwiz/utility/misc/Std.hpp
"
25
26
27
using namespace
pwiz::util
;
28
using namespace
pwiz::data
;
29
30
ostream*
os_
= 0;
31
32
33
void
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
74
int
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
92
TEST_EPILOG
93
}
pwiz::data::MemoryIndex::find
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 ...
main
int main(int argc, char *argv[])
Definition:
MemoryIndexTest.cpp:74
MemoryIndex.hpp
pwiz::data::Index::Entry::offset
stream_offset offset
Definition:
Index.hpp:50
pwiz::data::MemoryIndex
index implementation in memory; find(string id) is O(logN); find(ordinal index) is O(1); memory footp...
Definition:
MemoryIndex.hpp:37
TEST_EPILOG
#define TEST_EPILOG
Definition:
unit.hpp:182
pwiz::data::Index::Entry::id
std::string id
Definition:
Index.hpp:48
Std.hpp
pwiz::data::Index::EntryPtr
boost::shared_ptr< Entry > EntryPtr
Definition:
Index.hpp:53
pwiz::data::MemoryIndex::create
virtual void create(std::vector< Entry > &entries)
create the index from specified list of entries; the list is non-const because the index implementati...
boost::lexical_cast
float lexical_cast(const std::string &str)
Definition:
optimized_lexical_cast.hpp:62
unit.hpp
pwiz::util
Definition:
almost_equal.hpp:33
TEST_FAILED
#define TEST_FAILED(x)
Definition:
unit.hpp:176
pwiz::data::Index::stream_offset
boost::iostreams::stream_offset stream_offset
Definition:
Index.hpp:43
pwiz::data::MemoryIndex::size
virtual size_t size() const
returns the number of entries in the index
pwiz::data
Definition:
BinaryIndexStream.hpp:31
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition:
unit.hpp:174
test
void test()
Definition:
MemoryIndexTest.cpp:33
pwiz::data::Index::Entry
generic type identifying an indexed item by string id, ordinal index, and stream offset ...
Definition:
Index.hpp:46
pwiz::data::Index::Entry::index
boost::uint64_t index
Definition:
Index.hpp:49
unit_assert
#define unit_assert(x)
Definition:
unit.hpp:85
os_
ostream * os_
Definition:
MemoryIndexTest.cpp:30
Generated by
1.8.12