Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
moved_from.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
75#ifndef ALEPH_TESTS_MOVED_FROM_H
76#define ALEPH_TESTS_MOVED_FROM_H
77
78#include <concepts>
79#include <cstddef>
80#include <type_traits>
81#include <utility>
82
83#include <gtest/gtest.h>
84
92template <class T> struct Moved_From_Case;
93
101template <class Case, class T>
102auto moved_from_fingerprint(const T & x)
103{
104 if constexpr (requires { Case::fingerprint(x); })
105 return Case::fingerprint(x);
106 else if constexpr (requires { { x.size() } -> std::convertible_to<std::size_t>; })
107 return static_cast<std::size_t>(x.size());
108 else
109 return std::size_t(0);
110}
111
115template <class T> class MovedFrom : public ::testing::Test {};
116
118
119// The source of a move construction is an empty object that can be used,
120// and the destination has what the source had
121TYPED_TEST_P(MovedFrom, ConstructionLeavesAnEmptyUsableSource)
122{
123 using Case = Moved_From_Case<TypeParam>;
124 TypeParam src = Case::make(100);
125 const auto print = moved_from_fingerprint<Case>(src);
126 TypeParam dst(std::move(src));
127 EXPECT_EQ(moved_from_fingerprint<Case>(dst), print);
128 EXPECT_TRUE(Case::is_empty(src));
129 Case::use(src);
130 Case::use(dst);
131 Case::use(src); // the second use runs on what the first one left
132}
133
134// A move assignment, from a larger object and from a smaller one, gives
135// the destination what the source had and leaves both objects usable:
136// with an exchange, the source gets the state of the destination. Classes
137// that cannot be assigned skip it
138TYPED_TEST_P(MovedFrom, AssignmentLeavesBothObjectsUsable)
139{
140 if constexpr (std::is_move_assignable_v<TypeParam>)
141 {
142 using Case = Moved_From_Case<TypeParam>;
143 for (const std::size_t n : {std::size_t(2), std::size_t(1000)})
144 {
145 TypeParam dst = Case::make(n);
146 TypeParam src = Case::make(n == 2 ? 1000 : 2);
147 const auto print = moved_from_fingerprint<Case>(src);
148 dst = std::move(src);
149 EXPECT_EQ(moved_from_fingerprint<Case>(dst), print);
150 Case::use(src);
151 Case::use(dst);
152 }
153 }
154 else
155 GTEST_SKIP() << "the class has no move assignment";
156}
157
158REGISTER_TYPED_TEST_SUITE_P(MovedFrom, ConstructionLeavesAnEmptyUsableSource,
159 AssignmentLeavesBothObjectsUsable);
160
161#endif // ALEPH_TESTS_MOVED_FROM_H
The typed suite of the moved-from tests.
Definition moved_from.H:115
TYPED_TEST_P(MovedFrom, ConstructionLeavesAnEmptyUsableSource)
Definition moved_from.H:121
TYPED_TEST_SUITE_P(MovedFrom)
REGISTER_TYPED_TEST_SUITE_P(MovedFrom, ConstructionLeavesAnEmptyUsableSource, AssignmentLeavesBothObjectsUsable)
auto moved_from_fingerprint(const T &x)
What a move must carry from its source to its destination.
Definition moved_from.H:102
How the moved-from tests build, check and use objects of a class.
Definition moved_from.H:92
void print(const MemArray< T > &s)