~rbn/fit

ref: 257085b3f4eff0bdc980dac54db6d92a71c24621 fit/bin_test.go -rw-r--r-- 886 bytes
257085b3Ruben Schuller go.mod whoops 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package fit

import (
	"sort"
	"testing"
)

func TestBinAdd(t *testing.T) {
	b := newBin(10)
	x := testItem(3)
	y := testItem(4)
	b.add(&x)
	b.add(&y)

	if len(b.items) != 2 {
		t.Fail()
	}

	if b.items[0].Size() != 3 || b.items[1].Size() != 4 {
		t.Fail()
	}
}

func TestBinFits(t *testing.T) {
	b := newBin(10)
	x := testItem(3)
	y := testItem(8)
	z := testItem(6)
	b.add(&x)
	if b.fits(&y) {
		t.Fail()
	}

	if !b.fits(&z) {
		t.Fail()
	}
}

func TestBinItems(t *testing.T) {
	b := newBin(10)
	x := testItem(3)
	y := testItem(4)
	b.add(&x)
	b.add(&y)
	if b.Items()[0].Size() != 3 || b.Items()[1].Size() != 4 {
		t.Fail()
	}
}

func TestBinsBySize(t *testing.T) {
	bins := []Bin{Bin{capacity: 3, size: 3}, Bin{capacity: 2, size: 2}, Bin{capacity: 1, size: 1}}
	sort.Sort(BinsBySize(bins))

	if bins[0].capacity != 1 || bins[1].capacity != 2 || bins[2].capacity != 3 {
		t.Fail()
	}
}