source: Sophya/trunk/SophyaProg/Tests/tnt.cc@ 2828

Last change on this file since 2828 was 2828, checked in by ansari, 20 years ago

1/ include values.h commente ds tstboloread.cc tstcolread.cc (compil OSX)
2/ amelioration prog test TimeStamp (TimeStamp::ToDays() et TimeStamp(double days) en particulier)
3/ amelioration prog test tnt.cc : test MuTyV/DVList avec type TimeStamp et test DataTable , SwPPFDataTable avec champs ComplexField et DoubleComplexField

Reza , 2 Nov 2005

File size: 12.0 KB
RevLine 
[224]1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
[2322]4#include <iostream>
[224]5
[2615]6#include "sopnamsp.h"
[768]7#include "histinit.h"
[720]8#include "dvlist.h"
[224]9#include "ntuple.h"
[720]10#include "xntuple.h"
[2686]11#include "datatable.h"
[2702]12#include "swppfdtable.h"
[224]13
[2686]14/* Programme test des classes DVList, NTuple,, XNTuple */
15/* DataTable et les handlers PPF */
16/* SOPHYA - R. Ansari (LAL) - 2000 - 2005 */
17
[720]18void test_dvl();
19void test_ntuple();
20void test_xntuple() ;
21void test_Xntuple() ;
[2686]22void test_DataTable() ;
[2702]23void test_SwPPFDataTable() ;
[720]24
25int main(int narg, char *arg[])
[224]26{
[768]27 SophyaInit();
[720]28 if (narg < 2) {
[2702]29 cout << " tnt/Erreur arg - Usage: tnt d/n/x/X/DT/SWDT \n"
30 << " d:DVList n:NTuple x/X:XNTuple DT,SWDT: SwPPF/DataTable test \n" << endl;
[720]31 exit(0);
32 }
[224]33
[720]34 try {
35 if (*arg[1] == 'd') test_dvl();
36 else if (*arg[1] == 'n') test_ntuple();
37 else if (*arg[1] == 'x') test_xntuple();
38 else if (*arg[1] == 'X') test_Xntuple();
[2702]39 else if (strcmp(arg[1],"DT") == 0) test_DataTable();
40 else if (strcmp(arg[1],"SWDT") == 0) test_SwPPFDataTable();
[720]41 }
42 catch(PThrowable exc ) {
43 cerr << "tnt-main() , Catched exception: \n" << exc.Msg() << endl;
44 }
45 catch(std::exception ex) {
46 cerr << "tnt-main() , Catched exception ! " << (string)(ex.what()) << endl;
47 }
48 catch(...) {
49 cerr << "tnt-main() , Catched ... ! " << endl;
50 }
51}
[224]52
[720]53/* ***** Test de NTuple simple ***** */
54void test_ntuple()
55{
56 char * names[3] = {"XPos", "YPos", "Val"};
57 int i,j, k;
58 float xnt[3];
[224]59
[720]60 cout << "======= test_ntuple: Testing NTuple ======= " << endl;
[224]61
[720]62 cout << "Creation Ntuple avec X,Y,Val ... " << endl;
63 NTuple nt1(3, names, 20);
[224]64
[720]65 k = 0;
66 for(j=0; j<8; j++)
67 for(i=0; i<12; i++)
68 { xnt[0] = i+0.5; xnt[1] = j+0.5; xnt[2] = k;
69 nt1.Fill(xnt); k++; }
70
[1161]71 nt1.Info().Comment() = "NTuple de Test - Cree par tnt.cc";
72 nt1.Info()["Version"] = SophyaVersion();
[720]73 nt1.Show(cout);
74 nt1.Print(0, 5);
75 nt1.Print(18, 5);
76 nt1.Print(94, 5);
77
78 string fn = "nt.ppf";
79 {
80 cout << "Ecriture NTuple ds nt.ppf ... \n" << endl;
81 ObjFileIO<NTuple> fio(&nt1);
82 fio.Write(fn);
83 }
84
85 {
86 cout << "Lecture NTuple (nt2) ds nt.ppf ... \n" << endl;
[2702]87 PInPersist fsi(fn);
88 NTuple nt2;
89 fsi >> nt2;
90 nt2.Show(cout);
91 nt2.Print(0, 5);
92 nt2.Print(18, 5);
93 nt2.Print(94, 5);
[720]94 }
95
96
97}
98
99/* ***** Test de dvlist ***** */
100void test_dvl()
[583]101{
[720]102DVList dvl;
103
[1161]104cout << "\n ======= test_dvl: Testing MuTyV ======= " << endl;
105MuTyV zvs = " ( 1.41 -2.71) ";
106MuTyV dvs = "434.898";
107MuTyV fvu = 314.1596;
108MuTyV ivu = 7654321;
109cout << " float->string: fvu= " << fvu << " (string)fvu=" << (string)fvu << endl;
110cout << " int->string: ivu= " << ivu << " (string)ivu=" << (string)ivu << endl;
[1343]111 complex<double> zzd = zvs;
[1161]112cout << "String->complex<double>: zvs = " << zvs
[1343]113 << " (complex<double>)zvs= " << zzd << endl;
[1161]114cout << "String->double: dvs = " << dvs
[2828]115 << " (double)dvs= " << (double)dvs << endl;
116TimeStamp ts;
117MuTyV tv = ts;
118cout << "MuTyV=TimeStamp->String: tv = " << tv
119 << " ->double= " << (double)tv << " ->TimeStamp:"
120 << TimeStamp((double)tv).ToString() << endl;
[1161]121
122
123cout << "\n\n ======= test_dvl: Testing DVList ======= " << endl;
[720]124dvl.SetI("Var XXX", 12345);
125dvl.SetI("IV1-80", 80);
126dvl.SetI("IV2-330", 330);
127
128dvl.SetD("DV1-0.2", 0.2);
129dvl.SetD("DV2-4.5", 4.5);
[1082]130dvl.SetZ("ZV", complex<r_8>(2.0,-1.44));
[720]131
132dvl.SetI("IVV3-783", 783);
133dvl("IVV3-783-O") = 7783;
134// dvl["Avec[]"] = "operateur [] !";
135dvl.SetD("DVV3", 3.141592652141592652);
136dvl.SetComment("DVV3", "Comment for DVV3, r_8 type variable");
137dvl("DVV3avec()") = 44.555e-8;
138
139dvl.SetS("Blanc_White", "123.456Ma premiere chaine");
140dvl.SetI("IntegerValue", 55777);
141dvl.SetComment("IntegerValue", "This variable has a comment");
142dvl.Comment() = "This is a test DVL produced by the program tdvl.cc \n Using SOPHYA , Feb 2000";
[1161]143
144dvl["Sinf"] = "inf 0985";
[2828]145dvl["ToDay"] = ts;
[720]146dvl.Print();
147
[1161]148
[720]149double d = dvl("DV2-4.5");
150float f = dvl("DVV3");
151int i = dvl("IVV3-783");
[1082]152complex<r_8> z = dvl("ZV");
[720]153
154printf("\n \n Essai1 (IVV3 DVV3 DV2= ) %d %.20g %g \n", i, f, (float)d);
[1082]155printf("\n \n Essai ZV= (%.2g %g I) \n", z.real(), z.imag());
[720]156cout << "Test Comment/IntegerValue: " << dvl.GetComment("IntegerValue") << endl;
157cout << "Test Comment/DVV3: " << dvl.GetComment("DVV3") << endl;
158
159cout << "Test string recup " << (string)(dvl["Blanc_White"]) << endl;
160cout << "Test string recup(int=80) " << (string)(dvl["IV1-80"]) << endl;
161dvl("DVV3") = (double)3.141592652141592652;
162cout << "Test string recup(double=Pi..i) " << (string)(dvl["DVV3"]) << endl;
163
164 {
165 cout << " Writing DVList in file PPF dvl.ppf " << endl;
166 POutPersist os("dvl.ppf");
167 os << dvl ;
168 }
169
170 cout << "-------------------------- \n\n"
171 << " reading DVList from file dvl.ppf ... \n" << endl;
172
173 DVList dvlr("dvl.ppf");
174 double df1 = (double)( dvlr["DVV3"] );
175 double df2 = (double)3.141592652141592652 - df1;
[1082]176 cout << " Test Precision : Pi-Pi= " << df2 << "DVV3= " << df1 << endl;
[720]177
178 cout << dvlr;
179
[583]180}
[2686]181void test_DataTable()
182{
183 cout << "======= test_DataTable: simple DataTable test ======= " << endl;
184 DataTable dt(64);
185 dt.AddIntegerColumn("line");
[2733]186 dt.AddStringColumn("sline");
[2686]187 dt.AddDoubleColumn("x");
188 dt.AddFloatColumn("f_sin");
189 dt.AddFloatColumn("f_cos");
190 dt.AddDoubleColumn("f_sinxcos");
191 dt.AddDoubleColumn("f_x2");
[2828]192 dt.AddComplexColumn("cmplx_cos_sin");
193 dt.AddDoubleComplexColumn("dcmplx_cos_sin");
[2686]194 MuTyV rec[10];
195 cout << " 1/ First 1000 lines .... " << endl;
196 for(int k = 0; k<1000; k++) {
197 rec[0] = k;
[2733]198 string sline = "L-";
199 sline += (string)rec[0];
200 rec[1] = sline;
[2686]201 double x = M_PI*k/100.;
202 double fx = sin(x)*cos(x);
[2828]203 double sx = sin(x);
204 double cx = cos(x);
[2733]205 rec[2] = x;
[2828]206 rec[3] = sx;
207 rec[4] = cx;
[2733]208 rec[5] = fx;
209 rec[6] = x*x;
[2828]210 rec[7] = complex<r_4>(cx, sx);
211 rec[8] = complex<r_8>(cx, sx);
[2686]212 dt.AddLine(rec);
213 }
214 cout << "1.b/ Use of = operator to make copy of dt " << endl;
215 DataTable dtc1;
216 dtc1 = dt;
[224]217
[2686]218 dt.Show();
219 cout << " 2/ Lines 1000-2000 .... " << endl;
220 for(int k = 1000; k<2000; k++) {
221 rec[0] = k;
[2733]222 string sline = "L-";
223 sline += (string)rec[0];
224 rec[1] = sline;
[2686]225 double x = M_PI*k/100.;
226 double fx = sin(x)*cos(x);
[2828]227 double sx = sin(x);
228 double cx = cos(x);
[2733]229 rec[2] = x;
[2828]230 rec[3] = sx;
231 rec[4] = cx;
[2733]232 rec[5] = fx;
233 rec[6] = x*x;
[2828]234 rec[7] = complex<r_4>(cx, sx);
235 rec[8] = complex<r_8>(cx, sx);
[2686]236 dt.AddLine(rec);
237 }
238 cout << "2.b/ dt.Show(); : " << endl;
239 dt.Show();
240 cout << "2.c/ dtc1.Show(); : " << endl;
241 dtc1.Show();
242 cout << "2.d/ dt.LineHeaderToString() dt.LineToString(k) : " << endl;
243 cout << dt.LineHeaderToString() ;
244 for(int k = 0; k<1500; k+=75)
245 cout << "Line[" << k << "] " << dt.LineToString(k) << endl ;
246 {
247 cout << "3/ Writing DataTable dt to PPF stream dtable.ppf " << endl;
248 POutPersist po("dtable.ppf");
249 po << dt;
250 }
251 {
252 cout << "4/ Reading DataTable dtr from PPF stream dtable.ppf " << endl;
253 PInPersist pi("dtable.ppf");
254 DataTable dtr;
255 pi >> dtr;
256 cout << "4.b/ cout << dtr; " << endl;
257 cout << dtr;
258 cout << "4.c/ dtr.LineHeaderToString() dtr.LineToString(k) : " << endl;
259 cout << dtr.LineHeaderToString() ;
260 for(int k = 0; k<1500; k+=75)
261 cout << "Line[" << k << "] " << dtr.LineToString(k) << endl ;
262
263 }
264
265 cout << "============ FIN test_DataTable ============== " << endl;
266}
267
[2702]268void test_SwPPFDataTable()
269{
270 cout << "======= test_SwPPFDataTable(): simple SwPPFDataTable test ======= " << endl;
271 {
272 POutPersist po("swdtable.ppf");
273 SwPPFDataTable dt(po, 64);
274 dt.AddIntegerColumn("line");
275 dt.AddDoubleColumn("x");
276 dt.AddFloatColumn("f_sin");
277 dt.AddFloatColumn("f_cos");
278 dt.AddDoubleColumn("f_sinxcos");
279 dt.AddDoubleColumn("f_x2");
[2828]280 dt.AddComplexColumn("cmplx_cos_sin");
281 dt.AddDoubleComplexColumn("dcmplx_cos_sin");
[2702]282 MuTyV rec[10];
283 cout << " 1/ First 1000 lines .... " << endl;
284 for(int k = 0; k<1000; k++) {
285 rec[0] = k;
286 double x = M_PI*k/100.;
287 double fx = sin(x)*cos(x);
[2828]288 double sx = sin(x);
289 double cx = cos(x);
[2702]290 rec[1] = x;
[2828]291 rec[2] = sx;
292 rec[3] = cx;
[2702]293 rec[4] = fx;
294 rec[5] = x*x;
[2828]295 rec[6] = complex<r_4>(cx, sx);
296 rec[7] = complex<r_8>(cx, sx);
[2702]297 dt.AddLine(rec);
298 }
299 cout << "1.b/ Use of = operator to make copy of dt " << endl;
300 SwPPFDataTable dtc1;
301 dtc1 = dt;
302 dt.Show();
303 cout << " 2/ Lines 1000-2000 .... " << endl;
304 for(int k = 1000; k<2000; k++) {
305 rec[0] = k;
306 double x = M_PI*k/100.;
307 double fx = sin(x)*cos(x);
308 rec[1] = x;
309 rec[2] = sin(x);
310 rec[3] = cos(x);
311 rec[4] = fx;
312 rec[5] = x*x;
313 dt.AddLine(rec);
314 }
315 cout << "2.b/ dt.Show(); : " << endl;
316 dt.Show();
317 cout << "2.c/ dtc1.Show(); : " << endl;
318 dtc1.Show();
319 cout << "3/ Writing SwPPFDataTable dt to PPF stream swdtable.ppf " << endl;
320 po << dt;
321 }
322 {
323 cout << "4/ Reading SwPPFDataTable dtr from PPF stream swdtable.ppf " << endl;
324 PInPersist pi("swdtable.ppf");
325 SwPPFDataTable dtr;
326 pi >> dtr;
327 cout << "4.b/ cout << dtr; " << endl;
328 cout << dtr;
329 cout << "4.c/ dtr.LineHeaderToString() dtr.LineToString(k) : " << endl;
330 cout << dtr.LineHeaderToString() ;
331 for(int k = 0; k<1500; k+=75)
332 cout << "Line[" << k << "] " << dtr.LineToString(k) << endl ;
333 }
334
335 cout << "============ FIN test_SwPPFDataTable() ======== ======= " << endl;
336}
337
338
339
340
[720]341void test_Xntuple()
[583]342{
[720]343 char* names[] = {"str1", "str2", "str3", "str4", "str5"} ;
344 XNTuple nt(0, 0, 0, 5, names) ;
345 char** ce = new char*[5] ;
[1326]346 int i;
347 for(i = 0 ; i < 5 ; i++)
[720]348 ce[i] = new char[20] ;
[224]349
[720]350 cout << "======= test_Xtuple: simple XNTuple test ======= " << endl;
351
352 strncpy(ce[1], "toto a une auto", 20) ;
353 strncpy(ce[2], "titi a une iti", 20) ;
354 strncpy(ce[3], "tutu a une utu", 20) ;
355 strncpy(ce[4], "tata a une ata", 20) ;
[1326]356 for(i = 0 ; i < 100000 ; i++) {
[720]357 sprintf(ce[0], "%d", i) ;
358 nt.Fill(NULL, NULL, NULL, ce) ;
359 }
360
361 nt.Show() ;
362 cout << nt.LineHeaderToString() ;
363 cout << nt.LineToString(5027) << endl ;
364
365 char* names2[] = {"d0", "d1", "f0", "f1", "f2", "i0", "str0", "str1"} ;
366 XNTuple nt2(2, 3, 1, 2, names2) ;
367 double de[2] ; float fe[3] ; int ie ;
368 char** ce2 = new char*[2] ;
[1326]369 for(i = 0 ; i < 2 ; i++) ce2[i] = new char[20] ;
[720]370 strncpy(ce2[1], "glop glop", 20) ;
371
[1326]372 for(i = 0 ; i < 100000 ; i++) {
[720]373 de[0] = i ;
[1406]374 de[1] = sin((double)i) ;
[720]375 fe[0] = i ;
[1406]376 fe[1] = i * cos((double)i) ;
[720]377 fe[2] = 2*i ;
378 ie = -i;
379 sprintf(ce[0], "%d", i) ;
380 nt2.Fill(de, fe, &ie, ce) ;
381 }
382 nt2.Show() ;
383 nt2.LineHeaderToString() ;
384 // nt2.LineToString(20) ;
[583]385}
[224]386
387
[720]388void test_xntuple()
389{
390 char* names[] = {"dblval", "floval", "intval", "strval"} ;
391 XNTuple nt(1, 1, 1, 1, names) ;
392 double de ;
393 float fe ;
394 int ie ;
395 char* ce = new char[22] ;
396
397
398 cout << "======= test_Xtuple: XNTuple test ======= " << endl;
[1326]399 int i;
400 for(i = 0 ; i < nt.NVar() ; i++)
[720]401 printf(" +++ %s <--> %d \n",
402 nt.NomIndex(i).c_str(), nt.IndexNom(nt.NomIndex(i).c_str())) ;
[224]403
[1326]404 for(i = 0 ; i < 100000 ; i++) {
[720]405 de = fe = ie = i ;
406 sprintf(ce, "%d", i) ;
407 nt.Fill(&de, &fe, &ie, &ce) ;
408 }
409 nt.Show() ;
410 cout << nt.VarList_C("toto") ;
411 cout << nt.LineHeaderToString() ;
412 cout << nt.LineToString(20) << endl << endl ;
413
414
415 XNTuple nt2 ;
416 nt2.SwapPath() = "/tmp/sop/" ;
417 nt2.Copy(nt) ;
418 nt2.Show() ;
[1326]419 for(i = 0 ; i < 100000 ; i++) {
[720]420 de = fe = ie = i ;
421 sprintf(ce, "%d", i) ;
422 nt2.Fill(&de, &fe, &ie, &ce) ;
423 }
424 nt2.Show() ;
425
426 {
427 POutPersist os("xnt.ppf");
428 os << nt2 ;
429 }
[224]430
[720]431 XNTuple::SetSwapPath("/tmp/sop/") ;
432 XNTuple nt3("xnt.ppf") ;
433 nt3.Show() ;
434
[1326]435 for(i = 0 ; i < nt3.NEntry() ; i+= 1000)
[720]436 printf("%f %f %d %s\n",
437 nt3.GetDVal(i,0),
438 nt3.GetFVal(i,1),
439 nt3.GetIVal(i,2),
440 nt3.GetSVal(i,3).c_str()) ;
441
442 double min, max ;
[1326]443 for(i = 0 ; i < nt3.NVar() ; i++) {
[720]444 nt3.GetMinMax(i, min, max) ;
445 printf("GetMinMax(%s) : %f/%f\n",
446 nt3.NomIndex(i).c_str(), min, max) ;
447 }
448 // nt3.Show() ;
[224]449}
450
Note: See TracBrowser for help on using the repository browser.