initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / Dictionary / DictionaryTest.C
blob0af4dab6612e97fc340d434a29246d24f3e08a57
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 Application
27 Description
29 \*---------------------------------------------------------------------------*/
31 #include "OSspecific.H"
33 #include "scalar.H"
35 #include "IOstreams.H"
36 #include "Dictionary.H"
37 #include "PtrDictionary.H"
39 using namespace Foam;
41 class ent
43     public Dictionary<ent>::link
45     word keyword_;
46     int i_;
48 public:
50     ent(const word& keyword, int i)
51     :
52         keyword_(keyword),
53         i_(i)
54     {}
56     const word& keyword() const
57     {
58         return keyword_;
59     }
61     friend Ostream& operator<<(Ostream& os, const ent& e)
62     {
63         os << e.keyword_ << ' ' << e.i_ << endl;
64         return os;
65     }
69 class Scalar
71     scalar data_;
73 public:
75     Scalar()
76     :
77         data_(0)
78     {}
80     Scalar(scalar val)
81     :
82         data_(val)
83     {}
85     ~Scalar()
86     {
87         Info <<"delete Scalar: " << data_ << endl;
88     }
90     friend Ostream& operator<<(Ostream& os, const Scalar& val)
91     {
92         os << val.data_;
93         return os;
94     }
99 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100 //  Main program:
102 int main(int argc, char *argv[])
104     Dictionary<ent>* dictPtr = new Dictionary<ent>;
105     Dictionary<ent>& dict = *dictPtr;
107     for (int i = 0; i<10; i++)
108     {
109         ent* ePtr = new ent(word("ent") + name(i), i);
110         dict.append(ePtr->keyword(), ePtr);
111         dict.swapUp(ePtr);
112     }
114     Info<< dict << endl;
116     dict.swapDown(dict.first());
118     for
119     (
120         Dictionary<ent>::const_iterator iter = dict.begin();
121         iter != dict.end();
122         ++iter
123     )
124     {
125         Info<< "element : " << *iter;
126     }
128     Info<< "keys: " << dict.toc() << endl;
130     delete dictPtr;
132     Dictionary<ent> dict2;
134     for (int i = 0; i<10; i++)
135     {
136         ent* ePtr = new ent(word("ent") + name(i), i);
137         dict2.append(ePtr->keyword(), ePtr);
138         dict2.swapUp(ePtr);
139     }
141     Info<< "dict:\n" << dict2 << endl;
143     Info<< nl << "Testing transfer: " << nl << endl;
144     Info<< "original: " << dict2 << endl;
146     Dictionary<ent> newDict;
147     newDict.transfer(dict2);
149     Info<< nl << "source: " << dict2 << nl
150         << "keys: " << dict2.toc() << nl
151         << "target: " << newDict << nl
152         << "keys: " << newDict.toc() << endl;
155     PtrDictionary<Scalar> scalarDict;
156     for (int i = 0; i<10; i++)
157     {
158         word key("ent" + name(i));
159         scalarDict.insert(key, new Scalar(1.3*i));
160     }
162     Info<< nl << "scalarDict1: " << endl;
163     for
164     (
165         PtrDictionary<Scalar>::const_iterator iter = scalarDict.begin();
166         iter != scalarDict.end();
167         ++iter
168     )
169     {
170         Info<< " = " << iter() << endl;
171     }
172     
173     PtrDictionary<Scalar> scalarDict2;
174     for (int i = 8; i<15; i++)
175     {
176         word key("ent" + name(i));
177         scalarDict2.insert(key, new Scalar(1.3*i));
178     }
179     Info<< nl << "scalarDict2: " << endl;
180     for
181     (
182         PtrDictionary<Scalar>::const_iterator iter = scalarDict2.begin();
183         iter != scalarDict2.end();
184         ++iter
185     )
186     {
187         Info<< "elem = " << *iter << endl;
188     }
189     
190     scalarDict.transfer(scalarDict2);
192     
193     Scalar* p = scalarDict.lookupPtr("ent8");
194     
195     // This does not (yet) work
196     // Scalar* q = scalarDict.remove("ent10");
198     if (p)
199     {
200         Info << "found: " << *p << endl;
201     }
202     else
203     {
204         Info << "no p: " << endl;
205     }
207     scalarDict.clear();
209     // Info<< " = " << *iter << endl;
213     Info<< nl << "Done." << endl;
214     return 0;
218 // ************************************************************************* //