From cdcdaf88d6baea4ff46bd797b1e76ea12f45bc53 Mon Sep 17 00:00:00 2001 From: Maxim Babichev Date: Fri, 12 Jul 2024 18:36:00 +0300 Subject: [PATCH] chore: golang 1.22+ --- .github/workflows/golangci-lint.yml | 10 +-- .github/workflows/unit.yml | 25 ++---- .golangci.yml | 19 ++--- Makefile | 10 +++ bench_test.go | 2 +- box.go | 2 +- copyptr.go | 15 ++-- go.mod | 9 +- go.sum | 48 +---------- internal/visualize3d/main.go | 127 ---------------------------- item.go | 8 +- packer.go | 11 ++- packer_test.go | 38 ++++----- 13 files changed, 75 insertions(+), 249 deletions(-) create mode 100644 Makefile delete mode 100644 internal/visualize3d/main.go diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index aa183fd..15673ac 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -12,15 +12,15 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.21' ] + go-version: [ '1.22' ] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} cache: true - name: golangci-lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v6 with: - version: v1.54.0 + version: v1.59.1 \ No newline at end of file diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 13a0c47..0650808 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -1,30 +1,21 @@ -name: gotest-unit +name: Unit on: - push: - branches: - - master pull_request: -permissions: - contents: read + jobs: test: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.21' ] + go-version: [ '1.22' ] steps: - - uses: actions/checkout@v3 + - + name: Checkout + uses: actions/checkout@v4 - name: Setup Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} cache: true - - name: Install dependencies - run: go get . - name: Test with Go - run: go test -json > TestResults-${{ matrix.go-version }}.json - - name: Upload Go test results - uses: actions/upload-artifact@v3 - with: - name: Go-results-${{ matrix.go-version }} - path: TestResults-${{ matrix.go-version }}.json + run: go test ./... diff --git a/.golangci.yml b/.golangci.yml index f02d500..49eba7f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,24 +1,21 @@ run: + go: "1.22" timeout: 1m linters: enable-all: true disable: # deprecated - - nosnakecase - - structcheck - - interfacer - - deadcode - - exhaustivestruct - - maligned - - ifshort - - varcheck - - golint - - scopelint + - execinquery + - gomnd # not relevant + - wrapcheck + - paralleltest + - exhaustruct + - tagalign - varnamelen linters-settings: lll: - line-length: 160 + line-length: 140 gci: sections: - Standard diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..66b6495 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +.PHONY: * + +test: + go test -tags mock -race -cover ./... + +lint: + go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1 run --color always ${args} + +lint-fix: + make lint args=--fix diff --git a/bench_test.go b/bench_test.go index 3e62dc7..06cf3a0 100644 --- a/bench_test.go +++ b/bench_test.go @@ -13,7 +13,7 @@ import ( func BenchmarkPacker(b *testing.B) { items := make([]*boxpacker3.Item, 0, 100) - for x := 0; x < 100; x++ { + for range 100 { w, _ := rand.Int(rand.Reader, big.NewInt(150)) l, _ := rand.Int(rand.Reader, big.NewInt(150)) h, _ := rand.Int(rand.Reader, big.NewInt(150)) diff --git a/box.go b/box.go index a127c85..e308880 100644 --- a/box.go +++ b/box.go @@ -1,7 +1,7 @@ package boxpacker3 import ( - "golang.org/x/exp/slices" + "slices" ) type Box struct { diff --git a/copyptr.go b/copyptr.go index cc0e09b..0b770bc 100644 --- a/copyptr.go +++ b/copyptr.go @@ -1,17 +1,22 @@ package boxpacker3 -func copyPtr[T any](original *T) *T { +// CopyPtr creates a copy of a pointer. +func CopyPtr[T any](original *T) *T { + if original == nil { + return nil + } + copyOfValue := *original return ©OfValue } -func copySlicePtr[T any](data []*T) []*T { +// CopySlicePtr creates a copy of a slice of pointers. +func CopySlicePtr[T any](data []*T) []*T { result := make([]*T, len(data)) - for i := range data { - val := *data[i] - result[i] = &val + for i, item := range data { + result[i] = CopyPtr(item) } return result diff --git a/go.mod b/go.mod index 93a9ea3..0d8aab2 100644 --- a/go.mod +++ b/go.mod @@ -1,20 +1,15 @@ module github.com/bavix/boxpacker3 -go 1.20 +go 1.22 require ( - github.com/g3n/engine v0.2.0 github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.9.0 - golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b + golang.org/x/exp v0.0.0-20240707233637-46b078467d37 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect - github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/image v0.11.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index cff6617..93e963a 100644 --- a/go.sum +++ b/go.sum @@ -1,58 +1,14 @@ 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/g3n/engine v0.2.0 h1:7dmj4c+3xHcBnYrVmRuVf/oZ2JycxJU9Y+2FQj1Af2Y= -github.com/g3n/engine v0.2.0/go.mod h1:rnj8jiLdKEDI8VbveKhmdL4rovjjy+uxNP5YROg2x8g= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210410170116-ea3d685f79fb/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= -github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI= -golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= -golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= -golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo= -golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w= +golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= 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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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/internal/visualize3d/main.go b/internal/visualize3d/main.go deleted file mode 100644 index 40f1c3f..0000000 --- a/internal/visualize3d/main.go +++ /dev/null @@ -1,127 +0,0 @@ -//nolint:all -package main - -import ( - "math/rand" - "time" - - "github.com/g3n/engine/app" - "github.com/g3n/engine/camera" - "github.com/g3n/engine/core" - "github.com/g3n/engine/geometry" - "github.com/g3n/engine/gls" - "github.com/g3n/engine/graphic" - "github.com/g3n/engine/light" - "github.com/g3n/engine/material" - "github.com/g3n/engine/math32" - "github.com/g3n/engine/renderer" - "github.com/google/uuid" - - "github.com/bavix/boxpacker3" -) - -const ( - BoxTypeF = "8ec81501-11a4-4b3f-9a52-7cd2f9c8370c" - BoxTypeE = "9c69baf8-1ca3-46a0-9fc2-6f15ad9fef9a" - BoxTypeG = "2c5279d3-48ad-451b-b673-f6d9be7fc6f6" - BoxTypeC = "7f1cc68f-d554-4094-8734-c68df5c13154" - BoxTypeB = "76cede41-86bb-4487-bfb0-9513f032d53e" - BoxTypeA = "8e10cebf-cee6-4136-b060-1587b993d083" - BoxTypeStd = "ba973206-aa64-493b-b37a-c53192cde8fd" - BoxTypeNotStd1 = "cb1ed5b8-7405-48c5-bfd0-d86f75c99261" - BoxTypeNotStd2 = "d91e2661-aebb-4a55-bfb5-4ff9c6e3c008" - BoxTypeNotStd3 = "a0ecd730-375a-4313-bbe8-820710606b3d" - BoxTypeNotStd4 = "6dff37f0-4dd1-4143-abdc-c19ab94f2e68" - BoxTypeNotStd5 = "abac6d59-b51f-4d62-a338-42aca7afe1cc" - BoxTypeNotStd6 = "981ffb30-a7b9-4d9e-820e-04de2145763e" -) - -func NewDefaultBoxList() []*boxpacker3.Box { - return []*boxpacker3.Box{ - boxpacker3.NewBox(BoxTypeF, 220, 185, 50, 20000), // 0 - boxpacker3.NewBox(BoxTypeE, 165, 215, 100, 20000), // 1 - boxpacker3.NewBox(BoxTypeG, 265, 165, 190, 20000), // 2 - boxpacker3.NewBox(BoxTypeC, 425, 165, 190, 20000), // 3 - boxpacker3.NewBox(BoxTypeB, 425, 265, 190, 20000), // 4 - boxpacker3.NewBox(BoxTypeA, 425, 265, 380, 20000), // 5 - boxpacker3.NewBox(BoxTypeStd, 530, 380, 265, 20000), // 6 - boxpacker3.NewBox(BoxTypeNotStd1, 1000, 500, 500, 20000), // 7 - boxpacker3.NewBox(BoxTypeNotStd2, 1000, 1000, 1000, 20000), // 8 - boxpacker3.NewBox(BoxTypeNotStd3, 2000, 500, 500, 20000), // 9 - boxpacker3.NewBox(BoxTypeNotStd4, 2000, 2000, 2000, 20000), // 10 - boxpacker3.NewBox(BoxTypeNotStd5, 2500, 2500, 2500, 20000), // 11 - boxpacker3.NewBox(BoxTypeNotStd6, 3000, 3000, 3000, 20000), // 12 - } -} - -func main() { - a := app.App() - scene := core.NewNode() - - cam := camera.NewPerspective(1, 1, 3000, 60, camera.Vertical) - cam.SetPosition(0, 0, 3) - scene.Add(cam) - camera.NewOrbitControl(cam) - - packer := boxpacker3.NewPacker() - - boxes := NewDefaultBoxList() - items := []*boxpacker3.Item{ - // 5 - boxpacker3.NewItem(uuid.New().String(), 100, 100, 5, 2690), - boxpacker3.NewItem(uuid.New().String(), 100, 5, 100, 2690), - boxpacker3.NewItem(uuid.New().String(), 5, 100, 100, 2690), - boxpacker3.NewItem(uuid.New().String(), 5, 100, 100, 2690), - boxpacker3.NewItem(uuid.New().String(), 5, 100, 100, 2690), - boxpacker3.NewItem(uuid.New().String(), 5, 100, 100, 2690), - - // 35 - boxpacker3.NewItem(uuid.New().String(), 35, 100, 100, 2690), - boxpacker3.NewItem(uuid.New().String(), 35, 100, 100, 2690), - } - - packResult := packer.Pack(boxes, items) - - for _, box := range packResult.Boxes { - if len(box.GetItems()) == 0 { - continue - } - - boxGeo := geometry.NewBox(float32(box.GetWidth()), float32(box.GetHeight()), float32(box.GetDepth())) - mat := material.NewStandard(math32.NewColor("DarkGreen")) - mat.SetOpacity(0.25) - boxMesh := graphic.NewMesh(boxGeo, mat) - boxMesh.SetPosition(0, 0, 0) - scene.Add(boxMesh) - - for _, item := range box.GetItems() { - dimension := item.GetDimension() - - itemGeo := geometry.NewBox(float32(dimension[0]), float32(dimension[1]), float32(dimension[2])) - mat := material.NewStandard(math32.NewColorHex(uint(rand.Uint32()))) - itemMesh := graphic.NewMesh(itemGeo, mat) - mat.SetOpacity(0.7) - - itemMesh.SetPosition( - float32(item.GetPosition()[boxpacker3.WidthAxis])-float32(box.GetWidth())/2+float32(dimension[0])/2, - float32(item.GetPosition()[boxpacker3.HeightAxis])-float32(box.GetHeight())/2+float32(dimension[1])/2, - float32(item.GetPosition()[boxpacker3.DepthAxis])-float32(box.GetDepth())/2+float32(dimension[2])/2) - - scene.Add(itemMesh) - } - - break - } - - scene.Add(light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.8)) - pointLight := light.NewPoint(&math32.Color{1, 1, 0}, 5.0) - pointLight.SetPosition(1, 0, 2) - scene.Add(pointLight) - - a.Gls().ClearColor(0.5, 0.5, 0.5, 1.0) - - a.Run(func(renderer *renderer.Renderer, deltaTime time.Duration) { - a.Gls().Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT) - renderer.Render(scene, cam) - }) -} diff --git a/item.go b/item.go index d7961d4..9d34831 100644 --- a/item.go +++ b/item.go @@ -105,10 +105,10 @@ func (i *Item) Intersect(it *Item) bool { // rectIntersect Checks if two rectangles intersect from the x and y axes of elements i1 and i2. func rectIntersect(d1, d2 Dimension, i1, i2 *Item, x, y Axis) bool { - cx1 := i1.position[x] + d1[x]/2 //nolint:gomnd - cy1 := i1.position[y] + d1[y]/2 //nolint:gomnd - cx2 := i2.position[x] + d2[x]/2 //nolint:gomnd - cy2 := i2.position[y] + d2[y]/2 //nolint:gomnd + cx1 := i1.position[x] + d1[x]/2 //nolint:mnd + cy1 := i1.position[y] + d1[y]/2 //nolint:mnd + cx2 := i2.position[x] + d2[x]/2 //nolint:mnd + cy2 := i2.position[y] + d2[y]/2 //nolint:mnd ix := math.Max(cx1, cx2) - math.Min(cx1, cx2) iy := math.Max(cy1, cy2) - math.Min(cy1, cy2) diff --git a/packer.go b/packer.go index 2771d34..e59ae24 100644 --- a/packer.go +++ b/packer.go @@ -1,9 +1,8 @@ package boxpacker3 import ( + "slices" "sort" - - "golang.org/x/exp/slices" ) type Packer struct{} @@ -18,8 +17,8 @@ func NewPacker() *Packer { } func (p *Packer) Pack(inputBoxes []*Box, inputItems []*Item) *Result { - boxes := boxSlice(copySlicePtr(inputBoxes)) - items := itemSlice(copySlicePtr(inputItems)) + boxes := boxSlice(CopySlicePtr(inputBoxes)) + items := itemSlice(CopySlicePtr(inputItems)) sort.Sort(boxes) sort.Sort(items) @@ -107,8 +106,8 @@ func (p *Packer) packToBox(b *Box, items []*Item) []*Item { } if !fitted { - backup := copyPtr(b) - copyItems := copySlicePtr(b.items) + backup := CopyPtr(b) + copyItems := CopySlicePtr(b.items) b.purge() diff --git a/packer_test.go b/packer_test.go index 916a656..482ea85 100644 --- a/packer_test.go +++ b/packer_test.go @@ -87,8 +87,8 @@ func (s *PackerSuit) TestEmptyBoxes() { packResult := packer.Pack(nil, nil) require.NotNil(t, packResult) - require.Len(t, packResult.Boxes, 0) - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.Boxes) + require.Empty(t, packResult.UnfitItems) } func (s *PackerSuit) TestEmptyItems() { @@ -102,7 +102,7 @@ func (s *PackerSuit) TestEmptyItems() { packResult := packer.Pack(boxes, nil) require.NotNil(t, packResult) require.Len(t, packResult.Boxes, len(boxes)) - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) } func (s *PackerSuit) TestMinBox() { @@ -125,9 +125,9 @@ func (s *PackerSuit) TestMinBox() { BoxTypeF: 1, } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()]) } } @@ -154,9 +154,9 @@ func (s *PackerSuit) TestRotate() { BoxTypeStd: 5, } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()], packResult.Boxes[i].GetID()) } } @@ -183,9 +183,9 @@ func (s *PackerSuit) TestStd() { BoxTypeStd: 5, } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()], packResult.Boxes[i].GetID()) } } @@ -218,9 +218,9 @@ func (s *PackerSuit) TestBoxTypeF() { BoxTypeF: 8, } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()], packResult.Boxes[i].GetID()) } } @@ -254,9 +254,9 @@ func (s *PackerSuit) TestBoxTypeF_Weight() { BoxTypeE: 1, } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()], packResult.Boxes[i].GetID()) } } @@ -294,9 +294,9 @@ func (s *PackerSuit) TestPacker_AllBoxes() { packResult := packer.Pack(boxes, items) require.NotNil(t, packResult) - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), 1) } } @@ -320,8 +320,8 @@ func (s *PackerSuit) TestPacker_UnfitItems() { require.Len(t, packResult.UnfitItems, 4) - for i := 0; i < len(packResult.Boxes); i++ { - require.Len(t, packResult.Boxes[i].GetItems(), 0, packResult.Boxes[i].GetID()) + for i := range len(packResult.Boxes) { + require.Empty(t, packResult.Boxes[i].GetItems(), packResult.Boxes[i].GetID()) } } @@ -378,9 +378,9 @@ func (s *PackerSuit) TestPacker_MinAndStd() { BoxTypeNotStd6: 1, // 14 } - require.Len(t, packResult.UnfitItems, 0) + require.Empty(t, packResult.UnfitItems) - for i := 0; i < len(packResult.Boxes); i++ { + for i := range len(packResult.Boxes) { require.Len(t, packResult.Boxes[i].GetItems(), checks[packResult.Boxes[i].GetID()], packResult.Boxes[i].GetID()) } }