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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3355 - (show annotations) (download) (as text)
Mon Apr 16 05:56:23 2018 UTC (6 months ago) by jpye
File MIME type: text/x-csrc
File size: 6245 byte(s)
unexpected behaviour in FindOrDefineUnits eg with 'N/m)'

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.
19 */
20 #include <ascend/compiler/units.h>
21 #include <ascend/compiler/symtab.h>
22
23 #include <ascend/general/env.h>
24 #include <ascend/general/list.h>
25 #include <ascend/general/platform.h>
26 #include <ascend/utilities/ascEnvVar.h>
27 #include <ascend/utilities/error.h>
28
29 #include <test/common.h>
30
31 static unsigned long get_num_units_defined(void){
32 unsigned long c, nc = 0;
33 struct Units *p;
34 for(c=0;c<UNITS_HASH_SIZE;c++) {
35 for(p = g_units_hash_table[c];p!=NULL;p=p->next)nc++;
36 }
37 return nc;
38 }
39
40 static void test_test1(void){
41 // test setup and destruction of the global list
42
43 gl_init_pool();
44 gl_init();
45 InitDimenList();
46 InitSymbolTable();
47 InitUnitsTable();
48
49 // 10 dimensions plus wild plus dimensionless
50 CU_TEST(12==get_num_units_defined());
51 DumpUnits(stderr);
52
53 DestroyUnitsTable();
54 DestroyStringSpace();
55 DestroySymbolTable();
56 DestroyDimenList();
57 gl_destroy_pool();
58 }
59
60 static void test_test2(void){
61 // test setup and destruction of the global list
62
63 gl_init_pool();
64 gl_init();
65 InitDimenList();
66 InitSymbolTable();
67 InitUnitsTable();
68
69 // LookupUnits
70
71 const struct Units *u = LookupUnits("kg");
72 CU_TEST(NULL!=u);
73
74 u = LookupUnits("MPa");
75 CU_TEST(NULL==u);
76
77 // CreateUnitDef
78
79 struct UnitDefinition *ud;
80 ud = CreateUnitDef(AddSymbol("N"),"kg*m/s^2","somefile.a4c",15);
81 CU_TEST(NULL!=ud);
82 CU_TEST(NULL==CreateUnitDef(NULL,"kg*m/s^2","somefile.a4c",15));
83 CU_TEST(NULL==CreateUnitDef(AddSymbol("N"),NULL,"somefile.a4c",15));
84 CU_TEST(NULL==CreateUnitDef(AddSymbol("N"),"kg*m/s^2",NULL,15));
85 DestroyUnitDef(ud);
86
87 // ProcessUnitDef
88
89 ud = CreateUnitDef(AddSymbol("N"),"kg*m/s^2","somefile.a4c",16);
90 ProcessUnitDef(ud);
91 DestroyUnitDef(ud);
92
93 u = LookupUnits("N");
94 CU_TEST(NULL!=u);
95
96 // CheckNewUnits (via ProcessUnitDef)
97
98 ud = CreateUnitDef(AddSymbol("NN"),"kg*MMM/s^2","somefile.a4c",17);
99 ProcessUnitDef(ud);
100 DestroyUnitDef(ud);
101 u = LookupUnits("NN");
102 CU_TEST(NULL==u);
103
104 unsigned long nc0 = get_num_units_defined();
105 ud = CreateUnitDef(AddSymbol("m"),"s/kg","somefile.a4c",18);
106 ProcessUnitDef(ud);
107 DestroyUnitDef(ud);
108 CU_TEST(nc0==get_num_units_defined()); // nothing added
109
110 nc0 = get_num_units_defined();
111 ud = CreateUnitDef(AddSymbol("m"),"s-kg","somefile.a4c",18);
112 ProcessUnitDef(ud);
113 DestroyUnitDef(ud);
114 CU_TEST(nc0==get_num_units_defined()); // nothing added
115
116 // parser checks...
117
118 unsigned long pos = 359;
119 int errcode = 229;
120 u = FindOrDefineUnits("m", &pos, &errcode);
121 CU_TEST(0==errcode);
122 CU_TEST(NULL!=u);
123 CU_TEST(0==pos);
124
125 #define EXPECT_ERROR(USTR,ERRCODE) \
126 u = FindOrDefineUnits(USTR,&pos,&errcode);\
127 if(errcode && errcode!=ERRCODE){\
128 CONSOLE_DEBUG("Expected error code %d, got %d",ERRCODE,errcode);\
129 char **e1 = UnitsExplainError(USTR,pos,errcode);\
130 CONSOLE_DEBUG("error: %s",e1[1]);\
131 CONSOLE_DEBUG("-------%s %s",e1[2],e1[0]);\
132 }\
133 CU_TEST(ERRCODE==errcode);\
134 if(ERRCODE)CU_TEST(NULL==u) else CU_TEST(NULL!=u);
135
136 EXPECT_ERROR("NNN/mmm",1);
137
138 EXPECT_ERROR("N",0);
139 EXPECT_ERROR("N^2",0);
140 EXPECT_ERROR("N^-2",0);
141 EXPECT_ERROR("N/m",0);
142 EXPECT_ERROR("N/m/kg",0);
143 EXPECT_ERROR("N/m*kg",0);
144
145 EXPECT_ERROR("N/(m",2);
146 EXPECT_ERROR("N-m",3);
147 EXPECT_ERROR("/m",8);
148 EXPECT_ERROR("N/m*kg/",7);
149
150 EXPECT_ERROR("N^1/2",0);
151 char *s1 = UnitsStringSI(u);
152 CONSOLE_DEBUG("string = %s",s1);
153 ASC_FREE(s1);
154
155 EXPECT_ERROR("3.5*m",0);
156 EXPECT_ERROR("3.5*m*",7);
157
158 EXPECT_ERROR(".678e-2*m",0);
159
160 EXPECT_ERROR("XaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
161 XaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
162 XaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
163 XaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
164 XaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
165 YaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
166 YaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
167 YaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
168 YaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
169 YaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa\
170 ZaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaaAaaaabaaaa"
171 ,5);
172
173 EXPECT_ERROR("3.6N",6);
174
175 #if 0
176 EXPECT_ERROR("N/m)",9);
177 EXPECT_ERROR("kg^0.3",0)
178
179 u = FindOrDefineUnits("N^(3/10)", &pos, &errcode);
180 CONSOLE_DEBUG("error code = %d, pos = %lu",errcode,pos);
181 CU_TEST(0==errcode);
182 CU_TEST(NULL!=u);
183
184 u = FindOrDefineUnits("N/(m)", &pos, &errcode);
185 CONSOLE_DEBUG("error code = %d, pos = %lu",errcode,pos);
186 CU_TEST(0==errcode);
187 CU_TEST(NULL!=u);
188
189 u = FindOrDefineUnits("N/(m*K)", &pos, &errcode);
190 CONSOLE_DEBUG("error code = %d",errcode);
191 CU_TEST(0==errcode);
192 CU_TEST(NULL!=u);
193 #endif
194
195 #if 0
196 u = FindOrDefineUnits("3.5e9e0*m", &pos, &errcode);
197 CONSOLE_DEBUG("error code = %d",errcode);
198 CU_TEST(4==errcode);
199 CU_TEST(NULL==u);
200 #endif
201
202 DestroyUnitsTable();
203 DestroyStringSpace();
204 DestroySymbolTable();
205 DestroyDimenList();
206 gl_destroy_pool();
207 }
208
209 /*===========================================================================*/
210 /* Registration information */
211
212 /* the list of tests */
213
214 #define TESTS(T) \
215 T(test1) \
216 T(test2)
217
218
219 REGISTER_TESTS_SIMPLE(compiler_units, TESTS)
220

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