~rbn/fit

be89c47f35a510556bf10a989ba06db073c166f8 — Ruben Schuller 1 year, 9 months ago 277c36d
rename item field Path to Data and change type to []byte
5 files changed, 24 insertions(+), 21 deletions(-)

M cmd/fit/input.go
M cmd/fit/main.go
M item.go
M pack.go
M strategy_test.go
M cmd/fit/input.go => cmd/fit/input.go +1 -1
@@ 25,7 25,7 @@ func parseDu(r io.Reader) ([]fit.Item, error) {
			return nil, fmt.Errorf("invalid size in input: %v", s.Text())
		}

		items = append(items, fit.Item{Size: size, Path: fs[1]})
		items = append(items, fit.Item{Size: size, Data: []byte(fs[1])})
	}

	if err := s.Err(); err != nil {

M cmd/fit/main.go => cmd/fit/main.go +1 -1
@@ 15,7 15,7 @@ func printBins(bins []fit.Bin, w io.Writer) error {
	for _, bin := range bins {
		paths := []string{}
		for _, item := range bin.Items() {
			paths = append(paths, item.Path)
			paths = append(paths, string(item.Data))
		}
		_, err := fmt.Fprintln(w, strings.Join(paths, "\t"))
		if err != nil {

M item.go => item.go +1 -1
@@ 1,7 1,7 @@
package fit

type Item struct {
	Path string
	Data []byte
	Size int64
}


M pack.go => pack.go +1 -1
@@ 14,7 14,7 @@ func Pack(strategy StrategyFunc, capacity int64, items []Item) ([]Bin, error) {
		x, items = items[len(items)-1], items[:len(items)-1]

		if x.Size > capacity {
			return nil, fmt.Errorf("%v exceeds capacity: size: %v capacity: %v", x.Path, x.Size, capacity)
			return nil, fmt.Errorf("%v exceeds capacity: size: %v capacity: %v", string(x.Data), x.Size, capacity)
		}

		bins = strategy(bins, capacity, x)

M strategy_test.go => strategy_test.go +20 -17
@@ 1,6 1,7 @@
package fit

import (
	"bytes"
	"testing"
)



@@ 12,27 13,27 @@ type strategyTest struct {

var firstTests = []strategyTest{
	{
		item: Item{Path: "a", Size: 8},
		item: Item{Data: []byte("a"), Size: 8},
		bin:  0,
		pos:  0,
	},
	{
		item: Item{Path: "b", Size: 1},
		item: Item{Data: []byte("b"), Size: 1},
		bin:  0,
		pos:  1,
	},
	{
		item: Item{Path: "c", Size: 3},
		item: Item{Data: []byte("c"), Size: 3},
		bin:  1,
		pos:  0,
	},
	{
		item: Item{Path: "d", Size: 1},
		item: Item{Data: []byte("d"), Size: 1},
		bin:  0,
		pos:  2,
	},
	{
		item: Item{Path: "e", Size: 4},
		item: Item{Data: []byte("e"), Size: 4},
		bin:  1,
		pos:  1,
	},


@@ 41,27 42,27 @@ var firstTests = []strategyTest{
// one must swap the bins in head, as they are sorted in place. this isn't nice.
var bestTests = []strategyTest{
	{
		item: Item{Path: "a", Size: 6},
		item: Item{Data: []byte("a"), Size: 6},
		bin:  0,
		pos:  0,
	},
	{
		item: Item{Path: "b", Size: 8},
		item: Item{Data: []byte("b"), Size: 8},
		bin:  1,
		pos:  0,
	},
	{
		item: Item{Path: "c", Size: 1},
		item: Item{Data: []byte("c"), Size: 1},
		bin:  0,
		pos:  1,
	},
	{
		item: Item{Path: "d", Size: 2},
		item: Item{Data: []byte("d"), Size: 2},
		bin:  1,
		pos:  1,
	},
	{
		item: Item{Path: "e", Size: 1},
		item: Item{Data: []byte("e"), Size: 1},
		bin:  0,
		pos:  2,
	},


@@ 69,27 70,27 @@ var bestTests = []strategyTest{

var worstTests = []strategyTest{
	{
		item: Item{Path: "a", Size: 6},
		item: Item{Data: []byte("a"), Size: 6},
		bin:  0,
		pos:  0,
	},
	{
		item: Item{Path: "b", Size: 8},
		item: Item{Data: []byte("b"), Size: 8},
		bin:  1,
		pos:  0,
	},
	{
		item: Item{Path: "c", Size: 1},
		item: Item{Data: []byte("c"), Size: 1},
		bin:  0,
		pos:  1,
	},
	{
		item: Item{Path: "d", Size: 2},
		item: Item{Data: []byte("d"), Size: 2},
		bin:  0,
		pos:  2,
	},
	{
		item: Item{Path: "e", Size: 1},
		item: Item{Data: []byte("e"), Size: 1},
		bin:  0,
		pos:  1,
	},


@@ 101,8 102,10 @@ func testStrategy(t *testing.T, strategy StrategyFunc, tests []strategyTest) {
	for _, test := range tests {
		bins = strategy(bins, 10, test.item)
		t.Logf("%#v",bins)
		if bins[test.bin].Items()[test.pos] != test.item {
			t.Logf("%+v %+v", test.item, bins[test.bin].Items()[test.pos])

		binItem := bins[test.bin].Items()[test.pos]
		if !bytes.Equal(binItem.Data, test.item.Data) || binItem.Size != test.item.Size {
			t.Logf("items differ: %+v %+v", binItem, test.item)
			t.Fail()
		}
	}