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
dms3
go-cidutil
Commits
e0fddd4a
Commit
e0fddd4a
authored
Aug 02, 2018
by
Steven Allen
Committed by
GitHub
Aug 02, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #63 from ipfs/tests/set
Add tests for Set type
parents
b2038702
0d4d6b33
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
0 deletions
+92
-0
set_test.go
set_test.go
+92
-0
No files found.
set_test.go
0 → 100644
View file @
e0fddd4a
package
cid
import
(
"crypto/rand"
"errors"
"testing"
mh
"github.com/multiformats/go-multihash"
)
func
makeRandomCid
(
t
*
testing
.
T
)
*
Cid
{
p
:=
make
([]
byte
,
256
)
_
,
err
:=
rand
.
Read
(
p
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
h
,
err
:=
mh
.
Sum
(
p
,
mh
.
SHA3
,
4
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
cid
:=
&
Cid
{
codec
:
7
,
version
:
1
,
hash
:
h
,
}
return
cid
}
func
TestSet
(
t
*
testing
.
T
)
{
cid
:=
makeRandomCid
(
t
)
cid2
:=
makeRandomCid
(
t
)
s
:=
NewSet
()
s
.
Add
(
cid
)
if
!
s
.
Has
(
cid
)
{
t
.
Error
(
"should have the CID"
)
}
if
s
.
Len
()
!=
1
{
t
.
Error
(
"should report 1 element"
)
}
keys
:=
s
.
Keys
()
if
len
(
keys
)
!=
1
||
!
keys
[
0
]
.
Equals
(
cid
)
{
t
.
Error
(
"key should correspond to Cid"
)
}
if
s
.
Visit
(
cid
)
{
t
.
Error
(
"visit should return false"
)
}
foreach
:=
[]
*
Cid
{}
foreachF
:=
func
(
c
*
Cid
)
error
{
foreach
=
append
(
foreach
,
c
)
return
nil
}
if
err
:=
s
.
ForEach
(
foreachF
);
err
!=
nil
{
t
.
Error
(
err
)
}
if
len
(
foreach
)
!=
1
{
t
.
Error
(
"ForEach should have visited 1 element"
)
}
foreachErr
:=
func
(
c
*
Cid
)
error
{
return
errors
.
New
(
"test"
)
}
if
err
:=
s
.
ForEach
(
foreachErr
);
err
==
nil
{
t
.
Error
(
"Should have returned an error"
)
}
if
!
s
.
Visit
(
cid2
)
{
t
.
Error
(
"should have visited a new Cid"
)
}
if
s
.
Len
()
!=
2
{
t
.
Error
(
"len should be 2 now"
)
}
s
.
Remove
(
cid2
)
if
s
.
Len
()
!=
1
{
t
.
Error
(
"len should be 1 now"
)
}
}
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