Debian Codegraph @

dune/istl/test/vbvectortest.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 #include <dune/istl/vbvector.hh>
6 #include <dune/common/fvector.hh>
7 #include <dune/common/typetraits.hh>
8 #include <dune/common/test/testsuite.hh>
9
10 #include <dune/common/test/iteratortest.hh>
11
12 #include <dune/istl/test/vectortest.hh>
13
14 using namespace Dune;
15
16 int main()
17 {
18 TestSuite suite;
19
20 VariableBlockVector<FieldVector<double,1> > v1;
21 VariableBlockVector<FieldVector<double,1> > v2 = v1;
22 VariableBlockVector<FieldVector<double,1> > v3(10);
23 VariableBlockVector<FieldVector<double,1> > v4(10,4);
24
25 v3.resize(20);
26 v4.resize(20,8);
27
28 v3 = v4;
29
30 /*
31 v3 is now fully initialized due to the former copy operation with
32 the initialized vector v4.
33 Calling the create iterator is not allowed, now.
34 We have to un-initialize it first:
35 */
36 std::size_t size = 20;
37 v3.resize(size); // this makes v3 uninitialized again
38
39 // Set block sizes with CreateIterator:
40 for (auto cIt = v3.createbegin(); cIt!=v3.createend(); ++cIt)
41 cIt.setblocksize(3);
42
43 v3 = 1.0;
44
45 // Test whether something from <algorithm> can be used to set the block sizes
46 // We can't use std::fill() here, as that requires a forward iterator, std::fill_n()
47 // is more lenient and settles for an output iterator
48 v1.resize(size);
49 std::fill_n(v1.createbegin(), size, 10);
50
51 // More formally: test whether the CreateIterator is an output iterator in the stl sense
52 v1.resize(5);
53 testOutputIterator(v1.createbegin(), 5, 10);
54
55 /* Copy-ing specific blocks with `auto` from a VariableBlockVector is tricky, because
56 * the returned object will be a reference:
57 */
58 auto block0_copy_reference = v3[0];
59 block0_copy_reference[0] = 4.2; // change first entry in the copy which has reference semantics. This also changes v3!
60 suite.check(v3[0][0] != 1.0, "Show auto x = v3[0] has reference semantics")
61 << "Unexpected behaviour: v3[0][0] is " << v3[0][0];
62
63 v3[0][0]=1.0; // reset v3
64
65 // For an actual copy, use the Dune::autoCopy() mechanism
66 // This will give a BlockVector with the contents of v3[0].
67 auto block0_autoCopy = Dune::autoCopy(v3[0]);
68 block0_autoCopy[0] = 4.2;
69 suite.check(v3[0][0] == 1.0, "Show that v3 was not modified when copying via autoCopy")
70 << "Unexpected behaviour: v3[0][0] is " << v3[0][0];
71
72
73 // Perform more general vector tests:
74 testHomogeneousRandomAccessContainer(v3);
75 Dune::testConstructibility<VariableBlockVector<FieldVector<double,1> > >();
76 testNorms(v3);
77 testVectorSpaceOperations(v3);
78 testScalarProduct(v3);
79
80 // Perform tests with a scalar vector entry
81 VariableBlockVector<double> v5(10);
82
83 testHomogeneousRandomAccessContainer(v5);
84 Dune::testConstructibility<VariableBlockVector<double> >();
85 testNorms(v5);
86 testVectorSpaceOperations(v5);
87 testScalarProduct(v5);
88
89 // check move construction
90 auto v6 = std::move(v4);
91 suite.check(v6.size()==20, "Check size of moved-constructed object");
92 suite.check(v6[0].size() == 8, "Check if blocksize of move constructed vector survived");
93
94 return suite.exit();
95 }