Skip to content

Commit

Permalink
rename Pack/Unpack to Encode/Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
vipally committed Nov 1, 2017
1 parent b47d83d commit 706e725
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
32 changes: 16 additions & 16 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func BenchmarkWriteRegedStruct(b *testing.B) {
data := regedStruct(_struct)
testBenchWrite(b, data, "BenchmarkWriteRegedStruct")
}
func BenchmarkPackStruct(b *testing.B) {
func BenchmarkEncodeStruct(b *testing.B) {
data := _struct
testBenchPack(b, data, "BenchmarkPackStruct")
testBenchEncode(b, data, "BenchmarkEncodeStruct")
}
func BenchmarkPackRegedStruct(b *testing.B) {
func BenchmarkEncodeRegedStruct(b *testing.B) {
data := regedStruct(_struct)
testBenchPack(b, data, "BenchmarkPackRegedStruct")
testBenchEncode(b, data, "BenchmarkEncodeRegedStruct")
}
func BenchmarkGobDecodeStruct(b *testing.B) {
data := _struct
Expand All @@ -90,14 +90,14 @@ func BenchmarkReadRegedStruct(b *testing.B) {
dataC := regedStruct(wStruct)
testBenchRead(b, &data, &dataC, "BenchmarkReadRegedStruct")
}
func BenchmarkUnackStruct(b *testing.B) {
func BenchmarkDecodeStruct(b *testing.B) {
data := _struct
testBenchUnpack(b, &data, &wStruct, "BenchmarkUnackStruct")
testBenchDecode(b, &data, &wStruct, "BenchmarkDecodeStruct")
}
func BenchmarkUnpackRegedStruct(b *testing.B) {
func BenchmarkDecodeRegedStruct(b *testing.B) {
data := regedStruct(_struct)
dataC := regedStruct(wStruct)
testBenchUnpack(b, &data, &dataC, "BenchmarkUnpackRegedStruct")
testBenchDecode(b, &data, &dataC, "BenchmarkDecodeRegedStruct")
}

//////////////////////////////////////////////////////////////////Int1000
Expand All @@ -113,9 +113,9 @@ func BenchmarkWriteInt1000(b *testing.B) {
data := u32Array1000
testBenchWrite(b, data, "BenchmarkWriteInt1000")
}
func BenchmarkPackInt1000(b *testing.B) {
func BenchmarkEncodeInt1000(b *testing.B) {
data := u32Array1000
testBenchPack(b, data, "BenchmarkPackInt1000")
testBenchEncode(b, data, "BenchmarkEncodeInt1000")
} //BUG: this case will fail
//func BenchmarkGobDecodeInt1000(b *testing.B) {
// data := u32Array1000
Expand All @@ -131,7 +131,7 @@ func BenchmarkReadInt1000(b *testing.B) {
}
func BenchmarkUnackInt1000(b *testing.B) {
data := u32Array1000
testBenchUnpack(b, &data, &u32Array1000W, "BenchmarkUnackInt1000")
testBenchDecode(b, &data, &u32Array1000W, "BenchmarkUnackInt1000")
}

//////////////////////////////////////////////////////////////////String
Expand All @@ -147,9 +147,9 @@ func BenchmarkWriteString(b *testing.B) {
data := str
testBenchWrite(b, data, "BenchmarkWriteString")
}
func BenchmarkPackString(b *testing.B) {
func BenchmarkEncodeString(b *testing.B) {
data := str
testBenchPack(b, data, "BenchmarkPackString")
testBenchEncode(b, data, "BenchmarkEncodeString")
}
func BenchmarkGobDecodeString(b *testing.B) {
data := str
Expand All @@ -165,7 +165,7 @@ func BenchmarkReadString(b *testing.B) {
}
func BenchmarkUnackString(b *testing.B) {
data := str
testBenchUnpack(b, &data, &strW, "BenchmarkUnackString")
testBenchDecode(b, &data, &strW, "BenchmarkUnackString")
}

func newSame(v reflect.Value) (value reflect.Value) {
Expand Down Expand Up @@ -226,7 +226,7 @@ func testBenchWrite(b *testing.B, data interface{}, caseName string) {
}
b.StopTimer()
}
func testBenchPack(b *testing.B, data interface{}, caseName string) {
func testBenchEncode(b *testing.B, data interface{}, caseName string) {
b.SetBytes(int64(Sizeof(data)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -309,7 +309,7 @@ func testBenchRead(b *testing.B, data, w interface{}, caseName string) {
b.Fatalf("%s doesn't match:\ngot %#v;\nwant %#v", caseName, w, data)
}
}
func testBenchUnpack(b *testing.B, data, w interface{}, caseName string) {
func testBenchDecode(b *testing.B, data, w interface{}, caseName string) {
buf, err := Encode(data, buff)
if err != nil {
b.Error(caseName, err)
Expand Down
52 changes: 26 additions & 26 deletions coder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ var littleFullAll = []byte{
0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
}

func TestPack(t *testing.T) {
func TestEncode(t *testing.T) {
v := reflect.ValueOf(full)
vt := v.Type()
n := v.NumField()
Expand All @@ -351,7 +351,7 @@ func TestPack(t *testing.T) {
}

////map fields will case uncertain bytes order but it does't matter
//b2, err := Pack(full, nil)
//b2, err := Encode(full, nil)
//if err != nil {
// t.Error(err)
//}
Expand All @@ -361,7 +361,7 @@ func TestPack(t *testing.T) {
//}
}

func TestUnpack(t *testing.T) {
func TestDecode(t *testing.T) {
var v fullStruct
err := Decode(littleFullAll, &v)
if err != nil {
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestReset(t *testing.T) {
}
}

func TestPackEmptyPointer(t *testing.T) {
func TestEncodeEmptyPointer(t *testing.T) {
var s struct {
PString *string
PSlice *[]int
Expand Down Expand Up @@ -623,10 +623,10 @@ func TestFastValue(t *testing.T) {
}
}

func TestPackDonotSupportedType(t *testing.T) {
func TestEncodeDonotSupportedType(t *testing.T) {
ts := doNotSupportTypes
if _, err := Encode(ts, nil); err == nil {
t.Errorf("PackDonotSupportedType: have err == nil, want non-nil")
t.Errorf("EncodeDonotSupportedType: have err == nil, want non-nil")
}

buff := make([]byte, 0)
Expand All @@ -636,25 +636,25 @@ func TestPackDonotSupportedType(t *testing.T) {
tv := reflect.Indirect(reflect.ValueOf(&ts))
for i, n := 0, tv.NumField(); i < n; i++ {
if _, err := Encode(tv.Field(i).Interface(), nil); err == nil {
t.Errorf("PackDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
t.Errorf("EncodeDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
} else {
//fmt.Println(err)
}

if err := ecoder.Value(tv.Field(i).Interface()); err == nil {
t.Errorf("PackDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
t.Errorf("EncodeDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
} else {
//fmt.Println(err)
}

if err := Decode(buff, tv.Field(i).Addr().Interface()); err == nil {
t.Errorf("Unpack DonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
t.Errorf("Decode DonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
} else {
//fmt.Printf("Unpack error: %#v\n%s\n", tv.Field(i).Addr().Type().String(), err.Error())
//fmt.Printf("Decode error: %#v\n%s\n", tv.Field(i).Addr().Type().String(), err.Error())
}

if err := decoder.value(tv.Field(i), true); err == nil {
t.Errorf("PackDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
t.Errorf("EncodeDonotSupportedType.%v: have err == nil, want non-nil", tv.Field(i).Type())
} else {
//fmt.Println(err)
}
Expand Down Expand Up @@ -797,15 +797,15 @@ type decoderOnly struct {

func (this *decoderOnly) Decode(buffer []byte) error { return nil }

type sizepackerOnly struct {
type sizeencoderOnly struct {
sizerOnly
encoderOnly
}
type sizeunpackerOnly struct {
type sizedecoderOnly struct {
sizerOnly
decoderOnly
}
type packunpackerOnly struct {
type encodedecoderOnly struct {
encoderOnly
decoderOnly
}
Expand All @@ -822,13 +822,13 @@ func (this *fullSerializerError) Decode(buffer []byte) error {
return fmt.Errorf("expected error")
}

func TestPackUnpacker(t *testing.T) {
func TestBinarySerializer(t *testing.T) {
var a sizerOnly
var b encoderOnly
var c decoderOnly
var d sizepackerOnly
var e sizeunpackerOnly
var f packunpackerOnly
var d sizeencoderOnly
var e sizedecoderOnly
var f encodedecoderOnly
var g fullSerializerError
var h fullSerializer

Expand Down Expand Up @@ -873,28 +873,28 @@ func TestPackUnpacker(t *testing.T) {
}

if info := testCode(&a); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&b); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&c); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&d); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&e); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&f); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&g); info == nil {
t.Errorf("PackUnpacker: have err == nil, want none-nil")
t.Errorf("BinarySerializer: have err == nil, want none-nil")
}
if info := testCode(&h); info != nil {
t.Errorf("PackUnpacker: have err == %#v, want nil", info)
t.Errorf("BinarySerializer: have err == %#v, want nil", info)
}
}

Expand Down
16 changes: 8 additions & 8 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func ExampleRead() {
// 3.141592653589793
}

func ExamplePack() {
func ExampleEncode() {
var s struct {
A uint32
B int
Expand All @@ -69,13 +69,13 @@ func ExamplePack() {
s.C = "hello"
b, err := binary.Encode(s, nil)
if err != nil {
fmt.Println("binary.Pack failed:", err)
fmt.Println("binary.Encode failed:", err)
}
fmt.Printf("%#v", b)
// Output:
// []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
}
func ExamplePack_withbuffer() {
func ExampleEncode_withbuffer() {
var s struct {
A uint32
B int
Expand All @@ -88,13 +88,13 @@ func ExamplePack_withbuffer() {
buffer := make([]byte, size)
b, err := binary.Encode(s, buffer)
if err != nil {
fmt.Println("binary.Pack failed:", err)
fmt.Println("binary.Encode failed:", err)
}
fmt.Printf("%#v", b)
// Output:
// []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
}
func ExampleUnpack() {
func ExampleDecode() {
var s struct {
A uint32
B int
Expand All @@ -103,7 +103,7 @@ func ExampleUnpack() {
buffer := []byte{0x44, 0x33, 0x22, 0x11, 0x9, 0x5, 0x68, 0x65, 0x6c, 0x6c, 0x6f}
err := binary.Decode(buffer, &s)
if err != nil {
fmt.Println("binary.Unpack failed:", err)
fmt.Println("binary.Decode failed:", err)
}
fmt.Printf("%+v", s)
// Output:
Expand Down Expand Up @@ -191,11 +191,11 @@ func ExampleBinarySerializer() {
b, err := binary.Encode(&s, nil)

if err != nil {
fmt.Println("binary.Pack failed:", err)
fmt.Println("binary.Encode failed:", err)
}
err = binary.Decode(b, &ss)
if err != nil {
fmt.Println("binary.Unpack failed:", err)
fmt.Println("binary.Decode failed:", err)
}
fmt.Printf("[%+v\n%#v\n%+v]", s, b, ss)
// Output:
Expand Down
4 changes: 2 additions & 2 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//

//
// Package binary is uesed to Pack/Unpack between go data and byte slice.
// Package binary is uesed to Encode/Decode between go data and byte slice.
//
// The main purpose of this package is to replace package "std.binary".
// The design goal is to take both advantages of std.binary(encoding/binary) and gob.
Expand Down Expand Up @@ -206,7 +206,7 @@ func Decode(buffer []byte, data interface{}) error {
func MakeEncodeBuffer(data interface{}, buffer []byte) ([]byte, error) {
size := Sizeof(data)
if size < 0 {
return nil, errors.New("binary.Pack: invalid type " + reflect.TypeOf(data).String())
return nil, errors.New("binary.MakeEncodeBuffer: invalid type " + reflect.TypeOf(data).String())
}

buff := buffer
Expand Down

0 comments on commit 706e725

Please sign in to comment.