emlabcpp
modern opinionated embedded C++ library
interface.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../protocol/packet_handler.h"
27 #include "../../result.h"
28 #include "../linked_list.h"
29 #include "./base.h"
30 #include "./coroutine.h"
31 
32 #include <span>
33 
34 namespace emlabcpp::testing
35 {
36 
38 {
39 public:
40  test_interface() = default;
41 
42  test_interface( test_interface const& ) = delete;
44  test_interface( test_interface&& other ) = default;
46 
47  [[nodiscard]] virtual std::string_view get_name() const = 0;
48 
52 
53  virtual ~test_interface() = default;
54 };
55 
57 
58 template < typename T >
59 concept valid_test_callable = requires( T t, pmr::memory_resource& mem_resource ) {
60  { t( mem_resource ) } -> std::same_as< coroutine< void > >;
61 };
62 
63 template < typename T >
65 
66 template < valid_test_callable Callable >
68 {
69 public:
70  test_callable( std::string_view const name, Callable cb )
71  : name_( name )
72  , cb_( std::move( cb ) )
73  {
74  }
75 
76  test_callable( std::string_view const name, auto& rec, Callable cb )
77  : name_( name )
78  , cb_( std::move( cb ) )
79  {
80  rec.register_test( this );
81  }
82 
83  test_callable( test_callable&& ) noexcept = default;
84  test_callable& operator=( test_callable&& ) noexcept = default;
85 
86  [[nodiscard]] std::string_view get_name() const override
87  {
88  return std::string_view{ name_.data(), name_.size() };
89  }
90 
92  {
93  return cb_( mem_resource );
94  }
95 
96 private:
97  name_buffer name_;
98  Callable cb_;
99 };
100 
101 template < valid_test_callable Callable >
102 test_callable( std::string_view const name, Callable cb ) -> test_callable< Callable >;
103 
105 {
106  [[nodiscard]] std::string_view get_name() const override
107  {
108  return "";
109  }
110 
112  {
113  co_return;
114  }
115 };
116 
118 
119 template < valid_test_callable Callable >
121 
122 // TODO: the sad thing is that they won't be deallocated this way /o\...
123 template < typename Unit, typename Reactor, typename... Args >
125 {
126  using unode = test_unit< Unit >;
127  void* p = mem_res.allocate( sizeof( unode ), alignof( unode ) );
128  if ( p == nullptr )
129  return result::ERROR;
130  unode* node =
131  std::construct_at( reinterpret_cast< unode* >( p ), std::forward< Args >( args )... );
132 
133  reactor.register_test( *node );
134 
135  return result::SUCCESS;
136 }
137 
138 template < typename Callable, typename Reactor >
140  pmr::memory_resource& mem_res,
141  Reactor& reactor,
142  std::string_view name,
143  Callable callable )
144 {
145  return construct_test_unit< test_callable< Callable > >(
146  mem_res, reactor, name, std::move( callable ) );
147 }
148 
149 template < typename Callable, typename Reactor >
151  pmr::memory_resource& mem_res,
152  Reactor& reactor,
153  std::string_view name,
154  Callable callable,
155  result& res )
156 {
157  if ( res != result::SUCCESS )
158  return;
159  res = construct_test_unit< test_callable< Callable > >(
160  mem_res, reactor, name, std::move( callable ) );
161 }
162 
163 } // namespace emlabcpp::testing
Definition: linked_list.h:33
Definition: linked_list.h:138
Definition: memory_resource.h:33
virtual void * allocate(std::size_t bytes, std::size_t alignment)=0
Definition: reactor.h:37
void register_test(linked_list_node_base< test_interface > &test)
Definition: reactor.h:75
Definition: interface.h:68
test_callable(std::string_view const name, Callable cb)
Definition: interface.h:70
test_callable(std::string_view const name, auto &rec, Callable cb)
Definition: interface.h:76
coroutine< void > run(pmr::memory_resource &mem_resource) final
Definition: interface.h:91
std::string_view get_name() const override
Definition: interface.h:86
test_callable(test_callable &&) noexcept=default
Definition: interface.h:38
virtual std::string_view get_name() const =0
test_interface & operator=(test_interface const &)=delete
test_interface(test_interface const &)=delete
virtual coroutine< void > run(pmr::memory_resource &)=0
virtual coroutine< void > setup(pmr::memory_resource &)
test_interface & operator=(test_interface &&)=default
virtual coroutine< void > teardown(pmr::memory_resource &)
test_interface(test_interface &&other)=default
MIT License.
Definition: base.h:37
result construct_test_unit(pmr::memory_resource &mem_res, Reactor &reactor, Args &&... args)
Definition: interface.h:124
test_callable(std::string_view const name, Callable cb) -> test_callable< Callable >
requires(N<=string_buffer::capacity) struct value_type_converter< emlabcpp requires(alternative_of< T, value_type >) struct value_type_converter< T > requires(!std::same_as< T, int64_t > &&std::is_integral_v< T > &&!std::same_as< T, bool >) struct value_type_converter< T >
Definition: convert.h:113
result construct_test_callable(pmr::memory_resource &mem_res, Reactor &reactor, std::string_view name, Callable callable)
Definition: interface.h:139
concept valid_test_callable
Definition: interface.h:59
Args const & args
Definition: min_max.h:83
T res
Definition: algorithm.h:505
result represents an result of some operation, as an alternative to returning just bool with true/fal...
Definition: result.h:42
constexpr std::size_t size() const
Definition: string_buffer.h:75
Definition: interface.h:105
std::string_view get_name() const override
Definition: interface.h:106
coroutine< void > run(pmr::memory_resource &) override
Definition: interface.h:111