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

Last change on this file since 93 was 93, checked in by dkearney, 19 years ago
  1. corrected c interface language binding function names so they are

consistant with previous c interface function names

  1. separated object dictionaries from fortran code, placed them in

the RpBindings.[h,cc] files so matlab bindings can use them too.

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