test_buffer.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * fkie_message_filters
4  * Copyright © 2018-2020 Fraunhofer FKIE
5  * Author: Timo Röhling
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ****************************************************************************/
20 #include "test.h"
24 
26 {
27  using Source = mf::UserSource<int_M>;
28  using Buffer = mf::Buffer<int_M>;
29  using Sink = mf::SimpleUserFilter<int_M>;
30 
31  std::size_t callback_counts = 0;
32  Source src;
33  Buffer buf(mf::BufferPolicy::Queue, 3);
34  Sink snk;
35  snk.set_processing_function(
36  [&](const int_M& i) -> bool
37  {
38  ++callback_counts;
39  if (i != 0) throw std::domain_error("invalid value");
40  return true;
41  }
42  );
43  mf::chain(src, buf, snk);
44  ASSERT_FALSE(buf.has_some());
45  // Purposely overflow the buffer queue to verify that the first item gets discarded
46  src(int_M(10000));
47  src(int_M(0));
48  src(int_M(0));
49  src(int_M(0));
50  ASSERT_TRUE(buf.has_some());
51  ASSERT_EQ(0u, callback_counts);
52  // Check manual processing functions
53  buf.process_one();
54  ASSERT_EQ(1u, callback_counts);
55  buf.spin_once();
56  ASSERT_FALSE(buf.has_some());
57  ASSERT_EQ(3u, callback_counts);
58  buf.spin_once();
59  ASSERT_EQ(3u, callback_counts);
60  src(int_M(0));
61  buf.spin_once();
62  ASSERT_EQ(4u, callback_counts);
63 }
Store and forward data.
Definition: buffer.h:103
TEST(fkie_message_filters, Buffer)
Definition: test_buffer.cpp:25
Primary namespace.
Definition: buffer.h:33
void chain(Filter1 &flt1, Filter2 &flt2, MoreFilters &... filters) noexcept
Convenience function to chain multiple filters.
Definition: filter_impl.h:53
NotDefaultConstructable< int > int_M
Definition: test.h:47
Manually operated data source.
Definition: user_source.h:37
Simplified filter with user-defined callback function.


fkie_message_filters
Author(s): Timo Röhling
autogenerated on Mon Feb 28 2022 22:21:43