1 /**
2  * \file    TestConstraint_newSetters.c
3  * \brief   Constraint unit tests for new set function API
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
ConstraintTest1_setup(void)68 ConstraintTest1_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
ConstraintTest1_teardown(void)80 ConstraintTest1_teardown (void)
81 {
82   Constraint_free(C);
83 }
84 
85 
START_TEST(test_Constraint_setMath1)86 START_TEST (test_Constraint_setMath1)
87 {
88   ASTNode_t *math = SBML_parseFormula("2 * k");
89 
90   int i = Constraint_setMath(C, math);
91 
92   fail_unless( i == LIBSBML_OPERATION_SUCCESS);
93   fail_unless( Constraint_getMath(C) != math );
94   fail_unless( Constraint_isSetMath(C) );
95 
96   i = Constraint_setMath(C, NULL);
97 
98   fail_unless( i == LIBSBML_OPERATION_SUCCESS);
99   fail_unless( Constraint_getMath(C) == NULL );
100   fail_unless( !Constraint_isSetMath(C) );
101 
102   ASTNode_free(math);
103 }
104 END_TEST
105 
106 
START_TEST(test_Constraint_setMath2)107 START_TEST (test_Constraint_setMath2)
108 {
109   ASTNode_t *math = ASTNode_createWithType(AST_DIVIDE);
110 
111   int i = Constraint_setMath(C, math);
112 
113   fail_unless( i == LIBSBML_INVALID_OBJECT);
114   fail_unless( !Constraint_isSetMath(C) );
115 
116   ASTNode_free(math);
117 }
118 END_TEST
119 
120 
START_TEST(test_Constraint_setMessage1)121 START_TEST (test_Constraint_setMessage1)
122 {
123   XMLNode_t *node = XMLNode_create();
124 
125   int i = Constraint_setMessage(C, node);
126 
127   fail_unless(i == LIBSBML_INVALID_OBJECT);
128   fail_unless( Constraint_isSetMessage(C) == 0);
129 
130   i = Constraint_unsetMessage(C);
131 
132   fail_unless(i == LIBSBML_OPERATION_SUCCESS);
133   fail_unless( !Constraint_isSetMessage(C) );
134 
135   if (Constraint_getMessage(C) != NULL)
136   {
137     fail("Constraint_unsetMessage(C) did not clear XMLNode.");
138   }
139 
140   XMLNode_free(node);
141 }
142 END_TEST
143 
144 
START_TEST(test_Constraint_setMessage2)145 START_TEST (test_Constraint_setMessage2)
146 {
147   //const char * expected = (
148   //  "<message>\n"
149   //  "  <p xmlns=\"http://www.w3.org/1999/xhtml\"> Some text </p>\n"
150   //  "</message>");
151 
152   XMLNode_t *text = XMLNode_convertStringToXMLNode(" Some text ", NULL);
153   XMLTriple_t *triple = XMLTriple_createWith("p", "http://www.w3.org/1999/xhtml", "");
154   XMLAttributes_t *att = XMLAttributes_create();
155   XMLNamespaces_t *xmlns = XMLNamespaces_create();
156   XMLNamespaces_add(xmlns, "http://www.w3.org/1999/xhtml", "");
157 
158   XMLNode_t *p = XMLNode_createStartElementNS(triple, att, xmlns);
159   XMLNode_addChild(p, text);
160 
161   XMLTriple_t *triple1 = XMLTriple_createWith("message", "", "");
162   XMLAttributes_t *att1 = XMLAttributes_create();
163   XMLNode_t *node = XMLNode_createStartElement(triple1, att1);
164 
165   XMLNode_addChild(node, p);
166 
167   int i = Constraint_setMessage(C, node);
168 
169   fail_unless(i == LIBSBML_OPERATION_SUCCESS);
170   fail_unless( Constraint_isSetMessage(C) == 1);
171   /* FIX ME
172   printf("Expected: %s\n", expected);
173   printf("String  : %s\n", Constraint_getMessage(C));
174   fail_unless( strcmp(Constraint_getMessageString(C),
175     expected) == 0);
176   */
177   i = Constraint_unsetMessage(C);
178 
179   fail_unless(i == LIBSBML_OPERATION_SUCCESS);
180   fail_unless( !Constraint_isSetMessage(C) );
181 
182   if (Constraint_getMessage(C) != NULL)
183   {
184     fail("Constraint_unsetMessage(C) did not clear XMLNode.");
185   }
186 
187   XMLNode_free(text);
188   XMLTriple_free(triple);
189   XMLAttributes_free(att);
190   XMLNamespaces_free(xmlns);
191   XMLNode_free(p);
192   XMLTriple_free(triple1);
193   XMLAttributes_free(att1);
194   XMLNode_free(node);
195 }
196 END_TEST
197 
198 
199 Suite *
create_suite_Constraint_newSetters(void)200 create_suite_Constraint_newSetters (void)
201 {
202   Suite *suite = suite_create("Constraint_newSetters");
203   TCase *tcase = tcase_create("Constraint_newSetters");
204 
205 
206   tcase_add_checked_fixture( tcase,
207                              ConstraintTest1_setup,
208                              ConstraintTest1_teardown );
209 
210   tcase_add_test( tcase, test_Constraint_setMath1     );
211   tcase_add_test( tcase, test_Constraint_setMath2     );
212   tcase_add_test( tcase, test_Constraint_setMessage1  );
213   tcase_add_test( tcase, test_Constraint_setMessage2  );
214 
215 
216   suite_add_tcase(suite, tcase);
217 
218   return suite;
219 }
220 
221 END_C_DECLS
222 
223 
224