Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
ZEO
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
ZEO
Commits
1fc26e1d
Commit
1fc26e1d
authored
Jun 25, 2002
by
Tim Peters
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added non-trivial tests for weightedUnion and weightedIntersection, but
leaving negative weights alone for now.
parent
6f38370e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
6 deletions
+89
-6
src/BTrees/tests/testSetOps.py
src/BTrees/tests/testSetOps.py
+89
-6
No files found.
src/BTrees/tests/testSetOps.py
View file @
1fc26e1d
...
@@ -326,6 +326,8 @@ class TestImports(TestCase):
...
@@ -326,6 +326,8 @@ class TestImports(TestCase):
# Subclasses must set up (as class variables):
# Subclasses must set up (as class variables):
# weightedUnion, weightedIntersection
# weightedUnion, weightedIntersection
# builders -- sequence of constructors, taking items
# builders -- sequence of constructors, taking items
# union, intersection -- the module routines of those names
# mkbucket -- the module bucket builder
class
Weighted
(
TestCase
):
class
Weighted
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -336,6 +338,12 @@ class Weighted(TestCase):
...
@@ -336,6 +338,12 @@ class Weighted(TestCase):
self
.
Bs
=
[
make
(
self
.
Bitems
)
for
make
in
self
.
builders
]
self
.
Bs
=
[
make
(
self
.
Bitems
)
for
make
in
self
.
builders
]
self
.
emptys
=
[
make
([])
for
make
in
self
.
builders
]
self
.
emptys
=
[
make
([])
for
make
in
self
.
builders
]
weights
=
[]
for
w1
in
0
,
1
,
7
:
# -3, -1, 0, 1, 7: XXX negative weights buggy
for
w2
in
0
,
1
,
7
:
# -3, -1, 0, 1, 7: XXX negative weights buggy
weights
.
append
((
w1
,
w2
))
self
.
weights
=
weights
def
testBothNone
(
self
):
def
testBothNone
(
self
):
for
op
in
self
.
weightedUnion
,
self
.
weightedIntersection
:
for
op
in
self
.
weightedUnion
,
self
.
weightedIntersection
:
w
,
C
=
op
(
None
,
None
)
w
,
C
=
op
(
None
,
None
)
...
@@ -368,6 +376,78 @@ class Weighted(TestCase):
...
@@ -368,6 +376,78 @@ class Weighted(TestCase):
self
.
assert_
(
C
is
A
)
self
.
assert_
(
C
is
A
)
self
.
assertEqual
(
w
,
42
)
self
.
assertEqual
(
w
,
42
)
# If obj is a set, return a bucket with values all 1; else return obj.
def
_normalize
(
self
,
obj
):
if
isaset
(
obj
):
obj
=
self
.
mkbucket
(
zip
(
obj
.
keys
(),
[
1
]
*
len
(
obj
)))
return
obj
# Python simulation of weightedUnion.
def
_wunion
(
self
,
A
,
B
,
w1
=
1
,
w2
=
1
):
if
isaset
(
A
)
and
isaset
(
B
):
return
1
,
self
.
union
(
A
,
B
).
keys
()
A
=
self
.
_normalize
(
A
)
B
=
self
.
_normalize
(
B
)
result
=
[]
for
key
in
self
.
union
(
A
,
B
):
v1
=
A
.
get
(
key
,
0
)
v2
=
B
.
get
(
key
,
0
)
result
.
append
((
key
,
v1
*
w1
+
v2
*
w2
))
return
1
,
result
def
testUnion
(
self
):
inputs
=
self
.
As
+
self
.
Bs
+
self
.
emptys
for
A
in
inputs
:
for
B
in
inputs
:
want_w
,
want_s
=
self
.
_wunion
(
A
,
B
)
got_w
,
got_s
=
self
.
weightedUnion
(
A
,
B
)
self
.
assertEqual
(
got_w
,
want_w
)
if
isaset
(
got_s
):
self
.
assertEqual
(
got_s
.
keys
(),
want_s
)
else
:
self
.
assertEqual
(
got_s
.
items
(),
want_s
)
for
w1
,
w2
in
self
.
weights
:
want_w
,
want_s
=
self
.
_wunion
(
A
,
B
,
w1
,
w2
)
got_w
,
got_s
=
self
.
weightedUnion
(
A
,
B
,
w1
,
w2
)
self
.
assertEqual
(
got_w
,
want_w
)
if
isaset
(
got_s
):
self
.
assertEqual
(
got_s
.
keys
(),
want_s
)
else
:
self
.
assertEqual
(
got_s
.
items
(),
want_s
)
# Python simulation weightedIntersection.
def
_wintersection
(
self
,
A
,
B
,
w1
=
1
,
w2
=
1
):
if
isaset
(
A
)
and
isaset
(
B
):
return
w1
+
w2
,
self
.
intersection
(
A
,
B
).
keys
()
A
=
self
.
_normalize
(
A
)
B
=
self
.
_normalize
(
B
)
result
=
[]
for
key
in
self
.
intersection
(
A
,
B
):
result
.
append
((
key
,
A
[
key
]
*
w1
+
B
[
key
]
*
w2
))
return
1
,
result
def
testIntersection
(
self
):
inputs
=
self
.
As
+
self
.
Bs
+
self
.
emptys
for
A
in
inputs
:
for
B
in
inputs
:
want_w
,
want_s
=
self
.
_wintersection
(
A
,
B
)
got_w
,
got_s
=
self
.
weightedIntersection
(
A
,
B
)
self
.
assertEqual
(
got_w
,
want_w
)
if
isaset
(
got_s
):
self
.
assertEqual
(
got_s
.
keys
(),
want_s
)
else
:
self
.
assertEqual
(
got_s
.
items
(),
want_s
)
for
w1
,
w2
in
self
.
weights
:
want_w
,
want_s
=
self
.
_wintersection
(
A
,
B
,
w1
,
w2
)
got_w
,
got_s
=
self
.
weightedIntersection
(
A
,
B
,
w1
,
w2
)
self
.
assertEqual
(
got_w
,
want_w
)
if
isaset
(
got_s
):
self
.
assertEqual
(
got_s
.
keys
(),
want_s
)
else
:
self
.
assertEqual
(
got_s
.
items
(),
want_s
)
# Given a set builder (like OITreeSet or OISet), return a function that
# Given a set builder (like OITreeSet or OISet), return a function that
# takes a list of (key, value) pairs and builds a set out of the keys.
# takes a list of (key, value) pairs and builds a set out of the keys.
def
itemsToSet
(
setbuilder
):
def
itemsToSet
(
setbuilder
):
...
@@ -375,21 +455,24 @@ def itemsToSet(setbuilder):
...
@@ -375,21 +455,24 @@ def itemsToSet(setbuilder):
return
setbuilder
([
key
for
key
,
value
in
items
])
return
setbuilder
([
key
for
key
,
value
in
items
])
return
result
return
result
# 'thing' is a bucket, btree, set or treeset. Return true iff it's one of the
# latter two.
def
isaset
(
thing
):
return
not
hasattr
(
thing
,
'values'
)
class
TestWeightedII
(
Weighted
):
class
TestWeightedII
(
Weighted
):
from
BTrees.IIBTree
import
weightedUnion
,
weightedIntersection
from
BTrees.IIBTree
import
weightedUnion
,
weightedIntersection
from
BTrees.IIBTree
import
union
,
intersection
from
BTrees.IIBTree
import
IIBucket
as
mkbucket
builders
=
IIBucket
,
IIBTree
,
itemsToSet
(
IISet
),
itemsToSet
(
IITreeSet
)
builders
=
IIBucket
,
IIBTree
,
itemsToSet
(
IISet
),
itemsToSet
(
IITreeSet
)
class
TestWeightedOI
(
Weighted
):
class
TestWeightedOI
(
Weighted
):
from
BTrees.OIBTree
import
weightedUnion
,
weightedIntersection
from
BTrees.OIBTree
import
weightedUnion
,
weightedIntersection
from
BTrees.OIBTree
import
union
,
intersection
from
BTrees.OIBTree
import
OIBucket
as
mkbucket
builders
=
OIBucket
,
OIBTree
,
itemsToSet
(
OISet
),
itemsToSet
(
OITreeSet
)
builders
=
OIBucket
,
OIBTree
,
itemsToSet
(
OISet
),
itemsToSet
(
OITreeSet
)
# 'thing' is a bucket, btree, set or treeset. Return true iff it's one of the
# latter two.
def
isaset
(
thing
):
return
not
hasattr
(
thing
,
'values'
)
def
test_suite
():
def
test_suite
():
s
=
TestSuite
()
s
=
TestSuite
()
for
klass
in
(
TestIIMultiUnion
,
TestIOMultiUnion
,
for
klass
in
(
TestIIMultiUnion
,
TestIOMultiUnion
,
...
...
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