Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
moved_from.H File Reference

The pattern test of the moved-from objects. More...

#include <concepts>
#include <cstddef>
#include <type_traits>
#include <utility>
#include <gtest/gtest.h>
Include dependency graph for moved_from.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  MovedFrom< T >
 The typed suite of the moved-from tests. More...
 

Functions

template<class Case , class T >
auto moved_from_fingerprint (const T &x)
 What a move must carry from its source to its destination.
 
 TYPED_TEST_SUITE_P (MovedFrom)
 
 TYPED_TEST_P (MovedFrom, ConstructionLeavesAnEmptyUsableSource)
 
 TYPED_TEST_P (MovedFrom, AssignmentLeavesBothObjectsUsable)
 
 REGISTER_TYPED_TEST_SUITE_P (MovedFrom, ConstructionLeavesAnEmptyUsableSource, AssignmentLeavesBothObjectsUsable)
 

Detailed Description

The pattern test of the moved-from objects.

A class that keeps a size or a parameter in a member of its own, beside the array that it describes, is left inconsistent by the implicit moves: they take the array, which is left empty in the source (or exchanged, in a move assignment), but copy the scalar. The moved-from object then reads or writes storage that it no longer has. These typed tests move from objects of each class given to them and use the sources and the destinations, so that a class with the pattern fails: with an exception or an assertion of a checked access, and under AddressSanitizer with a report of the unchecked accesses.

Each class is given by specializing Moved_From_Case for it, and the suite is instantiated with a list of them:

template <> struct Moved_From_Case<Some_Class>
{
static Some_Class make(size_t n); // an object of about n items
static bool is_empty(Some_Class & x); // what a move leaves
static void use(Some_Class & x); // ordinary operations
static auto fingerprint(const Some_Class & x); // optional, see below
};
using Classes = ::testing::Types<Some_Class, ...>;
INSTANTIATE_TYPED_TEST_SUITE_P(traverses, Container, Ctypes)
The typed suite of the moved-from tests.
Definition moved_from.H:115
How the moved-from tests build, check and use objects of a class.
Definition moved_from.H:92

is_empty() may take the object as const or not.

use() must do what an ordinary user does with any object of the class, an empty one included: walk the positions that the object reports, read and write them, insert, query and clear. A use that only destroys or assigns the object does not show the pattern.

The destination of a move must have what the source had. The tests compare fingerprint() of the source before the move with that of the destination after it; a class without one is compared by its size(), if it has one. A move assignment may leave in the source the previous state of the destination (an exchange) or an empty object, so the tests do not compare the source after it.

Definition in file moved_from.H.

Function Documentation

◆ moved_from_fingerprint()

template<class Case , class T >
auto moved_from_fingerprint ( const T &  x)

What a move must carry from its source to its destination.

Template Parameters
CaseThe Moved_From_Case of the class.
TThe class.
Parameters
[in]xThe object.
Returns
Case::fingerprint(x) if the case gives one, else the size of x if it has one, else 0 (nothing to compare).

Definition at line 102 of file moved_from.H.

◆ REGISTER_TYPED_TEST_SUITE_P()

REGISTER_TYPED_TEST_SUITE_P ( MovedFrom  ,
ConstructionLeavesAnEmptyUsableSource  ,
AssignmentLeavesBothObjectsUsable   
)

◆ TYPED_TEST_P() [1/2]

TYPED_TEST_P ( MovedFrom  ,
AssignmentLeavesBothObjectsUsable   
)

Definition at line 138 of file moved_from.H.

References print().

◆ TYPED_TEST_P() [2/2]

TYPED_TEST_P ( MovedFrom  ,
ConstructionLeavesAnEmptyUsableSource   
)

Definition at line 121 of file moved_from.H.

References print().

◆ TYPED_TEST_SUITE_P()

TYPED_TEST_SUITE_P ( MovedFrom  )