initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / DynamicList / DynamicListTest.C
blob355c65605fe0210fce847c34152def5943a6b6b3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "DynamicList.H"
30 #include "IOstreams.H"
32 using namespace Foam;
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 // Main program:
37 int main(int argc, char *argv[])
39     List<DynamicList<label, 1, 0> > ldl(2);
41     ldl[0](0) = 0;
42     ldl[0](2) = 2;
43     ldl[0](3) = 3;
44     ldl[0](1) = 1;
46     ldl[0].setCapacity(5);    // increase allocated size
47     ldl[1].setCapacity(10);   // increase allocated size
48     ldl[0].reserve(15);       // should increase allocated size
49     ldl[1].reserve(5);        // should not decrease allocated size
50     ldl[1](3) = 2;            // allocates space and sets value
52     // this works without a segfault, but doesn't change the list size
53     ldl[0][4] = 4;
55     ldl[1] = 3;
57     Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
58     forAll(ldl, i)
59     {
60         Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
61     }
62     Info<< endl;
64     List<List<label> > ll(2);
65     ll[0].transfer(ldl[0]);
66     ll[1].transfer(ldl[1].shrink());
68     Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
69     forAll(ldl, i)
70     {
71         Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
72     }
73     Info<< endl;
75     Info<< "<ll>" << ll << "</ll>" << nl << endl;
78     // test the transfer between DynamicLists
79     DynamicList<label, 1, 0> dlA;
80     DynamicList<label, 1, 0> dlB;
82     for (label i = 0; i < 5; i++)
83     {
84         dlA.append(i);
85     }
86     dlA.setCapacity(10);
88     Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
89         << " " << dlA.size() << "/" << dlA.capacity() << endl;
91     dlB.transfer(dlA);
93     // provokes memory error if previous transfer did not maintain
94     // the correct allocated space
95     dlB[6] = 6;
97     Info<< "Transferred to dlB" << endl;
98     Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
99         << " " << dlA.size() << "/" << dlA.capacity() << endl;
100     Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
101         << " " << dlB.size() << "/" << dlB.capacity() << endl;
103     // try with a normal list:
104     List<label> lstA;
105     lstA.transfer(dlB);
106     Info<< "Transferred to normal list" << endl;
107     Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: "
108         << " " << lstA.size() << endl;
109     Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
110         << " " << dlB.size() << "/" << dlB.capacity() << endl;
112     // Copy back and append a few time
113     for (label i=0; i < 3; i++)
114     {
115         dlB.append(lstA);
116     }
118     Info<< "appended list a few times" << endl;
119     Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
120         << " " << dlB.size() << "/" << dlB.capacity() << endl;
122     // assign the list (should maintain allocated space)
123     dlB = lstA;
124     Info<< "assigned list" << endl;
125     Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
126         << " " << dlB.size() << "/" << dlB.capacity() << endl;
129     // Copy back and append a few time
130     for (label i=0; i < 3; i++)
131     {
132         dlB.append(lstA);
133     }
136     // check allocation granularity
137     DynamicList<label, 6, 0> dlC;
139     Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
140         << " " << dlC.size() << "/" << dlC.capacity() << endl;
142     dlC.reserve(dlB.size());
143     dlC = dlB;
145     Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
146         << " " << dlC.size() << "/" << dlC.capacity() << endl;
148     List<label> lstB(dlC.xfer());
150     Info<< "Transferred to normal list via the xfer() method" << endl;
151     Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
152         << " " << lstB.size() << endl;
153     Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
154         << " " << dlC.size() << "/" << dlC.capacity() << endl;
156     DynamicList<label> dlD(lstB.xfer());
158     Info<< "Transfer construct from normal list" << endl;
159     Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
160         << " " << lstB.size() << endl;
161     Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: "
162         << " " << dlD.size() << "/" << dlD.capacity() << endl;
164     DynamicList<label,10> dlE1(10);
165     DynamicList<label> dlE2(dlE1);   // construct dissimilar
167     Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: "
168         << " " << dlE1.size() << "/" << dlE1.capacity() << endl;
169     Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: "
170         << " " << dlE2.size() << "/" << dlE2.capacity() << endl;
172     for (label elemI=0; elemI < 5; ++elemI)
173     {
174         dlE1.append(4 - elemI);
175         dlE2.append(elemI);
176     }
178     Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl;
180     DynamicList<label> dlE3(dlE2);   // construct identical
181     Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
183     dlE3 = dlE1;   // assign dissimilar
184     Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
186     dlE3 = dlE2;   // assign identical
187     Info<< "<dlE3>" << dlE3 << "</dlE3>" << endl;
190     Info<< "\nEnd\n";
192     return 0;
196 // ************************************************************************* //