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

Last change on this file since 116 was 116, checked in by dkearney, 19 years ago
  1. rewrote RpUnits::define(...) and RpUnits::convert(...) fxns.
  2. added functionality so you no longer need to call add_presets(...)
  3. RpUnits can now handle conversions as follows 1cm2/Vs -> 1e-7m2/kVus
  4. Cannot handle conversions dealing with Temperature very well because

Temperature conversions have offsets (+/- 32...). you can still do
F->C and Fs->Cs, but Fms->Cs provides unreliable results. (not to
mention that i'm still unsure how to do a conversion like this.

  1. still need to add Fo (delta fahrenheit) and Co (delta celcius)

units and conversions. These should not be effected by the notorious
temperature

  1. adjusted RpUnits_test.cc for testing. python.fortran and matlab

bindings have not been tested yet.

File size: 12.1 KB
Line 
1/*
2 * ======================================================================
3 *  Copyright (c) 2004-2005  Purdue Research Foundation
4 *
5 *  See the file "license.terms" for information on usage and
6 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 * ======================================================================
8 */
9#include "RpUnits.h"
10
11
12void success()
13{
14    printf ("Test Successful\n");
15}
16
17void fail()
18{
19    printf ("Test FAILED !!!!!!!!!!!!!!!!!\n");
20    exit(-1);
21}
22
23
24int main()
25{
26    double value = 0.0;
27    std::string strValue;
28    // int failTest = 0;
29    int result = 0;
30    // std::list<double,RpUnits *>
31
32    //
33    // use main to test class functionality
34    //
35
36    // test define(units,basis)
37    // this should create an object of type RpUnits
38    // with the units of "m" and basis should be NULL
39    // which tells us that this object represents a fundamental system
40    // of units
41    //
42    printf ("=============== TEST 1 ===============\n");
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/*
57    const RpUnits* mobility = RpUnits::defineCmplx("cm2/Vs", NULL);
58    std::cout << "mobility = :" << mobility->getUnitsName() <<":"<< std::endl;
59
60    const RpUnits* mobility2 = RpUnits::find("cm2V-1s-1");
61    if (mobility2) {
62        std::cout << "mobility2 exists" << std::endl;
63        std::cout << "mobility2 = :" << mobility2->getUnits() <<":"<< std::endl;
64        std::cout << "mobility2 = :" << mobility2->getUnitsName() <<":"<< std::endl;
65    }
66    else {
67        std::cout << "mobility2 dn exists" << std::endl;
68    }
69*/
70
71    const RpUnits* meters = RpUnits::find("m");
72    const RpUnits* cmeters = RpUnits::find("cm");
73    const RpUnits* angstrom = RpUnits::find("A");
74
75    value = angstrom->convert(meters,1.0,&result);
76    std::cout << "1 angstrom = " << value << " meters" << std::endl;
77    std::cout << "result = " << result << std::endl;
78
79    result = 0;
80    value = cmeters->convert(angstrom,1e-8,&result);
81    std::cout << "1.0e-8 centimeter = " << value << " angstroms" << std::endl;
82
83
84    const RpUnits* fahrenheit  = RpUnits::find("F");
85    const RpUnits* celcius  = RpUnits::find("C");
86    const RpUnits* kelvin  = RpUnits::find("K");
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::find("eV");
259    const RpUnits* joules  = RpUnits::find("J");
260
261    value = joules->convert(eV,1,&result);
262    std::cout << "1 joule = " << value << " electronVolts" << std::endl;
263
264    value = eV->convert(joules,1,&result);
265    std::cout << "1 electronVolt = " << value << " joules" << std::endl;
266
267    strValue = RpUnits::convert("10eV","J",1);
268    std::cout << "strValue convert(10eV,J,1) = " << strValue << std::endl;
269
270    strValue = RpUnits::convert("1eV","J",0);
271    std::cout << "strValue convert(1eV,J,0) = " << strValue << std::endl;
272
273    strValue = RpUnits::convert("10J","eV",1);
274    std::cout << "strValue convert(10J,eV,1) = " << strValue << std::endl;
275
276    strValue = RpUnits::convert("1J","eV",0);
277    std::cout << "strValue convert(1J,eV,0) = " << strValue << std::endl;
278
279
280
281
282
283
284    std::cout << "TESTING COPY CONSTRUCTOR" << std::endl;
285
286    RpUnits *origRpUnits = RpUnits::define("obj2", NULL);
287    RpUnits copyRpUnits = *origRpUnits;
288
289    std::cout << "origRpUnits = " << origRpUnits->getUnitsName() << std::endl;
290    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
291    std::cout << "modifying origRpUnits" << std::endl;
292    delete origRpUnits;
293    origRpUnits = RpUnits::define("obj3",NULL);
294    std::cout << "origRpUnits = " << origRpUnits->getUnitsName() << std::endl;
295    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
296    std::cout << "deleting origRpUnits" << std::endl;
297    delete origRpUnits;
298    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
299
300    std::cout << "TESTING COPY ASSIGNMENT OPERATOR" << std::endl;
301
302    RpUnits *testRpUnits = RpUnits::define("test2", NULL);
303    copyRpUnits = *testRpUnits;
304
305    std::cout << "testRpUnits = " << testRpUnits->getUnitsName() << std::endl;
306    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
307    std::cout << "modifying testRpUnits" << std::endl;
308    delete testRpUnits;
309    testRpUnits = RpUnits::define("test3",NULL);
310    std::cout << "testRpUnits = " << testRpUnits->getUnitsName() << std::endl;
311    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
312    std::cout << "deleting testRpUnits" << std::endl;
313    delete testRpUnits;
314    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
315
316
317    // test deleting a const object
318
319    const RpUnits* myobj = RpUnits::define("myunit",NULL);
320    delete myobj;
321
322    // test /cm2
323    // std::cout << "convert (3m3 -> cm3)     = " << RpUnits::convert("3m3","cm3",0) << std::endl;
324    // std::cout << "convert (3/m3 -> /cm3)   = " << RpUnits::convert("3/m3","/cm3",0) << std::endl;
325    // std::cout << "convert (300cm3 -> m3)   = " << RpUnits::convert("300cm3","m3",0) << std::endl;
326    // std::cout << "convert (300/cm3 -> /m3) = " << RpUnits::convert("300/cm3","/m3",0) << std::endl;
327
328    std::cout << "convert (3m3 -> cm3)     = " << RpUnits::convert("3m3","cm3",0) << std::endl;
329    std::cout << "convert (3/m3 -> /cm3)   = " << RpUnits::convert("3/m3","/cm3",0) << std::endl;
330    std::cout << "convert (300cm3 -> m3)   = " << RpUnits::convert("300cm3","m3",0) << std::endl;
331    std::cout << "convert (300/cm3 -> /m3) = " << RpUnits::convert("300/cm3","/m3",0) << std::endl;
332    std::cout << "convert (72F -> 22.22C) = " << RpUnits::convert("72F","C",0) << std::endl;
333    std::cout << "convert (5J -> 3.12075e+28neV) = " << RpUnits::convert("5J","neV",0) << std::endl;
334    std::cout << "convert (5J2 -> 1.9478e+56neV2) = " << RpUnits::convert("5J2","neV2",0) << std::endl;
335
336    // testing complex units
337    std::cout << "convert (1cm2/Vs -> 0.0001m2/Vs) = " << RpUnits::convert("1cm2/Vs","m2/Vs",0) << std::endl;
338    std::cout << "convert (1cm2/Vs -> 0.1m2/kVs) = " << RpUnits::convert("1cm2/Vs","m2/kVs",0) << std::endl;
339    std::cout << "convert (1cm2/Vs -> 1e-7m2/kVus) = " << RpUnits::convert("1cm2/Vs","m2/kVus",0) << std::endl;
340
341    return 0;
342
343}
Note: See TracBrowser for help on using the repository browser.