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
mf
go-multiaddr
Commits
d462e342
Commit
d462e342
authored
Nov 29, 2018
by
Cole Brown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix yet another bug in VarintSize, add test
parent
1a8ba091
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
+18
-2
varint.go
varint.go
+2
-2
varint_test.go
varint_test.go
+16
-0
No files found.
varint.go
View file @
d462e342
...
...
@@ -11,7 +11,7 @@ func VarintSize(num int) int {
bits
:=
bits
.
Len
(
uint
(
num
))
q
,
r
:=
bits
/
7
,
bits
%
7
size
:=
q
if
r
>
0
{
if
r
>
0
||
size
==
0
{
size
++
}
return
size
...
...
@@ -19,7 +19,7 @@ func VarintSize(num int) int {
// CodeToVarint converts an integer to a varint-encoded []byte
func
CodeToVarint
(
num
int
)
[]
byte
{
buf
:=
make
([]
byte
,
bits
.
Len
(
uint
(
num
))
/
7
+
1
)
buf
:=
make
([]
byte
,
VarintSize
(
num
))
n
:=
binary
.
PutUvarint
(
buf
,
uint64
(
num
))
return
buf
[
:
n
]
}
...
...
varint_test.go
0 → 100644
View file @
d462e342
package
multiaddr
import
"testing"
func
expectVarint
(
t
*
testing
.
T
,
x
,
expected
int
)
{
size
:=
VarintSize
(
x
)
if
size
!=
expected
{
t
.
Fatalf
(
"expected varintsize of %d to be %d, got %d"
,
x
,
expected
,
size
)
}
}
func
TestVarintSize
(
t
*
testing
.
T
)
{
expectVarint
(
t
,
(
1
<<
7
)
-
1
,
1
)
expectVarint
(
t
,
0
,
1
)
expectVarint
(
t
,
1
<<
7
,
2
)
}
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