ProteoWizard
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"
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 
93 }