zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Erwin Rol <erwin@erwinrol.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/sys/__assert.h>
9 
10 #include <zephyr/kernel.h>
11 
12 #include<string.h>
13 
14 #include <zpp.hpp>
15 
16 
17 ZTEST_SUITE(test_zpp_result, NULL, NULL, NULL, NULL, NULL);
18 
19 namespace {
20 
21 class ResultData {
22 public:
23  ResultData() = delete;
24  ResultData(int v) noexcept : m_data(v) {}
25 private:
26  int m_data{42};
27 };
28 
29 class ErrorData {
30 public:
31  ErrorData() = delete;
32  ErrorData(int v) noexcept : m_data(v) {}
33 private:
34  int m_data{42};
35 };
36 
37 } // namespace
38 
39 ZTEST(test_zpp_result, test_result)
40 {
44 
45  zassert_true(!res_a, "res_a should be false\n");
46  zassert_false(res_b, "res_b should be false\n");
47  zassert_true(res_c == false, "res_c should be false\n");
48 
50 
51  zassert_true(res_a == false, "res_a should be false\n");
52 
53  res_a = 12345;
54 
55  zassert_true(res_a == true, "res_a should be true\n");
56  zassert_true(res_a.value() == 12345, "res_a.value() should be 12345\n");
57 
58  auto res_d = res_a;
59  zassert_true(res_d == true, "res_d should be true\n");
60  zassert_true(res_d.value() == 12345, "res_d.value() should be 12345\n");
61 
62  res_d.assign_error(zpp::error_code::k_already);
63  zassert_false(res_d, "res_d should be false\n");
64  zassert_true(res_d.error() == zpp::error_code::k_already, "res_d should hold zpp::error_code::k_already");
65 
67  zassert_false(res_e, "res_d should be true\n");
68 
69  res_e.assign_value();
70  zassert_true(!!res_e, "res_d should be true\n");
71 
72  res_e = zpp::error_result(0);
73  zassert_true(res_e.error() == 0, "res_d should hold 0");
74 
76  zassert_false(res_f, "res_f should be false\n");
77 
78  zpp::result<void, ErrorData> res_g(zpp::error_result(ErrorData(13)));
79  zassert_false(res_g, "res_g should be false\n");
80 
82  zassert_false(res_h, "res_h should be false\n");
83 
84 }
helper class for error result
Definition: result.hpp:25
result class
Definition: result.hpp:89
void assign_value(const T_Ok &v) noexcept
Definition: result.hpp:197
T_Ok & value() noexcept
return a reference to the OK value
Definition: result.hpp:327
T_Error & error() noexcept
return a reference to the OK value
Definition: result.hpp:358
@ k_inval
Invalid argument.
@ k_already
Operation already in progress.
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition: main.cpp:23
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)