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

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

initial add for test directory which will hold test cases for using rappture

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