zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #include <zephyr/ztest.h>
8 
9 #include <zephyr/kernel.h>
10 
11 #include <zpp/fifo.hpp>
12 #include <zpp/thread.hpp>
13 #include <zpp/sem.hpp>
14 
15 #include <array>
16 
17 ZTEST_SUITE(test_zpp_fifo, NULL, NULL, NULL, NULL, NULL);
18 
19 namespace {
20 
21 ZPP_THREAD_STACK_DEFINE(tstack, 1024);
23 
24 struct item {
25  void* fifo_reserved{};
26  uint32_t data{};
27  uint32_t more_data{};
28 };
29 
30 std::array<item, 4> g_item_array;
31 
32 zpp::fifo<item> g_fifo;
33 
34 } // namespace
35 
36 ZTEST(test_zpp_fifo, test_fifo)
37 {
38  using namespace zpp;
39  using namespace std::chrono;
40 
41  const thread_attr attr(
46  );
47 
48  //
49  // Put items into fifo
50  //
51  for (auto& item: g_item_array) {
52  item.data = 0x1234;
53  item.more_data = 0x5678;
54  g_fifo.push_back(&item);
55  }
56 
57  auto t = thread(
58  tcb, tstack(), attr,
59  []() noexcept {
60  //
61  // Get items from fifo
62  //
63  for (auto& item: g_item_array) {
64  auto res = g_fifo.try_pop_front();
65  zassert_equal(res->data, 0x1234, nullptr);
66  zassert_equal(res->more_data, 0x5678, nullptr);
67  zassert_equal(res, &item, nullptr);
68  }
69 
70  //
71  // Put items into fifo
72  //
73  for (auto& item: g_item_array) {
74  g_fifo.push_back(&item);
75  }
76  });
77 
78  //
79  // Let the child thread run
80  //
81  auto res = t.join();
82  zassert_equal(!!res, true, "");
83 
84  //
85  // Get items from fifo
86  //
87  for (auto& item: g_item_array) {
88  auto res = g_fifo.try_pop_front();
89  zassert_equal(res->data, 0x1234, nullptr);
90  zassert_equal(res->more_data, 0x5678, nullptr);
91  zassert_equal(res, &item, nullptr);
92  }
93 }
fifo that manages a k_fifo object
Definition: fifo.hpp:175
Thread creation attributes.
Definition: thread_attr.hpp:35
thread_data holds the stack and thread control block memory
Definition: thread_data.hpp:19
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
The class thread repecents a single Zephyr thread.
Definition: thread.hpp:147
auto join() noexcept
join the thread this object mamages.
Definition: thread.hpp:483
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition: main.cpp:23
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)
#define ZPP_THREAD_STACK_DEFINE(sym, size)