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

Last change on this file since 115 was 115, checked in by mmc, 19 years ago

Updated all copyright notices.

File size: 11.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    const RpUnits * meters = RpUnits::define("m", NULL);
44    // (meters) ? success() : fail();
45    // RpUnits * centimeter = RpUnits::define("cm", NULL);
46
47    RpUnits::makeMetric(meters);
48    RpUnits::define("V", NULL);
49    RpUnits::define("s", NULL);
50
51/*
52    std::string srch_str = std::string("cm");
53
54    RpUnits* meters2 = RpUnits::find("cm");
55    if (meters2) {
56        std::cout << "meters2 exists" << std::endl;
57        std::cout << "meters2 = :" << meters2->getUnitsName() <<":"<< std::endl;
58    }
59
60    std::cout << "complete"<< std::endl;
61
62*/
63   
64    const RpUnits* mobility = RpUnits::defineCmplx("cm2/Vs", NULL);
65    std::cout << "mobility = :" << mobility->getUnitsName() <<":"<< std::endl;
66
67    const RpUnits* mobility2 = RpUnits::find("cm2V-1s-1");
68    if (mobility2) {
69        std::cout << "mobility2 exists" << std::endl;
70        std::cout << "mobility2 = :" << mobility2->getUnits() <<":"<< std::endl;
71        std::cout << "mobility2 = :" << mobility2->getUnitsName() <<":"<< std::endl;
72    }
73    else {
74        std::cout << "mobility2 dn exists" << std::endl;
75    }
76
77    const RpUnits* cmeters = RpUnits::find("cm");
78    const RpUnits* angstrom = RpUnits::define("A", NULL);
79    RpUnits::define(angstrom, meters, angstrom2meter, meter2angstrom);
80
81    value = angstrom->convert(meters,1.0,&result);
82    std::cout << "1 angstrom = " << value << " meters" << std::endl;
83
84    result = 0;
85    value = cmeters->convert(angstrom,1e-8,&result);
86    std::cout << "1.0e-8 centimeter = " << value << " angstroms" << std::endl;
87
88
89    const RpUnits* fahrenheit  = RpUnits::define("F", NULL);
90    const RpUnits* celcius  = RpUnits::define("C", NULL);
91    const RpUnits* kelvin  = RpUnits::define("K", NULL);
92   
93    RpUnits::define(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit);
94    RpUnits::define(celcius, kelvin, centigrade2kelvin, kelvin2centigrade);
95   
96    value = fahrenheit->convert(celcius,72,&result);
97    std::cout << "72 degrees fahrenheit = " << value << " degrees celcius" << std::endl;
98   
99    value = celcius->convert(fahrenheit,value,&result);
100    std::cout << "22.222 degrees celcius = " << value << " degrees fahrenheit" << std::endl;
101   
102    value = celcius->convert(kelvin,20,&result);
103    std::cout << "20 degrees celcius = " << value << " kelvin" << std::endl;
104
105    value = kelvin->convert(celcius,300,&result);
106    std::cout << "300 kelvin = " << value << " degrees celcius" << std::endl;
107
108/*
109    // test getUnits() member function of objects of type RpUnits
110    // this test should return the units of "m" for the oject
111    // meters
112    //
113    printf ("=============== TEST 2 ===============\n");
114    (meters->getUnits().c_str()) ? success() : fail();
115    printf("meters units = :%s:\n", meters->getUnits().c_str());
116   
117    // test getBasis() functionality of objects of type RpUnits
118    // create an object of type RpUnits and associate it with the
119    // meters RpUnits object as its basis.
120    // print out what it reports its basis is.
121    //
122    printf ("=============== TEST 3 ===============\n");
123    std::string t3units = "cm";
124    // RpUnits * centimeters = RpUnits::define("cm", meters);
125    RpUnits * centimeters = RpUnits::define(t3units, meters);
126    (centimeters) ? success() : fail();
127    printf("cm units = :%s:\n", centimeters->getUnits().c_str() );
128    printf("cm basis = :%s:\n", centimeters->getBasis()->getUnits().c_str() );
129
130    // test makeBasis()
131    //
132    printf ("=============== TEST 4.1 ===============\n");
133    // value = centimeters->makeBasis(100.00);
134    value = 100.00;
135    failTest = centimeters->makeBasis(&value);
136    (value == 1 && !failTest) ? success() : fail();
137    printf("100 cm = :%f: meters\n", value);
138
139    // test makeBasis() with a list of values, all the same units
140    //
141    printf ("=============== TEST 4.2 ===============\n");
142//   
143//    double * valueArr = (double *) calloc(12,sizeof(double));
144//    if (!valueArr) {
145//        // complain that malloc failed
146//        exit(-2);
147//    }
148//
149//    valueArr = centimeters->makeBasis(100.00);
150//
151    // (value == 1) ? success() : fail();
152    // printf("100 cm = :%f: meters\n", value);
153
154    // test makeBasis() with a list of values, all different units
155    //
156    printf ("=============== TEST 4.3 ===============\n");
157    // value = centimeters->makeBasis(100.00);
158    // (value == 1) ? success() : fail();
159    // printf("100 cm = :%f: meters\n", value);
160
161*/
162
163    printf ("=============== TEST 4.4 ===============\n");
164
165    const RpUnits * millimeter = RpUnits::find("mm");
166    const RpUnits * micrometer = RpUnits::find("um");
167    const RpUnits * nanometer  = RpUnits::find("nm");
168    const RpUnits * picometer  = RpUnits::find("pm");
169    const RpUnits * femtometer = RpUnits::find("fm");
170    const RpUnits * attometer  = RpUnits::find("am");
171    const RpUnits * kilometer  = RpUnits::find("km");
172    const RpUnits * megameter  = RpUnits::find("Mm");
173    const RpUnits * gigameter  = RpUnits::find("Gm");
174    const RpUnits * terameter  = RpUnits::find("Tm");
175    const RpUnits * petameter  = RpUnits::find("Pm");
176
177    value = 1.0e+3;
178    millimeter->makeBasis(&value);
179    (value == 1) ? success() : fail();
180    printf("1.0e3 mm = :%f: meters\n", value);
181
182    value = 1.0e+6;
183    micrometer->makeBasis(&value);
184    (value == 1) ? success() : fail();
185    printf("1.0e6 um = :%f: meters\n", value);
186
187    value = 1.0e+9;
188    nanometer->makeBasis(&value);
189    (value == 1) ? success() : fail();
190    printf("1.0e9 nm = :%f: meters\n", value);
191
192    value = 1.0e+12;
193    picometer->makeBasis(&value);
194    (value == 1) ? success() : fail();
195    printf("1.0e12 pm = :%f: meters\n", value);
196
197    value = 1.0e+15;
198    femtometer->makeBasis(&value);
199    (value == 1) ? success() : fail();
200    printf("1.0e15 fm = :%f: meters\n", value);
201
202    value = 1.0e+18;
203    attometer->makeBasis(&value);
204    (value == 1) ? success() : fail();
205    printf("1.0e18 am = :%f: meters\n", value);
206
207    value = 1.0e-3;
208    kilometer->makeBasis(&value);
209    (value == 1) ? success() : fail();
210    printf("1.0e-3 km = :%f: meters\n", value);
211
212    value = 1.0e-6;
213    megameter->makeBasis(&value);
214    (value == 1) ? success() : fail();
215    printf("1.0e-6 Mm = :%f: meters\n", value);
216
217    value = 1.0e-9;
218    gigameter->makeBasis(&value);
219    (value == 1) ? success() : fail();
220    printf("1.0e-9 Gm = :%f: meters\n", value);
221
222    value = 1.0e-12;
223    terameter->makeBasis(&value);
224    (value == 1) ? success() : fail();
225    printf("1.0e-12 Gm = :%f: meters\n", value);
226
227    value = 1.0e-15;
228    petameter->makeBasis(&value);
229    (value == 1) ? success() : fail();
230    printf("1.0e-15 Pm = :%f: meters\n", value);
231
232    value = 2;
233    meters->makeBasis(&value);
234    (value == 2) ? success() : fail();
235    printf("2 m = :%f: meters\n", value);
236
237    printf ("=============== TEST 5 ===============\n");
238
239    strValue = RpUnits::convert("72F","C",1);
240    std::cout << "result = " << result << std::endl;
241    std::cout << "strValue convert(72F,C,1) = " << strValue << std::endl;
242
243    strValue = RpUnits::convert("72F","C",0);
244    std::cout << "result = " << result << std::endl;
245    std::cout << "strValue convert(72F,C,0) = " << strValue << std::endl;
246
247    strValue = RpUnits::convert("20C","K",1);
248    std::cout << "result = " << result << std::endl;
249    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
250
251    strValue = RpUnits::convert("20C","K",0);
252    std::cout << "result = " << result << std::endl;
253    std::cout << "strValue convert(20C,K,1) = " << strValue << std::endl;
254
255    strValue = RpUnits::convert("300K","C",1);
256    std::cout << "result = " << result << std::endl;
257    std::cout << "strValue convert(300K,C,1) = " << strValue << std::endl;
258
259    strValue = RpUnits::convert("300K","C",0);
260    std::cout << "result = " << result << std::endl;
261    std::cout << "strValue convert(300K,C,0) = " << strValue << std::endl;
262
263
264
265
266    const RpUnits* eV  = RpUnits::define("eV", NULL);
267    const RpUnits* joules  = RpUnits::define("J", NULL);
268
269    RpUnits::define(eV, joules, electronVolt2joule, joule2electronVolt);
270
271    value = joules->convert(eV,1,&result);
272    std::cout << "1 joule = " << value << " electronVolts" << std::endl;
273
274    value = eV->convert(joules,1,&result);
275    std::cout << "1 electronVolt = " << value << " joules" << std::endl;
276
277    strValue = RpUnits::convert("10eV","J",1);
278    std::cout << "strValue convert(10eV,J,1) = " << strValue << std::endl;
279
280    strValue = RpUnits::convert("1eV","J",0);
281    std::cout << "strValue convert(1eV,J,0) = " << strValue << std::endl;
282
283    strValue = RpUnits::convert("10J","eV",1);
284    std::cout << "strValue convert(10J,eV,1) = " << strValue << std::endl;
285
286    strValue = RpUnits::convert("1J","eV",0);
287    std::cout << "strValue convert(1J,eV,0) = " << strValue << std::endl;
288
289
290
291
292
293
294    std::cout << "TESTING COPY CONSTRUCTOR" << std::endl;
295
296    RpUnits *origRpUnits = RpUnits::define("obj2", NULL);
297    RpUnits copyRpUnits = *origRpUnits;
298
299    std::cout << "origRpUnits = " << origRpUnits->getUnitsName() << std::endl;
300    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
301    std::cout << "modifying origRpUnits" << std::endl;
302    delete origRpUnits;
303    origRpUnits = RpUnits::define("obj3",NULL);
304    std::cout << "origRpUnits = " << origRpUnits->getUnitsName() << std::endl;
305    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
306    std::cout << "deleting origRpUnits" << std::endl;
307    delete origRpUnits;
308    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
309
310    std::cout << "TESTING COPY ASSIGNMENT OPERATOR" << std::endl;
311
312    RpUnits *testRpUnits = RpUnits::define("test2", NULL);
313    copyRpUnits = *testRpUnits;
314
315    std::cout << "testRpUnits = " << testRpUnits->getUnitsName() << std::endl;
316    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
317    std::cout << "modifying testRpUnits" << std::endl;
318    delete testRpUnits;
319    testRpUnits = RpUnits::define("test3",NULL);
320    std::cout << "testRpUnits = " << testRpUnits->getUnitsName() << std::endl;
321    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
322    std::cout << "deleting testRpUnits" << std::endl;
323    delete testRpUnits;
324    std::cout << "copyRpUnits = " << copyRpUnits.getUnitsName() << std::endl;
325    return 0;
326}
Note: See TracBrowser for help on using the repository browser.