Debian Codegraph @

dune/istl/test/bvectortest.cc

raw
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
6 // hack to ensure assert() does something
7 // really, assert() should not be used in unit tests.
8 #ifdef NDEBUG
9 #undef NDEBUG
10 #endif
11
12 #include <cassert>
13 #include <cmath>
14 #include <complex>
15 #include <cstdlib>
16 #include <iostream>
17 #include <memory>
18
19 #include <dune/common/classname.hh>
20 #include <dune/common/debugallocator.hh>
21 #include <dune/common/fvector.hh>
22 #include <dune/common/poolallocator.hh>
23 #include <dune/common/scalarvectorview.hh>
24
25 #include <dune/istl/bvector.hh>
26 #include <dune/istl/test/vectortest.hh>
27
28 template<typename VectorBlock, typename V>
29 void assign(VectorBlock& b, const V& i)
30 {
31 for (auto& entry : Dune::Impl::asVector(b))
32 entry = i;
33 }
34
35
36 template<class VectorBlock, class A=std::allocator<void> >
37 int testVector()
38 {
39 using Alloc = typename std::allocator_traits<A>::template rebind_alloc<VectorBlock>;
40 typedef Dune::BlockVector<VectorBlock, Alloc> Vector;
41
42 // empty vector
43 Vector v, w, v1(20), v2(20,100);
44 #ifdef FAIL
45 Vector v3(20,100.0);
46 #endif
47 v.reserve(100);
48 assert(100==v.capacity());
49 assert(20==v1.capacity());
50 assert(100==v2.capacity());
51 assert(20==v1.N());
52 assert(20==v2.N());
53
54 v.resize(25);
55
56 assert(25==v.N());
57
58 for(typename Vector::size_type i=0; i < v.N(); ++i)
59 v[i] = i;
60
61 for(typename Vector::size_type i=0; i < v2.N(); ++i)
62 v2[i] = i*10;
63 w = v;
64
65 testHomogeneousRandomAccessContainer(v);
66 Dune::testConstructibility<Vector>();
67 testNorms(v);
68 testVectorSpaceOperations(v);
69 testScalarProduct(v);
70
71 assert(w.N()==v.N());
72
73 for(typename Vector::size_type i=0; i < v.N(); ++i)
74 assert(v[i] == w[i]);
75
76 Vector z(w);
77
78 assert(w.N()==z.N());
79 assert(w.capacity()==z.capacity());
80
81 for(typename Vector::size_type i=0; i < w.N(); ++i)
82 assert(z[i] == w[i]);
83
84 v.reserve(150);
85 assert(150==v.capacity());
86 assert(25==v.N());
87
88 VectorBlock b;
89
90 // check the entries
91 for(typename Vector::size_type i=0; i < v.N(); ++i) {
92 assign(b, i);
93 assert(v[i] == b);
94 }
95
96 return 0;
97 }
98
99 void testCapacity()
100 {
101 typedef Dune::FieldVector<double,2> SmallVector;
102 typedef Dune::BlockVector<Dune::BlockVector<SmallVector> > ThreeLevelVector;
103 ThreeLevelVector vec;
104 vec.reserve(10);
105 vec.resize(10);
106 for(int i=0; i<10; ++i)
107 vec[i]=Dune::BlockVector<SmallVector>(10);
108 [[maybe_unused]] ThreeLevelVector vec1 = vec;
109 }
110
111 template <class V>
112 void checkNormNAN(V const &v, int line) {
113 if (!std::isnan(v.one_norm())) {
114 std::cerr << "error: norm not NaN: one_norm() on line "
115 << line << " (type: " << Dune::className(v[0][0]) << ")"
116 << std::endl;
117 std::exit(-1);
118 }
119 if (!std::isnan(v.two_norm())) {
120 std::cerr << "error: norm not NaN: two_norm() on line "
121 << line << " (type: " << Dune::className(v[0][0]) << ")"
122 << std::endl;
123 std::exit(-1);
124 }
125 if (!std::isnan(v.infinity_norm())) {
126 std::cerr << "error: norm not NaN: infinity_norm() on line "
127 << line << " (type: " << Dune::className(v[0][0]) << ")"
128 << std::endl;
129 std::exit(-1);
130 }
131 }
132
133 // Make sure that vectors with NaN entries have norm NaN.
134 // See also bug flyspray/FS#1147
135 template <typename T>
136 void
137 test_nan(T const &mynan)
138 {
139 using FV = Dune::FieldVector<T,2>;
140 using V = Dune::BlockVector<FV>;
141 T n(0);
142 {
143 V v = {
144 { mynan, n },
145 { n, n }
146 };
147 checkNormNAN(v, __LINE__);
148 }
149 {
150 V v = {
151 { n, mynan },
152 { n, n }
153 };
154 checkNormNAN(v, __LINE__);
155 }
156 {
157 V v = {
158 { n, n },
159 { mynan, n }
160 };
161 checkNormNAN(v, __LINE__);
162 }
163 {
164 V v = {
165 { n, n },
166 { n, mynan }
167 };
168 checkNormNAN(v, __LINE__);
169 }
170 {
171 V v = {
172 { mynan, mynan },
173 { mynan, mynan }
174 };
175 checkNormNAN(v, __LINE__);
176 }
177 }
178
179 int main()
180 {
181 typedef std::complex<double> value_type;
182 //typedef double value_type;
183 typedef Dune::FieldVector<value_type,1> VectorBlock;
184 typedef Dune::BlockVector<VectorBlock> Vector;
185
186 // Test a BlockVector of BlockVectors
187 typedef Dune::BlockVector<Vector> VectorOfVector;
188 VectorOfVector vv = {{1.0, 2.0}, {3.0, 4.0, 5.0}, {6.0}};
189
190 testHomogeneousRandomAccessContainer(vv);
191 Dune::testConstructibility<VectorOfVector>();
192 testNorms(vv);
193 testVectorSpaceOperations(vv);
194 testScalarProduct(vv);
195
196 // Test construction from initializer_list
197 Vector fromInitializerList = {0,1,2};
198 assert(fromInitializerList.size() == 3);
199 assert(fromInitializerList[0] == value_type(0));
200 assert(fromInitializerList[1] == value_type(1));
201 assert(fromInitializerList[2] == value_type(2));
202
203 {
204 double nan = std::nan("");
205 test_nan(nan);
206 }
207 {
208 std::complex<double> nan( std::nan(""), 17 );
209 test_nan(nan);
210 }
211
212 int ret = 0;
213
214 ret += testVector<Dune::FieldVector<double,1> >();
215 // ret += testVector<1, Dune::PoolAllocator<void,1000000> >();
216 #if HAVE_MPROTECT
217 ret += testVector<Dune::FieldVector<double,1> , Dune::DebugAllocator<void> >();
218 #endif
219 ret += testVector<Dune::FieldVector<double,3> >();
220 // ret += testVector<3, Dune::PoolAllocator<void,1000000> >();
221 #if HAVE_MPROTECT
222 ret += testVector<Dune::FieldVector<double,1> , Dune::DebugAllocator<void> >();
223 #endif
224
225 ret += testVector<double>();
226 ret += testVector<std::complex<double> >();
227
228 testCapacity();
229
230 return ret;
231 }