diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 6b9abea..66f33b6 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.20' ] + go-version: [ '1.20', '1.21' ] steps: - uses: actions/checkout@v3 - name: Setup Go @@ -23,4 +23,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.53.3 + version: v1.54.0 diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index c0fe53a..a3266eb 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.20' ] + go-version: [ '1.20', '1.21' ] steps: - uses: actions/checkout@v3 - name: Setup Go diff --git a/packer.go b/packer.go index 8136249..2771d34 100644 --- a/packer.go +++ b/packer.go @@ -72,10 +72,11 @@ func (p *Packer) preferredSort(boxes boxSlice, items itemSlice) boxSlice { func (p *Packer) packToBox(b *Box, items []*Item) []*Item { var fitted bool - unpacked := make([]*Item, 0, len(items)) + cntItems := len(items) + unpacked := make([]*Item, 0, cntItems) pv := Pivot{} - if b.items == nil && b.PutItem(items[0], pv) { + if b.items == nil && cntItems > 0 && b.PutItem(items[0], pv) { items = items[1:] } diff --git a/packer_test.go b/packer_test.go index eac813b..916a656 100644 --- a/packer_test.go +++ b/packer_test.go @@ -79,6 +79,32 @@ func TestBoxPackerSuite(t *testing.T) { suite.Run(t, new(PackerSuit)) } +func (s *PackerSuit) TestEmptyBoxes() { + t := s.T() + t.Parallel() + + packer := boxpacker3.NewPacker() + + packResult := packer.Pack(nil, nil) + require.NotNil(t, packResult) + require.Len(t, packResult.Boxes, 0) + require.Len(t, packResult.UnfitItems, 0) +} + +func (s *PackerSuit) TestEmptyItems() { + t := s.T() + t.Parallel() + + packer := boxpacker3.NewPacker() + + boxes := NewDefaultBoxList() + + packResult := packer.Pack(boxes, nil) + require.NotNil(t, packResult) + require.Len(t, packResult.Boxes, len(boxes)) + require.Len(t, packResult.UnfitItems, 0) +} + func (s *PackerSuit) TestMinBox() { t := s.T() t.Parallel()