1 /**
2 * \file TestFormulaParser.c
3 * \brief FormulaParser unit tests
4 * \author Ben Bornstein
5 *
6 * <!--------------------------------------------------------------------------
7 * This file is part of libSBML. Please visit http://sbml.org for more
8 * information about SBML, and the latest version of libSBML.
9 *
10 * Copyright (C) 2020 jointly by the following organizations:
11 * 1. California Institute of Technology, Pasadena, CA, USA
12 * 2. University of Heidelberg, Heidelberg, Germany
13 * 3. University College London, London, UK
14 *
15 * Copyright (C) 2019 jointly by the following organizations:
16 * 1. California Institute of Technology, Pasadena, CA, USA
17 * 2. University of Heidelberg, Heidelberg, Germany
18 *
19 * Copyright (C) 2013-2018 jointly by the following organizations:
20 * 1. California Institute of Technology, Pasadena, CA, USA
21 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22 * 3. University of Heidelberg, Heidelberg, Germany
23 *
24 * Copyright (C) 2009-2013 jointly by the following organizations:
25 * 1. California Institute of Technology, Pasadena, CA, USA
26 * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27 *
28 * Copyright (C) 2006-2008 by the California Institute of Technology,
29 * Pasadena, CA, USA
30 *
31 * Copyright (C) 2002-2005 jointly by the following organizations:
32 * 1. California Institute of Technology, Pasadena, CA, USA
33 * 2. Japan Science and Technology Agency, Japan
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the GNU Lesser General Public License as published by
37 * the Free Software Foundation. A copy of the license agreement is provided
38 * in the file named "LICENSE.txt" included with this software distribution
39 * and also available online as http://sbml.org/software/libsbml/license.html
40 * ---------------------------------------------------------------------- -->*/
41
42 #include <sbml/common/common.h>
43 #include <sbml/math/FormulaParser.h>
44
45 #include <check.h>
46
47 #if defined(__cplusplus)
48 LIBSBML_CPP_NAMESPACE_USE
49 CK_CPPSTART
50 #endif
51
52
53 /**
54 * The following are private and used only within FormulaParser.c; however,
55 * I don't know how else to make them "public" only for testing than to put
56 * them here
57 */
58 #define START_STATE 0
59 #define ACCEPT_STATE 0
60 #define ERROR_STATE 27
61 #define NUM_STATES 27
62
63
64 /**
65 * The Action[] table contains 144 entries. To test them all would be
66 * laborious and silly. Instead, for a few token type, test the first,
67 * last and middle entry in each token "run". Also, test some error
68 * states.
69 */
START_TEST(test_FormulaParser_getAction)70 START_TEST (test_FormulaParser_getAction)
71 {
72 int i;
73 Token_t *t = Token_create();
74
75
76 t->type = TT_NAME;
77 fail_unless( FormulaParser_getAction( 0, t) == 6, NULL );
78 fail_unless( FormulaParser_getAction(10, t) == 6, NULL );
79 fail_unless( FormulaParser_getAction(25, t) == 6, NULL );
80 fail_unless( FormulaParser_getAction( 1, t) == ERROR_STATE, NULL );
81
82 t->type = TT_INTEGER;
83 fail_unless( FormulaParser_getAction( 0, t) == 1, NULL );
84 fail_unless( FormulaParser_getAction(10, t) == 1, NULL );
85 fail_unless( FormulaParser_getAction(25, t) == 1, NULL );
86 fail_unless( FormulaParser_getAction( 1, t) == ERROR_STATE, NULL );
87
88 t->type = TT_PLUS;
89 fail_unless( FormulaParser_getAction( 1, t) == -9, NULL );
90 fail_unless( FormulaParser_getAction(16, t) == -2, NULL );
91 fail_unless( FormulaParser_getAction(24, t) == -11, NULL );
92 fail_unless( FormulaParser_getAction( 2, t) == ERROR_STATE, NULL );
93
94 t->type = TT_MINUS;
95 fail_unless( FormulaParser_getAction( 0, t) == 5, NULL );
96 fail_unless( FormulaParser_getAction(16, t) == -2, NULL );
97 fail_unless( FormulaParser_getAction(25, t) == 5, NULL );
98 fail_unless( FormulaParser_getAction( 2, t) == ERROR_STATE, NULL );
99
100 t->type = TT_END;
101 fail_unless( FormulaParser_getAction( 1, t) == -9, NULL );
102 fail_unless( FormulaParser_getAction(17, t) == -5, NULL );
103 fail_unless( FormulaParser_getAction(24, t) == -11, NULL );
104 fail_unless( FormulaParser_getAction( 3, t) == ERROR_STATE, NULL );
105
106
107 /**
108 * TT_UNKNOWN should always yield an error state.
109 */
110 t->type = TT_UNKNOWN;
111 for (i = 0; i < NUM_STATES; i++)
112 {
113 fail_unless( FormulaParser_getAction(i, t) == ERROR_STATE, NULL );
114 }
115
116 Token_free(t);
117 }
118 END_TEST
119
120
START_TEST(test_FormulaParser_getGoto)121 START_TEST (test_FormulaParser_getGoto)
122 {
123 int r;
124
125
126 fail_unless( FormulaParser_getGoto( 0, 1) == 2, NULL );
127 fail_unless( FormulaParser_getGoto(14, 12) == 21, NULL );
128 fail_unless( FormulaParser_getGoto(14, 13) == 21, NULL );
129 fail_unless( FormulaParser_getGoto(14, 14) == 22, NULL );
130 fail_unless( FormulaParser_getGoto(14, 15) == 22, NULL );
131
132 for (r = 2; r < 12; r++)
133 {
134 fail_unless( FormulaParser_getGoto( 0, r) == 4, NULL );
135 fail_unless( FormulaParser_getGoto( 3, r) == 7, NULL );
136 fail_unless( FormulaParser_getGoto( 5, r) == 13, NULL );
137 fail_unless( FormulaParser_getGoto( 8, r) == 16, NULL );
138 fail_unless( FormulaParser_getGoto( 9, r) == 17, NULL );
139 fail_unless( FormulaParser_getGoto(10, r) == 18, NULL );
140 fail_unless( FormulaParser_getGoto(11, r) == 19, NULL );
141 fail_unless( FormulaParser_getGoto(12, r) == 20, NULL );
142 fail_unless( FormulaParser_getGoto(14, r) == 23, NULL );
143 fail_unless( FormulaParser_getGoto(25, r) == 26, NULL );
144 }
145 }
146 END_TEST
147
148
START_TEST(test_SBML_parseFormula_1)149 START_TEST (test_SBML_parseFormula_1)
150 {
151 ASTNode_t *r = SBML_parseFormula("1");
152
153
154 fail_unless( ASTNode_getType (r) == AST_INTEGER, NULL );
155 fail_unless( ASTNode_getInteger (r) == 1, NULL );
156 fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
157
158 ASTNode_free(r);
159 }
160 END_TEST
161
162
START_TEST(test_SBML_parseFormula_2)163 START_TEST (test_SBML_parseFormula_2)
164 {
165 ASTNode_t *r = SBML_parseFormula("2.1");
166
167
168 fail_unless( ASTNode_getType (r) == AST_REAL, NULL );
169 fail_unless( ASTNode_getReal (r) == 2.1, NULL );
170 fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
171
172 ASTNode_free(r);
173 }
174 END_TEST
175
176
START_TEST(test_SBML_parseFormula_3)177 START_TEST (test_SBML_parseFormula_3)
178 {
179 ASTNode_t *r = SBML_parseFormula("2.1e5");
180
181
182 fail_unless( ASTNode_getType (r) == AST_REAL_E, NULL );
183 fail_unless( ASTNode_getMantissa (r) == 2.1, NULL );
184 fail_unless( ASTNode_getExponent (r) == 5, NULL );
185 fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
186
187 ASTNode_free(r);
188 }
189 END_TEST
190
191
START_TEST(test_SBML_parseFormula_4)192 START_TEST (test_SBML_parseFormula_4)
193 {
194 ASTNode_t *r = SBML_parseFormula("foo");
195
196
197 fail_unless( ASTNode_getType(r) == AST_NAME , NULL );
198 fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL );
199 fail_unless( ASTNode_getNumChildren(r) == 0 , NULL );
200
201 ASTNode_free(r);
202 }
203 END_TEST
204
205
START_TEST(test_SBML_parseFormula_5)206 START_TEST (test_SBML_parseFormula_5)
207 {
208 ASTNode_t *r = SBML_parseFormula("1 + foo");
209 ASTNode_t *c;
210
211
212
213 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
214 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
215 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
216
217 c = ASTNode_getLeftChild(r);
218
219 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
220 fail_unless( ASTNode_getInteger (c) == 1, NULL );
221 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
222
223 c = ASTNode_getRightChild(r);
224
225 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
226 fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL );
227 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
228
229 ASTNode_free(r);
230 }
231 END_TEST
232
233
START_TEST(test_SBML_parseFormula_6)234 START_TEST (test_SBML_parseFormula_6)
235 {
236 ASTNode_t *r = SBML_parseFormula("1 + 2");
237 ASTNode_t *c;
238
239
240
241 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
242 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
243 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
244
245 c = ASTNode_getLeftChild(r);
246
247 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
248 fail_unless( ASTNode_getInteger (c) == 1, NULL );
249 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
250
251 c = ASTNode_getRightChild(r);
252
253 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
254 fail_unless( ASTNode_getInteger (c) == 2, NULL );
255 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
256
257 ASTNode_free(r);
258 }
259 END_TEST
260
261
START_TEST(test_SBML_parseFormula_7)262 START_TEST (test_SBML_parseFormula_7)
263 {
264 ASTNode_t *r = SBML_parseFormula("1 + 2 * 3");
265 ASTNode_t *c;
266
267
268
269 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
270 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
271 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
272
273 c = ASTNode_getLeftChild(r);
274
275 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
276 fail_unless( ASTNode_getInteger (c) == 1, NULL );
277 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
278
279 c = ASTNode_getRightChild(r);
280
281 fail_unless( ASTNode_getType (c) == AST_TIMES, NULL );
282 fail_unless( ASTNode_getCharacter (c) == '*', NULL );
283 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
284
285 c = ASTNode_getLeftChild(c);
286
287 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
288 fail_unless( ASTNode_getInteger (c) == 2, NULL );
289 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
290
291 c = ASTNode_getRightChild( ASTNode_getRightChild(r) );
292
293 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
294 fail_unless( ASTNode_getInteger (c) == 3, NULL );
295 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
296
297 ASTNode_free(r);
298 }
299 END_TEST
300
301
START_TEST(test_SBML_parseFormula_8)302 START_TEST (test_SBML_parseFormula_8)
303 {
304 ASTNode_t *r = SBML_parseFormula("(1 - 2) * 3");
305 ASTNode_t *c;
306
307
308 fail_unless( ASTNode_getType (r) == AST_TIMES, NULL );
309 fail_unless( ASTNode_getCharacter (r) == '*', NULL );
310 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
311
312 c = ASTNode_getLeftChild(r);
313
314 fail_unless( ASTNode_getType (c) == AST_MINUS, NULL );
315 fail_unless( ASTNode_getCharacter (c) == '-', NULL );
316 fail_unless( ASTNode_getNumChildren(c) == 2, NULL );
317
318 c = ASTNode_getRightChild(r);
319
320 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
321 fail_unless( ASTNode_getInteger (c) == 3, NULL );
322 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
323
324 c = ASTNode_getLeftChild( ASTNode_getLeftChild(r) );
325
326 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
327 fail_unless( ASTNode_getInteger (c) == 1, NULL );
328 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
329
330 c = ASTNode_getRightChild( ASTNode_getLeftChild(r) );
331
332 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
333 fail_unless( ASTNode_getInteger (c) == 2, NULL );
334 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
335
336 ASTNode_free(r);
337 }
338 END_TEST
339
340
START_TEST(test_SBML_parseFormula_9)341 START_TEST (test_SBML_parseFormula_9)
342 {
343 ASTNode_t *r = SBML_parseFormula("1 + -2 / 3");
344 ASTNode_t *c;
345
346
347 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
348 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
349 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
350
351 c = ASTNode_getLeftChild(r);
352
353 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
354 fail_unless( ASTNode_getInteger (c) == 1, NULL );
355 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
356
357 c = ASTNode_getRightChild(r);
358
359 fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL );
360 fail_unless( ASTNode_getCharacter (c) == '/', NULL );
361 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
362
363 c = ASTNode_getLeftChild(c);
364
365 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
366 fail_unless( ASTNode_getInteger (c) == -2, NULL );
367 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
368
369 c = ASTNode_getRightChild( ASTNode_getRightChild(r) );
370
371 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
372 fail_unless( ASTNode_getInteger (c) == 3, NULL );
373 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
374
375 ASTNode_free(r);
376 }
377 END_TEST
378
379
START_TEST(test_SBML_parseFormula_10)380 START_TEST (test_SBML_parseFormula_10)
381 {
382 ASTNode_t *r = SBML_parseFormula("1 + -2e100 / 3");
383 ASTNode_t *c;
384
385
386 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
387 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
388 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
389
390 c = ASTNode_getLeftChild(r);
391
392 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
393 fail_unless( ASTNode_getInteger (c) == 1, NULL );
394 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
395
396 c = ASTNode_getRightChild(r);
397
398 fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL );
399 fail_unless( ASTNode_getCharacter (c) == '/', NULL );
400 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
401
402 c = ASTNode_getLeftChild(c);
403
404 fail_unless( ASTNode_getType (c) == AST_REAL_E, NULL );
405 fail_unless( ASTNode_getMantissa (c) == -2, NULL );
406 fail_unless( ASTNode_getExponent (c) == 100, NULL );
407 fail_unless( util_isEqual(ASTNode_getReal (c), -2e+100), NULL );
408 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
409
410 c = ASTNode_getRightChild( ASTNode_getRightChild(r) );
411
412 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
413 fail_unless( ASTNode_getInteger (c) == 3, NULL );
414 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
415
416 ASTNode_free(r);
417 }
418 END_TEST
419
420
START_TEST(test_SBML_parseFormula_11)421 START_TEST (test_SBML_parseFormula_11)
422 {
423 ASTNode_t *r = SBML_parseFormula("1 - -foo / 3");
424 ASTNode_t *c;
425
426
427
428 fail_unless( ASTNode_getType (r) == AST_MINUS, NULL );
429 fail_unless( ASTNode_getCharacter (r) == '-', NULL );
430 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
431
432 c = ASTNode_getLeftChild(r);
433
434 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
435 fail_unless( ASTNode_getInteger (c) == 1, NULL );
436 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
437
438 c = ASTNode_getRightChild(r);
439
440 fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL );
441 fail_unless( ASTNode_getCharacter (c) == '/', NULL );
442 fail_unless( ASTNode_getNumChildren(c) == 2, NULL );
443
444 c = ASTNode_getLeftChild( ASTNode_getRightChild(r) );
445
446 fail_unless( ASTNode_getType (c) == AST_MINUS, NULL );
447 fail_unless( ASTNode_getCharacter (c) == '-', NULL );
448 fail_unless( ASTNode_getNumChildren(c) == 1 , NULL );
449
450 c = ASTNode_getLeftChild( ASTNode_getLeftChild( ASTNode_getRightChild(r) ) );
451
452 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
453 fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL );
454 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
455
456 c = ASTNode_getRightChild( ASTNode_getRightChild(r) );
457
458 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
459 fail_unless( ASTNode_getInteger (c) == 3, NULL );
460 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
461
462 ASTNode_free(r);
463 }
464 END_TEST
465
466
START_TEST(test_SBML_parseFormula_12)467 START_TEST (test_SBML_parseFormula_12)
468 {
469 ASTNode_t *r = SBML_parseFormula("2 * foo^bar + 3.0");
470 ASTNode_t *c;
471
472
473 fail_unless( ASTNode_getType (r) == AST_PLUS, NULL );
474 fail_unless( ASTNode_getCharacter (r) == '+', NULL );
475 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
476
477 c = ASTNode_getLeftChild(r);
478
479 fail_unless( ASTNode_getType (c) == AST_TIMES, NULL );
480 fail_unless( ASTNode_getCharacter (c) == '*', NULL );
481 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
482
483 c = ASTNode_getRightChild(r);
484
485 fail_unless( ASTNode_getType (c) == AST_REAL, NULL );
486 fail_unless( ASTNode_getReal (c) == 3.0, NULL );
487 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
488
489 c = ASTNode_getLeftChild( ASTNode_getLeftChild(r) );
490
491 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
492 fail_unless( ASTNode_getInteger (c) == 2, NULL );
493 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
494
495 c = ASTNode_getRightChild( ASTNode_getLeftChild(r) );
496
497 fail_unless( ASTNode_getType (c) == AST_POWER, NULL );
498 fail_unless( ASTNode_getCharacter (c) == '^', NULL );
499 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
500
501 c = ASTNode_getLeftChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) );
502
503 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
504 fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL );
505 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
506
507 c = ASTNode_getRightChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) );
508
509 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
510 fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL );
511 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
512
513 ASTNode_free(r);
514 }
515 END_TEST
516
517
START_TEST(test_SBML_parseFormula_13)518 START_TEST (test_SBML_parseFormula_13)
519 {
520 ASTNode_t *r = SBML_parseFormula("foo()");
521
522
523 fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL );
524 fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL );
525 fail_unless( ASTNode_getNumChildren(r) == 0 , NULL );
526
527 ASTNode_free(r);
528 }
529 END_TEST
530
531
START_TEST(test_SBML_parseFormula_14)532 START_TEST (test_SBML_parseFormula_14)
533 {
534 ASTNode_t *r = SBML_parseFormula("foo(1)");
535 ASTNode_t *c;
536
537
538 fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL );
539 fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL );
540 fail_unless( ASTNode_getNumChildren(r) == 1 , NULL );
541
542 c = ASTNode_getLeftChild(r);
543
544 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
545 fail_unless( ASTNode_getInteger (c) == 1, NULL );
546 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
547
548 ASTNode_free(r);
549 }
550 END_TEST
551
552
START_TEST(test_SBML_parseFormula_15)553 START_TEST (test_SBML_parseFormula_15)
554 {
555 ASTNode_t *r = SBML_parseFormula("foo(1, bar)");
556 ASTNode_t *c;
557
558
559 fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL );
560 fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL );
561 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
562
563 c = ASTNode_getLeftChild(r);
564
565 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
566 fail_unless( ASTNode_getInteger (c) == 1, NULL );
567 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
568
569 c = ASTNode_getRightChild(r);
570
571 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
572 fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL );
573 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
574
575 ASTNode_free(r);
576 }
577 END_TEST
578
579
START_TEST(test_SBML_parseFormula_16)580 START_TEST (test_SBML_parseFormula_16)
581 {
582 ASTNode_t *r = SBML_parseFormula("foo(1, bar, 2^-3)");
583 ASTNode_t *c;
584
585
586 fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL );
587 fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL );
588 fail_unless( ASTNode_getNumChildren(r) == 3 , NULL );
589
590 c = ASTNode_getChild(r, 0);
591
592 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
593 fail_unless( ASTNode_getInteger (c) == 1, NULL );
594 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
595
596 c = ASTNode_getChild(r, 1);
597
598 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
599 fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL );
600 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
601
602 c = ASTNode_getChild(r, 2);
603
604 fail_unless( ASTNode_getType (c) == AST_POWER, NULL );
605 fail_unless( ASTNode_getCharacter (c) == '^', NULL );
606 fail_unless( ASTNode_getNumChildren(c) == 2 , NULL );
607
608 c = ASTNode_getLeftChild( ASTNode_getChild(r, 2) );
609
610 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
611 fail_unless( ASTNode_getInteger (c) == 2, NULL );
612 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
613
614 c = ASTNode_getRightChild( ASTNode_getChild(r, 2) );
615
616 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
617 fail_unless( ASTNode_getInteger (c) == -3, NULL );
618 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
619
620 ASTNode_free(r);
621 }
622 END_TEST
623
624
START_TEST(test_SBML_parseFormula_17)625 START_TEST (test_SBML_parseFormula_17)
626 {
627 ASTNode_t *r = SBML_parseFormula("1//1");
628
629
630 /* Pathetic error handling */
631 fail_unless(r == NULL, NULL);
632
633 ASTNode_free(r);
634 }
635 END_TEST
636
637
START_TEST(test_SBML_parseFormula_18)638 START_TEST (test_SBML_parseFormula_18)
639 {
640 ASTNode_t *r = SBML_parseFormula("1+2*3 4");
641
642
643 /* Pathetic error handling */
644 fail_unless(r == NULL, NULL);
645
646 ASTNode_free(r);
647 }
648 END_TEST
649
650
START_TEST(test_SBML_parseFormula_19)651 START_TEST (test_SBML_parseFormula_19)
652 {
653 ASTNode_t *r = SBML_parseFormula("2.1e5-");
654
655 fail_unless (r == NULL);
656
657
658 //fail_unless( ASTNode_getType (r) == AST_REAL_E, NULL );
659 //fail_unless( ASTNode_getMantissa (r) == 2.1, NULL );
660 //fail_unless( ASTNode_getExponent (r) == 5, NULL );
661 //fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
662
663 //ASTNode_free(r);
664 }
665 END_TEST
666
667
START_TEST(test_SBML_parseFormula_negInf)668 START_TEST (test_SBML_parseFormula_negInf)
669 {
670 ASTNode_t *r = SBML_parseFormula("-inf");
671
672
673 fail_unless( ASTNode_getType(r) == AST_REAL, NULL );
674 fail_unless( util_isInf(ASTNode_getReal(r)) == -1, NULL );
675 fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
676
677 ASTNode_free(r);
678 }
679 END_TEST
680
681
START_TEST(test_SBML_parseFormula_negZero)682 START_TEST (test_SBML_parseFormula_negZero)
683 {
684 ASTNode_t *r = SBML_parseFormula("-0.0");
685
686
687 fail_unless( ASTNode_getType(r) == AST_REAL, NULL );
688 fail_unless( util_isNegZero(ASTNode_getReal(r)) == 1, NULL );
689 fail_unless( ASTNode_getNumChildren(r) == 0, NULL );
690
691 ASTNode_free(r);
692 }
693 END_TEST
694
START_TEST(test_FormulaParser_accessWithNULL)695 START_TEST (test_FormulaParser_accessWithNULL)
696 {
697 fail_unless ( FormulaParser_getAction(0, NULL) == ERROR_STATE);
698 fail_unless ( SBML_parseFormula(NULL) == NULL);
699 }
700 END_TEST
701
START_TEST(test_SBML_parse_sqrt)702 START_TEST (test_SBML_parse_sqrt)
703 {
704 ASTNode_t *r = SBML_parseFormula("sqrt(1, bar)");
705 ASTNode_t *c;
706
707
708 fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL );
709 fail_unless( !strcmp(ASTNode_getName(r), "sqrt") , NULL );
710 fail_unless( ASTNode_getNumChildren(r) == 2 , NULL );
711
712 c = ASTNode_getLeftChild(r);
713
714 fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL );
715 fail_unless( ASTNode_getInteger (c) == 1, NULL );
716 fail_unless( ASTNode_getNumChildren(c) == 0, NULL );
717
718 c = ASTNode_getRightChild(r);
719
720 fail_unless( ASTNode_getType(c) == AST_NAME , NULL );
721 fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL );
722 fail_unless( ASTNode_getNumChildren(c) == 0 , NULL );
723
724 ASTNode_free(r);
725 }
726 END_TEST
727
728
START_TEST(test_SBML_parseFormula_named_lambda_arguments1)729 START_TEST(test_SBML_parseFormula_named_lambda_arguments1)
730 {
731 ASTNode_t *r = SBML_parseFormula("lambda(time, time+2)");
732 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
733 fail_unless(ASTNode_getNumChildren(r) == 2, NULL);
734 ASTNode_t *c = ASTNode_getChild(r, 0);
735 fail_unless(ASTNode_getType(c) == AST_NAME);
736 fail_unless(!strcmp(ASTNode_getName(c), "time"));
737
738 c = ASTNode_getChild(r, 1);
739 fail_unless(ASTNode_getType(c) == AST_PLUS);
740 ASTNode_t *c2 = ASTNode_getChild(c, 0);
741 fail_unless(ASTNode_getType(c2) == AST_NAME);
742 fail_unless(!strcmp(ASTNode_getName(c2), "time"));
743 c2 = ASTNode_getChild(c, 1);
744 fail_unless(ASTNode_getType(c2) == AST_INTEGER);
745 fail_unless(ASTNode_getValue(c2) == 2);
746
747 ASTNode_free(r);
748
749 }
750 END_TEST
751
752
START_TEST(test_SBML_parseFormula_named_lambda_arguments2)753 START_TEST(test_SBML_parseFormula_named_lambda_arguments2)
754 {
755 ASTNode_t *r = SBML_parseFormula("lambda(time, avogadro, time+avogadro)");
756 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
757 fail_unless(ASTNode_getNumChildren(r) == 3, NULL);
758
759 ASTNode_t *c = ASTNode_getChild(r, 0);
760 fail_unless(ASTNode_getType(c) == AST_NAME);
761 fail_unless(!strcmp(ASTNode_getName(c), "time"));
762
763 c = ASTNode_getChild(r, 1);
764 fail_unless(ASTNode_getType(c) == AST_NAME);
765 fail_unless(!strcmp(ASTNode_getName(c), "avogadro"));
766
767 c = ASTNode_getChild(r, 2);
768 fail_unless(ASTNode_getType(c) == AST_PLUS);
769 ASTNode_t *c2 = ASTNode_getChild(c, 0);
770 fail_unless(ASTNode_getType(c2) == AST_NAME);
771 fail_unless(!strcmp(ASTNode_getName(c2), "time"));
772 c2 = ASTNode_getChild(c, 1);
773 fail_unless(ASTNode_getType(c2) == AST_NAME);
774 fail_unless(!strcmp(ASTNode_getName(c2), "avogadro"));
775
776 ASTNode_free(r);
777
778 }
779 END_TEST
780
781
START_TEST(test_SBML_parseFormula_named_lambda_arguments3)782 START_TEST(test_SBML_parseFormula_named_lambda_arguments3)
783 {
784 ASTNode_t *r = SBML_parseFormula("lambda(true, false, true+false)");
785 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
786 fail_unless(ASTNode_getNumChildren(r) == 3, NULL);
787
788 ASTNode_t *c = ASTNode_getChild(r, 0);
789 fail_unless(ASTNode_getType(c) == AST_NAME);
790 fail_unless(!strcmp(ASTNode_getName(c), "true"));
791
792 c = ASTNode_getChild(r, 1);
793 fail_unless(ASTNode_getType(c) == AST_NAME);
794 fail_unless(!strcmp(ASTNode_getName(c), "false"));
795
796 c = ASTNode_getChild(r, 2);
797 fail_unless(ASTNode_getType(c) == AST_PLUS);
798 ASTNode_t *c2 = ASTNode_getChild(c, 0);
799 fail_unless(ASTNode_getType(c2) == AST_NAME);
800 fail_unless(!strcmp(ASTNode_getName(c2), "true"));
801 c2 = ASTNode_getChild(c, 1);
802 fail_unless(ASTNode_getType(c2) == AST_NAME);
803 fail_unless(!strcmp(ASTNode_getName(c2), "false"));
804
805 ASTNode_free(r);
806
807 }
808 END_TEST
809
810
START_TEST(test_SBML_parseFormula_named_lambda_arguments4)811 START_TEST(test_SBML_parseFormula_named_lambda_arguments4)
812 {
813 ASTNode_t *r = SBML_parseFormula("lambda(pi, exponentiale, pi+exponentiale)");
814 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
815 fail_unless(ASTNode_getNumChildren(r) == 3, NULL);
816
817 ASTNode_t *c = ASTNode_getChild(r, 0);
818 fail_unless(ASTNode_getType(c) == AST_NAME);
819 fail_unless(!strcmp(ASTNode_getName(c), "pi"));
820
821 c = ASTNode_getChild(r, 1);
822 fail_unless(ASTNode_getType(c) == AST_NAME);
823 fail_unless(!strcmp(ASTNode_getName(c), "exponentiale"));
824
825 c = ASTNode_getChild(r, 2);
826 fail_unless(ASTNode_getType(c) == AST_PLUS);
827 ASTNode_t *c2 = ASTNode_getChild(c, 0);
828 fail_unless(ASTNode_getType(c2) == AST_NAME);
829 fail_unless(!strcmp(ASTNode_getName(c2), "pi"));
830 c2 = ASTNode_getChild(c, 1);
831 fail_unless(ASTNode_getType(c2) == AST_NAME);
832 fail_unless(!strcmp(ASTNode_getName(c2), "exponentiale"));
833
834 ASTNode_free(r);
835
836 }
837 END_TEST
838
839
START_TEST(test_SBML_parseFormula_named_lambda_arguments5)840 START_TEST(test_SBML_parseFormula_named_lambda_arguments5)
841 {
842 ASTNode_t *r = SBML_parseFormula("lambda(time, avogadro, true, false, pi, exponentiale, time+avogadro+true+false+pi+exponentiale)");
843 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
844 fail_unless(ASTNode_getNumChildren(r) == 7, NULL);
845
846 ASTNode_t *c = ASTNode_getChild(r, 0);
847 fail_unless(ASTNode_getType(c) == AST_NAME);
848 fail_unless(!strcmp(ASTNode_getName(c), "time"));
849
850 c = ASTNode_getChild(r, 1);
851 fail_unless(ASTNode_getType(c) == AST_NAME);
852 fail_unless(!strcmp(ASTNode_getName(c), "avogadro"));
853
854 c = ASTNode_getChild(r, 2);
855 fail_unless(ASTNode_getType(c) == AST_NAME);
856 fail_unless(!strcmp(ASTNode_getName(c), "true"));
857
858 c = ASTNode_getChild(r, 3);
859 fail_unless(ASTNode_getType(c) == AST_NAME);
860 fail_unless(!strcmp(ASTNode_getName(c), "false"));
861
862 c = ASTNode_getChild(r, 4);
863 fail_unless(ASTNode_getType(c) == AST_NAME);
864 fail_unless(!strcmp(ASTNode_getName(c), "pi"));
865
866 c = ASTNode_getChild(r, 5);
867 fail_unless(ASTNode_getType(c) == AST_NAME);
868 fail_unless(!strcmp(ASTNode_getName(c), "exponentiale"));
869
870 c = ASTNode_getChild(r, 6);
871 fail_unless(ASTNode_getType(c) == AST_PLUS);
872 // parseFormula will reduce plus to binary
873 fail_unless(ASTNode_getNumChildren(c) == 2, NULL);
874 ASTNode_t *c2 = ASTNode_getChild(c, 0);
875 fail_unless(ASTNode_getNumChildren(c2) == 2, NULL);
876 fail_unless(ASTNode_getType(c2) == AST_PLUS);
877 ASTNode_t *c3 = ASTNode_getChild(c, 1);
878 fail_unless(ASTNode_getType(c3) == AST_NAME);
879 fail_unless(!strcmp(ASTNode_getName(c3), "exponentiale"));
880 c3 = ASTNode_getChild(c2, 0);
881 fail_unless(ASTNode_getNumChildren(c3) == 2, NULL);
882 fail_unless(ASTNode_getType(c3) == AST_PLUS);
883 ASTNode_t *c4 = ASTNode_getChild(c2, 1);
884 fail_unless(ASTNode_getType(c4) == AST_NAME);
885 fail_unless(!strcmp(ASTNode_getName(c4), "pi"));
886 c4 = ASTNode_getChild(c3, 0);
887 fail_unless(ASTNode_getNumChildren(c4) == 2, NULL);
888 fail_unless(ASTNode_getType(c4) == AST_PLUS);
889 ASTNode_t *c5 = ASTNode_getChild(c3, 1);
890 fail_unless(ASTNode_getType(c5) == AST_NAME);
891 fail_unless(!strcmp(ASTNode_getName(c5), "false"));
892 c5 = ASTNode_getChild(c4, 0);
893 fail_unless(ASTNode_getNumChildren(c5) == 2, NULL);
894 fail_unless(ASTNode_getType(c5) == AST_PLUS);
895 ASTNode_t *c6 = ASTNode_getChild(c4, 1);
896 fail_unless(ASTNode_getType(c6) == AST_NAME);
897 fail_unless(!strcmp(ASTNode_getName(c6), "true"));
898 c6 = ASTNode_getChild(c5, 0);
899 fail_unless(ASTNode_getType(c6) == AST_NAME);
900 fail_unless(!strcmp(ASTNode_getName(c6), "time"));
901 c6 = ASTNode_getChild(c5, 1);
902 fail_unless(ASTNode_getType(c6) == AST_NAME);
903 fail_unless(!strcmp(ASTNode_getName(c6), "avogadro"));
904
905 ASTNode_free(r);
906
907 }
908 END_TEST
909
910
START_TEST(test_SBML_parseFormula_named_lambda_arguments6)911 START_TEST(test_SBML_parseFormula_named_lambda_arguments6)
912 {
913 ASTNode_t *r = SBML_parseFormula("lambda(time, time+pi)");
914 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
915 fail_unless(ASTNode_getNumChildren(r) == 2, NULL);
916
917 ASTNode_t *c = ASTNode_getChild(r, 0);
918 fail_unless(ASTNode_getType(c) == AST_NAME);
919 fail_unless(!strcmp(ASTNode_getName(c), "time"));
920
921 c = ASTNode_getChild(r, 1);
922 fail_unless(ASTNode_getType(c) == AST_PLUS);
923 ASTNode_t *c2 = ASTNode_getChild(c, 0);
924 fail_unless(ASTNode_getType(c2) == AST_NAME);
925 fail_unless(!strcmp(ASTNode_getName(c2), "time"));
926 c2 = ASTNode_getChild(c, 1);
927 fail_unless(ASTNode_getType(c2) == AST_CONSTANT_PI);
928
929 ASTNode_free(r);
930
931 }
932 END_TEST
933
934
START_TEST(test_SBML_parseFormula_named_lambda_arguments7)935 START_TEST(test_SBML_parseFormula_named_lambda_arguments7)
936 {
937 ASTNode_t *r = SBML_parseFormula("lambda(time+avogadro+true+false+pi+exponentiale)");
938 fail_unless(ASTNode_getType(r) == AST_LAMBDA, NULL);
939 fail_unless(ASTNode_getNumChildren(r) == 1, NULL);
940
941 ASTNode_t *c = ASTNode_getChild(r, 0);
942 fail_unless(ASTNode_getType(c) == AST_PLUS);
943 fail_unless(ASTNode_getNumChildren(c) == 2, NULL);
944 ASTNode_t *c2 = ASTNode_getChild(c, 0);
945 fail_unless(ASTNode_getNumChildren(c2) == 2, NULL);
946 fail_unless(ASTNode_getType(c2) == AST_PLUS);
947 ASTNode_t *c3 = ASTNode_getChild(c, 1);
948 fail_unless(ASTNode_getType(c3) == AST_CONSTANT_E);
949 c3 = ASTNode_getChild(c2, 0);
950 fail_unless(ASTNode_getNumChildren(c3) == 2, NULL);
951 fail_unless(ASTNode_getType(c3) == AST_PLUS);
952 ASTNode_t *c4 = ASTNode_getChild(c2, 1);
953 fail_unless(ASTNode_getType(c4) == AST_CONSTANT_PI);
954 c4 = ASTNode_getChild(c3, 0);
955 fail_unless(ASTNode_getNumChildren(c4) == 2, NULL);
956 fail_unless(ASTNode_getType(c4) == AST_PLUS);
957 ASTNode_t *c5 = ASTNode_getChild(c3, 1);
958 fail_unless(ASTNode_getType(c5) == AST_CONSTANT_FALSE);
959 c5 = ASTNode_getChild(c4, 0);
960 fail_unless(ASTNode_getNumChildren(c5) == 2, NULL);
961 fail_unless(ASTNode_getType(c5) == AST_PLUS);
962 ASTNode_t *c6 = ASTNode_getChild(c4, 1);
963 fail_unless(ASTNode_getType(c6) == AST_CONSTANT_TRUE);
964
965 c6 = ASTNode_getChild(c5, 0);
966 // expected
967 //fail_unless(ASTNode_getType(c6) == AST_NAME_TIME);
968 // but get
969 fail_unless(ASTNode_getType(c6) == AST_NAME);
970 fail_unless(!strcmp(ASTNode_getName(c6), "time"));
971
972 c6 = ASTNode_getChild(c5, 1);
973 // expected
974 // fail_unless(ASTNode_getType(c6) == AST_NAME_AVOGADRO);
975 // but get
976 fail_unless(ASTNode_getType(c6) == AST_NAME);
977 fail_unless(!strcmp(ASTNode_getName(c6), "avogadro"));
978
979
980
981
982 //fail_unless(ASTNode_getNumChildren(c) == 6, NULL);
983 //ASTNode_t *c2 = ASTNode_getChild(c, 0);
984 //fail_unless(ASTNode_getType(c2) == AST_NAME_TIME);
985 //c2 = ASTNode_getChild(c, 1);
986 //fail_unless(ASTNode_getType(c2) == AST_NAME_AVOGADRO);
987 //c2 = ASTNode_getChild(c, 2);
988 //fail_unless(ASTNode_getType(c2) == AST_CONSTANT_TRUE);
989 //c2 = ASTNode_getChild(c, 3);
990 //fail_unless(ASTNode_getType(c2) == AST_CONSTANT_FALSE);
991 //c2 = ASTNode_getChild(c, 4);
992 //fail_unless(ASTNode_getType(c2) == AST_CONSTANT_PI);
993 //c2 = ASTNode_getChild(c, 5);
994 //fail_unless(ASTNode_getType(c2) == AST_CONSTANT_E);
995
996 ASTNode_free(r);
997
998 }
999 END_TEST
1000
1001 Suite *
create_suite_FormulaParser(void)1002 create_suite_FormulaParser (void)
1003 {
1004 Suite *suite = suite_create("FormulaParser");
1005 TCase *tcase = tcase_create("FormulaParser");
1006
1007
1008 tcase_add_test( tcase, test_FormulaParser_getAction );
1009 tcase_add_test( tcase, test_FormulaParser_getGoto );
1010 tcase_add_test( tcase, test_FormulaParser_accessWithNULL );
1011
1012 tcase_add_test( tcase, test_SBML_parseFormula_1 );
1013 tcase_add_test( tcase, test_SBML_parseFormula_2 );
1014 tcase_add_test( tcase, test_SBML_parseFormula_3 );
1015 tcase_add_test( tcase, test_SBML_parseFormula_4 );
1016 tcase_add_test( tcase, test_SBML_parseFormula_5 );
1017 tcase_add_test( tcase, test_SBML_parseFormula_6 );
1018 tcase_add_test( tcase, test_SBML_parseFormula_7 );
1019 tcase_add_test( tcase, test_SBML_parseFormula_8 );
1020 tcase_add_test( tcase, test_SBML_parseFormula_9 );
1021 tcase_add_test( tcase, test_SBML_parseFormula_10 );
1022 tcase_add_test( tcase, test_SBML_parseFormula_11 );
1023 tcase_add_test( tcase, test_SBML_parseFormula_12 );
1024 tcase_add_test( tcase, test_SBML_parseFormula_13 );
1025 tcase_add_test( tcase, test_SBML_parseFormula_14 );
1026 tcase_add_test( tcase, test_SBML_parseFormula_15 );
1027 tcase_add_test( tcase, test_SBML_parseFormula_16 );
1028 tcase_add_test( tcase, test_SBML_parseFormula_17 );
1029 tcase_add_test( tcase, test_SBML_parseFormula_18 );
1030 tcase_add_test( tcase, test_SBML_parseFormula_19 );
1031 tcase_add_test( tcase, test_SBML_parseFormula_negInf );
1032 tcase_add_test( tcase, test_SBML_parseFormula_negZero );
1033 tcase_add_test( tcase, test_SBML_parse_sqrt );
1034
1035 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments1);
1036 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments2);
1037 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments3);
1038 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments4);
1039 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments5);
1040 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments6);
1041 tcase_add_test(tcase, test_SBML_parseFormula_named_lambda_arguments7);
1042
1043 suite_add_tcase(suite, tcase);
1044
1045 return suite;
1046 }
1047
1048 #if defined(__cplusplus)
1049 CK_CPPEND
1050 #endif
1051