COPASI API  4.16.103
test000102.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/sbml/unittests/test000102.cpp,v $
3 // $Revision: 1.2 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2011/12/01 19:54:56 $
7 // End CVS Header
8 
9 // Copyright (C) 2011 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 #include "test000102.h"
15 
16 #include <string>
17 
20 
21 #include "sbml/xml/XMLNode.h"
22 #include "sbml/xml/XMLAttributes.h"
23 #include "sbml/xml/XMLInputStream.h"
24 
25 // libsbml will not add units to unit definitions of Level 3 documents
26 // if not all attributes are explicitely set.
27 // Prior versions of the exporter did not set the multiplier on all units and therefore
28 // most units were not exported correctly to Level 3.
29 
31 {
32  // Create the root container.
33  CCopasiRootContainer::init(0, NULL, false);
35 }
36 
38 {
40 }
41 
42 
43 
45 {
47  std::string s = pDataModel->exportSBMLToString(NULL, 3, 1);
48  // we have to check that the string contains all the units
49  // and that all units are declared on the model element
50  XMLInputStream inputstream(s.c_str(), false);
51  XMLNode node(inputstream);
52  CPPUNIT_ASSERT(node.getName() == "sbml");
53  unsigned int i, iMax = node.getNumChildren();
54  CPPUNIT_ASSERT(iMax > 0);
55  const XMLNode* pModelNode = NULL;
56  const XMLNode* pUnitDefinitionsNode = NULL;
57  const XMLNode* pLengthNode = NULL;
58  const XMLNode* pAreaNode = NULL;
59  const XMLNode* pVolumeNode = NULL;
60  const XMLNode* pTimeNode = NULL;
61  const XMLNode* pSubstanceNode = NULL;
62  const XMLNode* pListOfUnitsNode = NULL;
63  const XMLNode* pUnitNode = NULL;
64  const XMLNode* pChild = NULL;
65  const XMLAttributes* pAttr = NULL;
66 
67  // get the model node and check that all unit
68  // attributes are set
69  for (unsigned int i = 0; i < iMax; ++i)
70  {
71  pChild = &node.getChild(i);
72 
73  if (pChild->getName() == "model")
74  {
75  pModelNode = pChild;
76  pChild = NULL;
77  break;
78  }
79  }
80 
81  assert(pModelNode != NULL);
82  pAttr = &pModelNode->getAttributes();
83  int index = pAttr->getIndex("lengthUnits");
84  CPPUNIT_ASSERT(index != -1);
85  std::string v = pAttr->getValue(index);
86  CPPUNIT_ASSERT(v == "length");
87 
88  index = pAttr->getIndex("areaUnits");
89  CPPUNIT_ASSERT(index != -1);
90  v = pAttr->getValue(index);
91  CPPUNIT_ASSERT(v == "area");
92 
93  index = pAttr->getIndex("volumeUnits");
94  CPPUNIT_ASSERT(index != -1);
95  v = pAttr->getValue(index);
96  CPPUNIT_ASSERT(v == "volume");
97 
98  index = pAttr->getIndex("timeUnits");
99  CPPUNIT_ASSERT(index != -1);
100  v = pAttr->getValue(index);
101  CPPUNIT_ASSERT(v == "time");
102 
103  index = pAttr->getIndex("substanceUnits");
104  CPPUNIT_ASSERT(index != -1);
105  v = pAttr->getValue(index);
106  CPPUNIT_ASSERT(v == "substance");
107 
108  index = pAttr->getIndex("extentUnits");
109  CPPUNIT_ASSERT(index != -1);
110  v = pAttr->getValue(index);
111  CPPUNIT_ASSERT(v == "substance");
112 
113 
114  // find the list of UnitDefinitions
115  iMax = pModelNode->getNumChildren();
116  CPPUNIT_ASSERT(iMax > 0);
117 
118  for (i = 0; i < iMax; ++i)
119  {
120  pChild = &pModelNode->getChild(i);
121 
122  if (pChild->getName() == "listOfUnitDefinitions")
123  {
124  pUnitDefinitionsNode = pChild;
125  pChild = NULL;
126  break;
127  }
128  }
129 
130  CPPUNIT_ASSERT(pUnitDefinitionsNode != NULL);
131 
132  iMax = pUnitDefinitionsNode->getNumChildren();
133  // there have to be at least 5 unit definitions
134  CPPUNIT_ASSERT(iMax > 4);
135 
136  // find the length unit
137  // find the area unit
138  // find the volume unit
139  // find the time unit
140  // find the substance unit
141  for (i = 0; i < iMax; ++i)
142  {
143  pChild = &pUnitDefinitionsNode->getChild(i);
144 
145  if (pChild->getName() == "unitDefinition")
146  {
147  pAttr = &pChild->getAttributes();
148  index = pAttr->getIndex("id");
149  CPPUNIT_ASSERT(index != -1);
150  v = pAttr->getValue(index);
151 
152  if (v == "area")
153  {
154  pAreaNode = pChild;
155  continue;
156  }
157  else if (v == "length")
158  {
159  pLengthNode = pChild;
160  continue;
161  }
162  else if (v == "volume")
163  {
164  pVolumeNode = pChild;
165  continue;
166  }
167  else if (v == "time")
168  {
169  pTimeNode = pChild;
170  continue;
171  }
172  else if (v == "substance")
173  {
174  pSubstanceNode = pChild;
175  continue;
176  }
177  }
178  }
179 
180  unsigned int numUnitChildren = 0;
181  CPPUNIT_ASSERT(pAreaNode != NULL);
182  iMax = pAreaNode->getNumChildren();
183  CPPUNIT_ASSERT(iMax > 0);
184 
185  for (i = 0; i < iMax; ++i)
186  {
187  pChild = &pAreaNode->getChild(i);
188 
189  if (pChild->getName() == "listOfUnits")
190  {
191  pListOfUnitsNode = pChild;
192  break;
193  }
194  }
195 
196  CPPUNIT_ASSERT(pListOfUnitsNode != NULL);
197  iMax = pListOfUnitsNode->getNumChildren();
198  CPPUNIT_ASSERT(iMax > 0);
199 
200  for (i = 0; i < iMax; ++i)
201  {
202  pChild = &pListOfUnitsNode->getChild(i);
203 
204  if (pChild->getName() == "unit")
205  {
206  ++numUnitChildren;
207  pUnitNode = pChild;
208  }
209  }
210 
211  // check the unit(s)
212  // TODO the numbers should be tested after
213  // TODO conversion to a numerical value
214  // TODO but for now the string comparison will do
215  CPPUNIT_ASSERT(numUnitChildren == 1);
216  CPPUNIT_ASSERT(pUnitNode != NULL);
217  pAttr = &pUnitNode->getAttributes();
218  // kind should be square metre
219  index = pAttr->getIndex("kind");
220  CPPUNIT_ASSERT(index != -1);
221  v = pAttr->getValue(index);
222  CPPUNIT_ASSERT(v == "metre");
223  // multiplier = 1
224  index = pAttr->getIndex("multiplier");
225  CPPUNIT_ASSERT(index != -1);
226  v = pAttr->getValue(index);
227  CPPUNIT_ASSERT(v == "1");
228  // scale =-3
229  index = pAttr->getIndex("scale");
230  CPPUNIT_ASSERT(index != -1);
231  v = pAttr->getValue(index);
232  CPPUNIT_ASSERT(v == "-3");
233  // exponent = 2
234  index = pAttr->getIndex("exponent");
235  CPPUNIT_ASSERT(index != -1);
236  v = pAttr->getValue(index);
237  CPPUNIT_ASSERT(v == "2");
238 
239 
240  CPPUNIT_ASSERT(pLengthNode != NULL);
241  iMax = pLengthNode->getNumChildren();
242  numUnitChildren = 0;
243  CPPUNIT_ASSERT(iMax > 0);
244  pListOfUnitsNode = NULL;
245 
246  for (i = 0; i < iMax; ++i)
247  {
248  pChild = &pLengthNode->getChild(i);
249 
250  if (pChild->getName() == "listOfUnits")
251  {
252  pListOfUnitsNode = pChild;
253  break;
254  }
255  }
256 
257  CPPUNIT_ASSERT(pListOfUnitsNode != NULL);
258  iMax = pListOfUnitsNode->getNumChildren();
259  CPPUNIT_ASSERT(iMax > 0);
260 
261  for (i = 0; i < iMax; ++i)
262  {
263  pChild = &pListOfUnitsNode->getChild(i);
264 
265  if (pChild->getName() == "unit")
266  {
267  ++numUnitChildren;
268  pUnitNode = pChild;
269  }
270  }
271 
272  // check the unit(s)
273  CPPUNIT_ASSERT(numUnitChildren == 1);
274  CPPUNIT_ASSERT(pUnitNode != NULL);
275  pAttr = &pUnitNode->getAttributes();
276  // kind should be square metre
277  index = pAttr->getIndex("kind");
278  CPPUNIT_ASSERT(index != -1);
279  v = pAttr->getValue(index);
280  CPPUNIT_ASSERT(v == "metre");
281  // multiplier = 1
282  index = pAttr->getIndex("multiplier");
283  CPPUNIT_ASSERT(index != -1);
284  v = pAttr->getValue(index);
285  CPPUNIT_ASSERT(v == "1");
286  // scale = 0
287  index = pAttr->getIndex("scale");
288  CPPUNIT_ASSERT(index != -1);
289  v = pAttr->getValue(index);
290  CPPUNIT_ASSERT(v == "0");
291  // exponent = 1
292  index = pAttr->getIndex("exponent");
293  CPPUNIT_ASSERT(index != -1);
294  v = pAttr->getValue(index);
295  CPPUNIT_ASSERT(v == "1");
296 
297  CPPUNIT_ASSERT(pVolumeNode != NULL);
298  iMax = pVolumeNode->getNumChildren();
299  numUnitChildren = 0;
300  CPPUNIT_ASSERT(iMax > 0);
301  pListOfUnitsNode = NULL;
302 
303  for (i = 0; i < iMax; ++i)
304  {
305  pChild = &pVolumeNode->getChild(i);
306 
307  if (pChild->getName() == "listOfUnits")
308  {
309  pListOfUnitsNode = pChild;
310  break;
311  }
312  }
313 
314  CPPUNIT_ASSERT(pListOfUnitsNode != NULL);
315  iMax = pListOfUnitsNode->getNumChildren();
316  CPPUNIT_ASSERT(iMax > 0);
317 
318  for (i = 0; i < iMax; ++i)
319  {
320  pChild = &pListOfUnitsNode->getChild(i);
321 
322  if (pChild->getName() == "unit")
323  {
324  ++numUnitChildren;
325  pUnitNode = pChild;
326  }
327  }
328 
329  // check the unit(s)
330  CPPUNIT_ASSERT(numUnitChildren == 1);
331  CPPUNIT_ASSERT(pUnitNode != NULL);
332  pAttr = &pUnitNode->getAttributes();
333  // kind should be square metre
334  index = pAttr->getIndex("kind");
335  CPPUNIT_ASSERT(index != -1);
336  v = pAttr->getValue(index);
337  CPPUNIT_ASSERT(v == "litre");
338  // multiplier = 1
339  index = pAttr->getIndex("multiplier");
340  CPPUNIT_ASSERT(index != -1);
341  v = pAttr->getValue(index);
342  CPPUNIT_ASSERT(v == "1");
343  // scale = 0
344  index = pAttr->getIndex("scale");
345  CPPUNIT_ASSERT(index != -1);
346  v = pAttr->getValue(index);
347  CPPUNIT_ASSERT(v == "0");
348  // exponent = 1
349  index = pAttr->getIndex("exponent");
350  CPPUNIT_ASSERT(index != -1);
351  v = pAttr->getValue(index);
352  CPPUNIT_ASSERT(v == "1");
353 
354  CPPUNIT_ASSERT(pTimeNode != NULL);
355  iMax = pTimeNode->getNumChildren();
356  numUnitChildren = 0;
357  CPPUNIT_ASSERT(iMax > 0);
358  pListOfUnitsNode = NULL;
359 
360  for (i = 0; i < iMax; ++i)
361  {
362  pChild = &pTimeNode->getChild(i);
363 
364  if (pChild->getName() == "listOfUnits")
365  {
366  pListOfUnitsNode = pChild;
367  break;
368  }
369  }
370 
371  CPPUNIT_ASSERT(pListOfUnitsNode != NULL);
372  iMax = pListOfUnitsNode->getNumChildren();
373  CPPUNIT_ASSERT(iMax > 0);
374 
375  for (i = 0; i < iMax; ++i)
376  {
377  pChild = &pListOfUnitsNode->getChild(i);
378 
379  if (pChild->getName() == "unit")
380  {
381  ++numUnitChildren;
382  pUnitNode = pChild;
383  }
384  }
385 
386  // check the unit(s)
387  CPPUNIT_ASSERT(numUnitChildren == 1);
388  CPPUNIT_ASSERT(pUnitNode != NULL);
389  pAttr = &pUnitNode->getAttributes();
390  // kind should be square metre
391  index = pAttr->getIndex("kind");
392  CPPUNIT_ASSERT(index != -1);
393  v = pAttr->getValue(index);
394  CPPUNIT_ASSERT(v == "second");
395  // multiplier = 1
396  index = pAttr->getIndex("multiplier");
397  CPPUNIT_ASSERT(index != -1);
398  v = pAttr->getValue(index);
399  CPPUNIT_ASSERT(v == "1");
400  // scale = 0
401  index = pAttr->getIndex("scale");
402  CPPUNIT_ASSERT(index != -1);
403  v = pAttr->getValue(index);
404  CPPUNIT_ASSERT(v == "0");
405  // exponent = 1
406  index = pAttr->getIndex("exponent");
407  CPPUNIT_ASSERT(index != -1);
408  v = pAttr->getValue(index);
409  CPPUNIT_ASSERT(v == "1");
410 
411 
412  CPPUNIT_ASSERT(pSubstanceNode != NULL);
413  iMax = pSubstanceNode->getNumChildren();
414  numUnitChildren = 0;
415  CPPUNIT_ASSERT(iMax > 0);
416  pListOfUnitsNode = NULL;
417 
418  for (i = 0; i < iMax; ++i)
419  {
420  pChild = &pSubstanceNode->getChild(i);
421 
422  if (pChild->getName() == "listOfUnits")
423  {
424  pListOfUnitsNode = pChild;
425  break;
426  }
427  }
428 
429  CPPUNIT_ASSERT(pListOfUnitsNode != NULL);
430  iMax = pListOfUnitsNode->getNumChildren();
431  CPPUNIT_ASSERT(iMax > 0);
432 
433  for (i = 0; i < iMax; ++i)
434  {
435  pChild = &pListOfUnitsNode->getChild(i);
436 
437  if (pChild->getName() == "unit")
438  {
439  ++numUnitChildren;
440  pUnitNode = pChild;
441  }
442  }
443 
444  // check the unit(s)
445  CPPUNIT_ASSERT(numUnitChildren == 1);
446  CPPUNIT_ASSERT(pUnitNode != NULL);
447  pAttr = &pUnitNode->getAttributes();
448  // kind should be square mole
449  index = pAttr->getIndex("kind");
450  CPPUNIT_ASSERT(index != -1);
451  v = pAttr->getValue(index);
452  CPPUNIT_ASSERT(v == "mole");
453  // multiplier = 1
454  index = pAttr->getIndex("multiplier");
455  CPPUNIT_ASSERT(index != -1);
456  v = pAttr->getValue(index);
457  CPPUNIT_ASSERT(v == "1");
458  // scale = 0
459  index = pAttr->getIndex("scale");
460  CPPUNIT_ASSERT(index != -1);
461  v = pAttr->getValue(index);
462  CPPUNIT_ASSERT(v == "0");
463  // exponent = 1
464  index = pAttr->getIndex("exponent");
465  CPPUNIT_ASSERT(index != -1);
466  v = pAttr->getValue(index);
467  CPPUNIT_ASSERT(v == "1");
468 
469 
470 }
471 
472 const char* test000102::SBML_STRING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
473  "<sbml xmlns=\"http://www.sbml.org/sbml/level3/version1/core\" xmlns:comp=\"http://www.sbml.org/sbml/level3/version1/comp/version1\" level=\"3\" version=\"1\" > \n"
474  "<model id=\"Module_1_1\" name=\"Module_1\" areaUnits=\"area\" substanceUnits=\"mole\" >\n"
475  " <listOfUnitDefinitions>\n"
476  " <unitDefinition id=\"area\" name=\"area\">\n"
477  " <listOfUnits>\n"
478  " <unit exponent=\"2\" kind=\"metre\" multiplier=\"1.0\" scale=\"-3\"/>\n"
479  " </listOfUnits>\n"
480  " </unitDefinition>\n"
481  " </listOfUnitDefinitions>\n"
482  " <listOfCompartments>\n"
483  " <compartment id=\"cell_1\" name=\"cell\" size=\"1.0\" constant=\"false\" spatialDimensions=\"2.0\" />\n"
484  " </listOfCompartments>\n"
485  " <listOfSpecies>\n"
486  " <species id=\"CycB_1\" name=\"CycB\" compartment=\"cell_1\" initialConcentration=\"1.0\" hasOnlySubstanceUnits=\"false\" boundaryCondition=\"false\" constant=\"false\" />\n"
487  " </listOfSpecies> \n"
488  " </model>\n"
489  "</sbml>\n";
std::string exportSBMLToString(CProcessReport *pExportHandler, int sbmlLevel, int sbmlVersion)
void setUp()
Definition: test000102.cpp:30
CCopasiDataModel * pDataModel
Definition: test000102.h:41
static const char * SBML_STRING
Definition: test000102.h:39
static CCopasiDataModel * addDatamodel()
bool importSBMLFromString(const std::string &sbmlDocumentText, CProcessReport *pImportHandler=NULL, const bool &deleteOldData=true)
static void init(int argc, char *argv[], const bool &withGui=false)
void test_bug1741()
Definition: test000102.cpp:44
void tearDown()
Definition: test000102.cpp:37