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
47ed0625
Commit
47ed0625
authored
Jun 13, 2012
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented an optimization in new validation implementation.
parent
f6d30399
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
26 deletions
+51
-26
src/validation.py
src/validation.py
+22
-6
tests/rulestestcase.py
tests/rulestestcase.py
+13
-4
tests/test_validation.py
tests/test_validation.py
+16
-16
No files found.
src/validation.py
View file @
47ed0625
...
...
@@ -12,7 +12,7 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with TRS. If not, see <http://www.gnu.org/licenses/>.
from
parser
import
Parser
from
parser
import
Parser
,
MAXIMUM_REWRITE_STEPS
from
possibilities
import
apply_suggestion
from
strategy
import
find_possibilities
from
tests.parser
import
ParserWrapper
...
...
@@ -26,7 +26,7 @@ VALIDATE_ERROR = 3
def
validate
(
a
,
b
):
"""
Validate that a =>
*
b.
Validate that a => b.
"""
parser
=
ParserWrapper
(
Parser
)
...
...
@@ -34,13 +34,29 @@ def validate(a, b):
a
=
parser
.
run
([
a
])
b
=
parser
.
run
([
b
])
if
a
.
equals
(
b
):
return
VALIDATION_NOPROGRESS
# Evaluate a and b, counting the number of steps
# TODO: Optimization: if b is encountered while evaluating a, return
# VALIDATION_SUCCESS
# Optimization: if b is encountered while evaluating a, return
parser
.
set_root_node
(
a
)
A
,
a_steps
=
parser
.
rewrite_and_count_all
()
A
=
a
a_steps
=
0
for
i
in
xrange
(
MAXIMUM_REWRITE_STEPS
):
obj
=
parser
.
rewrite
()
if
not
obj
:
break
# If b is some reduction of a, it will be detected here
if
obj
.
equals
(
b
):
return
VALIDATE_SUCCESS
A
=
obj
a_steps
+=
1
if
not
a
:
if
not
A
:
return
VALIDATE_ERROR
parser
.
set_root_node
(
b
)
...
...
tests/rulestestcase.py
View file @
47ed0625
...
...
@@ -17,7 +17,8 @@ import doctest
from
src.node
import
ExpressionNode
from
src.parser
import
Parser
from
src.validation
import
validate
from
src.validation
import
validate
,
VALIDATE_SUCCESS
,
\
VALIDATE_FAILURE
,
VALIDATE_NOPROGRESS
from
tests.parser
import
ParserWrapper
...
...
@@ -92,6 +93,14 @@ class RulesTestCase(unittest.TestCase):
raise
def
assertValidate
(
self
,
exp
,
result
):
self
.
assertTrue
(
validate
(
exp
,
result
),
'Validation failed: %s !=> %s'
)
def
assertValidateSuccess
(
self
,
a
,
b
):
self
.
assertEqual
(
validate
(
a
,
b
),
VALIDATE_SUCCESS
,
'Validation failed: %s !=> %s'
%
(
a
,
b
))
def
assertValidateFailure
(
self
,
a
,
b
):
self
.
assertEqual
(
validate
(
a
,
b
),
VALIDATE_FAILURE
,
'Validation dit not fail: %s => %s'
%
(
a
,
b
))
def
assertValidateNoprogress
(
self
,
a
,
b
):
self
.
assertEqual
(
validate
(
a
,
b
),
VALIDATE_NOPROGRESS
,
'Validation '
'did detect progress or failed for %s => %s'
%
(
a
,
b
))
tests/test_validation.py
View file @
47ed0625
...
...
@@ -12,43 +12,43 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with TRS. If not, see <http://www.gnu.org/licenses/>.
from
unittest
import
TestCase
from
rulestestcase
import
Rules
TestCase
from
src.validation
import
validate
,
VALIDATE_SUCCESS
as
OK
,
\
VALIDATE_FAILURE
as
FAIL
,
VALIDATE_NOPROGRESS
as
NP
class
TestValidation
(
TestCase
):
class
TestValidation
(
Rules
TestCase
):
def
test_simple_success
(
self
):
self
.
assert
Equal
(
validate
(
'3a + a'
,
'4a'
),
OK
)
self
.
assert
ValidateSuccess
(
'3a + a'
,
'4a'
)
def
test_simple_failure
(
self
):
self
.
assert
Equal
(
validate
(
'3a + a'
,
'4a + 1'
),
FAIL
)
self
.
assert
ValidateFailure
(
'3a + a'
,
'4a + 1'
)
def
test_intermediate_success
(
self
):
self
.
assert
Equal
(
validate
(
'3a + a + b + 2b'
,
'4a + 3b'
),
OK
)
self
.
assert
Equal
(
validate
(
'a / b / (c / d)'
,
'(ad) / (bc)'
),
OK
)
self
.
assert
ValidateSuccess
(
'3a + a + b + 2b'
,
'4a + 3b'
)
self
.
assert
ValidateSuccess
(
'a / b / (c / d)'
,
'(ad) / (bc)'
)
def
test_intermediate_failure
(
self
):
self
.
assert
Equal
(
validate
(
'3a + a + b + 2b'
,
'4a + 4b'
),
FAIL
)
self
.
assert
ValidateFailure
(
'3a + a + b + 2b'
,
'4a + 4b'
)
def
test_success
(
self
):
self
.
assert
Equal
(
validate
(
'x^2 + x - 2x^2 + 3x + 1'
,
'x^2 + 4x - 2x^2 + 1'
)
,
OK
)
self
.
assert
ValidateSuccess
(
'x^2 + x - 2x^2 + 3x + 1'
,
'x^2 + 4x - 2x^2 + 1'
)
def
test_indefinite_integral
(
self
):
self
.
assert
Equal
(
validate
(
'int_2^4 x^2'
,
'4^3/3 - 2^3/3'
),
OK
)
self
.
assert
ValidateSuccess
(
'int_2^4 x^2'
,
'4^3/3 - 2^3/3'
)
def
test_advanced_failure
(
self
):
self
.
assert
Equal
(
validate
(
'(x-1)^3+(x-1)^3'
,
'4a+4b'
),
FAIL
)
self
.
assert
ValidateFailure
(
'(x-1)^3+(x-1)^3'
,
'4a+4b'
)
def
test_sphere_volume
(
self
):
self
.
assert
Equal
(
validate
(
'int_(-r)^(r) pi * (r^2 - x^2) dx'
,
'4 / 3 * pi * r ^ 3'
)
,
OK
)
self
.
assert
ValidateSuccess
(
'int_(-r)^(r) pi * (r^2 - x^2) dx'
,
'4 / 3 * pi * r ^ 3'
)
def
test_sphere_volume_alternative_notation
(
self
):
self
.
assert
Equal
(
validate
(
'int_(-r)^(r) pi * (r^2 - x^2) dx'
,
'4 * pi * r ^ 3 / 3'
)
,
OK
)
self
.
assert
ValidateSuccess
(
'int_(-r)^(r) pi * (r^2 - x^2) dx'
,
'4 * pi * r ^ 3 / 3'
)
def
test_noprogress_simple
(
self
):
self
.
assert
Equal
(
validate
(
'2 + 2'
,
'3 + 1'
),
NP
)
self
.
assert
ValidateNoprogress
(
'2 + 2'
,
'3 + 1'
)
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