source: ThirdParty/mpqc_open/src/lib/util/state/statetest.cc@ 860145

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 860145 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 12.3 KB
Line 
1//
2// statetest.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28// a simple program to test the state stuff
29
30#include <iostream>
31
32#include <util/misc/formio.h>
33
34#include <util/class/class.h>
35#include <util/keyval/keyval.h>
36#include <util/state/state.h>
37
38#include <util/state/linkage.h>
39
40using namespace std;
41using namespace sc;
42
43#ifdef __GNUG__
44#pragma implementation "stattmpl"
45#pragma implementation "clastmpl"
46#endif
47
48#if 0 // normally 0
49# define StateOutTypeA StateOutText
50# define StateInTypeA StateInText
51# include <util/state/state_text.h>
52#else
53# define StateOutTypeA StateOutBin
54# define StateInTypeA StateInBin
55# include <util/state/state_bin.h>
56#endif
57
58#if 0 // normally 0
59# define StateOutTypeB StateOutBin
60# define StateInTypeB StateInBin
61# include <util/state/state_bin.h>
62#else
63# define StateOutTypeB StateOutText
64# define StateInTypeB StateInText
65# include <util/state/state_text.h>
66#endif
67
68class A: virtual public SavableState {
69 private:
70 int ia;
71 int* array;
72 double d;
73 char *t1c;
74 char *t2c;
75 public:
76 A();
77 A(const Ref<KeyVal>&);
78 A(StateIn&);
79 ~A();
80 void save_data_state(StateOut&);
81 inline int& a() { return ia; };
82 virtual void print (ostream&s = ExEnv::out0())
83 {
84 s << "A::t1c = " << t1c << '\n';
85 s << "A::t2c = " << t2c << '\n';
86 s << "A::a = " << a() << '\n';
87 s << "A::d = " << d << '\n';
88 s << "A::array = {"
89 << array[0] << ' '
90 << array[1] << ' '
91 << array[2] << ' '
92 << array[3]
93 << "}\n";
94 }
95};
96
97A::A():
98 ia(1),
99 array(new int[4]),
100 d(-1.24)
101{
102 array[0] = 4;
103 array[1] = 3;
104 array[2] = 2;
105 array[3] = 1;
106 const char* t1 = "test string";
107 const char* t2 = "test2\nstring";
108 t1c = strcpy(new char[strlen(t1)+1],t1);
109 t2c = strcpy(new char[strlen(t2)+1],t2);
110}
111A::A(const Ref<KeyVal>&keyval):
112 ia(keyval->intvalue("a")),
113 array(new int[4]),
114 d(-1.24)
115
116{
117 array[0] = 4;
118 array[1] = 3;
119 array[2] = 2;
120 array[3] = 8;
121 const char* t1 = "test string";
122 const char* t2 = "test2\nstring";
123 t1c = strcpy(new char[strlen(t1)+1],t1);
124 t2c = strcpy(new char[strlen(t2)+1],t2);
125}
126A::A(StateIn&s):
127 SavableState(s)
128{
129 s.get(d,"d");
130 s.getstring(t1c);
131 s.get(ia,"a");
132 s.getstring(t2c);
133 s.get(array);
134}
135A::~A()
136{
137 delete[] array;
138 delete[] t1c;
139 delete[] t2c;
140}
141void
142A::save_data_state(StateOut&s)
143{
144 s.put(d);
145 s.putstring(t1c);
146 s.put(ia);
147 s.putstring(t2c);
148 s.put(array,4);
149}
150
151static ClassDesc A_cd(typeid(A),"A",1,"virtual public SavableState",
152 create<A>, create<A>, create<A>);
153
154class B: public A {
155 private:
156 int ib;
157 public:
158 B();
159 B(const Ref<KeyVal>&);
160 B(StateIn&);
161 void save_data_state(StateOut&);
162 inline int& b() { return ib; };
163 virtual void print (ostream&s = cout)
164 {
165 A::print(s);
166 s << "B::b = " << b() << '\n';
167 }
168};
169
170B::B():
171 ib(2)
172{
173}
174B::B(const Ref<KeyVal>&keyval):
175 A(keyval),
176 ib(keyval->intvalue("b"))
177{
178}
179B::B(StateIn&s):
180 SavableState(s),
181 A(s)
182{
183 s.get(ib);
184}
185void
186B::save_data_state(StateOut&s)
187{
188 A::save_data_state(s);
189 s.put(ib);
190}
191
192static ClassDesc B_cd(typeid(B),"B",1,"public A",
193 create<B>,create<B>,create<B>);
194
195class C: virtual public SavableState {
196 private:
197 int ic;
198 public:
199 C();
200 C(const Ref<KeyVal>&keyval);
201 C(StateIn&);
202 void save_data_state(StateOut&);
203 inline int& c() { return ic; };
204 virtual void print (ostream&s = cout)
205 {
206 s << "C::c = " << c() << '\n';
207 }
208};
209
210C::C():
211 ic(3)
212{
213}
214C::C(const Ref<KeyVal>&keyval):
215 ic(keyval->intvalue("c"))
216{
217}
218C::C(StateIn&s):
219 SavableState(s)
220{
221 s.get(ic);
222}
223void
224C::save_data_state(StateOut&s)
225{
226 s.put(ic);
227}
228
229static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
230 create<C>,create<C>,create<C>);
231
232class D: public B, public C {
233 private:
234 int id;
235 char cd;
236 float fd;
237 double dd;
238 Ref<A> _a;
239 Ref<B> _b;
240 char *cdat;
241 int *idat;
242 float *fdat;
243 double *ddat;
244 std::string sdat;
245 public:
246 D();
247 D(const Ref<KeyVal>&);
248 D(StateIn&);
249 ~D();
250 void save_data_state(StateOut&);
251 inline int& d() { return id; }
252 inline Ref<A> da() { return _a; }
253 inline Ref<B> db() { return _b; }
254 virtual void print (ostream&s = cout)
255 {
256 B::print(s);
257 C::print(s);
258 s << "D::a:\n";
259 if (da().nonnull()) {
260 da()->print(s);
261 }
262 else {
263 s << "null\n";
264 }
265 if ( _a.pointer() == dynamic_cast<A*>(db().pointer()))
266 {
267 cout << "a == b\n";
268 }
269 else {
270 s << "D::b:\n"; db()->print(s);
271 }
272 s << "D::d = " << d() << '\n';
273 s << "D::sdat = " << sdat << std::endl;
274 }
275};
276
277D::D()
278{
279 id = 4;
280 cd = 'd';
281 fd = 4.1;
282 dd = 8.2;
283 ddat = new double[4];
284 fdat = new float[4];
285 idat = new int[4];
286 cdat = new char[4];
287 cdat[0]=(cdat[1]=(cdat[2]=(cdat[3]='a')+1)+1)+1;
288 idat[0]=(idat[1]=(idat[2]=(idat[3]=1)+1)+1)+1;
289 fdat[0]=(fdat[1]=(fdat[2]=(fdat[3]=1.0)+1)+1)+1;
290 ddat[0]=(ddat[1]=(ddat[2]=(ddat[3]=1.0)+1)+1)+1;
291 sdat = "Test of std::string";
292}
293D::D(const Ref<KeyVal>&keyval):
294 B(keyval),
295 C(keyval),
296 id(keyval->intvalue("di")),
297 cd(keyval->charvalue("dc")),
298 fd(keyval->floatvalue("df")),
299 dd(keyval->doublevalue("dd")),
300 _a(dynamic_cast<A*>(keyval->describedclassvalue("da").pointer())),
301 _b(dynamic_cast<B*>(keyval->describedclassvalue("db").pointer()))
302{
303 ddat = new double[4];
304 fdat = new float[4];
305 idat = new int[4];
306 cdat = new char[4];
307 cdat[0]=(cdat[1]=(cdat[2]=(cdat[3]='a')+1)+1)+1;
308 idat[0]=(idat[1]=(idat[2]=(idat[3]=1)+1)+1)+1;
309 fdat[0]=(fdat[1]=(fdat[2]=(fdat[3]=1.0)+1)+1)+1;
310 ddat[0]=(ddat[1]=(ddat[2]=(ddat[3]=1.0)+1)+1)+1;
311 sdat = "Test of std::string";
312}
313D::D(StateIn&s):
314 SavableState(s),
315 B(s),
316 C(s)
317{
318 s.get(id,"di");
319 s.get(cd,"dc");
320 s.get(fd,"df");
321 s.get(dd,"dd");
322 char *junk;
323 s.getstring(junk);
324 delete[] junk;
325 _a << SavableState::key_restore_state(s,"da");
326 s.getstring(junk);
327 delete[] junk;
328 _b << SavableState::key_restore_state(s,"db");
329 s.get(ddat);
330 s.get(fdat);
331 s.get(idat);
332 s.get(cdat);
333 s.get(sdat);
334}
335void
336D::save_data_state(StateOut&s)
337{
338 B::save_data_state(s);
339 C::save_data_state(s);
340 s.put(id);
341 s.put(cd);
342 s.put(fd);
343 s.put(dd);
344 s.putstring("here begins _a");
345 SavableState::save_state(_a.pointer(), s);
346 s.putstring("here begins _b");
347 SavableState::save_state(_b.pointer(),s);
348 s.put(ddat,4);
349 s.put(fdat,4);
350 s.put(idat,4);
351 s.put(cdat,4);
352 s.put(sdat);
353}
354D::~D()
355{
356 delete[] ddat;
357 delete[] fdat;
358 delete[] idat;
359 delete[] cdat;
360}
361
362static ClassDesc D_cd(typeid(D),"D",1,"public B, public C",
363 create<D>, create<D>, create<D>);
364
365int
366main()
367{
368 Ref<A> ra;
369
370 ClassDesc::list_all_classes();
371
372 ra = 0;
373
374 A a;
375 cout << "A name:" << a.class_name() << endl;
376
377 D d;
378 cout << "D name:" << d.class_name() << endl;
379
380 cout << "&d = " << (void*) &d << endl;
381 cout << "dynamic_cast<D*>(&d) = " << (void*) dynamic_cast<D*>(&d) << endl;
382 cout << "dynamic_cast<B*>(&d) = " << (void*) dynamic_cast<B*>(&d) << endl;
383 cout << "dynamic_cast<A*>(&d) = " << (void*) dynamic_cast<A*>(&d) << endl;
384 cout << "dynamic_cast<C*>(&d) = " << (void*) dynamic_cast<C*>(&d) << endl;
385 cout << "dynamic_cast<DescribedClass*>(&d) = "
386 << (void*) dynamic_cast<DescribedClass*>(&d) << endl;
387
388 Ref<AssignedKeyVal> akv (new AssignedKeyVal);
389
390 akv->assign(":x",1);
391 akv->assign(":y",3.0);
392
393#define stringize(arg) # arg
394#define show( arg ) do{cout<<" " stringize(arg) "="<<(arg);}while(0)
395
396 show( akv->exists(":x") ); show( akv->errormsg() ); cout << endl;
397 show( akv->exists(":z") ); show (akv->errormsg() ); cout << endl;
398 show( akv->intvalue(":y") ); show( akv->errormsg() ); cout << endl;
399 show( akv->doublevalue(":x") ); show( akv->errormsg() ); cout << endl;
400 show( akv->intvalue(":x") ); show (akv->errormsg() ); cout << endl;
401 show( akv->intvalue("x") ); show (akv->errormsg() ); cout << endl;
402 show( akv->intvalue(":z") ); show (akv->errormsg() ); cout << endl;
403
404 Ref<KeyVal> pkv = new ParsedKeyVal(SRCDIR "/statetest.in");
405
406 show( pkv->exists(":x") ); show( pkv->errormsg() ); cout << endl;
407 show( pkv->exists(":z") ); show (pkv->errormsg() ); cout << endl;
408 show( pkv->intvalue(":y") ); show( pkv->errormsg() ); cout << endl;
409 show( pkv->doublevalue(":x") ); show( pkv->errormsg() ); cout << endl;
410 show( pkv->intvalue(":x") ); show (pkv->errormsg() ); cout << endl;
411 show( pkv->intvalue("x") ); show (pkv->errormsg() ); cout << endl;
412 show( pkv->intvalue(":z") ); show (pkv->errormsg() ); cout << endl;
413
414 Ref<DescribedClass> rdc = pkv->describedclassvalue("test:object");
415 show (pkv->errormsg() ); cout << endl;
416 show( rdc.pointer() ); cout << endl;
417 ra = dynamic_cast<A*>(rdc.pointer());
418 show( ra.pointer() ); cout << endl;
419
420 show( pkv->intvalue(":test:object:d") ); cout << endl;
421
422 //pkv->dump();
423
424 show( ra.pointer() ); cout << endl;
425 if (ra.nonnull()) { ra->print(); cout << endl; }
426
427 ////////////////////////////////////////////////////////////////////
428 // state tests
429
430 cout << " ------------- saving state ----------------" << endl;
431
432 cout << " --- saving to A ---" << endl;
433 StateOutTypeA soa("statetest.a.out");
434 ra = new A(new PrefixKeyVal("test:object_a",pkv));
435 cout << " first a" << endl;
436 ra->save_object_state(soa);
437 soa.forget_references();
438 cout << " second a" << endl;
439 ra->save_object_state(soa);
440 ra = dynamic_cast<A*>(rdc.pointer());
441 ra->save_state(soa);
442 soa.flush();
443 soa.close();
444 cout << " --- saving to B ---" << endl;
445 StateOutTypeB so("statetest.out");
446 SavableState::save_state(ra.pointer(),so);
447 Ref<A> ra2;
448 SavableState::save_state(ra2.pointer(),so);
449 so.close();
450
451 cout << " ------------- restoring state ----------------" << endl;
452
453 cout << " --- restoring from A ---" << endl;
454 StateInTypeA sia("statetest.a.out");
455 cout << " first a" << endl;
456 ra = new A(sia);
457 cout << " second a" << endl;
458 ra = new A(sia);
459 cout << " last object" << endl;
460 ra << SavableState::restore_state(sia);
461 if (ra.nonnull()) { ra->print(); cout << endl; }
462 if (sia.use_directory()) {
463 cout << " --- restoring from A's directory ---" << endl;
464 ra << SavableState::dir_restore_state(sia,"B:1");
465 cout << "B:1 classname = " << ra->class_name() << endl;
466 }
467 sia.close();
468 cout << " --- restoring from B ---" << endl;
469 StateInTypeB si("statetest.out");
470 //ra = A::restore_state(si);
471 ra << SavableState::restore_state(si);
472 ra2 << SavableState::restore_state(si);
473 if (ra.nonnull()) { ra->print(); cout << endl; }
474 cout << "ra2.nonnull() = " << ra2.nonnull() << "(should be 0)\n";
475 si.close();
476
477 if (sia.use_directory()) {
478 sia.open("statetest.a.out");
479 ExEnv::out0() << indent
480 << " --- restoring from A's directory (2) ---" << endl;
481 ra << SavableState::dir_restore_state(sia,"B:1");
482 ExEnv::out0() << indent
483 << "B:1 classname = " << ra->class_name() << endl;
484 Ref<A> ra3;
485 ra3 << SavableState::dir_restore_state(sia,"B:1");
486 ExEnv::out0() << indent
487 <<"first B:1: " << (void*) ra.pointer()
488 << " second B:1: " << (void*) ra3.pointer()
489 << endl;
490 }
491 ExEnv::out0() << indent << "objects in sia" << endl;
492 sia.list_objects();
493
494 if (sia.use_directory()) {
495 cout << " ----- proxy tests ----- " << endl;
496 Ref<D> d1; d1 << pkv->describedclassvalue("test2:proxy1");
497 Ref<D> d2; d2 << pkv->describedclassvalue("test2:proxy2");
498 cout << "d1 = " << (void*)d1.pointer()
499 << " d2 = " << (void*)d2.pointer() << endl;
500 if (d1.nonnull()) d1->print();
501 }
502
503 return 0;
504}
Note: See TracBrowser for help on using the repository browser.