You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
1.9 KiB

package main_test
import (
"testing"
ks "git.jxs.me/r/knapsack"
"github.com/carlmjohnson/be"
)
func TestDefaultsZero(t *testing.T) {
result := ks.MaxValue(0, []ks.Book{})
be.Equal(t, 0, result)
}
func TestEmpty(t *testing.T) {
result := ks.DynamicMaxValue(1, []ks.Book{})
be.Equal(t, 0, result)
}
func TestFitsOne(t *testing.T) {
result := ks.MaxValue(1, []ks.Book{
{Weight: 1, Value: 2},
})
be.Equal(t, 2, result)
}
func TestFitsHigher(t *testing.T) {
result := ks.MaxValue(1, []ks.Book{
{Weight: 1, Value: 2},
{Weight: 1, Value: 3},
})
be.Equal(t, 3, result)
}
func TestPrefersMultiple(t *testing.T) {
result := ks.MaxValue(3, []ks.Book{
{Weight: 3, Value: 4},
{Weight: 1, Value: 1},
{Weight: 1, Value: 2},
{Weight: 1, Value: 3},
})
be.Equal(t, 6, result)
}
func TestIgnoresTooBig(t *testing.T) {
result := ks.MaxValue(1, []ks.Book{
{Weight: 10, Value: 4},
{Weight: 1, Value: 1},
})
be.Equal(t, 1, result)
}
// Dynamic
func TestDynamicDefaultsZero(t *testing.T) {
result := ks.DynamicMaxValue(0, []ks.Book{})
be.Equal(t, 0, result)
}
func TestDynamicFitsOne(t *testing.T) {
result := ks.DynamicMaxValue(1, []ks.Book{
{Weight: 1, Value: 2},
})
be.Equal(t, 2, result)
}
func TestDynamicFitsHigher(t *testing.T) {
result := ks.DynamicMaxValue(1, []ks.Book{
{Weight: 1, Value: 3},
{Weight: 1, Value: 2},
})
be.Equal(t, 3, result)
}
func TestDynamicPrefersMultiple(t *testing.T) {
result := ks.DynamicMaxValue(3, []ks.Book{
{Weight: 3, Value: 4},
{Weight: 1, Value: 1},
{Weight: 1, Value: 2},
{Weight: 1, Value: 3},
})
be.Equal(t, 6, result)
}
func TestDynamicIgnoresTooBig(t *testing.T) {
result := ks.DynamicMaxValue(1, []ks.Book{
{Weight: 10, Value: 4},
{Weight: 1, Value: 1},
})
be.Equal(t, 1, result)
}
func TestDynamicEmpty(t *testing.T) {
result := ks.DynamicMaxValue(1, []ks.Book{})
be.Equal(t, 0, result)
}