ct_common  1.0.1
Common library for combinatorial testing
src/ct_common/common/constraint_l_ivld.cpp
Go to the documentation of this file.
00001 //===----- ct_common/common/constraint_l_ivld.cpp ---------------*- C++ -*-===//
00002 //
00003 //                      The ct_common Library
00004 //
00005 // This file is distributed under the MIT license. See LICENSE for details.
00006 //
00007 //===----------------------------------------------------------------------===//
00008 //
00009 // This file contains the function definitions of class Constraint_L_IVLD
00010 //
00011 //===----------------------------------------------------------------------===//
00012 
00013 #include <ct_common/common/constraint_l_ivld.h>
00014 #include <ct_common/common/exp_a.h>
00015 #include <ct_common/common/exp_s.h>
00016 
00017 using namespace ct::common;
00018 
00019 Constraint_L_IVLD::Constraint_L_IVLD(void)
00020     : Constraint_L_Atom(), pid_(PID_BOUND) {
00021 }
00022 
00023 Constraint_L_IVLD::Constraint_L_IVLD(const Constraint_L_IVLD &from)
00024     : Constraint_L_Atom(from), pid_(from.pid_) {
00025 }
00026 
00027 Constraint_L_IVLD::~Constraint_L_IVLD(void) {
00028 }
00029 
00030 Constraint_L_IVLD &Constraint_L_IVLD::operator = (const Constraint_L_IVLD &right) {
00031   Constraint_L_Atom::operator =(right);
00032   this->pid_ = right.pid_;
00033   return *this;
00034 }
00035 
00036 std::string Constraint_L_IVLD::get_class_name(void) const {
00037   return Constraint_L_IVLD::class_name();
00038 }
00039 
00040 std::string Constraint_L_IVLD::class_name(void) {
00041   return "Constraint_L_IVLD";
00042 }
00043 
00044 EvalType_Bool Constraint_L_IVLD::Evaluate( const std::vector<boost::shared_ptr<ParamSpec> > &param_specs,
00045                                 const Assignment &assignment) const {
00046   if (!assignment.IsContainParam(this->pid_)) {
00047     CT_EXCEPTION("parameter not found in the test case");
00048     return EvalType_Bool(false, false);
00049   }
00050   // FIXME: need to reconsider the logics here, typically auto parameters should not be invalidated
00051   if (param_specs[this->pid_]->is_auto()) {
00052     for (std::size_t i = 0; i < param_specs[this->pid_]->auto_value_specs().size(); ++i) {
00053       boost::shared_ptr<Constraint> cond = boost::dynamic_pointer_cast<Constraint>(param_specs[this->pid_]->auto_value_specs()[i].first);
00054       if (cond) {
00055         EvalType_Bool cond_value;
00056         cond_value = cond->Evaluate(param_specs, assignment);
00057         if (cond_value.is_valid_ && cond_value.value_) {
00058           // condition met, taking the value
00059           boost::shared_ptr<EvalType> result = this->EvaluateAutoCaseExp(param_specs, assignment, param_specs[this->pid_]->auto_value_specs()[i].second);
00060           if (result->is_valid_) {
00061             return EvalType_Bool(false, true);
00062           }
00063           return EvalType_Bool(true, true);
00064         }
00065       } else {
00066         CT_EXCEPTION("Error: encountering invalid auto value condition");
00067         return EvalType_Bool(false, false);
00068       }
00069     }
00070     // FIXME: suppressing error
00071     //CT_EXCEPTION(std::string("Error: encountering unhandled auto value condition for parameter ")+ptr->get_param_name());
00072     return EvalType_Bool(true, true); // no conditions match
00073   }
00074   return EvalType_Bool(param_specs[this->pid_]->is_vid_invalid(assignment.GetValue(this->pid_)), true);
00075 }
00076 
00077 
00078 boost::shared_ptr<EvalType> Constraint_L_IVLD::EvaluateAutoCaseExp(
00079                       const std::vector<boost::shared_ptr<ParamSpec> > &param_specs,
00080                       const Assignment &assignment,
00081                       const boost::shared_ptr<TreeNode> &exp) const {
00082   if (boost::dynamic_pointer_cast<Constraint>(exp)) {
00083     boost::shared_ptr<Constraint> c_exp = boost::dynamic_pointer_cast<Constraint>(exp);
00084     return boost::shared_ptr<EvalType>(new EvalType_Bool(c_exp->Evaluate(param_specs, assignment)));
00085   } else if (boost::dynamic_pointer_cast<Exp_A>(exp)) {
00086     boost::shared_ptr<Exp_A> c_exp = boost::dynamic_pointer_cast<Exp_A>(exp);
00087     if (c_exp->get_type() == EAT_INT) {
00088       return boost::shared_ptr<EvalType>(new EvalType_Int(c_exp->EvaluateInt(param_specs, assignment)));
00089     } else if (c_exp->get_type() == EAT_DOUBLE) {
00090       return boost::shared_ptr<EvalType>(new EvalType_Double(c_exp->EvaluateDouble(param_specs, assignment)));
00091     } else {
00092       CT_EXCEPTION("unrecognized arithmetic expression type");
00093     }
00094   } else if (boost::dynamic_pointer_cast<Exp_S>(exp)) {
00095     boost::shared_ptr<Exp_S> c_exp = boost::dynamic_pointer_cast<Exp_S>(exp);
00096     return boost::shared_ptr<EvalType>(new EvalType_String(c_exp->Evaluate(param_specs, assignment)));
00097   } else {
00098     CT_EXCEPTION("unknown expression type");
00099     return boost::shared_ptr<EvalType>(new EvalType_Bool(false, false));
00100   }  
00101 }
00102 
00103 void Constraint_L_IVLD::inner_touch_leaf_pids(const std::vector<boost::shared_ptr<ParamSpec> > &param_specs,
00104                                               std::set<std::size_t> &pids_to_touch) const {
00105   // auto and aux parameters cannot be invalidated, so no need to check
00106   pids_to_touch.insert(this->pid_);
00107 }
00108 
00109 void Constraint_L_IVLD::dump(std::ostream &os, const std::vector<boost::shared_ptr<ParamSpec> > &param_specs) const {
00110   os << "#";
00111   os << "(";
00112   os << param_specs[this->pid_]->get_param_name();
00113   os << ")";
00114 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines