test_set.cxx
Go to the documentation of this file.
1 #include "WireCellUtil/Testing.h"
2 
3 #include <vector>
4 #include <set>
5 #include <deque>
6 
7 #include <iterator> // std::back_inserter
8 #include <algorithm> // std::set_difference
9 #include <iostream>
10 #include <memory>
11 #include <random>
12 
13 
14 
15 using namespace std;
16 
17 
18 int main()
19 {
20  // http://stackoverflow.com/a/10604500
21  vector<int> items = {0,1,2,3,4,5,6,7,8,9};
22  set<int> die = {2,4,5,42};
23  set<int> die2;
24 
25  vector<int> result;
26  set_difference(items.begin(), items.end(),
27  die.begin(), die.end(),
28  back_inserter(result));
29  cout << "items: ";
30  for (auto item : items) {
31  cout << item << " ";
32  }
33  cout << endl;
34  cout << "alive: ";
35  for (auto alive: result) {
36  cout << alive << " ";
37  }
38  cout << endl;
39 
40  vector<int> result2;
41  set_difference(items.begin(), items.end(),
42  die2.begin(), die2.end(),
43  back_inserter(result2));
44 
45  Assert(items.size() == 10);
46  Assert(die.size() == 4);
47  Assert(result.size() == 7);
48 
49  Assert(die2.size() == 0);
50  Assert(result2.size() == 10);
51 
52  {
53  std::random_device rd;
54  std::default_random_engine re(rd());
55  std::uniform_real_distribution<> dist(0, 1000);
56 
57  typedef std::shared_ptr<int> Pint;
58  deque<Pint> queue;
59  const int nitems = 1000;
60  for (int ind=0; ind<nitems; ++ind) {
61  queue.push_back(Pint(new int(dist(re))));
62  }
63  for (int ind=0; ind<nitems; ++ind) {
64  Pint front = queue.front();
65  queue.pop_front();
66  cerr << ind << ": popped:" << *front
67  << " now with: " << queue.size() << " items"
68  << endl;
69  }
70  }
71 }
static QCString result
STL namespace.
#define Assert
Definition: Testing.h:7
int main()
Definition: test_set.cxx:18
QTextStream & endl(QTextStream &s)