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
RevLine 
[76]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//
[40]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
[93]50int main()
[40]51{
52
[93]53    RpUnits* meters = rpDefineUnit("m",NULL);
[40]54
55    RpUnits* centimeters = NULL;
56    RpUnits* nanometers = NULL;
57    RpUnits* cm_basis = NULL;
58
[93]59    RpUnits* angstrom = rpDefineUnit("A",NULL);
60    RpUnits* fahrenheit = rpDefineUnit("F",NULL);
61    RpUnits* celcius = rpDefineUnit("C",NULL);
62    RpUnits* kelvin = rpDefineUnit("K",NULL);
[40]63
[93]64    rpDefineConv(angstrom, meters, angstrom2meter, meter2angstrom);
65    rpDefineConv(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit);
66    rpDefineConv(celcius, kelvin, centigrade2kelvin, kelvin2centigrade);
67
68
[40]69    double cm_exp = 0;
70    double nm_conv = 0;
71    double value = 0;
[93]72
[40]73    int result = 0;
74    int showUnits = 0;
75
[76]76    const char* nm_conv_str = NULL;
77    const char* retStr = NULL;
[93]78
79    rpMakeMetric(meters);
80    centimeters = rpFind("cm");
81
[40]82    if (meters) {
[93]83        printf("meters sign is :%s:\n",rpGetUnitsName(meters));
[40]84    }
85
86    if (centimeters) {
[93]87        cm_exp = rpGetExponent(centimeters);
88        cm_basis = rpGetBasis(centimeters);
[40]89
[93]90        printf("centimeters sign is :%s:\n",rpGetUnits(centimeters));
[40]91        printf("cm_exp is :%f:\n",cm_exp);
92
93        if (cm_basis) {
[93]94            printf("cm_basis sign is :%s:\n",rpGetUnitsName(cm_basis));
[40]95        }
96        else {
97            printf("cm_basis is NULL\n");
98        }
99
100    }
101
[93]102    nanometers = rpFind("nm");
[40]103
104    if (nanometers) {
105
[93]106        nm_conv = rpConvert_ObjDbl(nanometers,meters,1.0e9,&result);
[40]107        printf("1.0e9 nm = %f m\tresult = %d\n",nm_conv,result);
108
[93]109        nm_conv = rpConvert_ObjDbl(nanometers,meters,1.0e9, NULL);
[40]110        printf("1.0e9 nm = %f m\n",nm_conv);
111
112        showUnits = 1;
[93]113        nm_conv_str = rpConvert_ObjStr(nanometers,meters,1.588e9,showUnits,NULL);
[40]114        printf("1.588e9 nm = %s\n",nm_conv_str);
115
116        showUnits = 0;
[93]117        nm_conv_str = rpConvert_ObjStr(nanometers,meters,1.588e9,showUnits,NULL);
[40]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) {
[93]125        value = rpConvert_ObjDbl(angstrom,meters,1.0,&result);
[40]126        printf("1 angstrom = %e meters\n",value);
127
[93]128        value = rpConvert_ObjDbl(centimeters,angstrom,1e-8,&result);
[40]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) {
[93]137        value = rpConvert_ObjDbl(fahrenheit,celcius,72,&result);
[40]138        printf("72 degrees fahrenheit = %f degrees celcius\n",value);
[93]139
140        value = rpConvert_ObjDbl(celcius,fahrenheit,value,&result);
[40]141        printf("22.222 degrees celcius = %f degrees fahrenheit\n",value);
142    }
143    else {
144        printf("fahrenheit or celcius is NULL\n");
145    }
[93]146
[40]147    if (celcius && kelvin) {
[93]148        value = rpConvert_ObjDbl(celcius,kelvin,20,&result);
[40]149        printf("20 degrees celcius = %f kelvin\n",value);
150
[93]151        value = rpConvert_ObjDbl(kelvin,celcius,300,&result);
[40]152        printf("300 kelvin = %f degrees celcius\n", value);
153    }
154    else {
155        printf("celcius or kelvin is NULL\n");
156    }
157
[76]158    printf("====== adding all preset units ======\n");
[93]159    rpAddPresets("all");
[76]160
161    printf("====== TESTING STATIC CONVERT FXNS ======\n");
162
[93]163    retStr = rpConvert("72F","C",1,&result);
[76]164    printf("72F = %s\tresult = %d\n", retStr,result);
165
[93]166    retStr = rpConvert("300K","F",1,&result);
[76]167    printf("300K = %s\tresult = %d\n", retStr,result);
168
[93]169    retStr = rpConvert("1eV","J",0,&result);
[76]170    printf("1eV = %s (no units)\tresult = %d\n", retStr,result);
171
[93]172    retStr = rpConvertStr("300K","R",1,&result);
[76]173    printf("300K = %s\tresult = %d\n", retStr,result);
174
[93]175    retStr = rpConvertStr("5m","ft",1,&result);
[76]176    printf("5m = %s\tresult = %d\n", retStr,result);
177
[93]178    value = rpConvertDbl("5000mV","V",&result);
[76]179    printf("5V = %f (double value)\n", value);
180
[93]181
[40]182    return 0;
183}
Note: See TracBrowser for help on using the repository browser.