97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDecomposeTree(t *testing.T) {
|
|
smallTree := getSmallTree()
|
|
singleNodeTree := getSingleNodeTree()
|
|
deepTree := getDeepTree()
|
|
wideTree := getWideTree()
|
|
assimmetricTree := getAssimmetricTree()
|
|
for name, tt := range map[string]struct {
|
|
inputTree map[string]*WeightedNode
|
|
inputThreshold uint16
|
|
outputParts []SubTree
|
|
err string
|
|
}{
|
|
"Normal tree 3x3": {
|
|
inputTree: smallTree.TreeWeighted,
|
|
inputThreshold: smallTree.Threshold,
|
|
outputParts: smallTree.Parts,
|
|
},
|
|
"Single node short tree": {
|
|
inputTree: singleNodeTree.TreeWeighted,
|
|
inputThreshold: singleNodeTree.Threshold,
|
|
outputParts: singleNodeTree.Parts,
|
|
},
|
|
"Single node short tree with small threshold": {
|
|
inputTree: singleNodeTree.TreeWeighted,
|
|
inputThreshold: 10,
|
|
err: "[GetNodePath]: threshold too small",
|
|
},
|
|
"Single branch deep tree": {
|
|
inputTree: deepTree.TreeWeighted,
|
|
inputThreshold: deepTree.Threshold,
|
|
outputParts: deepTree.Parts,
|
|
},
|
|
"Wide tree": {
|
|
inputTree: wideTree.TreeWeighted,
|
|
inputThreshold: wideTree.Threshold,
|
|
outputParts: wideTree.Parts,
|
|
},
|
|
"Asymmetric tree": {
|
|
inputTree: assimmetricTree.TreeWeighted,
|
|
inputThreshold: assimmetricTree.Threshold,
|
|
outputParts: assimmetricTree.Parts,
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
result, err := DecomposeTree(tt.inputTree, tt.inputThreshold)
|
|
if tt.err == "" {
|
|
require.NoError(t, err)
|
|
require.ElementsMatch(t, result, tt.outputParts)
|
|
} else {
|
|
require.EqualError(t, err, tt.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetNodePath(t *testing.T) {
|
|
testTree := getSmallTree()
|
|
for name, tt := range map[string]struct {
|
|
inputTree map[string]*WeightedNode
|
|
inputCurrentNodeID string
|
|
outputPath string
|
|
outputErr string
|
|
}{
|
|
"Normal path": {
|
|
inputTree: testTree.TreeWeighted,
|
|
inputCurrentNodeID: "1.2.3.3",
|
|
outputPath: "/folder2/folder3",
|
|
},
|
|
"Short path": {
|
|
inputTree: testTree.TreeWeighted,
|
|
inputCurrentNodeID: "1.0.0.0",
|
|
outputPath: "/",
|
|
},
|
|
"Not existent node": {
|
|
inputTree: testTree.TreeWeighted,
|
|
inputCurrentNodeID: "qwwrwteyu",
|
|
outputErr: "node not exists",
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
path, err := GetNodePath(tt.inputTree, tt.inputCurrentNodeID)
|
|
require.Equal(t, tt.outputPath, path)
|
|
if tt.outputErr != "" {
|
|
require.EqualError(t, err, tt.outputErr)
|
|
}
|
|
})
|
|
}
|
|
}
|