diff options
author | Rastislav Szabo <raszabo@cisco.com> | 2017-05-04 11:09:03 +0200 |
---|---|---|
committer | Rastislav Szabo <raszabo@cisco.com> | 2017-05-04 11:12:35 +0200 |
commit | a101d966133a70b8a76526be25070436d14fcf9f (patch) | |
tree | 75e2dbf20de615e58252b780b2ba5baae8fdcf82 /vendor/github.com/lunixbochs/struc/custom_test.go | |
parent | a968ead74525125dff9ae90b1c9a9102e4327900 (diff) |
initial commit
Signed-off-by: Rastislav Szabo <raszabo@cisco.com>
Diffstat (limited to 'vendor/github.com/lunixbochs/struc/custom_test.go')
-rw-r--r-- | vendor/github.com/lunixbochs/struc/custom_test.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/github.com/lunixbochs/struc/custom_test.go b/vendor/github.com/lunixbochs/struc/custom_test.go new file mode 100644 index 0000000..e601166 --- /dev/null +++ b/vendor/github.com/lunixbochs/struc/custom_test.go @@ -0,0 +1,97 @@ +package struc + +import ( + "bytes" + "encoding/binary" + "io" + "strconv" + "testing" +) + +type Int3 uint32 + +func (i *Int3) Pack(p []byte, opt *Options) (int, error) { + var tmp [4]byte + binary.BigEndian.PutUint32(tmp[:], uint32(*i)) + copy(p, tmp[1:]) + return 3, nil +} +func (i *Int3) Unpack(r io.Reader, length int, opt *Options) error { + var tmp [4]byte + if _, err := r.Read(tmp[1:]); err != nil { + return err + } + *i = Int3(binary.BigEndian.Uint32(tmp[:])) + return nil +} +func (i *Int3) Size(opt *Options) int { + return 3 +} +func (i *Int3) String() string { + return strconv.FormatUint(uint64(*i), 10) +} + +func TestCustom(t *testing.T) { + var buf bytes.Buffer + var i Int3 = 3 + if err := Pack(&buf, &i); err != nil { + t.Fatal(err) + } + if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { + t.Fatal("error packing custom int") + } + var i2 Int3 + if err := Unpack(&buf, &i2); err != nil { + t.Fatal(err) + } + if i2 != 3 { + t.Fatal("error unpacking custom int") + } +} + +type Int3Struct struct { + I Int3 +} + +func TestCustomStruct(t *testing.T) { + var buf bytes.Buffer + i := Int3Struct{3} + if err := Pack(&buf, &i); err != nil { + t.Fatal(err) + } + if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { + t.Fatal("error packing custom int struct") + } + var i2 Int3Struct + if err := Unpack(&buf, &i2); err != nil { + t.Fatal(err) + } + if i2.I != 3 { + t.Fatal("error unpacking custom int struct") + } +} + +// TODO: slices of custom types don't work yet +/* +type Int3SliceStruct struct { + I [2]Int3 +} + +func TestCustomSliceStruct(t *testing.T) { + var buf bytes.Buffer + i := Int3SliceStruct{[2]Int3{3, 4}} + if err := Pack(&buf, &i); err != nil { + t.Fatal(err) + } + if !bytes.Equal(buf.Bytes(), []byte{0, 0, 3}) { + t.Fatal("error packing custom int struct") + } + var i2 Int3SliceStruct + if err := Unpack(&buf, &i2); err != nil { + t.Fatal(err) + } + if i2.I[0] != 3 && i2.I[1] != 4 { + t.Fatal("error unpacking custom int struct") + } +} +*/ |