/[ascend]/trunk/ascend/compiler/test/test_func.c
ViewVC logotype

Contents of /trunk/ascend/compiler/test/test_func.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3387 - (show annotations) (download) (as text)
Sun Jun 10 08:22:02 2018 UTC (5 months ago) by jpye
File MIME type: text/x-csrc
File size: 9427 byte(s)
hyp trig fns

1 /* ASCEND modelling environment
2 Copyright (C) 2007 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *//**
17 @file
18 Unit test functions for compiler. Nothing here yet.
19 */
20 #include <string.h>
21
22 #include <ascend/general/env.h>
23 #include <ascend/general/list.h>
24 #include <ascend/general/platform.h>
25 #include <ascend/utilities/ascEnvVar.h>
26 #include <ascend/utilities/error.h>
27
28 #include <ascend/compiler/func.h>
29
30 #include <test/common.h>
31
32 static void test_test1(void){
33 gl_init_pool();
34 gl_init();
35 InitDimenList();
36
37 const struct Func *f = LookupFunc("lnxxxx");
38 CU_ASSERT(NULL == f);
39
40 #define EPS 1e-12
41
42 f = LookupFunc("ln");
43 CU_ASSERT(NULL != f);
44 CU_ASSERT(FuncEval(f,1)==0);
45 CU_ASSERT(FuncDeriv(f,1)==1);
46 CU_ASSERT(FuncEval(f,exp(1))==1);
47 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,exp(1)),1/exp(1),EPS);
48 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,exp(1)),-1/exp(1)/exp(1),EPS);
49 CU_ASSERT(FuncEval(f,exp(2))==2);
50 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,exp(2)),1/exp(2),EPS);
51 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,5),-1./25.,EPS);
52 CU_ASSERT(0==strcmp(FuncName(f),"ln"));
53 CU_ASSERT(0==strcmp(FuncCName(f),"log"));
54 CU_ASSERT(0==strcmp(FuncDeriv1CName(f),"dln"));
55 CU_ASSERT(0==strcmp(FuncDeriv2CName(f),"dln2"));
56 CU_ASSERT_FATAL(NULL!=FuncDimens(f));
57 CU_ASSERT(0==CmpDimen(FuncDimens(f),Dimensionless()));
58 CU_ASSERT(F_LN==FuncId(f));
59
60 f = LookupFunc("abs");
61 CU_ASSERT(NULL != f);
62 CU_ASSERT(FuncEval(f,1.5)==1.5);
63 CU_ASSERT(FuncEval(f,-1.5)==1.5);
64 CU_ASSERT(FuncEval(f,0)==0);
65 CU_ASSERT(FuncDeriv(f,1.5)==1);
66 CU_ASSERT(FuncDeriv(f,-1.5)==-1);
67 CU_ASSERT(FuncDeriv2(f,1.5)==0);
68 CU_ASSERT(FuncDeriv2(f,-1.5)==0);
69 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
70 CU_ASSERT(F_ABS==FuncId(f));
71
72 f = LookupFunc("sqrt");
73 CU_ASSERT(NULL != f);
74 CU_ASSERT(FuncEval(f,15*15)==15);
75 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2),1.41421356237309504880168872,EPS);
76 CU_ASSERT(FuncEval(f,1)==1);
77 CU_ASSERT(FuncEval(f,0)==0);
78 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,2),1./2./1.41421356237309504880168872,EPS);
79 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,2),-1.41421356237309504880168872/16.,EPS);
80 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
81 CU_ASSERT(F_SQRT==FuncId(f));
82
83 f = LookupFunc("sqrt");
84 CU_ASSERT(NULL != f);
85 CU_ASSERT(FuncEval(f,15*15)==15);
86 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2),1.41421356237309504880168872,EPS);
87 CU_ASSERT(FuncEval(f,1)==1);
88 CU_ASSERT(FuncEval(f,0)==0);
89 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,2),1./2./1.41421356237309504880168872,EPS);
90 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,2),-1.41421356237309504880168872/16.,EPS);
91 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
92 CU_ASSERT(F_SQRT==FuncId(f));
93
94 f = LookupFunc("log10");
95 CU_ASSERT(NULL != f);
96 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1000.),3.,EPS);
97 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1e6),6.,EPS);
98 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,100),1./100./log(10),EPS);
99 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,1/log(10)),-log(10),EPS);
100 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,1),-1/log(10),EPS);
101 CU_ASSERT(0==CmpDimen(FuncDimens(f),Dimensionless()));
102
103 f = LookupFunc("sin");
104 CU_ASSERT(NULL != f);
105 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
106 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2*M_PI),0,EPS);
107 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI/2),1,EPS);
108 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI),0,EPS);
109 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-M_PI),0,EPS);
110 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,3./2*M_PI),-1,EPS);
111 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-1./2*M_PI),-1,EPS);
112 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,0),1,EPS);
113 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,M_PI/2),0,EPS);
114 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,M_PI),-1,EPS);
115 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-M_PI/2),0,EPS);
116 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI/2),-1,EPS);
117 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-M_PI/2),+1,EPS);
118 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI),0,EPS);
119 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI/4),1/sqrt(2),EPS);
120 CU_ASSERT(0==CmpDimen(FuncDimens(f),TrigDimension()));
121
122 f = LookupFunc("tan");
123 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
124 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI/4),1,EPS);
125 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI),0,EPS);
126 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-M_PI),0,EPS);
127 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,3./4*M_PI),-1,EPS);
128 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-3./4*M_PI),1,EPS);
129 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-1./4*M_PI),-1,EPS);
130 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,M_PI/4),2,EPS);
131 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,3*M_PI/4),2,EPS);
132 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-3*M_PI/4),2,EPS);
133 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI/4),4,EPS);
134 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,3*M_PI/4.),-4,EPS);
135 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-3*M_PI/4.),4,EPS);
136 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,0),0,EPS);
137 CU_ASSERT(0==CmpDimen(FuncDimens(f),TrigDimension()));
138
139 f = LookupFunc("cos");
140 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),1,EPS);
141 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2*M_PI),1,EPS);
142 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI/4),1./sqrt(2),EPS);
143 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI/2),0,EPS);
144 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,3*M_PI/4),-1./sqrt(2),EPS);
145 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,M_PI),-1,EPS);
146 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-3*M_PI/4),-1./sqrt(2),EPS);
147 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-M_PI/2),0,EPS);
148 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-M_PI/4),1./sqrt(2),EPS);
149 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,M_PI/2),-1,EPS);
150 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-M_PI/2),+1,EPS);
151 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,M_PI),0,EPS);
152 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,0),0,EPS);
153 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,0),-1,EPS);
154 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI/4),-1/sqrt(2),EPS);
155 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI/2),0,EPS);
156 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,M_PI),1,EPS);
157 CU_ASSERT(0==CmpDimen(FuncDimens(f),TrigDimension()));
158
159 f = LookupFunc("sinh");
160 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
161 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1.),1.1752011936438,EPS);
162 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1./2),0.521095305493747,EPS);
163 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2),3.62686040784702,EPS);
164 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-2),-3.62686040784702,EPS);
165 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-2),3.76219569108363,EPS);
166 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-2),-3.62686040784702,EPS);
167
168 f = LookupFunc("cosh");
169 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),1,EPS);
170 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),1.54308063481524,EPS);
171 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1./2),1.12762596520638,EPS);
172 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2),3.76219569108363,EPS);
173 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-2),3.76219569108363,EPS);
174 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-2),-3.62686040784702,EPS);
175 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-2),3.76219569108363,EPS);
176
177 f = LookupFunc("cosh");
178 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),1,EPS);
179 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),1.54308063481524,EPS);
180 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1./2),1.12762596520638,EPS);
181 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,2),3.76219569108363,EPS);
182 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-2),3.76219569108363,EPS);
183 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-2),-3.62686040784702,EPS);
184 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-2),3.76219569108363,EPS);
185
186 f = LookupFunc("tanh");
187 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
188 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),0.761594155955765,EPS);
189 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,-1),-0.761594155955765,EPS);
190 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,-1),0.419974341614026,EPS);
191 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,-1),0.639700008449225,EPS);
192
193 f = LookupFunc("sqr");
194 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
195 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),1,EPS);
196 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,9),81,EPS);
197 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,9),18,EPS);
198 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,9),2,EPS);
199 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
200
201 f = LookupFunc("cube");
202 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
203 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),1,EPS);
204 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,4),64,EPS);
205 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,4),48,EPS);
206 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,4),24,EPS);
207 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
208
209 f = LookupFunc("cbrt");
210 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,0),0,EPS);
211 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,1),1,EPS);
212 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,64),4,EPS);
213 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,64),1./48,EPS);
214 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,64),-1./48./96.,EPS);
215 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
216
217 f = LookupFunc("hold");
218 CU_ASSERT_DOUBLE_EQUAL(FuncEval(f,37),37,EPS);
219 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv(f,37),0,EPS);
220 CU_ASSERT_DOUBLE_EQUAL(FuncDeriv2(f,37),0,EPS);
221 CU_ASSERT(0==CmpDimen(FuncDimens(f),WildDimension()));
222
223 DestroyDimenList();
224 gl_destroy_pool();
225 }
226
227 /*===========================================================================*/
228 /* Registration information */
229
230 /* the list of tests */
231
232 #define TESTS(T) \
233 T(test1)
234
235 REGISTER_TESTS_SIMPLE(compiler_func, TESTS)
236

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22