Co-authored-by: Hansen IT Solutions <gary@hansenits.com> Co-committed-by: Hansen IT Solutions <gary@hansenits.com>
This commit was merged in pull request #6.
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package traverse
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/hits/ExploreDNS/internal/dns"
|
||||
)
|
||||
|
||||
func TestNewStack(t *testing.T) {
|
||||
s := NewStack(10)
|
||||
if s.MaxDepth() != 10 {
|
||||
t.Errorf("MaxDepth = %d, want 10", s.MaxDepth())
|
||||
}
|
||||
if !s.IsEmpty() {
|
||||
t.Error("new stack should be empty")
|
||||
}
|
||||
if s.Len() != 0 {
|
||||
t.Errorf("Len = %d, want 0", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStackDefaultDepth(t *testing.T) {
|
||||
s := NewStack(0)
|
||||
if s.MaxDepth() != DefaultMaxDepth {
|
||||
t.Errorf("MaxDepth = %d, want %d", s.MaxDepth(), DefaultMaxDepth)
|
||||
}
|
||||
|
||||
s = NewStack(-5)
|
||||
if s.MaxDepth() != DefaultMaxDepth {
|
||||
t.Errorf("MaxDepth = %d, want %d", s.MaxDepth(), DefaultMaxDepth)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackPushPop(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
ref := NewReferral("example.com", dns.TypeA, ".", 0, 1.0, nil)
|
||||
|
||||
ok := s.Push(ref)
|
||||
if !ok {
|
||||
t.Error("Push should succeed")
|
||||
}
|
||||
if s.Len() != 1 {
|
||||
t.Errorf("Len = %d, want 1", s.Len())
|
||||
}
|
||||
|
||||
popped := s.Pop()
|
||||
if popped != ref {
|
||||
t.Error("popped referral should match pushed")
|
||||
}
|
||||
if s.Len() != 0 {
|
||||
t.Errorf("Len = %d, want 0", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackLIFO(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
r1 := NewReferral("a.com", dns.TypeA, ".", 0, 1.0, nil)
|
||||
r2 := NewReferral("b.com", dns.TypeA, ".", 1, 1.0, nil)
|
||||
r3 := NewReferral("c.com", dns.TypeA, ".", 2, 1.0, nil)
|
||||
|
||||
s.Push(r1)
|
||||
s.Push(r2)
|
||||
s.Push(r3)
|
||||
|
||||
if popped := s.Pop(); popped != r3 {
|
||||
t.Error("should pop r3 first (LIFO)")
|
||||
}
|
||||
if popped := s.Pop(); popped != r2 {
|
||||
t.Error("should pop r2 second")
|
||||
}
|
||||
if popped := s.Pop(); popped != r1 {
|
||||
t.Error("should pop r1 third")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackPushNil(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
ok := s.Push(nil)
|
||||
if ok {
|
||||
t.Error("Push(nil) should return false")
|
||||
}
|
||||
if s.Len() != 0 {
|
||||
t.Errorf("Len = %d, want 0", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackMaxDepth(t *testing.T) {
|
||||
s := NewStack(3)
|
||||
|
||||
r0 := NewReferral("a.com", dns.TypeA, ".", 0, 1.0, nil)
|
||||
r1 := NewReferral("b.com", dns.TypeA, ".", 1, 1.0, nil)
|
||||
r2 := NewReferral("c.com", dns.TypeA, ".", 2, 1.0, nil)
|
||||
r3 := NewReferral("d.com", dns.TypeA, ".", 3, 1.0, nil)
|
||||
|
||||
if !s.Push(r0) {
|
||||
t.Error("depth 0 should be accepted")
|
||||
}
|
||||
if !s.Push(r1) {
|
||||
t.Error("depth 1 should be accepted")
|
||||
}
|
||||
if !s.Push(r2) {
|
||||
t.Error("depth 2 should be accepted")
|
||||
}
|
||||
if s.Push(r3) {
|
||||
t.Error("depth 3 should be rejected (maxDepth=3)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackPopEmpty(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
if popped := s.Pop(); popped != nil {
|
||||
t.Error("Pop on empty stack should return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackPeek(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
if peek := s.Peek(); peek != nil {
|
||||
t.Error("Peek on empty stack should return nil")
|
||||
}
|
||||
|
||||
ref := NewReferral("example.com", dns.TypeA, ".", 0, 1.0, nil)
|
||||
s.Push(ref)
|
||||
|
||||
if peek := s.Peek(); peek != ref {
|
||||
t.Error("Peek should return top item")
|
||||
}
|
||||
if s.Len() != 1 {
|
||||
t.Errorf("Peek should not remove item, Len = %d, want 1", s.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackIsEmpty(t *testing.T) {
|
||||
s := NewStack(5)
|
||||
if !s.IsEmpty() {
|
||||
t.Error("new stack should be empty")
|
||||
}
|
||||
|
||||
s.Push(NewReferral("example.com", dns.TypeA, ".", 0, 1.0, nil))
|
||||
if s.IsEmpty() {
|
||||
t.Error("stack with item should not be empty")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user