Skip to content
Snippets Groups Projects
Commit afe988d4 authored by Adam Wardyński's avatar Adam Wardyński
Browse files

Adding Constant class - operator returning constant value

parent 607d1c73
Branches
No related merge requests found
#ifndef CONSTANT_H
#define CONSTANT_H
#include <boost/scoped_ptr.hpp>
#include <boost/concept_check.hpp>
#include <libwccl/ops/functions.h>
namespace Wccl {
/**
* Functional realisation of constant value of a given type
*/
template<class TRet>
class Constant : public Function<TRet> {
BOOST_CONCEPT_ASSERT((boost::CopyConstructible<TRet>));
public:
/*
* Constant function holds specific value to return when applying it
*/
Constant(const TRet& value)
: value_(new TRet(value))
{
BOOST_ASSERT(value_);
}
/**
* Operator name for constant is string representation of its value
*/
virtual const std::string operator_name(const Corpus2::Tagset& tagset) const {
return value_->to_string(tagset);
}
/**
* Operator name for constant is string representation of its value,
* tagset-independent in this case
*/
virtual const std::string raw_operator_name() const {
return value_->to_raw_string();
}
protected :
typedef FunctionBase::BaseRetValPtr BaseRetValPtr ;
/**
* Applying Constant function returns the held value of a constant
*/
virtual BaseRetValPtr apply_internal(const SentenceContext&) const {
return BaseRetValPtr (new TRet(*value_));
}
private:
const boost::scoped_ptr<const TRet> value_;
};
} /* end ns Wccl */
#endif // CONSTANT_H
#include <boost/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <libwccl/ops/constant.h>
#include <libwccl/variables.h>
#include <libwccl/values/bool.h>
#include <libwccl/sentencecontext.h>
using namespace Wccl;
BOOST_AUTO_TEST_SUITE(constant)
struct BoolFix
{
BoolFix()
: sc(boost::make_shared<Corpus2::Sentence>()),
tagset(),
true_value(true),
false_value(false),
true_constant(true_value),
false_constant(false_value)
{
}
SentenceContext sc;
Corpus2::Tagset tagset;
Bool true_value;
Bool false_value;
Constant<Bool> true_constant;
Constant<Bool> false_constant;
};
BOOST_FIXTURE_TEST_CASE(bool_apply, BoolFix)
{
BOOST_CHECK_EQUAL(true, true_value.get_value());
BOOST_CHECK_EQUAL(true, true_constant.apply(sc)->get_value());
BOOST_CHECK_EQUAL(false, false_value.get_value());
BOOST_CHECK_EQUAL(false, false_constant.apply(sc)->get_value());
}
BOOST_FIXTURE_TEST_CASE(bool_to_string, BoolFix)
{
BOOST_CHECK_EQUAL("true", true_value.to_string(tagset));
BOOST_CHECK_EQUAL(true_value.to_string(tagset), true_constant.to_string(tagset));
BOOST_CHECK_EQUAL("false", false_value.to_string(tagset));
BOOST_CHECK_EQUAL(false_value.to_string(tagset), false_constant.to_string(tagset));
}
BOOST_FIXTURE_TEST_CASE(bool_to_raw_string, BoolFix)
{
BOOST_CHECK_EQUAL("true", true_value.to_raw_string());
BOOST_CHECK_EQUAL(true_value.to_raw_string(), true_constant.to_raw_string());
BOOST_CHECK_EQUAL("false", false_value.to_raw_string());
BOOST_CHECK_EQUAL(false_value.to_raw_string(), false_constant.to_raw_string());
}
BOOST_FIXTURE_TEST_CASE(bool_value_preserved, BoolFix)
{
boost::shared_ptr<Bool> v = true_constant.apply(sc);
v->set_value(false);
BOOST_CHECK_EQUAL(true, true_constant.apply(sc)->get_value());
v = false_constant.apply(sc);
v->set_value(true);
BOOST_CHECK_EQUAL(false, false_constant.apply(sc)->get_value());
}
BOOST_AUTO_TEST_SUITE_END()
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment