COPASI API  4.16.103
test_node_conversion.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/compareExpressions/unittests/test_node_conversion.cpp,v $
3 // $Revision: 1.6 $
4 // $Name: $
5 // $Author: gauges $
6 // $Date: 2009/07/02 17:57:53 $
7 // End CVS Header
8 
9 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
11 // and The University of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc. and EML Research, gGmbH.
16 // All rights reserved.
17 
18 #include "test_node_conversion.hpp"
19 
20 #include <string>
21 
30 
32 
33 void test_node_conversion::setUp()
34 {
35  CCopasiRootContainer::init(false, 0, NULL);
36 }
37 
38 void test_node_conversion::tearDown()
39 {
41 }
42 
43 void test_node_conversion::test_0001()
44 {
45  std::string expr("A+G+D+E");
46  CEvaluationTree* pTree = new CEvaluationTree("tree", NULL, CEvaluationTree::Function);
47  pTree->setInfix(expr);
48  CNormalBase* pNormal = createNormalRepresentation(pTree->getRoot());
49  delete pTree;
50  CNormalFraction* pFraction = dynamic_cast<CNormalFraction*>(pNormal);
51  CPPUNIT_ASSERT(pFraction != NULL);
52  CPPUNIT_ASSERT(pFraction->checkDenominatorOne());
53  const CNormalSum& pSum = pFraction->getNumerator();
54  CPPUNIT_ASSERT(pSum.getFractions().size() == 0);
55  CPPUNIT_ASSERT(pSum.getProducts().size() == 4);
56  CEvaluationNode* pResultNode = convertToCEvaluationNode(pSum);
57  CEvaluationNodeOperator* pOperator = dynamic_cast<CEvaluationNodeOperator*>(pResultNode);
58  CPPUNIT_ASSERT(pOperator != NULL);
60  CEvaluationNodeVariable* pVariable = NULL;
61 
62  if (dynamic_cast<CEvaluationNodeOperator*>(pOperator->getChild()))
63  {
64  pOperator = static_cast<CEvaluationNodeOperator*>(pOperator->getChild());
65  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getSibling());
66  }
67  else
68  {
69  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getChild());
70  pOperator = static_cast<CEvaluationNodeOperator*>(pVariable->getSibling());
71  }
72 
73  CPPUNIT_ASSERT(pOperator != NULL);
74  CPPUNIT_ASSERT(pVariable != NULL);
75 
76  if (dynamic_cast<CEvaluationNodeOperator*>(pOperator->getChild()))
77  {
78  pOperator = static_cast<CEvaluationNodeOperator*>(pOperator->getChild());
79  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getSibling());
80  }
81  else
82  {
83  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getChild());
84  pOperator = static_cast<CEvaluationNodeOperator*>(pVariable->getSibling());
85  }
86 
87  CPPUNIT_ASSERT(pOperator != NULL);
88  CPPUNIT_ASSERT(pVariable != NULL);
89  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getChild());
90  CPPUNIT_ASSERT(pVariable != NULL);
91  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pVariable->getSibling());
92  CPPUNIT_ASSERT(pVariable != NULL);
93  delete pNormal;
94  delete pResultNode;
95 }
96 
97 void test_node_conversion::test_0002()
98 {
99  std::string expr("A*D+B*C");
100  CEvaluationTree* pTree = new CEvaluationTree("tree", NULL, CEvaluationTree::Function);
101  pTree->setInfix(expr);
102  CNormalBase* pNormal = createNormalRepresentation(pTree->getRoot());
103  delete pTree;
104  CNormalFraction* pFraction = dynamic_cast<CNormalFraction*>(pNormal);
105  CPPUNIT_ASSERT(pFraction != NULL);
106  CPPUNIT_ASSERT(pFraction->checkDenominatorOne());
107  const CNormalSum& pSum = pFraction->getNumerator();
108  CPPUNIT_ASSERT(pSum.getFractions().size() == 0);
109  CPPUNIT_ASSERT(pSum.getProducts().size() == 2);
110  CEvaluationNode* pResultNode = convertToCEvaluationNode(pSum);
111  CEvaluationNodeOperator* pOperator = dynamic_cast<CEvaluationNodeOperator*>(pResultNode);
112  CPPUNIT_ASSERT(pOperator != NULL);
114  pOperator = dynamic_cast<CEvaluationNodeOperator*>(pOperator->getChild());
115  CPPUNIT_ASSERT(pOperator != NULL);
117  CEvaluationNodeVariable* pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getChild());
118  CPPUNIT_ASSERT(pVariable != NULL);
119  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pVariable->getSibling());
120  CPPUNIT_ASSERT(pVariable != NULL);
121  pOperator = dynamic_cast<CEvaluationNodeOperator*>(pOperator->getSibling());
122  CPPUNIT_ASSERT(pOperator != NULL);
124  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pOperator->getChild());
125  CPPUNIT_ASSERT(pVariable != NULL);
126  pVariable = dynamic_cast<CEvaluationNodeVariable*>(pVariable->getSibling());
127  CPPUNIT_ASSERT(pVariable != NULL);
128  delete pNormal;
129  delete pResultNode;
130 }
131 
132 void test_node_conversion::test_0003()
133 {
134  std::string expr("1.0*3.0+1.0*5.0");
135  CEvaluationTree* pTree = new CEvaluationTree("tree", NULL, CEvaluationTree::Function);
136  pTree->setInfix(expr);
137  CNormalBase* pNormal = createNormalRepresentation(pTree->getRoot());
138  delete pTree;
139  CNormalFraction* pFraction = dynamic_cast<CNormalFraction*>(pNormal);
140  CPPUNIT_ASSERT(pFraction != NULL);
141  // for now, we see if it can be converted to a string without crashing
142  std::string s = pFraction->toString();
143  delete pNormal;
144 }
145 
146 void test_node_conversion::test_0004()
147 {
148  std::string expr("5.0^1.0*7.0^1.0");
149  CEvaluationTree* pTree = new CEvaluationTree("tree", NULL, CEvaluationTree::Function);
150  pTree->setInfix(expr);
151  CNormalBase* pNormal = createNormalRepresentation(pTree->getRoot());
152  delete pTree;
153  CNormalFraction* pFraction = dynamic_cast<CNormalFraction*>(pNormal);
154  CPPUNIT_ASSERT(pFraction != NULL);
155  // for now, we see if it can be converted to a string without crashing
156  std::string s = pFraction->toString();
157  delete pNormal;
158 }
159 
160 void test_node_conversion::test_0005()
161 {
162  std::string expr("A*(1/B)");
163  CEvaluationTree* pTree = new CEvaluationTree("tree", NULL, CEvaluationTree::Function);
164  pTree->setInfix(expr);
165  CNormalBase* pNormal = createNormalRepresentation(pTree->getRoot());
166  delete pTree;
167  CNormalFraction* pFraction = dynamic_cast<CNormalFraction*>(pNormal);
168  CPPUNIT_ASSERT(pFraction != NULL);
169  // for now, we see if it can be converted to a string without crashing
170  std::string s = pFraction->toString();
171  delete pNormal;
172 }
CNormalSum & getNumerator()
CCopasiNode< Data > * getSibling()
Definition: CCopasiNode.h:353
const Type & getType() const
CEvaluationNode * convertToCEvaluationNode(const CNormalFraction &fraction)
static Type subType(const Type &type)
bool checkDenominatorOne() const
virtual std::string toString() const
CNormalFraction * createNormalRepresentation(const CEvaluationNode *node)
static void init(int argc, char *argv[], const bool &withGui=false)
CCopasiNode< Data > * getChild()
Definition: CCopasiNode.h:210
CEvaluationNode * getRoot()
virtual bool setInfix(const std::string &infix)