Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jacobsa-fuse
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
jacobsa-fuse
Commits
e329c0ef
Commit
e329c0ef
authored
Dec 19, 2016
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a test for OutMessage.Reset.
parent
e1a6f0e2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
4 deletions
+56
-4
internal/buffer/out_message_test.go
internal/buffer/out_message_test.go
+56
-4
No files found.
internal/buffer/out_message_test.go
View file @
e329c0ef
...
...
@@ -4,16 +4,47 @@ import (
"crypto/rand"
"fmt"
"io"
"reflect"
"testing"
"unsafe"
)
func
toByteSlice
(
p
unsafe
.
Pointer
,
n
int
)
[]
byte
{
sh
:=
reflect
.
SliceHeader
{
Data
:
uintptr
(
p
),
Len
:
n
,
Cap
:
n
,
}
return
*
(
*
[]
byte
)(
unsafe
.
Pointer
(
&
sh
))
}
// fillWithGarbage writes random data to [p, p+n).
func
fillWithGarbage
(
p
unsafe
.
Pointer
,
n
int
)
(
err
error
)
{
b
:=
toByteSlice
(
p
,
n
)
_
,
err
=
io
.
ReadFull
(
rand
.
Reader
,
b
)
return
}
func
randBytes
(
n
int
)
(
b
[]
byte
,
err
error
)
{
b
=
make
([]
byte
,
n
)
_
,
err
=
io
.
ReadFull
(
rand
.
Reader
,
b
)
return
}
// findNonZero finds the offset of the first non-zero byte in [p, p+n). If
// none, it returns n.
func
findNonZero
(
p
unsafe
.
Pointer
,
n
int
)
int
{
b
:=
toByteSlice
(
p
,
n
)
for
i
,
x
:=
range
b
{
if
x
!=
0
{
return
i
}
}
return
n
}
func
TestMemclr
(
t
*
testing
.
T
)
{
// All sizes up to 32 bytes.
var
sizes
[]
int
...
...
@@ -48,15 +79,36 @@ func TestMemclr(t *testing.T) {
memclr
(
p
,
uintptr
(
len
(
b
)))
// Check
for
i
,
x
:=
range
b
{
if
x
!=
0
{
t
.
Fatalf
(
"non-zero byte %d at offset %d"
,
x
,
i
)
}
if
i
:=
findNonZero
(
p
,
len
(
b
));
i
!=
len
(
b
)
{
t
.
Fatalf
(
"non-zero byte at offset %d"
,
i
)
}
})
}
}
func
TestOutMessageReset
(
t
*
testing
.
T
)
{
var
om
OutMessage
h
:=
om
.
OutHeader
()
const
trials
=
100
for
i
:=
0
;
i
<
trials
;
i
++
{
fillWithGarbage
(
unsafe
.
Pointer
(
h
),
int
(
unsafe
.
Sizeof
(
*
h
)))
om
.
Reset
()
if
h
.
Len
!=
0
{
t
.
Fatalf
(
"non-zero Len %v"
,
h
.
Len
)
}
if
h
.
Error
!=
0
{
t
.
Fatalf
(
"non-zero Error %v"
,
h
.
Error
)
}
if
h
.
Unique
!=
0
{
t
.
Fatalf
(
"non-zero Unique %v"
,
h
.
Unique
)
}
}
}
func
BenchmarkOutMessageReset
(
b
*
testing
.
B
)
{
// A single buffer, which should fit in some level of CPU cache.
b
.
Run
(
"Single buffer"
,
func
(
b
*
testing
.
B
)
{
...
...
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