• R/O
  • SSH

posixpp: 提交

The main posixpp library and associated tests.


Commit MetaInfo

修订版e5ded4f9d6ea3f10f8510bfa74974e816b2972b7 (tree)
时间2021-04-26 15:22:11
作者Eric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Add tests that would've caught the previous error, as well as...
tests that make sure the move and copy semantics work as expected.

更改概述

差异

diff -r d78d228d0ec0 -r e5ded4f9d6ea tests/expected.cpp
--- a/tests/expected.cpp Sun Apr 25 13:48:39 2021 -0700
+++ b/tests/expected.cpp Sun Apr 25 23:22:11 2021 -0700
@@ -2,6 +2,46 @@
22 #include <cerrno>
33 #include <catch2/catch.hpp>
44
5+class move_detector {
6+ public:
7+ move_detector(move_detector &&other) { other.moved_from_ = true; }
8+ move_detector(move_detector const &) = default;
9+ move_detector() = default;
10+
11+ move_detector &operator =(move_detector &&other) {
12+ other.moved_from_ = true;
13+ return *this;
14+ }
15+
16+ move_detector &operator =(move_detector const &) = default;
17+
18+ bool was_moved_from() const { return moved_from_; }
19+
20+ private:
21+ bool moved_from_ = false;
22+};
23+
24+
25+class cant_copy {
26+ public:
27+ cant_copy() = default;
28+ cant_copy(cant_copy &&) = default;
29+ cant_copy(cant_copy const &) = delete;
30+ cant_copy &operator =(cant_copy &&) = default;
31+ cant_copy &operator =(cant_copy const &) = delete;
32+};
33+
34+
35+class cant_move {
36+ public:
37+ cant_move() = default;
38+ cant_move(cant_move &&) = delete;
39+ cant_move(cant_move const &) = default;
40+ cant_move &operator =(cant_move &&) = delete;
41+ cant_move &operator =(cant_move const &) = default;
42+};
43+
44+
545 SCENARIO( "expected holds results and throws exceptions", "[expected]" ) {
646 GIVEN( " An expected<int> result initialized with 5 " ) {
747 ::posixpp::expected<int> const result{5};
@@ -35,4 +75,51 @@
3575 ::std::system_category().default_error_condition(ENOENT));
3676 }
3777 }
78+ /* This can't (and shouldn't) compile */
79+#if 0
80+ GIVEN("An expected<cant_move>.") {
81+ using expected_t = ::posixpp::expected<cant_move>;
82+ cant_move cm;
83+ expected_t result{cm};
84+ }
85+#endif
86+ GIVEN("An expected<move_detector> created from a const move_detector.") {
87+ using expected_t = ::posixpp::expected<move_detector>;
88+ move_detector const md;
89+ expected_t result{md};
90+ THEN(" original move_detector wasn't moved.") {
91+ CHECK_FALSE(md.was_moved_from());
92+ } THEN(" move_detector inside expected also wasn't moved.") {
93+ CHECK_FALSE(result.result().was_moved_from());
94+ } THEN(" and checking its status didn't move it.") {
95+ CHECK_FALSE(result.result().was_moved_from());
96+ } THEN(" but assigning it out does move it.") {
97+ move_detector newmd{result.result()};
98+ CHECK_FALSE(newmd.was_moved_from());
99+ CHECK(result.result().was_moved_from());
100+ }
101+ }
102+ GIVEN("A const expected<move_detector>.") {
103+ using expected_t = ::posixpp::expected<move_detector>;
104+ expected_t const result{move_detector{}};
105+ THEN( " contained move_detector wasn't moved.") {
106+ CHECK_FALSE(result.result().was_moved_from());
107+ }
108+ THEN( " copying the result out doesn't move it.") {
109+ move_detector tmp{result.result()};
110+ CHECK_FALSE(result.result().was_moved_from());
111+ }
112+ THEN( " returning it from a lambda also doesn't move it.") {
113+ auto result2 = [&result]() -> expected_t { return result; }();
114+ CHECK_FALSE(result.result().was_moved_from());
115+ }
116+ }
117+ GIVEN("An expected<cant_copy>") {
118+ using expected_t = ::posixpp::expected<cant_copy>;
119+ expected_t result{cant_copy{}};
120+ THEN(" you can still get the result out.") {
121+ // This is just a test to see if the code compiles.
122+ auto tmp = result.result();
123+ }
124+ }
38125 }
Show on old repository browser