Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-xor
Commits
b1cd035c
Commit
b1cd035c
authored
Mar 26, 2020
by
Petar Maymounkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test checks that tries meet the invariant condition.
parent
c63e67eb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
trie/add_test.go
trie/add_test.go
+4
-0
trie/check.go
trie/check.go
+25
-0
trie/intersect_test.go
trie/intersect_test.go
+24
-0
No files found.
trie/add_test.go
View file @
b1cd035c
...
...
@@ -16,6 +16,8 @@ func TestMutableAndImmutableAddSame(t *testing.T) {
mut
.
Add
(
k
)
immut
=
Add
(
immut
,
k
)
}
mut
.
CheckInvariant
()
immut
.
CheckInvariant
()
if
!
Equal
(
mut
,
immut
)
{
t
.
Errorf
(
"mutable trie %v differs from immutable trie %v"
,
mut
,
immut
)
}
...
...
@@ -28,12 +30,14 @@ func TestAddIsOrderIndependent(t *testing.T) {
for
_
,
k
:=
range
s
.
Keys
{
base
.
Add
(
k
)
}
base
.
CheckInvariant
()
for
j
:=
0
;
j
<
100
;
j
++
{
perm
:=
rand
.
Perm
(
len
(
s
.
Keys
))
reordered
:=
New
()
for
i
:=
range
s
.
Keys
{
reordered
.
Add
(
s
.
Keys
[
perm
[
i
]])
}
reordered
.
CheckInvariant
()
if
!
Equal
(
base
,
reordered
)
{
t
.
Errorf
(
"trie %v differs from trie %v"
,
base
,
reordered
)
}
...
...
trie/check.go
0 → 100644
View file @
b1cd035c
package
trie
// CheckInvariant panics of the trie does not meet its invariant.
func
(
trie
*
Trie
)
CheckInvariant
()
{
switch
{
case
trie
.
IsLeaf
()
:
return
default
:
if
trie
.
IsEmpty
()
{
b0
,
b1
:=
trie
.
Branch
[
0
],
trie
.
Branch
[
1
]
b0
.
CheckInvariant
()
b1
.
CheckInvariant
()
switch
{
case
b0
.
IsEmptyLeaf
()
&&
b1
.
IsEmptyLeaf
()
:
panic
(
"intermediate node with two empty leaves"
)
case
b0
.
IsEmptyLeaf
()
&&
b1
.
IsNonEmptyLeaf
()
:
panic
(
"intermediate node with one empty leaf"
)
case
b0
.
IsNonEmptyLeaf
()
&&
b1
.
IsEmptyLeaf
()
:
panic
(
"intermediate node with one empty leaf"
)
}
}
else
{
panic
(
"intermediate node with a key"
)
}
}
}
trie/intersect_test.go
View file @
b1cd035c
...
...
@@ -32,13 +32,16 @@ func testIntersect(t *testing.T, sample *testIntersectSample) {
for
_
,
l
:=
range
sample
.
LeftKeys
{
left
.
Add
(
l
)
}
left
.
CheckInvariant
()
for
_
,
r
:=
range
sample
.
RightKeys
{
right
.
Add
(
r
)
}
right
.
CheckInvariant
()
for
_
,
s
:=
range
setIntersect
(
sample
.
LeftKeys
,
sample
.
RightKeys
)
{
expected
.
Add
(
s
)
}
got
:=
Intersect
(
left
,
right
)
got
.
CheckInvariant
()
if
!
Equal
(
expected
,
got
)
{
t
.
Errorf
(
"intersection of %v and %v: expected %v, got %v"
,
sample
.
LeftKeys
,
sample
.
RightKeys
,
expected
,
got
)
...
...
@@ -113,6 +116,27 @@ var testIntersectJSONSamples = []string{
"gAlMTjoCy6ZDZFN/0okF65fxscCLVxnQhlJsyfp6uWU=",
"qAISv6yZjs3WWlDC89iJUSdq45F0D/1y9fLnsPvavdA="
]
}
`
,
`
{
"LeftKeys": [
"BXmU8txOqn8ExHzXuXRtHm2XM99uD8lsgPo8OdDcNYE=",
"hVzcamVYfIqs4IlrVIM1qRalqTh8OMrlAeqwgJTI2xo=",
"JbILHEM3RcA+ksq0BvU+9Zfc+jnpsxPUQLe9lrHqBwc=",
"VUeFiK5V64F8G2rvnIyoopfOzICF0h79FmeiLQqrVAI=",
"tYRdsKlUTbTXOpgVjUZtzh2DRG0e5nPXIrkN60PI5GE=",
"LatyclJiSPEaCoLxbabddv7Rqrsy+J1hf2Pd9BmmN1U=",
"XX0wXrGF4IytkKmStxesXOiGFK+dm5ran6lWu7xNhIw=",
"PcZb8TBEHhEqpFfaRWyhit3Uc03895uOkMgiiBgW9Uk="
],
"RightKeys": [
"BXmU8txOqn8ExHzXuXRtHm2XM99uD8lsgPo8OdDcNYE=",
"hVzcamVYfIqs4IlrVIM1qRalqTh8OMrlAeqwgJTI2xo=",
"VUeFiK5V64F8G2rvnIyoopfOzICF0h79FmeiLQqrVAI=",
"tYRdsKlUTbTXOpgVjUZtzh2DRG0e5nPXIrkN60PI5GE=",
"XX0wXrGF4IytkKmStxesXOiGFK+dm5ran6lWu7xNhIw="
]
}
`
,
}
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