source: trunk/oldtest/src/RpUnitsC_test.c @ 4635

Last change on this file since 4635 was 3177, checked in by mmc, 9 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

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