1 /**
2  * @file    TestWriteL3v2EMExtension.cpp
3  * @brief   Unit tests of writing L3v2extendedmathExtension
4  * @author  Akiya Jouraku
5  *
6  * $Id: $
7  * $HeadURL: $
8  *
9  * <!--------------------------------------------------------------------------
10  * This file is part of libSBML.  Please visit http://sbml.org for more
11  * information about SBML, and the latest version of libSBML.
12  *
13  * Copyright (C) 2020 jointly by the following organizations:
14  *     1. California Institute of Technology, Pasadena, CA, USA
15  *     2. University of Heidelberg, Heidelberg, Germany
16  *     3. University College London, London, UK
17  *
18  * Copyright (C) 2019 jointly by the following organizations:
19  *     1. California Institute of Technology, Pasadena, CA, USA
20  *     2. University of Heidelberg, Heidelberg, Germany
21  *
22  * Copyright (C) 2013-2018 jointly by the following organizations:
23  *     1. California Institute of Technology, Pasadena, CA, USA
24  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
25  *     3. University of Heidelberg, Heidelberg, Germany
26  *
27  * Copyright (C) 2009-2011 jointly by the following organizations:
28  *     1. California Institute of Technology, Pasadena, CA, USA
29  *     2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
30  *
31  * Copyright (C) 2006-2008 by the California Institute of Technology,
32  *     Pasadena, CA, USA
33  *
34  * Copyright (C) 2002-2005 jointly by the following organizations:
35  *     1. California Institute of Technology, Pasadena, CA, USA
36  *     2. Japan Science and Technology Agency, Japan
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the GNU Lesser General Public License as published by
40  * the Free Software Foundation.  A copy of the license agreement is provided
41  * in the file named "LICENSE.txt" included with this software distribution
42  * and also available online as http://sbml.org/software/libsbml/license.html
43  * ---------------------------------------------------------------------- -->*/
44 
45 
46 #include <limits>
47 
48 #include <iostream>
49 #include <check.h>
50 #include <sbml/SBMLTypes.h>
51 #include <sbml/extension/SBMLExtensionRegistry.h>
52 #include <sbml/packages/l3v2extendedmath/extension/L3v2extendedmathExtension.h>
53 #include <sbml/packages/l3v2extendedmath/common/L3v2extendedmathExtensionTypes.h>
54 #include <string>
55 
56 /** @cond doxygenIgnored */
57 
58 using namespace std;
59 LIBSBML_CPP_NAMESPACE_USE
60 
61 /** @endcond doxygenIgnored */
62 
63 
64 CK_CPPSTART
65 
66 static string L3V2EM_XMLNS_L3V1V1;
67 static L3v2extendedmathExtension* G;
68 static L3v2extendedmathPkgNamespaces* GNS;
69 
70 void
WriteL3v2EMExtensionTest_setup(void)71 WriteL3v2EMExtensionTest_setup (void)
72 {
73   try
74   {
75     G = new L3v2extendedmathExtension();
76     GNS = new L3v2extendedmathPkgNamespaces();
77     L3V2EM_XMLNS_L3V1V1 = GNS->getURI();
78   }
79   catch(...)
80   {
81     fail("Failed to create a L3v2extendedmathExtension object");
82   }
83 }
84 
85 
86 void
WriteL3v2EMExtensionTest_teardown(void)87 WriteL3v2EMExtensionTest_teardown (void)
88 {
89   delete G;
90   delete GNS;
91 }
92 
START_TEST(test_L3v2EMExtension_create_and_write_L3V1V1)93 START_TEST (test_L3v2EMExtension_create_and_write_L3V1V1)
94 {
95   const char* s1 =
96     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
97     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
98     "  <model>\n"
99     "    <listOfParameters>\n"
100     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
101     "    </listOfParameters>\n"
102     "    <listOfRules>\n"
103     "      <assignmentRule variable=\"p\">\n"
104     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
105     "          <apply>\n"
106     "            <rem/>\n"
107     "            <cn type=\"integer\"> 5 </cn>\n"
108     "            <cn type=\"integer\"> 5 </cn>\n"
109     "          </apply>\n"
110     "        </math>\n"
111     "      </assignmentRule>\n"
112     "    </listOfRules>\n"
113     "  </model>\n"
114     "</sbml>\n"
115     ;
116 
117   L3v2extendedmathPkgNamespaces sbmlns(3,1,1);
118 
119   // create the document
120 
121   SBMLDocument document(&sbmlns);
122 
123   //// mark l3v2extendedmath as required
124 
125   document.setPackageRequired("l3v2extendedmath", true);
126 
127   // create the Model
128 
129   Model* model=document.createModel();
130 
131   Parameter * p = model->createParameter();
132   p->setConstant(false);
133   p->setId("p");
134   p->setValue(1);
135 
136   Rule * r = model->createAssignmentRule();
137   r->setVariable("p");
138   ASTNode * math = SBML_parseL3Formula("rem(5,5)");
139   r->setMath(math);
140   delete math;
141   string s2 = writeSBMLToStdString(&document);
142 
143   fail_unless(strcmp(s1,s2.c_str()) == 0);
144 
145   // check clone()
146 
147   SBMLDocument document2 = document;
148   s2 = writeSBMLToStdString(&document2);
149   fail_unless(strcmp(s1,s2.c_str()) == 0);
150 
151   // check operator=
152 
153   Model* m = document.getModel();
154   document2.setModel(m);
155   s2 = writeSBMLToStdString(&document2);
156 
157   fail_unless(strcmp(s1,s2.c_str()) == 0);
158 
159 }
160 END_TEST
161 
162 
START_TEST(test_L3v2EMExtension_create_add_and_write_L3V1V1)163 START_TEST (test_L3v2EMExtension_create_add_and_write_L3V1V1)
164 {
165   const char* s1 =
166     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
167     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
168     "  <model>\n"
169     "    <listOfParameters>\n"
170     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
171     "    </listOfParameters>\n"
172     "    <listOfRules>\n"
173     "      <assignmentRule variable=\"p\">\n"
174     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
175     "          <apply>\n"
176     "            <rem/>\n"
177     "            <cn type=\"integer\"> 5 </cn>\n"
178     "            <cn type=\"integer\"> 5 </cn>\n"
179     "          </apply>\n"
180     "        </math>\n"
181     "      </assignmentRule>\n"
182     "    </listOfRules>\n"
183     "  </model>\n"
184     "</sbml>\n"
185     ;
186 
187   L3v2extendedmathPkgNamespaces sbmlns(3,1,1);
188 
189   // create the document
190 
191   SBMLDocument document(&sbmlns);
192 
193   //// mark l3v2extendedmath as required
194 
195   document.setPackageRequired("l3v2extendedmath", true);
196 
197   // create the Model
198 
199   Model model(&sbmlns);
200 
201   Parameter p(&sbmlns);
202   p.setConstant(false);
203   p.setId("p");
204   p.setValue(1);
205 
206   fail_unless(model.addParameter(&p) == LIBSBML_OPERATION_SUCCESS);
207 
208   AssignmentRule r(&sbmlns);
209   r.setVariable("p");
210   ASTNode * math = SBML_parseL3Formula("rem(5,5)");
211   fail_unless(r.setMath(math) == LIBSBML_OPERATION_SUCCESS);
212 
213   fail_unless(model.addRule(&r) == LIBSBML_OPERATION_SUCCESS);
214 
215   fail_unless(document.setModel(&model) == LIBSBML_OPERATION_SUCCESS);
216 
217   string s2 = writeSBMLToStdString(&document);
218 
219   fail_unless(strcmp(s1,s2.c_str()) == 0);
220   delete math;
221 }
222 END_TEST
223 
224 
START_TEST(test_L3v2EMExtension_read_enable_via_model_and_write_L3V1V1)225 START_TEST (test_L3v2EMExtension_read_enable_via_model_and_write_L3V1V1)
226 {
227   const char* s1 =
228     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
229     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" level=\"3\" version=\"1\">\n"
230     "  <model>\n"
231     "    <listOfParameters>\n"
232     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
233     "    </listOfParameters>\n"
234     "    <listOfRules>\n"
235     "      <assignmentRule variable=\"p\">\n"
236     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
237     "          <apply>\n"
238     "            <rem/>\n"
239     "            <cn type=\"integer\"> 5 </cn>\n"
240     "            <cn type=\"integer\"> 5 </cn>\n"
241     "          </apply>\n"
242     "        </math>\n"
243     "      </assignmentRule>\n"
244     "    </listOfRules>\n"
245     "  </model>\n"
246     "</sbml>\n"
247     ;
248 
249   const char* s1a =
250     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
251     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
252     "  <model>\n"
253     "    <listOfParameters>\n"
254     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
255     "    </listOfParameters>\n"
256     "    <listOfRules>\n"
257     "      <assignmentRule variable=\"p\">\n"
258     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
259     "          <apply>\n"
260     "            <rem/>\n"
261     "            <cn type=\"integer\"> 5 </cn>\n"
262     "            <cn type=\"integer\"> 5 </cn>\n"
263     "          </apply>\n"
264     "        </math>\n"
265     "      </assignmentRule>\n"
266     "    </listOfRules>\n"
267     "  </model>\n"
268     "</sbml>\n"
269     ;
270 
271   SBMLDocument *document = readSBMLFromString(s1);
272 
273   fail_unless(document->getNumPlugins()             == 0);
274 
275   // should report an error in math
276   fail_unless(document->getNumErrors() == 1);
277 
278   Model *model = document->getModel();
279 
280   fail_unless(model != NULL);
281   const ASTNode * math = model->getRule(0)->getMath();
282   fail_unless(math->getNumPlugins() == 0);
283 
284   //
285   // enable the l3v2extendedmath package by invoking enablePackage function with Model object
286   //
287   fail_unless(model->enablePackage(L3V2EM_XMLNS_L3V1V1, "l3v2extendedmath", true) == LIBSBML_OPERATION_SUCCESS);
288 
289   // mark l3v2extendedmath as required
290 
291   document->setPackageRequired("l3v2extendedmath", true);
292 
293   fail_unless(document->getNumPlugins() == 1);
294   model = document->getModel();
295   math = model->getRule(0)->getMath();
296   // for now this does not get enabled unless you reread the document
297   fail_unless(math->getNumPlugins() == 1);
298 
299   char *s2 = writeSBMLToString(document);
300 
301   fail_unless(strcmp(s1a,s2) == 0);
302 
303   free(s2);
304   delete document;
305 }
306 END_TEST
307 
308 
START_TEST(test_L3v2EMExtension_read_disable_via_model_and_write_L3V1V1)309 START_TEST (test_L3v2EMExtension_read_disable_via_model_and_write_L3V1V1)
310 {
311   const char* s1 =
312     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
313     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
314     "  <model>\n"
315     "    <listOfParameters>\n"
316     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
317     "    </listOfParameters>\n"
318     "    <listOfRules>\n"
319     "      <assignmentRule variable=\"p\">\n"
320     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
321     "          <apply>\n"
322     "            <rem/>\n"
323     "            <cn type=\"integer\"> 5 </cn>\n"
324     "            <cn type=\"integer\"> 5 </cn>\n"
325     "          </apply>\n"
326     "        </math>\n"
327     "      </assignmentRule>\n"
328     "    </listOfRules>\n"
329     "  </model>\n"
330     "</sbml>\n"
331     ;
332 
333   const char* s1d =
334     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
335     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" level=\"3\" version=\"1\">\n"
336     "  <model>\n"
337     "    <listOfParameters>\n"
338     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
339     "    </listOfParameters>\n"
340     "    <listOfRules>\n"
341     "      <assignmentRule variable=\"p\">\n"
342     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
343     "          <apply>\n"
344     "            <rem/>\n"
345     "            <cn type=\"integer\"> 5 </cn>\n"
346     "            <cn type=\"integer\"> 5 </cn>\n"
347     "          </apply>\n"
348     "        </math>\n"
349     "      </assignmentRule>\n"
350     "    </listOfRules>\n"
351     "  </model>\n"
352     "</sbml>\n"
353     ;
354 
355   SBMLDocument *document = readSBMLFromString(s1);
356 
357   fail_unless(document->getNumPlugins() == 1);
358 
359   Model *model = document->getModel();
360 
361   fail_unless(model != NULL);
362   const ASTNode * math = model->getRule(0)->getMath();
363   fail_unless(math->getNumPlugins() > 0);
364 
365   //
366   // disable the l3v2extendedmath package by invoking enablePackage function with Model object
367   //
368   fail_unless(model->enablePackage(L3V2EM_XMLNS_L3V1V1, "l3v2extendedmath", false) == LIBSBML_OPERATION_SUCCESS);
369 
370   fail_unless(document->getNumPlugins() == 0);
371   model = document->getModel();
372   math = model->getRule(0)->getMath();
373   // for now this does not get disabled unless you reread the document
374   //fail_unless(math->getNumPlugins()  > 0);
375 
376   char *s2 = writeSBMLToString(document);
377 
378   fail_unless(strcmp(s1d,s2) == 0);
379 
380   free(s2);
381   delete document;
382 }
383 END_TEST
384 
385 
START_TEST(test_L3v2EMExtension_read_enable_via_sbmldocument_and_write_L3V1V1)386 START_TEST (test_L3v2EMExtension_read_enable_via_sbmldocument_and_write_L3V1V1)
387 {
388   const char* s1 =
389     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
390     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" level=\"3\" version=\"1\">\n"
391     "  <model>\n"
392     "    <listOfParameters>\n"
393     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
394     "    </listOfParameters>\n"
395     "    <listOfRules>\n"
396     "      <assignmentRule variable=\"p\">\n"
397     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
398     "          <apply>\n"
399     "            <rem/>\n"
400     "            <cn type=\"integer\"> 5 </cn>\n"
401     "            <cn type=\"integer\"> 5 </cn>\n"
402     "          </apply>\n"
403     "        </math>\n"
404     "      </assignmentRule>\n"
405     "    </listOfRules>\n"
406     "  </model>\n"
407     "</sbml>\n"
408     ;
409 
410   const char* s1a =
411     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
412     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
413     "  <model>\n"
414     "    <listOfParameters>\n"
415     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
416     "    </listOfParameters>\n"
417     "    <listOfRules>\n"
418     "      <assignmentRule variable=\"p\">\n"
419     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
420     "          <apply>\n"
421     "            <rem/>\n"
422     "            <cn type=\"integer\"> 5 </cn>\n"
423     "            <cn type=\"integer\"> 5 </cn>\n"
424     "          </apply>\n"
425     "        </math>\n"
426     "      </assignmentRule>\n"
427     "    </listOfRules>\n"
428     "  </model>\n"
429     "</sbml>\n"
430     ;
431 
432   SBMLDocument *document = readSBMLFromString(s1);
433 
434   fail_unless(document->getNumPlugins()             == 0);
435   Model *model = document->getModel();
436 
437   fail_unless(model != NULL);
438   const ASTNode * math = model->getRule(0)->getMath();
439   fail_unless(math->getNumPlugins() == 0);
440 
441   //
442   // enable the l3v2extendedmath package by invoking enablePackage function with SBMLDocument object
443   //
444   fail_unless(document->enablePackage(L3V2EM_XMLNS_L3V1V1, "l3v2extendedmath", true) == LIBSBML_OPERATION_SUCCESS);
445 
446   // mark l3v2extendedmath as required
447 
448   document->setPackageRequired("l3v2extendedmath", true);
449 
450   fail_unless(document->getNumPlugins()             == 1);
451 
452   model = document->getModel();
453 
454   fail_unless(model != NULL);
455   math = model->getRule(0)->getMath();
456   // for now this does not get enabled unless you reread the document
457   //fail_unless(math->getNumPlugins() > 1);
458 
459   char *s2 = writeSBMLToString(document);
460 
461   fail_unless(strcmp(s1a,s2) == 0);
462 
463   free(s2);
464   delete document;
465 }
466 END_TEST
467 
468 
START_TEST(test_L3v2EMExtension_read_disable_via_sbmldocument_and_write_L3V1V1)469 START_TEST (test_L3v2EMExtension_read_disable_via_sbmldocument_and_write_L3V1V1)
470 {
471   const char* s1 =
472     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
473     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:l3v2extendedmath=\"http://www.sbml.org/sbml/level3/version1/l3v2extendedmath/version1\" level=\"3\" version=\"1\" l3v2extendedmath:required=\"true\">\n"
474     "  <model>\n"
475     "    <listOfParameters>\n"
476     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
477     "    </listOfParameters>\n"
478     "    <listOfRules>\n"
479     "      <assignmentRule variable=\"p\">\n"
480     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
481     "          <apply>\n"
482     "            <rem/>\n"
483     "            <cn type=\"integer\"> 5 </cn>\n"
484     "            <cn type=\"integer\"> 5 </cn>\n"
485     "          </apply>\n"
486     "        </math>\n"
487     "      </assignmentRule>\n"
488     "    </listOfRules>\n"
489     "  </model>\n"
490     "</sbml>\n"
491     ;
492 
493   const char* s1d =
494     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
495     "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" level=\"3\" version=\"1\">\n"
496     "  <model>\n"
497     "    <listOfParameters>\n"
498     "      <parameter id=\"p\" value=\"1\" constant=\"false\"/>\n"
499     "    </listOfParameters>\n"
500     "    <listOfRules>\n"
501     "      <assignmentRule variable=\"p\">\n"
502     "        <math xmlns=\"http://www.w3.org/1998/Math/MathML\">\n"
503     "          <apply>\n"
504     "            <rem/>\n"
505     "            <cn type=\"integer\"> 5 </cn>\n"
506     "            <cn type=\"integer\"> 5 </cn>\n"
507     "          </apply>\n"
508     "        </math>\n"
509     "      </assignmentRule>\n"
510     "    </listOfRules>\n"
511     "  </model>\n"
512     "</sbml>\n"
513     ;
514 
515   SBMLDocument *document = readSBMLFromString(s1);
516 
517   fail_unless(document->getNumPlugins() == 1);
518   Model *model = document->getModel();
519 
520   fail_unless(model != NULL);
521   const ASTNode * math = model->getRule(0)->getMath();
522   fail_unless(math->getNumPlugins() > 0);
523 
524   //
525   // disable the l3v2extendedmath package by invoking enablePackage function with Model object
526   //
527   fail_unless(document->enablePackage(L3V2EM_XMLNS_L3V1V1, "l3v2extendedmath", false) == LIBSBML_OPERATION_SUCCESS);
528 
529   fail_unless(document->getNumPlugins() == 0);
530 
531   model = document->getModel();
532 
533   fail_unless(model != NULL);
534   model = document->getModel();
535   math = model->getRule(0)->getMath();
536   // for now this does not get disabled unless you reread the document
537   //fail_unless(math->getNumPlugins()  > 0);
538 
539   char *s2 = writeSBMLToString(document);
540 
541   fail_unless(strcmp(s1d,s2) == 0);
542 
543   free(s2);
544   delete document;
545 }
546 END_TEST
547 
548 
549 
550 Suite *
create_suite_WriteL3v2EMExtension(void)551 create_suite_WriteL3v2EMExtension (void)
552 {
553   Suite *suite = suite_create("WriteL3v2EMExtension");
554   TCase *tcase = tcase_create("WriteL3v2EMExtension");
555 
556   tcase_add_checked_fixture(tcase, WriteL3v2EMExtensionTest_setup, WriteL3v2EMExtensionTest_teardown);
557 
558   tcase_add_test( tcase, test_L3v2EMExtension_create_and_write_L3V1V1);
559   tcase_add_test( tcase, test_L3v2EMExtension_create_add_and_write_L3V1V1);
560   //tcase_add_test( tcase, test_L3v2EMExtension_read_enable_via_model_and_write_L3V1V1);
561   //tcase_add_test( tcase, test_L3v2EMExtension_read_disable_via_model_and_write_L3V1V1);
562   //tcase_add_test( tcase, test_L3v2EMExtension_read_enable_via_sbmldocument_and_write_L3V1V1);
563   //tcase_add_test( tcase, test_L3v2EMExtension_read_disable_via_sbmldocument_and_write_L3V1V1);
564   suite_add_tcase(suite, tcase);
565 
566   return suite;
567 }
568 
569 
570 CK_CPPEND
571