2021-11-20 00:18:31 +08:00
|
|
|
package tree
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPrint(t *testing.T) {
|
|
|
|
|
rr1 := dns.A{
|
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
|
Name: dns.Fqdn("server1.example.com"),
|
|
|
|
|
Rrtype: 1,
|
|
|
|
|
Class: 1,
|
|
|
|
|
Ttl: 3600,
|
|
|
|
|
Rdlength: 0,
|
|
|
|
|
},
|
|
|
|
|
A: net.IPv4(10, 0, 1, 1),
|
|
|
|
|
}
|
|
|
|
|
rr2 := dns.A{
|
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
|
Name: dns.Fqdn("server2.example.com"),
|
|
|
|
|
Rrtype: 1,
|
|
|
|
|
Class: 1,
|
|
|
|
|
Ttl: 3600,
|
|
|
|
|
Rdlength: 0,
|
|
|
|
|
},
|
|
|
|
|
A: net.IPv4(10, 0, 1, 2),
|
|
|
|
|
}
|
|
|
|
|
rr3 := dns.A{
|
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
|
Name: dns.Fqdn("server3.example.com"),
|
|
|
|
|
Rrtype: 1,
|
|
|
|
|
Class: 1,
|
|
|
|
|
Ttl: 3600,
|
|
|
|
|
Rdlength: 0,
|
|
|
|
|
},
|
|
|
|
|
A: net.IPv4(10, 0, 1, 3),
|
|
|
|
|
}
|
|
|
|
|
rr4 := dns.A{
|
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
|
Name: dns.Fqdn("server4.example.com"),
|
|
|
|
|
Rrtype: 1,
|
|
|
|
|
Class: 1,
|
|
|
|
|
Ttl: 3600,
|
|
|
|
|
Rdlength: 0,
|
|
|
|
|
},
|
|
|
|
|
A: net.IPv4(10, 0, 1, 4),
|
|
|
|
|
}
|
|
|
|
|
tree := Tree{
|
|
|
|
|
Root: nil,
|
|
|
|
|
Count: 0,
|
|
|
|
|
}
|
|
|
|
|
tree.Insert(&rr1)
|
|
|
|
|
tree.Insert(&rr2)
|
|
|
|
|
tree.Insert(&rr3)
|
|
|
|
|
tree.Insert(&rr4)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
build a LLRB tree, the height of the tree is 3, look like:
|
|
|
|
|
|
|
|
|
|
server2.example.com.
|
|
|
|
|
/ \
|
|
|
|
|
server1.example.com. server4.example.com.
|
|
|
|
|
/
|
|
|
|
|
server3.example.com.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-27 20:02:55 +03:00
|
|
|
f, err := os.CreateTemp(t.TempDir(), "print_test_tmp")
|
2021-11-20 00:18:31 +08:00
|
|
|
if err != nil {
|
2026-07-19 13:52:23 +03:00
|
|
|
t.Fatal(err)
|
2021-11-20 00:18:31 +08:00
|
|
|
}
|
2026-07-19 13:52:23 +03:00
|
|
|
// Redirect Print output to a temp file, then restore stdout and close
|
|
|
|
|
// the handle so TempDir cleanup can remove it on Windows.
|
|
|
|
|
stdout := os.Stdout
|
2021-11-20 00:18:31 +08:00
|
|
|
os.Stdout = f
|
2026-07-19 13:52:23 +03:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
os.Stdout = stdout
|
|
|
|
|
f.Close()
|
|
|
|
|
})
|
2021-11-20 00:18:31 +08:00
|
|
|
|
|
|
|
|
tree.Print()
|
|
|
|
|
/**
|
|
|
|
|
server2.example.com.
|
|
|
|
|
server1.example.com. server4.example.com.
|
|
|
|
|
server3.example.com.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-07-19 13:52:23 +03:00
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2021-11-20 00:18:31 +08:00
|
|
|
buf := make([]byte, 256)
|
2026-07-19 13:52:23 +03:00
|
|
|
if _, err := f.Read(buf); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2021-11-20 00:18:31 +08:00
|
|
|
}
|
|
|
|
|
height := strings.Count(string(buf), ". \n")
|
|
|
|
|
//Compare the height of the print with the actual height of the tree
|
|
|
|
|
if height != 3 {
|
|
|
|
|
t.Fatal("The number of rows is inconsistent with the actual number of rows in the tree itself.")
|
|
|
|
|
}
|
|
|
|
|
}
|