Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
WCCL
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Analysers
WCCL
Commits
afe988d4
Commit
afe988d4
authored
14 years ago
by
Adam Wardyński
Browse files
Options
Downloads
Patches
Plain Diff
Adding Constant class - operator returning constant value
parent
607d1c73
Branches
Branches containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
libwccl/ops/constant.h
+60
-0
60 additions, 0 deletions
libwccl/ops/constant.h
tests/constant_tests.cpp
+69
-0
69 additions, 0 deletions
tests/constant_tests.cpp
with
129 additions
and
0 deletions
libwccl/ops/constant.h
0 → 100644
+
60
−
0
View file @
afe988d4
#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
This diff is collapsed.
Click to expand it.
tests/constant_tests.cpp
0 → 100644
+
69
−
0
View file @
afe988d4
#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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment