1 /**
2  * \file    TestConstraint.c
3  * \brief   SBML Constraint unit tests
4  * \author  Sarah Keating
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 <sbml/SBase.h>
46 #include <sbml/Constraint.h>
47 #include <sbml/xml/XMLNamespaces.h>
48 #include <sbml/xml/XMLAttributes.h>
49 #include <sbml/xml/XMLTriple.h>
50 #include <sbml/xml/XMLNode.h>
51 #include <sbml/SBMLDocument.h>
52 
53 #include <check.h>
54 
55 
56 
57 #include <sbml/common/extern.h>
58 
59 LIBSBML_CPP_NAMESPACE_USE
60 
61 
62 BEGIN_C_DECLS
63 
64 static Constraint_t *C;
65 
66 
67 void
ConstraintTest_setup(void)68 ConstraintTest_setup (void)
69 {
70   C = Constraint_create(2, 4);
71 
72   if (C == NULL)
73   {
74     fail("Constraint_create() returned a NULL pointer.");
75   }
76 }
77 
78 
79 void
ConstraintTest_teardown(void)80 ConstraintTest_teardown (void)
81 {
82   Constraint_free(C);
83 }
84 
85 
START_TEST(test_Constraint_create)86 START_TEST (test_Constraint_create)
87 {
88   fail_unless( SBase_getTypeCode  ((SBase_t *) C) == SBML_CONSTRAINT );
89   fail_unless( SBase_getMetaId    ((SBase_t *) C) == NULL );
90   fail_unless( SBase_getNotes     ((SBase_t *) C) == NULL );
91   fail_unless( SBase_getAnnotation((SBase_t *) C) == NULL );
92 
93   fail_unless( !Constraint_isSetMessage(C) );
94   fail_unless( !Constraint_isSetMath    (C) );
95 }
96 END_TEST
97 
98 
START_TEST(test_Constraint_free_NULL)99 START_TEST (test_Constraint_free_NULL)
100 {
101   Constraint_free(NULL);
102 }
103 END_TEST
104 
105 
106 //START_TEST (test_Constraint_createWithMath)
107 //{
108 //  ASTNode_t       *math = SBML_parseFormula("1 + 1");
109 //  Constraint_t *c   = Constraint_createWithMath(math);
110 //
111 //
112 //
113 //  fail_unless( SBase_getTypeCode  ((SBase_t *) c) == SBML_CONSTRAINT );
114 //  fail_unless( SBase_getMetaId    ((SBase_t *) c) == NULL );
115 //
116 //  fail_unless( Constraint_getMath(c) != math );
117 //  fail_unless( !Constraint_isSetMessage(c) );
118 //  fail_unless( Constraint_isSetMath    (c) );
119 //  Constraint_free(c);
120 //}
121 //END_TEST
122 
123 
START_TEST(test_Constraint_setMath)124 START_TEST (test_Constraint_setMath)
125 {
126   ASTNode_t *math = SBML_parseFormula("2 * k");
127 
128   Constraint_setMath(C, math);
129 
130   fail_unless( Constraint_getMath(C) != math );
131   fail_unless( Constraint_isSetMath(C) );
132 
133   /* Reflexive case (pathological) */
134   Constraint_setMath(C, (ASTNode_t *) Constraint_getMath(C));
135 
136   fail_unless( Constraint_getMath(C) != math );
137 
138   Constraint_setMath(C, NULL);
139   fail_unless( !Constraint_isSetMath(C) );
140 
141   if (Constraint_getMath(C) != NULL)
142   {
143     fail("Constraint_setMath(C, NULL) did not clear ASTNode.");
144   }
145 
146   ASTNode_free(math);
147 }
148 END_TEST
149 
150 
START_TEST(test_Constraint_setMessage)151 START_TEST (test_Constraint_setMessage)
152 {
153   XMLNode_t *text = XMLNode_convertStringToXMLNode(" Some text ", NULL);
154   XMLTriple_t *triple = XMLTriple_createWith("p", "http://www.w3.org/1999/xhtml", "");
155   XMLAttributes_t *att = XMLAttributes_create();
156   XMLNamespaces_t *xmlns = XMLNamespaces_create();
157   XMLNamespaces_add(xmlns, "http://www.w3.org/1999/xhtml", "");
158 
159   XMLNode_t *p = XMLNode_createStartElementNS(triple, att, xmlns);
160   XMLNode_addChild(p, text);
161 
162   XMLTriple_t *triple1 = XMLTriple_createWith("message", "", "");
163   XMLAttributes_t *att1 = XMLAttributes_create();
164   XMLNode_t *node = XMLNode_createStartElement(triple1, att1);
165 
166   XMLNode_addChild(node, p);
167 
168   Constraint_setMessage(C, node);
169 
170   fail_unless( Constraint_getMessage(C) != node );
171   fail_unless( Constraint_isSetMessage(C) == 1);
172 
173   /* Reflexive case (pathological) */
174   Constraint_setMessage(C, (XMLNode_t *) Constraint_getMessage(C));
175 
176   fail_unless( Constraint_getMessage(C) != node );
177 
178   char* str = Constraint_getMessageString(C) ;
179   fail_unless( str != NULL );
180   safe_free(str);
181 
182   Constraint_unsetMessage(C);
183   fail_unless( !Constraint_isSetMessage(C) );
184 
185   if (Constraint_getMessage(C) != NULL)
186   {
187     fail("Constraint_unsetMessage(C) did not clear XMLNode.");
188   }
189 
190   XMLNode_free(text);
191   XMLTriple_free(triple);
192   XMLAttributes_free(att);
193   XMLNamespaces_free(xmlns);
194   XMLNode_free(p);
195   XMLTriple_free(triple1);
196   XMLAttributes_free(att1);
197   XMLNode_free(node);
198 }
199 END_TEST
200 
201 
START_TEST(test_Constraint_createWithNS)202 START_TEST (test_Constraint_createWithNS )
203 {
204   XMLNamespaces_t *xmlns = XMLNamespaces_create();
205   XMLNamespaces_add(xmlns, "http://www.sbml.org", "testsbml");
206   SBMLNamespaces_t *sbmlns = SBMLNamespaces_create(2,2);
207   SBMLNamespaces_addNamespaces(sbmlns,xmlns);
208 
209   Constraint_t *object =
210     Constraint_createWithNS (sbmlns);
211 
212 
213   fail_unless( SBase_getTypeCode  ((SBase_t *) object) == SBML_CONSTRAINT );
214   fail_unless( SBase_getMetaId    ((SBase_t *) object) == NULL );
215   fail_unless( SBase_getNotes     ((SBase_t *) object) == NULL );
216   fail_unless( SBase_getAnnotation((SBase_t *) object) == NULL );
217 
218   fail_unless( SBase_getLevel       ((SBase_t *) object) == 2 );
219   fail_unless( SBase_getVersion     ((SBase_t *) object) == 2 );
220 
221   fail_unless( Constraint_getNamespaces     (object) != NULL );
222   fail_unless( XMLNamespaces_getLength(Constraint_getNamespaces(object)) == 2 );
223 
224   Constraint_free(object);
225   XMLNamespaces_free(xmlns);
226   SBMLNamespaces_free(sbmlns);
227 }
228 END_TEST
229 
230 
231 Suite *
create_suite_Constraint(void)232 create_suite_Constraint (void)
233 {
234   Suite *suite = suite_create("Constraint");
235   TCase *tcase = tcase_create("Constraint");
236 
237 
238   tcase_add_checked_fixture( tcase,
239                              ConstraintTest_setup,
240                              ConstraintTest_teardown );
241 
242   tcase_add_test( tcase, test_Constraint_create      );
243   //tcase_add_test( tcase, test_Constraint_createWithMath      );
244   tcase_add_test( tcase, test_Constraint_free_NULL   );
245   tcase_add_test( tcase, test_Constraint_setMath     );
246   tcase_add_test( tcase, test_Constraint_setMessage  );
247   tcase_add_test( tcase, test_Constraint_createWithNS         );
248 
249   suite_add_tcase(suite, tcase);
250 
251   return suite;
252 }
253 
254 END_C_DECLS
255 
256