sointu/go4k/algorithms_test.go
Veikko Sariola e36aea59a5 feat(go4k): Algorithm to construct small delay times tables by abusing overlapping of different delay times.
The problem of finding a string that contains all particular substrings is the "shortest superstring problem"; it is NP-hard and analogous to traveling salesman problem. We use simple greedy search instead of trying to find true optimum. But even with these algorithm, units that use exactly the same delay times will always appear only once in the delay times table.
2020-11-09 22:29:10 +02:00

37 lines
1.3 KiB
Go

package go4k_test
import (
"fmt"
"reflect"
"testing"
"github.com/vsariola/sointu/go4k"
)
func TestFindSuperIntArray(t *testing.T) {
var tests = []struct {
input [][]int
wantSuper []int
wantIndices []int
}{
{[][]int{}, []int{}, []int{}},
{[][]int{nil, nil}, []int{}, []int{0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}}, []int{1, 2, 3, 4, 5}, []int{2, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, nil}, []int{1, 2, 3, 4, 5}, []int{2, 0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, {}}, []int{1, 2, 3, 4, 5}, []int{2, 0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, {2, 3}}, []int{1, 2, 3, 4, 5}, []int{2, 0, 1}},
{[][]int{{1, 2, 3, 4, 5}, {1, 2, 3}}, []int{1, 2, 3, 4, 5}, []int{0, 0}},
{[][]int{{1, 2, 3, 4, 5}, {2, 3}}, []int{1, 2, 3, 4, 5}, []int{0, 1}},
{[][]int{{1, 2, 3, 4, 5}, {2, 3}, {5, 6, 7}}, []int{1, 2, 3, 4, 5, 6, 7}, []int{0, 1, 4}},
{[][]int{{1, 2, 3, 4}, {3, 4, 1}, {2, 3, 4, 5}}, []int{3, 4, 1, 2, 3, 4, 5}, []int{2, 0, 3}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("TestFindSuperIntArray %d", i), func(t *testing.T) {
super, indices := go4k.FindSuperIntArray(tt.input)
if !reflect.DeepEqual(super, tt.wantSuper) || !reflect.DeepEqual(indices, tt.wantIndices) {
t.Errorf("FindSuperIntArray(%v) got (%v,%v), want (%v,%v)", tt.input, super, indices, tt.wantSuper, tt.wantIndices)
}
})
}
}