source: trunk/test/src/RpUnits_test.cc @ 104

Last change on this file since 104 was 104, checked in by dkearney, 18 years ago

This update brings us one step closer to being const correct in the RpUnits
class. Added a few more comments. All other files touched were made to
comply with the new const correct RpUnits.[h,cc]

File size: 9.3 KB
Line 
1#include "RpUnits.h"
2
3
4void success()
5{
6    printf ("Test Successful\n");
7}
8
9void fail()
10{
11    printf ("Test FAILED !!!!!!!!!!!!!!!!!\n");
12    exit(-1);
13}
14
15
16int main()
17{
18    double value = 0.0;
19    std::string strValue;
20    // int failTest = 0;
21    int result = 0;
22    // std::list<double,RpUnits *>
23
24    //
25    // use main to test class functionality
26    //
27
28    // test define(units,basis)
29    // this should create an object of type RpUnits
30    // with the units of "m" and basis should be NULL
31    // which tells us that this object represents a fundamental system
32    // of units
33    //
34    printf ("=============== TEST 1 ===============\n");
35    const RpUnits * meters = RpUnits::define("m", NULL);
36    // (meters) ? success() : fail();
37    // RpUnits * centimeter = RpUnits::define("cm", NULL);
38
39    RpUnits::makeMetric(meters);
40    RpUnits::define("V", NULL);
41    RpUnits::define("s", NULL);
42
43/*
44    std::string srch_str = std::string("cm");
45
46    RpUnits* meters2 = RpUnits::find("cm");
47    if (meters2) {
48        std::cout << "meters2 exists" << std::endl;
49        std::cout << "meters2 = :" << meters2->getUnitsName() <<":"<< std::endl;
50    }
51
52    std::cout << "complete"<< std::endl;
53
54*/
55   
56    const RpUnits* mobility = RpUnits::defineCmplx("cm2/Vs", NULL);
57    std::cout << "mobility = :" << mobility->getUnitsName() <<":"<< std::endl;
58
59    const RpUnits* mobility2 = RpUnits::find("cm2V-1s-1");
60    if (mobility2) {
61        std::cout << "mobility2 exists" << std::endl;
62        std::cout << "mobility2 = :" << mobility2->getUnits() <<":"<< std::endl;
63        std::cout << "mobility2 = :" << mobility2->getUnitsName() <<":"<< std::endl;
64    }
65    else {
66        std::cout << "mobility2 dn exists" << std::endl;
67    }
68
69    const RpUnits* cmeters = RpUnits::find("cm");
70    const RpUnits* angstrom = RpUnits::define("A", NULL);
71    RpUnits::define(angstrom, meters, angstrom2meter, meter2angstrom);
72
73    value = angstrom->convert(meters,1.0,&result);
74    std::cout << "1 angstrom = " << value << " meters" << std::endl;
75
76    result = 0;
77    value = cmeters->convert(angstrom,1e-8,&result);
78    std::cout << "1.0e-8 centimeter = " << value << " angstroms" << std::endl;
79
80
81    const RpUnits* fahrenheit  = RpUnits::define("F", NULL);
82    const RpUnits* celcius  = RpUnits::define("C", NULL);
83    const RpUnits* kelvin  = RpUnits::define("K", NULL);
84   
85    RpUnits::define(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit);
86    RpUnits::define(celcius, kelvin, centigrade2kelvin, kelvin2centigrade);
87   
88    value = fahrenheit->convert(celcius,72,&result);
89    std::cout << "72 degrees fahrenheit = " << value << " degrees celcius" << std::endl;
90   
91    value = celcius->convert(fahrenheit,value,&result);
92    std::cout << "22.222 degrees celcius = " << value << " degrees fahrenheit" << std::endl;
93   
94    value = celcius->convert(kelvin,20,&result);
95    std::cout << "20 degrees celcius = " << value << " kelvin" << std::endl;
96
97    value = kelvin->convert(celcius,300,&result);
98    std::cout << "300 kelvin = " << value << " degrees celcius" << std::endl;
99
100/*
101    // test getUnits() member function of objects of type RpUnits
102    // this test should return the units of "m" for the oject
103    // meters
104    //
105    printf ("=============== TEST 2 ===============\n");
106    (meters->getUnits().c_str()) ? success() : fail();
107    printf("meters units = :%s:\n", meters->getUnits().c_str());
108   
109    // test getBasis() functionality of objects of type RpUnits
110    // create an object of type RpUnits and associate it with the
111    // meters RpUnits object as its basis.
112    // print out what it reports its basis is.
113    //
114    printf ("=============== TEST 3 ===============\n");
115    std::string t3units = "cm";
116    // RpUnits * centimeters = RpUnits::define("cm", meters);
117    RpUnits * centimeters = RpUnits::define(t3units, meters);
118    (centimeters) ? success() : fail();
119    printf("cm units = :%s:\n", centimeters->getUnits().c_str() );
120    printf("cm basis = :%s:\n", centimeters->getBasis()->getUnits().c_str() );
121
122    // test makeBasis()
123    //
124    printf ("=============== TEST 4.1 ===============\n");
125    // value = centimeters->makeBasis(100.00);
126    value = 100.00;
127    failTest = centimeters->makeBasis(&value);
128    (value == 1 && !failTest) ? success() : fail();
129    printf("100 cm = :%f: meters\n", value);
130
131    // test makeBasis() with a list of values, all the same units
132    //
133    printf ("=============== TEST 4.2 ===============\n");
134//   
135//    double * valueArr = (double *) calloc(12,sizeof(double));
136//    if (!valueArr) {
137//        // complain that malloc failed
138//        exit(-2);
139//    }
140//
141//    valueArr = centimeters->makeBasis(100.00);
142//
143    // (value == 1) ? success() : fail();
144    // printf("100 cm = :%f: meters\n", value);
145
146    // test makeBasis() with a list of values, all different units
147    //
148    printf ("=============== TEST 4.3 ===============\n");
149    // value = centimeters->makeBasis(100.00);
150    // (value == 1) ? success() : fail();
151    // printf("100 cm = :%f: meters\n", value);
152
153*/
154
155    printf ("=============== TEST 4.4 ===============\n");
156
157    const RpUnits * millimeter = RpUnits::find("mm");
158    const RpUnits * micrometer = RpUnits::find("um");
159    const RpUnits * nanometer  = RpUnits::find("nm");
160    const RpUnits * picometer  = RpUnits::find("pm");
161    const RpUnits * femtometer = RpUnits::find("fm");
162    const RpUnits * attometer  = RpUnits::find("am");
163    const RpUnits * kilometer  = RpUnits::find("km");
164    const RpUnits * megameter  = RpUnits::find("Mm");
165    const RpUnits * gigameter  = RpUnits::find("Gm");
166    const RpUnits * terameter  = RpUnits::find("Tm");
167    const RpUnits * petameter  = RpUnits::find("Pm");
168
169    value = 1.0e+3;
170    millimeter->makeBasis(&value);
171    (value == 1) ? success() : fail();
172    printf("1.0e3 mm = :%f: meters\n", value);
173
174    value = 1.0e+6;
175    micrometer->makeBasis(&value);
176    (value == 1) ? success() : fail();
177    printf("1.0e6 um = :%f: meters\n", value);
178
179    value = 1.0e+9;
180    nanometer->makeBasis(&value);
181    (value == 1) ? success() : fail();
182    printf("1.0e9 nm = :%f: meters\n", value);
183
184    value = 1.0e+12;
185    picometer->makeBasis(&value);
186    (value == 1) ? success() : fail();
187    printf("1.0e12 pm = :%f: meters\n", value);
188
189    value = 1.0e+15;
190    femtometer->makeBasis(&value);
191    (value == 1) ? success() : fail();
192    printf("1.0e15 fm = :%f: meters\n", value);
193
194    value = 1.0e+18;
195    attometer->makeBasis(&value);
196    (value == 1) ? success() : fail();
197    printf("1.0e18 am = :%f: meters\n", value);
198
199    value = 1.0e-3;
200    kilometer->makeBasis(&value);
201    (value == 1) ? success() : fail();
202    printf("1.0e-3 km = :%f: meters\n", value);
203
204    value = 1.0e-6;
205    megameter->makeBasis(&value);
206    (value == 1) ? success() : fail();
207    printf("1.0e-6 Mm = :%f: meters\n", value);
208
209    value = 1.0e-9;
210    gigameter->makeBasis(&value);
211    (value == 1) ? success() : fail();
212    printf("1.0e-9 Gm = :%f: meters\n", value);
213
214    value = 1.0e-12;
215    terameter->makeBasis(&value);
216    (value == 1) ? success() : fail();
217    printf("1.0e-12 Gm = :%f: meters\n", value);
218
219    value = 1.0e-15;
220    petameter->makeBasis(&value);
221    (value == 1) ? success() : fail();
222    printf("1.0e-15 Pm = :%f: meters\n", value);
223
224    value = 2;
225    meters->makeBasis(&value);
226    (value == 2) ? success() : fail();
227    printf("2 m = :%f: meters\n", value);
228
229    printf ("=============== TEST 5 ===============\n");
230
231    strValue = RpUnits::convert("72F","C",1);
232    std::cout << "result = " << result << std::endl;
233    std::cout << "strValue convert(72F,C,1) = " << strValue << std::endl;
234
235    strValue = RpUnits::convert("72F","C",0);
236    std::cout << "result = " << result << std::endl;
237    std::cout << "strValue convert(72F,C,0) = " << strValue << std::endl;
238
239    strValue = RpUnits::convert("20C","K",1);
240    std::cout << "result = " << result << std::endl;
241    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
242
243    strValue = RpUnits::convert("20C","K",0);
244    std::cout << "result = " << result << std::endl;
245    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
246
247    strValue = RpUnits::convert("300K","C",1);
248    std::cout << "result = " << result << std::endl;
249    std::cout << "strValue convert(300K,C,1) = " << strValue << std::endl;
250
251    strValue = RpUnits::convert("300K","C",0);
252    std::cout << "result = " << result << std::endl;
253    std::cout << "strValue convert(300K,C,0) = " << strValue << std::endl;
254
255
256
257
258    const RpUnits* eV  = RpUnits::define("eV", NULL);
259    const RpUnits* joules  = RpUnits::define("J", NULL);
260   
261    RpUnits::define(eV, joules, electronVolt2joule, joule2electronVolt);
262   
263    value = joules->convert(eV,1,&result);
264    std::cout << "1 joule = " << value << " electronVolts" << std::endl;
265   
266    value = eV->convert(joules,1,&result);
267    std::cout << "1 electronVolt = " << value << " joules" << std::endl;
268   
269    strValue = RpUnits::convert("10eV","J",1);
270    std::cout << "strValue convert(10eV,J,1) = " << strValue << std::endl;
271
272    strValue = RpUnits::convert("1eV","J",0);
273    std::cout << "strValue convert(1eV,J,0) = " << strValue << std::endl;
274   
275    strValue = RpUnits::convert("10J","eV",1);
276    std::cout << "strValue convert(10J,eV,1) = " << strValue << std::endl;
277
278    strValue = RpUnits::convert("1J","eV",0);
279    std::cout << "strValue convert(1J,eV,0) = " << strValue << std::endl;
280   
281    return 0;
282}
Note: See TracBrowser for help on using the repository browser.