From 9179ebe4cfb9227fa868f7e2714bc7ca101e4238 Mon Sep 17 00:00:00 2001
From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152>
Date: Tue, 28 Apr 2009 16:21:39 +0000
Subject: [PATCH] trunk preprocessor: use assert() function at several places

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2642 ac1d8469-bf42-47a9-8791-bf33cf982152
---
 ComputingTasks.cc     |  7 ++-----
 DataTree.cc           | 19 ++++---------------
 DynamicModel.cc       |  7 ++-----
 ExprNode.cc           | 27 ++++++---------------------
 ModFile.cc            |  2 +-
 ModelTree.cc          |  9 ++-------
 NumericalConstants.cc | 18 +++++-------------
 7 files changed, 22 insertions(+), 67 deletions(-)

diff --git a/ComputingTasks.cc b/ComputingTasks.cc
index 782c3832..a96bbe6f 100644
--- a/ComputingTasks.cc
+++ b/ComputingTasks.cc
@@ -18,6 +18,7 @@
  */
 
 #include <cstdlib>
+#include <cassert>
 #include <iostream>
 #include <sstream>
 
@@ -897,11 +898,7 @@ PlannerObjectiveStatement::~PlannerObjectiveStatement()
 void
 PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct)
 {
-  if (model_tree->equation_number() != 1)
-    {
-      cerr << "ERROR: planer_objective: should have only one equation!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(model_tree->equation_number() == 1);
 }
 
 void
diff --git a/DataTree.cc b/DataTree.cc
index d7469dff..b240e5fb 100644
--- a/DataTree.cc
+++ b/DataTree.cc
@@ -18,6 +18,7 @@
  */
 
 #include <cstdlib>
+#include <cassert>
 #include <iostream>
 
 #include "DataTree.hh"
@@ -73,11 +74,7 @@ DataTree::AddVariableInternal(const string &name, int lag)
 NodeID
 DataTree::AddVariable(const string &name, int lag)
 {
-  if (lag != 0)
-    {
-      cerr << "DataTree::AddVariable: a non-zero lag is forbidden here!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(lag == 0);
   return AddVariableInternal(name, lag);
 }
 
@@ -422,11 +419,7 @@ DataTree::AddLocalVariable(const string &name, NodeID value) throw (LocalVariabl
 {
   int id = symbol_table.getID(name);
 
-  if (symbol_table.getType(id) != eModelLocalVariable)
-    {
-      cerr << "Symbol " << name << " is not a model local variable!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(symbol_table.getType(id) == eModelLocalVariable);
 
   // Throw an exception if symbol already declared
   map<int, NodeID>::iterator it = local_variables_table.find(id);
@@ -441,11 +434,7 @@ DataTree::AddUnknownFunction(const string &function_name, const vector<NodeID> &
 {
   int id = symbol_table.getID(function_name);
 
-  if (symbol_table.getType(id) != eUnknownFunction)
-    {
-      cerr << "Symbol " << function_name << " is not a function name!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(symbol_table.getType(id) == eUnknownFunction);
 
   return new UnknownFunctionNode(*this, id, arguments);
 }
diff --git a/DynamicModel.cc b/DynamicModel.cc
index 9a425b80..cdf43438 100644
--- a/DynamicModel.cc
+++ b/DynamicModel.cc
@@ -19,6 +19,7 @@
 
 #include <cmath>
 #include <cstdlib>
+#include <cassert>
 
 #include "DynamicModel.hh"
 
@@ -2153,11 +2154,7 @@ void
 DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
                             const eval_context_type &eval_context, bool no_tmp_terms)
 {
-  if (!jacobianExo && (hessian || thirdDerivatives || paramsDerivatives))
-    {
-      cerr << "DynamicModel::computingPass: computing 2nd or 3rd order derivatives imply computing 1st derivatives w.r. to exogenous" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(jacobianExo || !(hessian || thirdDerivatives || paramsDerivatives));
 
   // Computes dynamic jacobian columns
   computeDynJacobianCols(jacobianExo);
diff --git a/ExprNode.cc b/ExprNode.cc
index 170ab263..500abf2f 100644
--- a/ExprNode.cc
+++ b/ExprNode.cc
@@ -21,6 +21,7 @@
 #include <iterator>
 #include <algorithm>
 
+#include <cassert>
 #include <cmath>
 
 #include "ExprNode.hh"
@@ -186,11 +187,7 @@ VariableNode::VariableNode(DataTree &datatree_arg, int symb_id_arg, int lag_arg,
   datatree.variable_node_map[make_pair(symb_id, lag)] = this;
 
   // It makes sense to allow a lead/lag on parameters: during steady state calibration, endogenous and parameters can be swapped
-  if ((type == eModelLocalVariable || type == eModFileLocalVariable || type == eUnknownFunction) && lag != 0)
-    {
-      cerr << "Attempt to construct a VariableNode for local variable or unknown function with non-zero lead/lag" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(lag == 0 || (type != eModelLocalVariable && type != eModFileLocalVariable && type != eUnknownFunction));
 
   // Fill in non_null_derivatives
   switch(type)
@@ -359,11 +356,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
           output << "x" << LPAR(output_type) << i << RPAR(output_type);
           break;
         case oMatlabOutsideModel:
-          if (lag != 0)
-            {
-              cerr << "VariableNode::writeOutput: lag != 0 for exogenous variable outside model scope!" << endl;
-              exit(EXIT_FAILURE);
-            }
+          assert(lag == 0);
           output <<  "oo_.exo_steady_state" << "(" << i << ")";
           break;
         }
@@ -397,11 +390,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
           output << "x" << LPAR(output_type) << i << RPAR(output_type);
           break;
         case oMatlabOutsideModel:
-          if (lag != 0)
-            {
-              cerr << "VariableNode::writeOutput: lag != 0 for exogenous determistic variable outside model scope!" << endl;
-              exit(EXIT_FAILURE);
-            }
+          assert(lag == 0);
           output <<  "oo_.exo_det_steady_state" << "(" << tsid + 1 << ")";
           break;
         }
@@ -1439,7 +1428,6 @@ BinaryOpNode::collectEndogenous(set<pair<int, int> > &result) const
   arg2->collectEndogenous(result);
 }
 
-
 void
 BinaryOpNode::collectExogenous(set<pair<int, int> > &result) const
 {
@@ -1729,11 +1717,8 @@ void
 TrinaryOpNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
                            const temporary_terms_type &temporary_terms) const
 {
-  if (!OFFSET(output_type))
-    {
-      cerr << "TrinaryOpNode not implemented for C output" << endl;
-      exit(EXIT_FAILURE);
-    }
+  // TrinaryOpNode not implemented for C output
+  assert(OFFSET(output_type));
 
   // If current node is a temporary term
   temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<TrinaryOpNode *>(this));
diff --git a/ModFile.cc b/ModFile.cc
index 043e4024..e775d433 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -154,7 +154,7 @@ ModFile::computingPass(bool no_tmp_terms)
         {
           if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3)
             {
-              cerr << "Incorrect order option..." << endl;
+              cerr << "ERROR: Incorrect order option..." << endl;
               exit(EXIT_FAILURE);
             }
           bool hessian = mod_file_struct.order_option >= 2;
diff --git a/ModelTree.cc b/ModelTree.cc
index fbdef3ea..708ac4f5 100644
--- a/ModelTree.cc
+++ b/ModelTree.cc
@@ -17,7 +17,7 @@
  * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <cstdlib>
+#include <cassert>
 #include <iostream>
 
 #include "ModelTree.hh"
@@ -217,12 +217,7 @@ void
 ModelTree::addEquation(NodeID eq)
 {
   BinaryOpNode *beq = dynamic_cast<BinaryOpNode *>(eq);
-
-  if (beq == NULL || beq->get_op_code() != oEqual)
-    {
-      cerr << "ModelTree::addEquation: you didn't provide an equal node!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(beq != NULL && beq->get_op_code() == oEqual);
 
   equations.push_back(beq);
 }
diff --git a/NumericalConstants.cc b/NumericalConstants.cc
index 3c89d5f7..eacb3434 100644
--- a/NumericalConstants.cc
+++ b/NumericalConstants.cc
@@ -18,6 +18,7 @@
  */
 
 #include <cstdlib>
+#include <cassert>
 #include <iostream>
 
 #include "NumericalConstants.hh"
@@ -26,15 +27,11 @@ int
 NumericalConstants::AddConstant(const string &iConst)
 {
   map<string, int>::iterator iter = numConstantsIndex.find(iConst);
-  //cout << "iConst=" << iConst << "\n" ;
+
   if (iter != numConstantsIndex.end())
     return iter->second;
 
-  if (atof(iConst.c_str()) < 0)
-    {
-      cerr << "Can't handle a negative constant..!" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(atof(iConst.c_str()) >= 0);
 
   int id = (int) mNumericalConstants.size();
   mNumericalConstants.push_back(iConst);
@@ -45,13 +42,8 @@ NumericalConstants::AddConstant(const string &iConst)
 string
 NumericalConstants::get(int ID) const
 {
-  if (ID < (int) mNumericalConstants.size())
-    return mNumericalConstants[ID];
-  else
-    {
-      cerr << "Unknown constant" << endl;
-      exit(EXIT_FAILURE);
-    }
+  assert(ID >= 0 && ID < (int) mNumericalConstants.size());
+  return mNumericalConstants[ID];
 }
 
 double
-- 
GitLab