mirror of
https://github.com/vsariola/sointu.git
synced 2025-06-03 00:58:26 -04:00
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.
37 lines
1.3 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|