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

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

fixed the python rpunits setup script so now it really finds PyRpUnits?.cc

added more conversions to RpUnitsStd?.[h,cc]

added static convert function to RpUnits.[h,cc] so now users can ask for a
conversion as without having to make an RpUnits Object. instead, the
user specifies the value to be converted (value with attached units) as well
as the units we should convert to and if they want units in the returned
string. if the units exist, the conversion is done, if not, the original
string should be returned.

also added the ability to load preset units into the dictionary.
use the static function RpUnits::addPresets(...) in c++ this is done
automatically in python (although, maybe it shouldn't be).

File size: 8.2 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    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    RpUnits* mobility = RpUnits::define("cm2/Vs", NULL);
57    std::cout << "mobility = :" << mobility->getUnitsName() <<":"<< std::endl;
58
59    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    RpUnits* cmeters = RpUnits::find("cm");
70    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    RpUnits* fahrenheit  = RpUnits::define("F", NULL);
82    RpUnits* celcius  = RpUnits::define("C", NULL);
83    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    printf ("=============== TEST 4.4 ===============\n");
155
156    RpUnits * millimeter = RpUnits::find("mm");
157    RpUnits * micrometer = RpUnits::find("um");
158    RpUnits * nanometer  = RpUnits::find("nm");
159    RpUnits * picometer  = RpUnits::find("pm");
160    RpUnits * femtometer = RpUnits::find("fm");
161    RpUnits * attometer  = RpUnits::find("am");
162    RpUnits * kilometer  = RpUnits::find("km");
163    RpUnits * megameter  = RpUnits::find("Mm");
164    RpUnits * gigameter  = RpUnits::find("Gm");
165    RpUnits * terameter  = RpUnits::find("Tm");
166    RpUnits * petameter  = RpUnits::find("Pm");
167
168    value = 1.0e+3;
169    millimeter->makeBasis(&value);
170    (value == 1) ? success() : fail();
171    printf("1.0e3 mm = :%f: meters\n", value);
172
173    value = 1.0e+6;
174    micrometer->makeBasis(&value);
175    (value == 1) ? success() : fail();
176    printf("1.0e6 um = :%f: meters\n", value);
177
178    value = 1.0e+9;
179    nanometer->makeBasis(&value);
180    (value == 1) ? success() : fail();
181    printf("1.0e9 nm = :%f: meters\n", value);
182
183    value = 1.0e+12;
184    picometer->makeBasis(&value);
185    (value == 1) ? success() : fail();
186    printf("1.0e12 pm = :%f: meters\n", value);
187
188    value = 1.0e+15;
189    femtometer->makeBasis(&value);
190    (value == 1) ? success() : fail();
191    printf("1.0e15 fm = :%f: meters\n", value);
192
193    value = 1.0e+18;
194    attometer->makeBasis(&value);
195    (value == 1) ? success() : fail();
196    printf("1.0e18 am = :%f: meters\n", value);
197
198    value = 1.0e-3;
199    kilometer->makeBasis(&value);
200    (value == 1) ? success() : fail();
201    printf("1.0e-3 km = :%f: meters\n", value);
202
203    value = 1.0e-6;
204    megameter->makeBasis(&value);
205    (value == 1) ? success() : fail();
206    printf("1.0e-6 Mm = :%f: meters\n", value);
207
208    value = 1.0e-9;
209    gigameter->makeBasis(&value);
210    (value == 1) ? success() : fail();
211    printf("1.0e-9 Gm = :%f: meters\n", value);
212
213    value = 1.0e-12;
214    terameter->makeBasis(&value);
215    (value == 1) ? success() : fail();
216    printf("1.0e-12 Gm = :%f: meters\n", value);
217
218    value = 1.0e-15;
219    petameter->makeBasis(&value);
220    (value == 1) ? success() : fail();
221    printf("1.0e-15 Pm = :%f: meters\n", value);
222
223    value = 2;
224    meters->makeBasis(&value);
225    (value == 2) ? success() : fail();
226    printf("2 m = :%f: meters\n", value);
227
228    printf ("=============== TEST 5 ===============\n");
229
230    strValue = RpUnits::convert("72F","C",1);
231    std::cout << "result = " << result << std::endl;
232    std::cout << "strValue convert(72F,C,1) = " << strValue << std::endl;
233
234    strValue = RpUnits::convert("72F","C",0);
235    std::cout << "result = " << result << std::endl;
236    std::cout << "strValue convert(72F,C,0) = " << strValue << std::endl;
237
238    strValue = RpUnits::convert("20C","K",1);
239    std::cout << "result = " << result << std::endl;
240    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
241
242    strValue = RpUnits::convert("20C","K",0);
243    std::cout << "result = " << result << std::endl;
244    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
245
246    strValue = RpUnits::convert("300K","C",1);
247    std::cout << "result = " << result << std::endl;
248    std::cout << "strValue convert(300K,C,1) = " << strValue << std::endl;
249
250    strValue = RpUnits::convert("300K","C",0);
251    std::cout << "result = " << result << std::endl;
252    std::cout << "strValue convert(300K,C,0) = " << strValue << std::endl;
253
254    return 0;
255}
Note: See TracBrowser for help on using the repository browser.