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

Last change on this file since 76 was 76, checked in by dkearney, 15 years ago
  1. cleaned up make files some more, deleted make.inc files
  2. updated RpUnits module and Cee and Fortran interfaces
  3. changed return values for some fortran interfaces,

error messages not working yet

  1. still need to add copy assignment code to RpUnits
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 = defineUnit("m",NULL);
54
55    RpUnits* centimeters = NULL;
56    RpUnits* nanometers = NULL;
57    RpUnits* cm_basis = NULL;
58
59    RpUnits* angstrom = defineUnit("A",NULL);
60    RpUnits* fahrenheit = defineUnit("F",NULL);
61    RpUnits* celcius = defineUnit("C",NULL);
62    RpUnits* kelvin = defineUnit("K",NULL);
63
64    defineConv(angstrom, meters, angstrom2meter, meter2angstrom);
65    defineConv(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit);
66    defineConv(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    makeMetric(meters);
80    centimeters = find("cm");
81   
82    if (meters) {
83        printf("meters sign is :%s:\n",getUnitsName(meters));
84    }
85
86    if (centimeters) {
87        cm_exp = getExponent(centimeters);
88        cm_basis = getBasis(centimeters);
89
90        printf("centimeters sign is :%s:\n",getUnits(centimeters));
91        printf("cm_exp is :%f:\n",cm_exp);
92
93        if (cm_basis) {
94            printf("cm_basis sign is :%s:\n",getUnitsName(cm_basis));
95        }
96        else {
97            printf("cm_basis is NULL\n");
98        }
99
100    }
101
102    nanometers = find("nm");
103
104    if (nanometers) {
105
106        nm_conv = convert_obj_double_result(nanometers,meters,1.0e9,&result);
107        printf("1.0e9 nm = %f m\tresult = %d\n",nm_conv,result);
108
109        nm_conv = convert_obj_double(nanometers,meters,1.0e9);
110        printf("1.0e9 nm = %f m\n",nm_conv);
111
112        showUnits = 1;
113        nm_conv_str = convert_obj_str(nanometers,meters,1.588e9,showUnits);
114        printf("1.588e9 nm = %s\n",nm_conv_str);
115
116        showUnits = 0;
117        nm_conv_str = convert_obj_str(nanometers,meters,1.588e9,showUnits);
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 = convert_obj_double_result(angstrom,meters,1.0,&result);
126        printf("1 angstrom = %e meters\n",value);
127
128        value = convert_obj_double_result(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 = convert_obj_double_result(fahrenheit,celcius,72,&result);
138        printf("72 degrees fahrenheit = %f degrees celcius\n",value);
139   
140        value = convert_obj_double_result(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 = convert_obj_double_result(celcius,kelvin,20,&result);
149        printf("20 degrees celcius = %f kelvin\n",value);
150
151        value = convert_obj_double_result(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    add_presets("all");
160
161    printf("====== TESTING STATIC CONVERT FXNS ======\n");
162
163    retStr = convert("72F","C",1,&result);
164    printf("72F = %s\tresult = %d\n", retStr,result);
165
166    retStr = convert("300K","F",1,&result);
167    printf("300K = %s\tresult = %d\n", retStr,result);
168
169    retStr = convert("1eV","J",0,&result);
170    printf("1eV = %s (no units)\tresult = %d\n", retStr,result);
171
172    retStr = convert_str("300K","R",1,&result);
173    printf("300K = %s\tresult = %d\n", retStr,result);
174
175    retStr = convert_str("5m","ft",1,&result);
176    printf("5m = %s\tresult = %d\n", retStr,result);
177
178    value = convert_dbl("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.