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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 7.0 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
12void test_dvl();
13void test_ntuple();
14void test_xntuple() ;
15void test_Xntuple() ;
16
17int main(int narg, char *arg[])
18{
19 SophyaInit();
20 if (narg < 2) {
21 cout << " tnt/Erreur arg - Usage: tnt d/n/x/X \n"
22 << " d:DVList n:NTuple x/X:XNTuple test \n" << endl;
23 exit(0);
24 }
25
26 try {
27 if (*arg[1] == 'd') test_dvl();
28 else if (*arg[1] == 'n') test_ntuple();
29 else if (*arg[1] == 'x') test_xntuple();
30 else if (*arg[1] == 'X') test_Xntuple();
31 }
32 catch(PThrowable exc ) {
33 cerr << "tnt-main() , Catched exception: \n" << exc.Msg() << endl;
34 }
35 catch(std::exception ex) {
36 cerr << "tnt-main() , Catched exception ! " << (string)(ex.what()) << endl;
37 }
38 catch(...) {
39 cerr << "tnt-main() , Catched ... ! " << endl;
40 }
41}
42
43/* ***** Test de NTuple simple ***** */
44void test_ntuple()
45{
46 char * names[3] = {"XPos", "YPos", "Val"};
47 int i,j, k;
48 float xnt[3];
49
50 cout << "======= test_ntuple: Testing NTuple ======= " << endl;
51
52 cout << "Creation Ntuple avec X,Y,Val ... " << endl;
53 NTuple nt1(3, names, 20);
54
55 k = 0;
56 for(j=0; j<8; j++)
57 for(i=0; i<12; i++)
58 { xnt[0] = i+0.5; xnt[1] = j+0.5; xnt[2] = k;
59 nt1.Fill(xnt); k++; }
60
61 nt1.Info().Comment() = "NTuple de Test - Cree par tnt.cc";
62 nt1.Info()["Version"] = SophyaVersion();
63 nt1.Show(cout);
64 nt1.Print(0, 5);
65 nt1.Print(18, 5);
66 nt1.Print(94, 5);
67
68 string fn = "nt.ppf";
69 {
70 cout << "Ecriture NTuple ds nt.ppf ... \n" << endl;
71 ObjFileIO<NTuple> fio(&nt1);
72 fio.Write(fn);
73 }
74
75 {
76 cout << "Lecture NTuple (nt2) ds nt.ppf ... \n" << endl;
77
78 ObjFileIO<NTuple> fio(fn);
79 NTuple* nt2 = (NTuple*)fio.DataObj();
80 nt2->Show(cout);
81 nt2->Print(0, 5);
82 nt2->Print(18, 5);
83 nt2->Print(94, 5);
84 }
85
86
87}
88
89/* ***** Test de dvlist ***** */
90void test_dvl()
91{
92DVList dvl;
93
94cout << "\n ======= test_dvl: Testing MuTyV ======= " << endl;
95MuTyV zvs = " ( 1.41 -2.71) ";
96MuTyV dvs = "434.898";
97MuTyV fvu = 314.1596;
98MuTyV ivu = 7654321;
99cout << " float->string: fvu= " << fvu << " (string)fvu=" << (string)fvu << endl;
100cout << " int->string: ivu= " << ivu << " (string)ivu=" << (string)ivu << endl;
101 complex<double> zzd = zvs;
102cout << "String->complex<double>: zvs = " << zvs
103 << " (complex<double>)zvs= " << zzd << endl;
104cout << "String->double: dvs = " << dvs
105 << " (double)zvs= " << (double)dvs << endl;
106
107
108cout << "\n\n ======= test_dvl: Testing DVList ======= " << endl;
109dvl.SetI("Var XXX", 12345);
110dvl.SetI("IV1-80", 80);
111dvl.SetI("IV2-330", 330);
112
113dvl.SetD("DV1-0.2", 0.2);
114dvl.SetD("DV2-4.5", 4.5);
115dvl.SetZ("ZV", complex<r_8>(2.0,-1.44));
116
117dvl.SetI("IVV3-783", 783);
118dvl("IVV3-783-O") = 7783;
119// dvl["Avec[]"] = "operateur [] !";
120dvl.SetD("DVV3", 3.141592652141592652);
121dvl.SetComment("DVV3", "Comment for DVV3, r_8 type variable");
122dvl("DVV3avec()") = 44.555e-8;
123
124dvl.SetS("Blanc_White", "123.456Ma premiere chaine");
125dvl.SetI("IntegerValue", 55777);
126dvl.SetComment("IntegerValue", "This variable has a comment");
127dvl.Comment() = "This is a test DVL produced by the program tdvl.cc \n Using SOPHYA , Feb 2000";
128
129dvl["Sinf"] = "inf 0985";
130dvl.Print();
131
132
133double d = dvl("DV2-4.5");
134float f = dvl("DVV3");
135int i = dvl("IVV3-783");
136complex<r_8> z = dvl("ZV");
137
138printf("\n \n Essai1 (IVV3 DVV3 DV2= ) %d %.20g %g \n", i, f, (float)d);
139printf("\n \n Essai ZV= (%.2g %g I) \n", z.real(), z.imag());
140cout << "Test Comment/IntegerValue: " << dvl.GetComment("IntegerValue") << endl;
141cout << "Test Comment/DVV3: " << dvl.GetComment("DVV3") << endl;
142
143cout << "Test string recup " << (string)(dvl["Blanc_White"]) << endl;
144cout << "Test string recup(int=80) " << (string)(dvl["IV1-80"]) << endl;
145dvl("DVV3") = (double)3.141592652141592652;
146cout << "Test string recup(double=Pi..i) " << (string)(dvl["DVV3"]) << endl;
147
148 {
149 cout << " Writing DVList in file PPF dvl.ppf " << endl;
150 POutPersist os("dvl.ppf");
151 os << dvl ;
152 }
153
154 cout << "-------------------------- \n\n"
155 << " reading DVList from file dvl.ppf ... \n" << endl;
156
157 DVList dvlr("dvl.ppf");
158 double df1 = (double)( dvlr["DVV3"] );
159 double df2 = (double)3.141592652141592652 - df1;
160 cout << " Test Precision : Pi-Pi= " << df2 << "DVV3= " << df1 << endl;
161
162 cout << dvlr;
163
164}
165
166void test_Xntuple()
167{
168 char* names[] = {"str1", "str2", "str3", "str4", "str5"} ;
169 XNTuple nt(0, 0, 0, 5, names) ;
170 char** ce = new char*[5] ;
171 int i;
172 for(i = 0 ; i < 5 ; i++)
173 ce[i] = new char[20] ;
174
175 cout << "======= test_Xtuple: simple XNTuple test ======= " << endl;
176
177 strncpy(ce[1], "toto a une auto", 20) ;
178 strncpy(ce[2], "titi a une iti", 20) ;
179 strncpy(ce[3], "tutu a une utu", 20) ;
180 strncpy(ce[4], "tata a une ata", 20) ;
181 for(i = 0 ; i < 100000 ; i++) {
182 sprintf(ce[0], "%d", i) ;
183 nt.Fill(NULL, NULL, NULL, ce) ;
184 }
185
186 nt.Show() ;
187 cout << nt.LineHeaderToString() ;
188 cout << nt.LineToString(5027) << endl ;
189
190 char* names2[] = {"d0", "d1", "f0", "f1", "f2", "i0", "str0", "str1"} ;
191 XNTuple nt2(2, 3, 1, 2, names2) ;
192 double de[2] ; float fe[3] ; int ie ;
193 char** ce2 = new char*[2] ;
194 for(i = 0 ; i < 2 ; i++) ce2[i] = new char[20] ;
195 strncpy(ce2[1], "glop glop", 20) ;
196
197 for(i = 0 ; i < 100000 ; i++) {
198 de[0] = i ;
199 de[1] = sin((double)i) ;
200 fe[0] = i ;
201 fe[1] = i * cos((double)i) ;
202 fe[2] = 2*i ;
203 ie = -i;
204 sprintf(ce[0], "%d", i) ;
205 nt2.Fill(de, fe, &ie, ce) ;
206 }
207 nt2.Show() ;
208 nt2.LineHeaderToString() ;
209 // nt2.LineToString(20) ;
210}
211
212
213void test_xntuple()
214{
215 char* names[] = {"dblval", "floval", "intval", "strval"} ;
216 XNTuple nt(1, 1, 1, 1, names) ;
217 double de ;
218 float fe ;
219 int ie ;
220 char* ce = new char[22] ;
221
222
223 cout << "======= test_Xtuple: XNTuple test ======= " << endl;
224 int i;
225 for(i = 0 ; i < nt.NVar() ; i++)
226 printf(" +++ %s <--> %d \n",
227 nt.NomIndex(i).c_str(), nt.IndexNom(nt.NomIndex(i).c_str())) ;
228
229 for(i = 0 ; i < 100000 ; i++) {
230 de = fe = ie = i ;
231 sprintf(ce, "%d", i) ;
232 nt.Fill(&de, &fe, &ie, &ce) ;
233 }
234 nt.Show() ;
235 cout << nt.VarList_C("toto") ;
236 cout << nt.LineHeaderToString() ;
237 cout << nt.LineToString(20) << endl << endl ;
238
239
240 XNTuple nt2 ;
241 nt2.SwapPath() = "/tmp/sop/" ;
242 nt2.Copy(nt) ;
243 nt2.Show() ;
244 for(i = 0 ; i < 100000 ; i++) {
245 de = fe = ie = i ;
246 sprintf(ce, "%d", i) ;
247 nt2.Fill(&de, &fe, &ie, &ce) ;
248 }
249 nt2.Show() ;
250
251 {
252 POutPersist os("xnt.ppf");
253 os << nt2 ;
254 }
255
256 XNTuple::SetSwapPath("/tmp/sop/") ;
257 XNTuple nt3("xnt.ppf") ;
258 nt3.Show() ;
259
260 for(i = 0 ; i < nt3.NEntry() ; i+= 1000)
261 printf("%f %f %d %s\n",
262 nt3.GetDVal(i,0),
263 nt3.GetFVal(i,1),
264 nt3.GetIVal(i,2),
265 nt3.GetSVal(i,3).c_str()) ;
266
267 double min, max ;
268 for(i = 0 ; i < nt3.NVar() ; i++) {
269 nt3.GetMinMax(i, min, max) ;
270 printf("GetMinMax(%s) : %f/%f\n",
271 nt3.NomIndex(i).c_str(), min, max) ;
272 }
273 // nt3.Show() ;
274}
275
Note: See TracBrowser for help on using the repository browser.