mirror of https://github.com/01-edu/public.git
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.
110 lines
3.1 KiB
110 lines
3.1 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
|
||
5 years ago
|
"../lib"
|
||
|
"./correct"
|
||
|
"./student"
|
||
5 years ago
|
)
|
||
|
|
||
|
func BTreeMinStu(root *student.TreeNode) *student.TreeNode {
|
||
|
if root == nil || root.Left == nil {
|
||
|
return root
|
||
|
}
|
||
|
return BTreeMinStu(root.Left)
|
||
|
}
|
||
|
|
||
5 years ago
|
func errorMessage_isbin(fn interface{}, root, a *correct.TreeNode, b *student.TreeNode) {
|
||
5 years ago
|
lib.Fatalf("%s(\n%s\n) == %s instead of %s\n",
|
||
5 years ago
|
"BTreeIsBinary",
|
||
5 years ago
|
correct.FormatTree(root),
|
||
5 years ago
|
b.Data,
|
||
|
a.Data,
|
||
|
)
|
||
|
}
|
||
|
|
||
5 years ago
|
func CompareNode_isbin(fn interface{}, arg1, a *correct.TreeNode, b *student.TreeNode) {
|
||
5 years ago
|
if a == nil || b == nil {
|
||
5 years ago
|
lib.Fatalf("Expected %v instead of %v\n", a, b)
|
||
5 years ago
|
}
|
||
|
if a.Data != b.Data {
|
||
5 years ago
|
errorMessage_isbin(fn, arg1, a, b)
|
||
5 years ago
|
}
|
||
5 years ago
|
if a.Parent != nil && b.Parent != nil && a.Parent.Data != b.Parent.Data {
|
||
|
errorMessage_isbin(fn, arg1, a, b)
|
||
5 years ago
|
lib.Fatalf("Expected parent value %v instead of %v\n", a.Parent.Data, b.Parent.Data)
|
||
5 years ago
|
}
|
||
5 years ago
|
if (a.Parent == nil && b.Parent != nil) || (a.Parent != nil && b.Parent == nil) {
|
||
5 years ago
|
lib.Fatalf("Expected parent value %v instead of %v\n", a.Parent, b.Parent)
|
||
5 years ago
|
}
|
||
5 years ago
|
if a.Right != nil && b.Right != nil && a.Right.Data != b.Right.Data {
|
||
|
errorMessage_isbin(fn, arg1, a, b)
|
||
5 years ago
|
lib.Fatalf("Expected right child value %v instead of %v\n", a.Right.Data, b.Right.Data)
|
||
5 years ago
|
}
|
||
|
if (a.Right == nil && b.Right != nil) || (a.Right != nil && b.Right == nil) {
|
||
5 years ago
|
lib.Fatalf("Expected right child value %v instead of %v\n", a.Right, b.Right)
|
||
5 years ago
|
}
|
||
|
if a.Left != nil && b.Left != nil && a.Left.Data != b.Left.Data {
|
||
|
errorMessage_isbin(fn, arg1, a, b)
|
||
5 years ago
|
lib.Fatalf("Expected left child value %v instead of %v\n", a.Left, b.Left)
|
||
5 years ago
|
}
|
||
|
if (a.Left == nil && b.Left != nil) || (a.Left != nil && b.Left == nil) {
|
||
5 years ago
|
lib.Fatalf("Expected left child value %v instead of %v\n", a, b)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func CompareReturn_isbin(fn1, fn2 interface{}, arg1 *correct.TreeNode, arg2 interface{}) {
|
||
5 years ago
|
arar1 := []interface{}{arg1}
|
||
|
arar2 := []interface{}{arg2}
|
||
|
|
||
5 years ago
|
out1 := lib.Monitor(fn1, arar1)
|
||
|
out2 := lib.Monitor(fn2, arar2)
|
||
5 years ago
|
|
||
|
for i, v := range out1.Results {
|
||
|
switch str := v.(type) {
|
||
5 years ago
|
case *correct.TreeNode:
|
||
5 years ago
|
CompareNode_isbin(fn1, arg1, str, out2.Results[i].(*student.TreeNode))
|
||
5 years ago
|
default:
|
||
|
if !reflect.DeepEqual(str, out2.Results[i]) {
|
||
5 years ago
|
lib.Fatalf("%s(\n%s) == %s instead of %s\n",
|
||
5 years ago
|
"BTreeIsBinary",
|
||
5 years ago
|
correct.FormatTree(arg1),
|
||
5 years ago
|
lib.Format(out2.Results...),
|
||
|
lib.Format(out1.Results...),
|
||
5 years ago
|
)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
root := &correct.TreeNode{Data: "04"}
|
||
5 years ago
|
rootS := &student.TreeNode{Data: "04"}
|
||
|
|
||
|
ins := []string{"01", "07", "05", "12", "02", "03", "10"}
|
||
|
|
||
|
for _, v := range ins {
|
||
5 years ago
|
root = correct.BTreeInsertData(root, v)
|
||
5 years ago
|
rootS = student.BTreeInsertData(rootS, v)
|
||
|
}
|
||
|
|
||
5 years ago
|
CompareReturn_isbin(correct.BTreeIsBinary, student.BTreeIsBinary, root, rootS)
|
||
5 years ago
|
|
||
5 years ago
|
rootNB := &correct.TreeNode{Data: "04"}
|
||
5 years ago
|
rootNB_stu := &student.TreeNode{Data: "04"}
|
||
5 years ago
|
// Test a non-binarysearch tree
|
||
5 years ago
|
for _, v := range ins {
|
||
5 years ago
|
rootNB = correct.BTreeInsertData(rootNB, v)
|
||
5 years ago
|
rootNB_stu = student.BTreeInsertData(rootNB_stu, v)
|
||
|
}
|
||
|
|
||
5 years ago
|
min := correct.BTreeMin(rootNB)
|
||
5 years ago
|
minStu := BTreeMinStu(rootNB_stu)
|
||
|
|
||
5 years ago
|
min.Left = &correct.TreeNode{Data: "123"}
|
||
5 years ago
|
minStu.Left = &student.TreeNode{Data: "123"}
|
||
|
|
||
5 years ago
|
CompareReturn_isbin(correct.BTreeIsBinary, student.BTreeIsBinary, rootNB, rootNB_stu)
|
||
5 years ago
|
}
|