tests/weighting_n_cutting_the_tree/cutting_test.go

96 lines
2.5 KiB
Go

package main
import (
"github.com/stretchr/testify/require"
"testing"
)
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
}{
"Single node short tree with small threshold": {
inputTree: singleNodeTree.TreeWeighted,
inputThreshold: 10,
err: "[GetNodePath]: threshold too small",
},
"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 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)
}
})
}
}