Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
17bd39e7
Commit
17bd39e7
authored
Apr 15, 2011
by
Ian Lance Taylor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gob: test case for indirection to large field.
R=r CC=golang-dev
https://golang.org/cl/4404048
parent
604c161e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
0 deletions
+84
-0
gobencdec_test.go
src/pkg/gob/gobencdec_test.go
+84
-0
No files found.
src/pkg/gob/gobencdec_test.go
View file @
17bd39e7
...
...
@@ -24,6 +24,10 @@ type StringStruct struct {
s
string
// not an exported field
}
type
ArrayStruct
struct
{
a
[
8192
]
byte
// not an exported field
}
type
Gobber
int
type
ValueGobber
string
// encodes with a value, decodes with a pointer.
...
...
@@ -74,6 +78,18 @@ func (g *StringStruct) GobDecode(data []byte) os.Error {
return
nil
}
func
(
a
*
ArrayStruct
)
GobEncode
()
([]
byte
,
os
.
Error
)
{
return
a
.
a
[
:
],
nil
}
func
(
a
*
ArrayStruct
)
GobDecode
(
data
[]
byte
)
os
.
Error
{
if
len
(
data
)
!=
len
(
a
.
a
)
{
return
os
.
ErrorString
(
"wrong length in array decode"
)
}
copy
(
a
.
a
[
:
],
data
)
return
nil
}
func
(
g
*
Gobber
)
GobEncode
()
([]
byte
,
os
.
Error
)
{
return
[]
byte
(
fmt
.
Sprintf
(
"VALUE=%d"
,
*
g
)),
nil
}
...
...
@@ -138,6 +154,16 @@ type GobTestIndirectEncDec struct {
G
***
StringStruct
// indirections to the receiver.
}
type
GobTestArrayEncDec
struct
{
X
int
// guarantee we have something in common with GobTest*
A
ArrayStruct
// not a pointer.
}
type
GobTestIndirectArrayEncDec
struct
{
X
int
// guarantee we have something in common with GobTest*
A
***
ArrayStruct
// indirections to a large receiver.
}
func
TestGobEncoderField
(
t
*
testing
.
T
)
{
b
:=
new
(
bytes
.
Buffer
)
// First a field that's a structure.
...
...
@@ -216,6 +242,64 @@ func TestGobEncoderIndirectField(t *testing.T) {
}
}
// Test with a large field with methods.
func
TestGobEncoderArrayField
(
t
*
testing
.
T
)
{
b
:=
new
(
bytes
.
Buffer
)
enc
:=
NewEncoder
(
b
)
var
a
GobTestArrayEncDec
a
.
X
=
17
for
i
:=
range
a
.
A
.
a
{
a
.
A
.
a
[
i
]
=
byte
(
i
)
}
err
:=
enc
.
Encode
(
a
)
if
err
!=
nil
{
t
.
Fatal
(
"encode error:"
,
err
)
}
dec
:=
NewDecoder
(
b
)
x
:=
new
(
GobTestArrayEncDec
)
err
=
dec
.
Decode
(
x
)
if
err
!=
nil
{
t
.
Fatal
(
"decode error:"
,
err
)
}
for
i
,
v
:=
range
x
.
A
.
a
{
if
v
!=
byte
(
i
)
{
t
.
Errorf
(
"expected %x got %x"
,
byte
(
i
),
v
)
break
}
}
}
// Test an indirection to a large field with methods.
func
TestGobEncoderIndirectArrayField
(
t
*
testing
.
T
)
{
b
:=
new
(
bytes
.
Buffer
)
enc
:=
NewEncoder
(
b
)
var
a
GobTestIndirectArrayEncDec
a
.
X
=
17
var
array
ArrayStruct
ap
:=
&
array
app
:=
&
ap
a
.
A
=
&
app
for
i
:=
range
array
.
a
{
array
.
a
[
i
]
=
byte
(
i
)
}
err
:=
enc
.
Encode
(
a
)
if
err
!=
nil
{
t
.
Fatal
(
"encode error:"
,
err
)
}
dec
:=
NewDecoder
(
b
)
x
:=
new
(
GobTestIndirectArrayEncDec
)
err
=
dec
.
Decode
(
x
)
if
err
!=
nil
{
t
.
Fatal
(
"decode error:"
,
err
)
}
for
i
,
v
:=
range
(
***
x
.
A
)
.
a
{
if
v
!=
byte
(
i
)
{
t
.
Errorf
(
"expected %x got %x"
,
byte
(
i
),
v
)
break
}
}
}
// As long as the fields have the same name and implement the
// interface, we can cross-connect them. Not sure it's useful
// and may even be bad but it works and it's hard to prevent
...
...
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