Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
daa5a857
Commit
daa5a857
authored
Sep 05, 2013
by
Dominik Charousset
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved pattern matching unit test
parent
622f6303
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
327 additions
and
297 deletions
+327
-297
unit_testing/test_match.cpp
unit_testing/test_match.cpp
+327
-297
No files found.
unit_testing/test_match.cpp
View file @
daa5a857
...
@@ -47,11 +47,7 @@ void foobar(index_token<Pos, Max>) {
...
@@ -47,11 +47,7 @@ void foobar(index_token<Pos, Max>) {
foobar
(
index_token
<
Pos
+
1
,
Max
>
{});
foobar
(
index_token
<
Pos
+
1
,
Max
>
{});
}
}
void
test_simple_guards
()
{
int
main
()
{
CPPA_TEST
(
test_match
);
/* test guard evaluation */
{
// ascending values
auto
ascending
=
[](
int
a
,
int
b
,
int
c
)
->
bool
{
auto
ascending
=
[](
int
a
,
int
b
,
int
c
)
->
bool
{
return
a
<
b
&&
b
<
c
;
return
a
<
b
&&
b
<
c
;
};
};
...
@@ -79,8 +75,9 @@ int main() {
...
@@ -79,8 +75,9 @@ int main() {
CPPA_CHECK
(
ge_invoke
(
expr4
,
string
(
"-h"
)));
CPPA_CHECK
(
ge_invoke
(
expr4
,
string
(
"-h"
)));
CPPA_CHECK
(
ge_invoke
(
expr4
,
string
(
"--help"
)));
CPPA_CHECK
(
ge_invoke
(
expr4
,
string
(
"--help"
)));
CPPA_CHECK
(
!
ge_invoke
(
expr4
,
string
(
"-g"
)));
CPPA_CHECK
(
!
ge_invoke
(
expr4
,
string
(
"-g"
)));
}
}
/* guard expressions on containers */
{
void
test_container_guards
()
{
auto
expr5
=
_x1
.
starts_with
(
"--"
);
auto
expr5
=
_x1
.
starts_with
(
"--"
);
CPPA_CHECK
(
ge_invoke
(
expr5
,
string
(
"--help"
)));
CPPA_CHECK
(
ge_invoke
(
expr5
,
string
(
"--help"
)));
CPPA_CHECK
(
!
ge_invoke
(
expr5
,
string
(
"-help"
)));
CPPA_CHECK
(
!
ge_invoke
(
expr5
,
string
(
"-help"
)));
...
@@ -114,8 +111,9 @@ int main() {
...
@@ -114,8 +111,9 @@ int main() {
CPPA_CHECK
(
!
ge_invoke
(
expr21
,
expr21_vec_b
));
CPPA_CHECK
(
!
ge_invoke
(
expr21
,
expr21_vec_b
));
auto
expr22
=
_x1
.
empty
()
&&
_x2
.
not_empty
();
auto
expr22
=
_x1
.
empty
()
&&
_x2
.
not_empty
();
CPPA_CHECK
(
ge_invoke
(
expr22
,
std
::
string
(
""
),
std
::
string
(
"abc"
)));
CPPA_CHECK
(
ge_invoke
(
expr22
,
std
::
string
(
""
),
std
::
string
(
"abc"
)));
}
}
/* user-defined expressions */
{
void
test_user_defined_expressions
()
{
int
ival
=
5
;
int
ival
=
5
;
auto
expr8
=
_x1
==
ival
;
auto
expr8
=
_x1
==
ival
;
auto
expr9
=
_x1
==
std
::
ref
(
ival
);
auto
expr9
=
_x1
==
std
::
ref
(
ival
);
...
@@ -146,8 +144,9 @@ int main() {
...
@@ -146,8 +144,9 @@ int main() {
CPPA_CHECK_EQUAL
((
ge_invoke
(
expr16_a
,
expr16_str
)),
expr16_str
.
size
());
CPPA_CHECK_EQUAL
((
ge_invoke
(
expr16_a
,
expr16_str
)),
expr16_str
.
size
());
expr16_str
.
front
()
=
'_'
;
expr16_str
.
front
()
=
'_'
;
CPPA_CHECK_EQUAL
((
ge_invoke
(
expr16_b
,
expr16_str
)),
false
);
CPPA_CHECK_EQUAL
((
ge_invoke
(
expr16_b
,
expr16_str
)),
false
);
}
}
/* user-defined expressions using 'gref' */
{
void
test_gref
()
{
int
expr17_value
=
42
;
int
expr17_value
=
42
;
auto
expr17
=
gref
(
expr17_value
)
==
42
;
auto
expr17
=
gref
(
expr17_value
)
==
42
;
CPPA_CHECK_EQUAL
(
ge_invoke
(
expr17
),
true
);
CPPA_CHECK_EQUAL
(
ge_invoke
(
expr17
),
true
);
...
@@ -179,8 +178,9 @@ int main() {
...
@@ -179,8 +178,9 @@ int main() {
};
};
CPPA_CHECK
(
expr21
(
make_any_tuple
(
atom
(
"add"
),
1
,
2
))
==
make_any_tuple
(
3
));
CPPA_CHECK
(
expr21
(
make_any_tuple
(
atom
(
"add"
),
1
,
2
))
==
make_any_tuple
(
3
));
CPPA_CHECK
(
!
expr21
(
make_any_tuple
(
atom
(
"sub"
),
1
,
2
)));
CPPA_CHECK
(
!
expr21
(
make_any_tuple
(
atom
(
"sub"
),
1
,
2
)));
}
}
/* test 'match' function */
{
void
test_match_function
()
{
auto
res0
=
match
(
5
)
(
auto
res0
=
match
(
5
)
(
on_arg_match
.
when
(
_x1
<
6
)
>>
[
&
](
int
i
)
{
on_arg_match
.
when
(
_x1
<
6
)
>>
[
&
](
int
i
)
{
CPPA_CHECK_EQUAL
(
i
,
5
);
CPPA_CHECK_EQUAL
(
i
,
5
);
...
@@ -219,8 +219,9 @@ int main() {
...
@@ -219,8 +219,9 @@ int main() {
}
}
);
);
CPPA_CHECK_EQUAL
(
res5
,
"C"
);
CPPA_CHECK_EQUAL
(
res5
,
"C"
);
}
}
/* test 'match_each' */
{
void
test_match_each
()
{
string
sum
;
string
sum
;
vector
<
string
>
sum_args
=
{
"-h"
,
"--version"
,
"-wtf"
};
vector
<
string
>
sum_args
=
{
"-h"
,
"--version"
,
"-wtf"
};
auto
iter1
=
match_each
(
begin
(
sum_args
),
end
(
sum_args
))
(
auto
iter1
=
match_each
(
begin
(
sum_args
),
end
(
sum_args
))
(
...
@@ -246,8 +247,9 @@ int main() {
...
@@ -246,8 +247,9 @@ int main() {
on
(
"-h"
)
>>
[]
{
}
on
(
"-h"
)
>>
[]
{
}
);
);
CPPA_CHECK
(
iter2
==
(
begin
(
sum_args
)
+
1
));
CPPA_CHECK
(
iter2
==
(
begin
(
sum_args
)
+
1
));
}
}
/* test match_stream */
{
void
test_match_stream
()
{
bool
success
=
false
;
bool
success
=
false
;
istringstream
iss
(
"hello world"
);
istringstream
iss
(
"hello world"
);
success
=
match_stream
<
string
>
(
iss
)
(
success
=
match_stream
<
string
>
(
iss
)
(
...
@@ -279,15 +281,15 @@ int main() {
...
@@ -279,15 +281,15 @@ int main() {
}
}
);
);
CPPA_CHECK_EQUAL
(
success
,
true
);
CPPA_CHECK_EQUAL
(
success
,
true
);
}
}
// utility macro to identify invoked functors
void
test_behavior
()
{
std
::
string
last_invoked_fun
;
std
::
string
last_invoked_fun
;
# define bhvr_check(pf, tup, expected_result, str) { \
# define bhvr_check(pf, tup, expected_result, str) { \
last_invoked_fun = ""; \
last_invoked_fun = ""; \
CPPA_CHECK_EQUAL(static_cast<bool>(pf(tup)), expected_result); \
CPPA_CHECK_EQUAL(static_cast<bool>(pf(tup)), expected_result); \
CPPA_CHECK_EQUAL(last_invoked_fun, str); \
CPPA_CHECK_EQUAL(last_invoked_fun, str); \
}
}
/* test if match hints are evaluated properly */
{
behavior
bhvr1
{
behavior
bhvr1
{
on
<
int
>
()
>>
[
&
](
int
i
)
->
match_hint
{
on
<
int
>
()
>>
[
&
](
int
i
)
->
match_hint
{
last_invoked_fun
=
"<int>@1"
;
last_invoked_fun
=
"<int>@1"
;
...
@@ -307,8 +309,6 @@ int main() {
...
@@ -307,8 +309,6 @@ int main() {
bhvr_check
(
bhvr1
,
make_any_tuple
(
24
),
true
,
"<int>@1"
);
bhvr_check
(
bhvr1
,
make_any_tuple
(
24
),
true
,
"<int>@1"
);
bhvr_check
(
bhvr1
,
make_any_tuple
(
2.
f
),
true
,
"<float>@2"
);
bhvr_check
(
bhvr1
,
make_any_tuple
(
2.
f
),
true
,
"<float>@2"
);
bhvr_check
(
bhvr1
,
make_any_tuple
(
""
),
true
,
"<*>@4"
);
bhvr_check
(
bhvr1
,
make_any_tuple
(
""
),
true
,
"<*>@4"
);
}
/* test or_else concatenation */
{
partial_function
pf0
{
partial_function
pf0
{
on
<
int
,
int
>
()
>>
[
&
]
{
last_invoked_fun
=
"<int, int>@1"
;
},
on
<
int
,
int
>
()
>>
[
&
]
{
last_invoked_fun
=
"<int, int>@1"
;
},
on
<
float
>
()
>>
[
&
]
{
last_invoked_fun
=
"<float>@2"
;
}
on
<
float
>
()
>>
[
&
]
{
last_invoked_fun
=
"<float>@2"
;
}
...
@@ -331,8 +331,9 @@ int main() {
...
@@ -331,8 +331,9 @@ int main() {
auto
pf13
=
pf11
.
or_else
(
pf12
);
auto
pf13
=
pf11
.
or_else
(
pf12
);
bhvr_check
(
pf13
,
make_any_tuple
(
42
),
true
,
"<int>@1"
);
bhvr_check
(
pf13
,
make_any_tuple
(
42
),
true
,
"<int>@1"
);
bhvr_check
(
pf13
,
make_any_tuple
(
42.24
f
),
true
,
"<float>@2"
);
bhvr_check
(
pf13
,
make_any_tuple
(
42.24
f
),
true
,
"<float>@2"
);
}
}
/* check result of pattern matching */
{
void
test_pattern_matching
()
{
auto
res
=
match
(
make_any_tuple
(
42
,
4.2
f
))
(
auto
res
=
match
(
make_any_tuple
(
42
,
4.2
f
))
(
on
(
42
,
arg_match
)
>>
[](
double
d
)
{
on
(
42
,
arg_match
)
>>
[](
double
d
)
{
return
d
;
return
d
;
...
@@ -351,6 +352,35 @@ int main() {
...
@@ -351,6 +352,35 @@ int main() {
}
}
);
);
CPPA_CHECK
(
!
res2
);
CPPA_CHECK
(
!
res2
);
}
void
test_wildcards
()
{
auto
expr
=
(
on
(
any_vals
,
arg_match
)
>>
[](
int
a
,
int
b
)
{
cout
<<
"invoked: "
<<
a
<<
", "
<<
b
<<
endl
;
}
}
);
CPPA_CHECK
(
expr
(
make_any_tuple
(
1
,
2
,
3
)));
CPPA_CHECK
(
expr
(
make_any_tuple
(
1
,
2
)));
partial_function
pf
=
expr
;
CPPA_CHECK
(
pf
(
make_any_tuple
(
1
,
2
)));
CPPA_CHECK
(
pf
(
make_any_tuple
(
1
,
2
,
3
)));
behavior
bhvr
=
expr
;
CPPA_CHECK
(
bhvr
(
make_any_tuple
(
1
,
2
)));
CPPA_CHECK
(
bhvr
(
make_any_tuple
(
1
,
2
,
3
)));
}
int
main
()
{
CPPA_TEST
(
test_match
);
test_wildcards
();
test_simple_guards
();
test_container_guards
();
test_user_defined_expressions
();
test_gref
();
test_match_function
();
test_match_each
();
test_match_stream
();
test_behavior
();
test_pattern_matching
();
return
CPPA_TEST_RESULT
();
return
CPPA_TEST_RESULT
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment