| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_TEST_VECTORTEST_HH |
| 6 |
#define DUNE_ISTL_TEST_VECTORTEST_HH |
| 7 |
|
| 8 |
/** \file |
| 9 |
* \brief Infrastructure for testing the dune-istl vector interface |
| 10 |
* |
| 11 |
* This file contains various methods that test parts of the dune-istl vector interface. |
| 12 |
* They only test very general features, i.e., features that every dune-istl vector should |
| 13 |
* have. |
| 14 |
* |
| 15 |
* At the same time, these tests should help to define what the dune-istl vector interface |
| 16 |
* actually is. They may not currently define the entire interface, but they are a lower |
| 17 |
* bound: any feature that is tested here is part of the dune-istl vector interface. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <dune/common/exceptions.hh> |
| 21 |
#include <dune/common/test/iteratortest.hh> |
| 22 |
|
| 23 |
namespace Dune |
| 24 |
{ |
| 25 |
|
| 26 |
/** \brief Test whether a given data type 'Vector' is a random-access container |
| 27 |
* |
| 28 |
* Somewhat self-recursively, this class defines what dune-istls means by 'random-access-container'. |
| 29 |
* |
| 30 |
* Not every dune-istl vector must comply with this: for example, MultiTypeBlockVector is a |
| 31 |
* heterogeneous container, and will get a separate test. |
| 32 |
*/ |
| 33 |
template <typename Vector> |
| 34 |
void testHomogeneousRandomAccessContainer(const Vector& v) |
| 35 |
{ |
| 36 |
// class value_type |
| 37 |
static_assert(std::is_same<typename Vector::value_type, typename Vector::value_type>::value, |
| 38 |
"Vector does not export 'value_type'"); |
| 39 |
|
| 40 |
// class block_type, must be equal to value_type |
| 41 |
static_assert(std::is_same<typename Vector::block_type, typename Vector::block_type>::value, |
| 42 |
"Vector does not export 'block_type'"); |
| 43 |
static_assert(std::is_same<typename Vector::block_type, typename Vector::value_type>::value, |
| 44 |
"'block_type' is not equal to 'value_type'"); |
| 45 |
|
| 46 |
// Check whether 'reference' and 'const_reference' are properly exported |
| 47 |
static_assert(std::is_same<typename Vector::reference, typename Vector::reference>::value, |
| 48 |
"Vector does not export 'reference'"); |
| 49 |
static_assert(std::is_same<typename Vector::const_reference, typename Vector::const_reference>::value, |
| 50 |
"Vector does not export 'const_reference'"); |
| 51 |
|
| 52 |
// class allocator_type |
| 53 |
#if 0 // Out-commented, because it is not clear whether vectors with static allocation should have this |
| 54 |
static_assert(std::is_same<typename Vector::allocator_type, typename Vector::allocator_type>::value, |
| 55 |
"Vector does not export 'allocator_type'"); |
| 56 |
#endif |
| 57 |
// Iterator / iterator |
| 58 |
static_assert(std::is_same<typename Vector::Iterator, typename Vector::Iterator>::value, |
| 59 |
"Vector does not export 'Iterator'"); |
| 60 |
static_assert(std::is_same<typename Vector::iterator, typename Vector::iterator>::value, |
| 61 |
"Vector does not export 'iterator'"); |
| 62 |
static_assert(std::is_same<typename Vector::Iterator, typename Vector::iterator>::value, |
| 63 |
"'Iterator' and 'iterator' are not the same type"); |
| 64 |
|
| 65 |
// - is random-access iterator |
| 66 |
Vector vMutable = v; |
| 67 |
auto noop = [](typename Vector::const_reference t){}; |
| 68 |
// This is testing the non-const iterators |
| 69 |
testRandomAccessIterator(vMutable.begin(), vMutable.end(), noop); |
| 70 |
// ConstIterator / const_iterator |
| 71 |
static_assert(std::is_same<typename Vector::ConstIterator, typename Vector::ConstIterator>::value, |
| 72 |
"Vector does not export 'ConstIterator'"); |
| 73 |
static_assert(std::is_same<typename Vector::const_iterator, typename Vector::const_iterator>::value, |
| 74 |
"Vector does not export 'const_iterator'"); |
| 75 |
static_assert(std::is_same<typename Vector::ConstIterator, typename Vector::const_iterator>::value, |
| 76 |
"'ConstIterator' and 'const_iterator' are not the same type"); |
| 77 |
|
| 78 |
// Test reference types |
| 79 |
static_assert(std::is_same<typename Vector::reference, typename Vector::reference>::value, |
| 80 |
"Vector does not export 'reference'"); |
| 81 |
static_assert(std::is_same<typename Vector::const_reference, typename Vector::const_reference>::value, |
| 82 |
"Vector does not export 'const_reference'"); |
| 83 |
|
| 84 |
// Test the const_iterator |
| 85 |
testRandomAccessIterator(v.begin(), v.end(), noop); |
| 86 |
|
| 87 |
// Check whether 'size_type' is exported |
| 88 |
static_assert(std::is_same<typename Vector::size_type, typename Vector::size_type>::value, |
| 89 |
"Vector does not export 'size_type'"); |
| 90 |
|
| 91 |
// size_type must be integral |
| 92 |
static_assert(std::is_integral<typename Vector::size_type>::value, "'size_type' is not integral!"); |
| 93 |
|
| 94 |
// Check whether methods size() and N() exist, and are consistent |
| 95 |
if (v.size() > v.N()) |
| 96 |
DUNE_THROW(RangeError, "size() must be less than or equal to N()"); |
| 97 |
|
| 98 |
// Check whether method 'find' works, and is consistent with operator[] |
| 99 |
for (typename Vector::size_type i=0; i<v.N(); ++i) |
| 100 |
{ |
| 101 |
// Check whether find(i) returns the correct type |
| 102 |
static_assert(std::is_same<typename Vector::const_iterator,decltype(v.find(i))>::value, "'find const' does not return const_iterator"); |
| 103 |
static_assert(std::is_same<typename Vector::iterator,decltype(vMutable.find(i))>::value, "'find' does not return iterator"); |
| 104 |
|
| 105 |
// Check that if 'find' returns something, it corresponds to the i-th entry |
| 106 |
if (v.find(i) != v.end()) |
| 107 |
if (v.find(i)-i != v.begin()) |
| 108 |
DUNE_THROW(RangeError, "'find(i)' does not return an iterator to the i-th entry"); |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
/** \brief Test whether the given type is default- and copy-constructible */ |
| 114 |
template <typename Vector> |
| 115 |
void testConstructibility() |
| 116 |
{ |
| 117 |
static_assert(std::is_default_constructible<Vector>::value, "Vector type is not default constructible"); |
| 118 |
static_assert(std::is_copy_constructible<Vector>::value, "Vector type is not copy constructible"); |
| 119 |
} |
| 120 |
|
| 121 |
/** \brief Test whether a given type implements all the norms required from a dune-istl vector |
| 122 |
*/ |
| 123 |
template <typename Vector> |
| 124 |
void testNorms(const Vector& v) |
| 125 |
{ |
| 126 |
using field_type = typename Vector::field_type; |
| 127 |
using real_type = typename FieldTraits<field_type>::real_type; |
| 128 |
|
| 129 |
// one_norm |
| 130 |
static_assert(std::is_same<decltype(v.one_norm()),real_type>::value, "'one_norm' does not return 'real_type'"); |
| 131 |
if (v.one_norm() < 0.0) |
| 132 |
DUNE_THROW(RangeError, "'one_norm' returns negative value"); |
| 133 |
|
| 134 |
// one_norm_real |
| 135 |
static_assert(std::is_same<decltype(v.one_norm_real()),real_type>::value, "'one_norm_real' does not return 'real_type'"); |
| 136 |
if (v.one_norm_real() < 0.0) |
| 137 |
DUNE_THROW(RangeError, "'one_norm_real' returns negative value"); |
| 138 |
|
| 139 |
// two_norm |
| 140 |
static_assert(std::is_same<decltype(v.two_norm()),real_type>::value, "'two_norm' does not return 'real_type'"); |
| 141 |
if (v.two_norm() < 0.0) |
| 142 |
DUNE_THROW(RangeError, "'two_norm' returns negative value"); |
| 143 |
|
| 144 |
// two_norm2 |
| 145 |
static_assert(std::is_same<decltype(v.two_norm2()),real_type>::value, "'two_norm2' does not return 'real_type'"); |
| 146 |
if (v.two_norm2() < 0.0) |
| 147 |
DUNE_THROW(RangeError, "'two_norm2' returns negative value"); |
| 148 |
|
| 149 |
// infinity_norm |
| 150 |
static_assert(std::is_same<decltype(v.infinity_norm()),real_type>::value, "'infinity_norm' does not return 'real_type'"); |
| 151 |
if (v.infinity_norm() < 0.0) |
| 152 |
DUNE_THROW(RangeError, "'infinity_norm' returns negative value"); |
| 153 |
|
| 154 |
// infinity_norm_real |
| 155 |
static_assert(std::is_same<decltype(v.infinity_norm_real()),real_type>::value, "'infinity_norm_real' does not return 'real_type'"); |
| 156 |
if (v.infinity_norm_real() < 0.0) |
| 157 |
DUNE_THROW(RangeError, "'infinity_norm_real' returns negative value"); |
| 158 |
} |
| 159 |
|
| 160 |
template <typename Vector> |
| 161 |
void testVectorSpaceOperations(const Vector& v) |
| 162 |
{ |
| 163 |
// Make a mutable copy of the argument |
| 164 |
Vector vMutable = v; |
| 165 |
|
| 166 |
// operator+= |
| 167 |
vMutable += v; |
| 168 |
// operator-= |
| 169 |
vMutable -= v; |
| 170 |
|
| 171 |
// operator*= |
| 172 |
vMutable *= 0.5; |
| 173 |
|
| 174 |
// operator/= |
| 175 |
vMutable /= 0.5; |
| 176 |
|
| 177 |
// axpy |
| 178 |
vMutable.axpy(0.5,v); |
| 179 |
} |
| 180 |
|
| 181 |
/** \brief Test whether the canonical scalar product behaves as it should */ |
| 182 |
template <typename Vector> |
| 183 |
void testScalarProduct(const Vector& v) |
| 184 |
{ |
| 185 |
using field_type = typename Vector::field_type; |
| 186 |
|
| 187 |
static_assert(std::is_same<decltype(v*v),field_type>::value, "operator* (vector product) does not return field_type"); |
| 188 |
} |
| 189 |
} // namespace Dune |
| 190 |
|
| 191 |
#endif |