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

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

Ajout test classe DataTable ds tnt.cc - Reza 25/4/2005

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