END_TEST START_TEST (test_SBML_parseFormula_12) { ASTNode_t *r = SBML_parseFormula("2 * foo^bar + 3.0"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_PLUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '+', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_TIMES, NULL ); fail_unless( ASTNode_getCharacter (c) == '*', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_REAL, NULL ); fail_unless( ASTNode_getReal (c) == 3.0, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getLeftChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 2, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_POWER, NULL ); fail_unless( ASTNode_getCharacter (c) == '^', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getLeftChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) ); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getRightChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) ); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_6) { ASTNode_t *r = SBML_parseFormula("1 + 2"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_PLUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '+', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 2, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_11) { ASTNode_t *r = SBML_parseFormula("1 - -foo / 3"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_MINUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '-', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL ); fail_unless( ASTNode_getCharacter (c) == '/', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2, NULL ); c = ASTNode_getLeftChild( ASTNode_getRightChild(r) ); fail_unless( ASTNode_getType (c) == AST_MINUS, NULL ); fail_unless( ASTNode_getCharacter (c) == '-', NULL ); fail_unless( ASTNode_getNumChildren(c) == 1 , NULL ); c = ASTNode_getLeftChild( ASTNode_getLeftChild( ASTNode_getRightChild(r) ) ); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getRightChild( ASTNode_getRightChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 3, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_10) { ASTNode_t *r = SBML_parseFormula("1 + -2e100 / 3"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_PLUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '+', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL ); fail_unless( ASTNode_getCharacter (c) == '/', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getLeftChild(c); fail_unless( ASTNode_getType (c) == AST_REAL_E, NULL ); fail_unless( ASTNode_getMantissa (c) == -2, NULL ); fail_unless( ASTNode_getExponent (c) == 100, NULL ); fail_unless( ASTNode_getReal (c) == -2e+100, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getRightChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 3, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_8) { ASTNode_t *r = SBML_parseFormula("(1 - 2) * 3"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_TIMES, NULL ); fail_unless( ASTNode_getCharacter (r) == '*', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_MINUS, NULL ); fail_unless( ASTNode_getCharacter (c) == '-', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2, NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 3, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getLeftChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 2, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); ASTNode_free(r); }
/** * Formats the given ASTNode as an SBML L1 operator and appends the result * to the given StringBuffer. */ void L3FormulaFormatter_formatOperator (StringBuffer_t *sb, const ASTNode_t *node) { ASTNodeType_t type = ASTNode_getType(node); if (type == AST_FUNCTION_POWER || type == AST_POWER) { StringBuffer_appendChar(sb, '^'); } else { StringBuffer_appendChar(sb, ' '); StringBuffer_appendChar(sb, ASTNode_getCharacter(node)); StringBuffer_appendChar(sb, ' '); } }
END_TEST START_TEST (test_SBML_parseFormula_16) { ASTNode_t *r = SBML_parseFormula("foo(1, bar, 2^-3)"); ASTNode_t *c; fail_unless( ASTNode_getType(r) == AST_FUNCTION , NULL ); fail_unless( !strcmp(ASTNode_getName(r), "foo") , NULL ); fail_unless( ASTNode_getNumChildren(r) == 3 , NULL ); c = ASTNode_getChild(r, 0); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getChild(r, 1); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getChild(r, 2); fail_unless( ASTNode_getType (c) == AST_POWER, NULL ); fail_unless( ASTNode_getCharacter (c) == '^', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getLeftChild( ASTNode_getChild(r, 2) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 2, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getChild(r, 2) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == -3, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); ASTNode_free(r); }
/** * Formats the given ASTNode as an SBML L1 operator and appends the result * to the given StringBuffer. */ void FormulaFormatter_formatOperator (StringBuffer_t *sb, const ASTNode_t *node) { ASTNodeType_t type = ASTNode_getType(node); if (type != AST_POWER) { StringBuffer_appendChar(sb, ' '); } StringBuffer_appendChar(sb, ASTNode_getCharacter(node)); if (type != AST_POWER) { StringBuffer_appendChar(sb, ' '); } }
/** * Formats the given ASTNode as a directed graph operator and returns the result * as a string. */ char * FormulaGraphvizFormatter_formatOperator (const ASTNode_t *node) { char *s; ASTNodeType_t type = ASTNode_getType(node); StringBuffer_t *p = StringBuffer_create(128); switch (type) { case AST_TIMES: s = "times"; break; case AST_DIVIDE: s = "divide"; break; case AST_PLUS: s = "plus"; break; case AST_MINUS: s = "minus"; break; case AST_POWER: s = "power"; break; default: StringBuffer_appendChar(p, ASTNode_getCharacter(node)); s = StringBuffer_toString(p); break; } free(p); return s; }
/** * Since graphviz will interpret identical names as referring to * the same node presentation-wise it is better if each function node * has a unique name. * * Returns the name of the operator with the name of the first child * prepended * * THIS COULD BE DONE BETTER */ char * FormulaGraphvizFormatter_OperatorGetUniqueName (const ASTNode_t *node) { char *s; char number[10]; StringBuffer_t *p = StringBuffer_create(128); ASTNodeType_t type = ASTNode_getType(node); if (FormulaGraphvizFormatter_isFunction(ASTNode_getChild(node,0)) || ASTNode_isOperator(ASTNode_getChild(node,0))) { StringBuffer_append(p, "func"); } else { if (ASTNode_isInteger(ASTNode_getChild(node, 0))) { sprintf(number, "%d", (int)ASTNode_getInteger(ASTNode_getChild(node, 0))); StringBuffer_append(p, number); } else if (ASTNode_isReal(ASTNode_getChild(node, 0))) { sprintf(number, "%ld", ASTNode_getNumerator(ASTNode_getChild(node, 0))); StringBuffer_append(p, number); } else { StringBuffer_append(p, ASTNode_getName(ASTNode_getChild(node,0))); } } switch (type) { case AST_TIMES: StringBuffer_append(p, "times"); break; case AST_DIVIDE: StringBuffer_append(p, "divide"); break; case AST_PLUS: StringBuffer_append(p, "plus"); break; case AST_MINUS: StringBuffer_append(p, "minus"); break; case AST_POWER: StringBuffer_append(p, "power"); break; default: StringBuffer_appendChar(p, ASTNode_getCharacter(node)); break; } s = StringBuffer_toString(p); free(p); return s; }