Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
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
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
ee315bc7
Commit
ee315bc7
authored
Jan 23, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added negation checks to Node.
parent
45fcaca5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
2 deletions
+39
-2
src/node.py
src/node.py
+18
-0
tests/test_node.py
tests/test_node.py
+21
-2
No files found.
src/node.py
View file @
ee315bc7
...
@@ -104,9 +104,27 @@ class ExpressionBase(object):
...
@@ -104,9 +104,27 @@ class ExpressionBase(object):
def
is_op
(
self
,
op
):
def
is_op
(
self
,
op
):
return
not
self
.
is_leaf
()
and
self
.
op
==
op
return
not
self
.
is_leaf
()
and
self
.
op
==
op
def
is_op_or_negated
(
self
,
op
):
if
self
.
is_leaf
():
return
False
if
self
.
op
==
OP_NEG
:
return
self
[
0
].
is_op
(
op
)
return
self
.
op
==
op
def
is_leaf
(
self
):
def
is_leaf
(
self
):
return
self
.
type
!=
TYPE_OPERATOR
return
self
.
type
!=
TYPE_OPERATOR
def
is_leaf_or_negated
(
self
):
if
self
.
is_leaf
():
return
True
if
self
.
is_op
(
OP_NEG
):
return
self
[
0
].
is_leaf
()
return
False
def
is_power
(
self
):
def
is_power
(
self
):
return
not
self
.
is_leaf
()
and
self
.
op
==
OP_POW
return
not
self
.
is_leaf
()
and
self
.
op
==
OP_POW
...
...
tests/test_node.py
View file @
ee315bc7
import
unittest
import
unittest
from
src.node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
from
src.node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
OP_ADD
from
tests.rulestestcase
import
tree
from
tests.rulestestcase
import
tree
...
@@ -23,7 +23,26 @@ class TestNode(unittest.TestCase):
...
@@ -23,7 +23,26 @@ class TestNode(unittest.TestCase):
<
N
(
'*'
,
L
(
3
),
N
(
'^'
,
L
(
'a'
),
L
(
'b'
))))
<
N
(
'*'
,
L
(
3
),
N
(
'^'
,
L
(
'a'
),
L
(
'b'
))))
self
.
assertFalse
(
N
(
'^'
,
L
(
'a'
),
L
(
3
))
<
N
(
'^'
,
L
(
'a'
),
L
(
2
)))
self
.
assertFalse
(
N
(
'^'
,
L
(
'a'
),
L
(
3
))
<
N
(
'^'
,
L
(
'a'
),
L
(
2
)))
def
test_is_power_true
(
self
):
def
test_is_op
(
self
):
self
.
assertTrue
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_op
(
OP_ADD
))
self
.
assertFalse
(
N
(
'-'
,
*
self
.
l
[:
2
]).
is_op
(
OP_ADD
))
def
test_is_op_or_negated
(
self
):
self
.
assertTrue
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_op_or_negated
(
OP_ADD
))
self
.
assertTrue
(
N
(
'-'
,
N
(
'+'
,
*
self
.
l
[:
2
])).
is_op_or_negated
(
OP_ADD
))
self
.
assertFalse
(
N
(
'-'
,
*
self
.
l
[:
2
]).
is_op_or_negated
(
OP_ADD
))
def
test_is_leaf
(
self
):
self
.
assertTrue
(
L
(
2
).
is_leaf
())
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_leaf
())
def
test_is_leaf_or_negated
(
self
):
self
.
assertTrue
(
L
(
2
).
is_leaf_or_negated
())
self
.
assertTrue
(
N
(
'-'
,
L
(
2
)).
is_leaf_or_negated
())
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_leaf_or_negated
())
self
.
assertFalse
(
N
(
'-'
,
N
(
'+'
,
*
self
.
l
[:
2
])).
is_leaf_or_negated
())
def
test_is_power
(
self
):
self
.
assertTrue
(
N
(
'^'
,
*
self
.
l
[:
2
]).
is_power
())
self
.
assertTrue
(
N
(
'^'
,
*
self
.
l
[:
2
]).
is_power
())
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_power
())
self
.
assertFalse
(
N
(
'+'
,
*
self
.
l
[:
2
]).
is_power
())
...
...
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