From 71948481eef4a8c272f9bbbe0f981e1f8f3d7867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 30 Sep 2022 22:10:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20pack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 2 +- go.mod | 11 ++++++++++- go.sum | 15 +++++++++++++++ pack.go | 5 +---- pack_test.go | 26 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 pack_test.go diff --git a/base.go b/base.go index 9bad97a..d910c23 100644 --- a/base.go +++ b/base.go @@ -42,7 +42,7 @@ func NewBase(off, til uint16, bit uint8) (*Base, error) { if tile > 0x10000 { return nil, ErrTailOverflow } - if tile > uint32(off) || uint32(til) < offe { + if tile > uint32(off) && tile <= offe { return nil, ErrTailInCodingArea } return &Base{ diff --git a/go.mod b/go.mod index 51d1d9e..3ce1473 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,13 @@ module github.com/fumiama/unibase2n go 1.18 -require golang.org/x/text v0.3.7 +require ( + github.com/stretchr/testify v1.8.0 + golang.org/x/text v0.3.7 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 1f78e03..d734a74 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pack.go b/pack.go index 77fe962..31b149c 100644 --- a/pack.go +++ b/pack.go @@ -2,7 +2,6 @@ package unibase2n import ( "encoding/binary" - "math/bits" "unsafe" ) @@ -49,9 +48,7 @@ func New(pack Pack) *Base { *(*Pack)(unsafe.Pointer(b)) = pack return b } - // change to native endian - n := bits.Reverse64(uint64(pack)) - field := (*[8]byte)(unsafe.Pointer(&n)) + field := (*[8]byte)(unsafe.Pointer(&pack)) if isitle { // packed in little endian but I am big b.off = binary.BigEndian.Uint16(field[6:8]) b.til = binary.BigEndian.Uint16(field[4:6]) diff --git a/pack_test.go b/pack_test.go new file mode 100644 index 0000000..c32e73e --- /dev/null +++ b/pack_test.go @@ -0,0 +1,26 @@ +package unibase2n + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPackUnpack(t *testing.T) { + bs, err := NewBase(0x1234, 0x5678, 8) + if err != nil { + t.Fatal(err) + } + p := bs.Pack() + bs1 := New(p) + assert.Equal(t, bs, bs1) + ismele := isLittleEndian() + if ismele { + // simulate be pack -> le unpack + bs2 := New(0x1234567808000000) + assert.Equal(t, bs, bs2) + } else { // simulate le pack -> be unpack + bs2 := New(0x0000000878563412) + assert.Equal(t, bs, bs2) + } +}