shared_queue_test.cc
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
4 #include <boost/thread.hpp>
5 #include <vector>
6 
7 TEST(SharedQueueTest, emptyOnConstuct) {
9 
10  ASSERT_TRUE(sq.fast_empty());
11  ASSERT_TRUE(sq.empty());
12 }
13 
14 TEST(SharedQueueTest, notEmptyAfterPush) {
16  sq.push(25);
17 
18  ASSERT_FALSE(sq.fast_empty());
19  ASSERT_FALSE(sq.empty());
20 }
21 
22 TEST(SharedQueueTest, notEmptyAfterPushMultiple) {
24  std::vector<int> v;
25 
26  for (int i = 0; i < 5; ++i) {
27  v.push_back(i);
28  }
29 
30  sq.push(v);
31 
32  ASSERT_FALSE(sq.fast_empty());
33  ASSERT_FALSE(sq.empty());
34 }
35 
36 TEST(SharedQueueTest, emptyAfterPushAndPop) {
38  sq.push(25);
39 
40  ASSERT_FALSE(sq.fast_empty());
41  ASSERT_FALSE(sq.empty());
42 
43  sq.pop();
44 
45  ASSERT_TRUE(sq.fast_empty());
46  ASSERT_TRUE(sq.empty());
47 }
48 
49 TEST(SharedQueueTest, pushFrontPop) {
51 
52  for (int i = 0; i < 5; ++i) {
53  sq.push(i);
54  }
55 
56  for (int i = 0; i < 5; ++i) {
57  ASSERT_EQ(i, sq.front());
58  sq.pop();
59  }
60 
61  for (int i = 0; i < 5; ++i) {
62  sq.push(i);
63  }
64 
65  for (int i = 0; i < 5; ++i) {
66  ASSERT_EQ(i, sq.front_pop());
67  }
68 
69  ASSERT_TRUE(sq.fast_empty());
70  ASSERT_TRUE(sq.empty());
71  ASSERT_EQ(0, sq.size());
72 }
73 
74 TEST(SharedQueueTest, copy) {
77 
78  for (int i = 0; i < 5; ++i) {
79  sq0.push(i);
80  }
81 
82  sq1 = sq0;
83  ASSERT_FALSE(sq1.fast_empty());
84  ASSERT_FALSE(sq1.empty());
85  ASSERT_EQ(5, sq1.size());
86 
87  for (int i = 0; i < 5; ++i) {
88  ASSERT_EQ(i, sq1.front_pop());
89  }
90 
91  ASSERT_TRUE(sq1.fast_empty());
92  ASSERT_TRUE(sq1.empty());
93  ASSERT_EQ(0, sq1.size());
94 
95  // Make sure that the original queue is untouched
96  ASSERT_FALSE(sq0.fast_empty());
97  ASSERT_FALSE(sq0.empty());
98  ASSERT_EQ(5, sq0.size());
99 }
100 
101 TEST(SharedQueueTest, copyConstruct) {
102  shared_queue<int> sq0;
103 
104  for (int i = 0; i < 5; ++i) {
105  sq0.push(i);
106  }
107 
108  shared_queue<int> sq1(sq0);
109  ASSERT_FALSE(sq1.fast_empty());
110  ASSERT_FALSE(sq1.empty());
111  ASSERT_EQ(5, sq1.size());
112 
113  for (int i = 0; i < 5; ++i) {
114  ASSERT_EQ(i, sq1.front_pop());
115  }
116 
117  ASSERT_TRUE(sq1.fast_empty());
118  ASSERT_TRUE(sq1.empty());
119  ASSERT_EQ(0, sq1.size());
120 
121  // Make sure that the original queue is untouched
122  ASSERT_FALSE(sq0.fast_empty());
123  ASSERT_FALSE(sq0.empty());
124  ASSERT_EQ(5, sq0.size());
125 }
126 
127 TEST(SharedQueueTest, pushConstCopyFront) {
129 
130  for (int i = 0; i < 5; ++i) {
131  sq.push(i);
132  }
133 
134  const shared_queue<int> csq = sq;
135 
136  ASSERT_FALSE(sq.fast_empty());
137  ASSERT_FALSE(sq.empty());
138  ASSERT_EQ(5, sq.size());
139 
140  const int &front = csq.front();
141  ASSERT_EQ(0, front);
142 }
143 
144 TEST(SharedQueueTest, pushPopSize) {
146 
147  for (int i = 0; i < 5; ++i) {
148  sq.push(i);
149  ASSERT_EQ(i + 1, sq.size());
150  }
151 
152  ASSERT_EQ(5, sq.size());
153 
154  for (int i = 0; i < 5; ++i) {
155  ASSERT_EQ(5 - i, sq.size());
156  sq.pop();
157  }
158 }
159 
160 TEST(SharedQueueTest, pushMultipleSize) {
162  std::vector<int> v;
163 
164  for (int i = 0; i < 5; ++i) {
165  v.push_back(i);
166  }
167 
168  sq.push(v);
169  ASSERT_EQ(5, sq.size());
170 }
171 
173  for (int i = 0; i < 100; ++i) {
174  ASSERT_FALSE(sq->fast_empty());
175  ASSERT_FALSE(sq->empty());
176  ASSERT_EQ(i, sq->front());
177  sq->pop();
178  }
179 }
180 
181 TEST(SharedQueueTest, pushFrontPopThreaded) {
183 
184  for (int i = 0; i < 100; ++i) {
185  sq.push(i);
186  }
187 
188  boost::thread popThread = boost::thread(pop_thread, &sq);
189  popThread.join();
190 
191  ASSERT_TRUE(sq.fast_empty());
192  ASSERT_TRUE(sq.empty());
193  ASSERT_EQ(0, sq.size());
194 }
195 
196 int main(int argc, char **argv) {
197  testing::InitGoogleTest(&argc, argv);
198  return RUN_ALL_TESTS();
199 }
shared_queue::fast_empty
bool fast_empty() const
Definition: shared_queue.h:117
shared_queue::push
void push(const T &value)
Definition: shared_queue.h:68
shared_queue::empty
bool empty() const
Definition: shared_queue.h:112
shared_queue::pop
void pop()
Definition: shared_queue.h:105
pop_thread
void pop_thread(shared_queue< int > *sq)
Definition: shared_queue_test.cc:172
shared_queue.h
TEST
TEST(SharedQueueTest, emptyOnConstuct)
Definition: shared_queue_test.cc:7
main
int main(int argc, char **argv)
Definition: shared_queue_test.cc:196
shared_queue::size
size_t size() const
Definition: shared_queue.h:119
shared_queue::front
T & front()
Definition: shared_queue.h:85
shared_queue::front_pop
T front_pop()
Definition: shared_queue.h:95
shared_queue
Definition: shared_queue.h:39


ubiquity_motor
Author(s):
autogenerated on Thu Nov 16 2023 03:30:55