source: trunk/test/src/RpUnitsC_test.c @ 115

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

Updated all copyright notices.

File size: 5.0 KB
Line 
1//----------------------------------------------------------------------
2// TEST: Cee's interface to RpUnits.
3//
4// Basic units conversion tests for the RpUnits portion of Rappture
5// written in Cee.
6//======================================================================
7// AUTHOR:  Derrick Kearney, Purdue University
8// Copyright (c) 2004-2005  Purdue Research Foundation
9//
10// See the file "license.terms" for information on usage and
11// redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12//======================================================================
13//
14#include "RpUnitsCInterface.h"
15#include <stdio.h>
16
17double fahrenheit2centigrade (double F);
18double centigrade2fahrenheit (double C);
19double centigrade2kelvin (double C);
20double kelvin2centigrade (double K);
21
22double angstrom2meter (double angstrom)
23{
24    return angstrom*1.0e-10;
25}
26
27double meter2angstrom (double meters)
28{
29    return meters*1.0e10;
30}
31
32double fahrenheit2centigrade (double F)
33{
34    return ((F-32.0)/(9.0/5.0));
35}
36
37double centigrade2fahrenheit (double C)
38{
39    return ((C*(9.0/5.0))+32.0);
40}
41
42double centigrade2kelvin (double C)
43{
44    return (C+273.15);
45}
46
47double kelvin2centigrade (double K)
48{
49    return (K-273.15);
50}
51
52int main()
53{
54
55    const RpUnits* meters = rpDefineUnit("m",NULL);
56
57    const RpUnits* centimeters = NULL;
58    const RpUnits* nanometers = NULL;
59    const RpUnits* cm_basis = NULL;
60
61    const RpUnits* angstrom = rpDefineUnit("A",NULL);
62    const RpUnits* fahrenheit = rpDefineUnit("F",NULL);
63    const RpUnits* celcius = rpDefineUnit("C",NULL);
64    const RpUnits* kelvin = rpDefineUnit("K",NULL);
65
66    rpDefineConv(angstrom, meters, angstrom2meter, meter2angstrom);
67    rpDefineConv(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit);
68    rpDefineConv(celcius, kelvin, centigrade2kelvin, kelvin2centigrade);
69
70
71    double cm_exp = 0;
72    double nm_conv = 0;
73    double value = 0;
74
75    int result = 0;
76    int showUnits = 0;
77
78    const char* nm_conv_str = NULL;
79    const char* retStr = NULL;
80
81    rpMakeMetric(meters);
82    centimeters = rpFind("cm");
83
84    if (meters) {
85        printf("meters sign is :%s:\n",rpGetUnitsName(meters));
86    }
87
88    if (centimeters) {
89        cm_exp = rpGetExponent(centimeters);
90        cm_basis = rpGetBasis(centimeters);
91
92        printf("centimeters sign is :%s:\n",rpGetUnits(centimeters));
93        printf("cm_exp is :%f:\n",cm_exp);
94
95        if (cm_basis) {
96            printf("cm_basis sign is :%s:\n",rpGetUnitsName(cm_basis));
97        }
98        else {
99            printf("cm_basis is NULL\n");
100        }
101
102    }
103
104    nanometers = rpFind("nm");
105
106    if (nanometers) {
107
108        nm_conv = rpConvert_ObjDbl(nanometers,meters,1.0e9,&result);
109        printf("1.0e9 nm = %f m\tresult = %d\n",nm_conv,result);
110
111        nm_conv = rpConvert_ObjDbl(nanometers,meters,1.0e9, NULL);
112        printf("1.0e9 nm = %f m\n",nm_conv);
113
114        showUnits = 1;
115        nm_conv_str = rpConvert_ObjStr(nanometers,meters,1.588e9,showUnits,NULL);
116        printf("1.588e9 nm = %s\n",nm_conv_str);
117
118        showUnits = 0;
119        nm_conv_str = rpConvert_ObjStr(nanometers,meters,1.588e9,showUnits,NULL);
120        printf("1.588e9 nm = %s\n",nm_conv_str);
121    }
122    else {
123        printf("nanometers is NULL\n");
124    }
125
126    if (meters && angstrom && centimeters) {
127        value = rpConvert_ObjDbl(angstrom,meters,1.0,&result);
128        printf("1 angstrom = %e meters\n",value);
129
130        value = rpConvert_ObjDbl(centimeters,angstrom,1e-8,&result);
131        printf("1.0e-8 centimeter = %f angstroms\n",value);
132    }
133    else {
134        printf("meters or angstrom or centimeters is NULL\n");
135    }
136
137
138    if (fahrenheit && celcius) {
139        value = rpConvert_ObjDbl(fahrenheit,celcius,72,&result);
140        printf("72 degrees fahrenheit = %f degrees celcius\n",value);
141
142        value = rpConvert_ObjDbl(celcius,fahrenheit,value,&result);
143        printf("22.222 degrees celcius = %f degrees fahrenheit\n",value);
144    }
145    else {
146        printf("fahrenheit or celcius is NULL\n");
147    }
148
149    if (celcius && kelvin) {
150        value = rpConvert_ObjDbl(celcius,kelvin,20,&result);
151        printf("20 degrees celcius = %f kelvin\n",value);
152
153        value = rpConvert_ObjDbl(kelvin,celcius,300,&result);
154        printf("300 kelvin = %f degrees celcius\n", value);
155    }
156    else {
157        printf("celcius or kelvin is NULL\n");
158    }
159
160    printf("====== adding all preset units ======\n");
161    rpAddPresets("all");
162
163    printf("====== TESTING STATIC CONVERT FXNS ======\n");
164
165    retStr = rpConvert("72F","C",1,&result);
166    printf("72F = %s\tresult = %d\n", retStr,result);
167
168    retStr = rpConvert("300K","F",1,&result);
169    printf("300K = %s\tresult = %d\n", retStr,result);
170
171    retStr = rpConvert("1eV","J",0,&result);
172    printf("1eV = %s (no units)\tresult = %d\n", retStr,result);
173
174    retStr = rpConvertStr("300K","R",1,&result);
175    printf("300K = %s\tresult = %d\n", retStr,result);
176
177    retStr = rpConvertStr("5m","ft",1,&result);
178    printf("5m = %s\tresult = %d\n", retStr,result);
179
180    value = rpConvertDbl("5000mV","V",&result);
181    printf("5V = %f (double value)\n", value);
182
183
184    return 0;
185}
Note: See TracBrowser for help on using the repository browser.