github.com/Nigel2392/go-datastructures@v1.1.5/linkedlist/linkedlist_test.go (about)

     1  package linkedlist_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Nigel2392/go-datastructures/linkedlist"
     7  )
     8  
     9  func TestSingly(t *testing.T) {
    10  	var s = new(linkedlist.Singly[string])
    11  	var letters = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    12  	for _, l := range letters {
    13  		s.Prepend(l)
    14  	}
    15  	var i = 0
    16  	for n := s.Head(); n != nil; n = n.Next() {
    17  		if n.Value() != letters[len(letters)-1-i] {
    18  			t.Errorf("Expected %s, got %s", letters[len(letters)-1-i], n.Value())
    19  		}
    20  		i++
    21  	}
    22  
    23  	for i := 0; i < 13; i++ {
    24  		var letter = s.Shift()
    25  		if letter != letters[len(letters)-1-i] {
    26  			t.Errorf("Expected %s, got %s", letters[len(letters)-1-i], letter)
    27  		}
    28  	}
    29  
    30  	if s.Len() != len(letters)/2 {
    31  		t.Errorf("Expected length %d, got %d", len(letters)/2, s.Len())
    32  	}
    33  
    34  	t.Log(s)
    35  }
    36  
    37  func TestDoubly(t *testing.T) {
    38  	var d = new(linkedlist.Doubly[string])
    39  	var letters = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
    40  	for _, l := range letters {
    41  		d.Append(l)
    42  	}
    43  	var i = 0
    44  	for n := d.Head(); n != nil; n = n.Next() {
    45  		if n.Value() != letters[i] {
    46  			t.Errorf("Expected %s, got %s", letters[i], n.Value())
    47  		}
    48  		i++
    49  	}
    50  
    51  	for i := 0; i < 13; i++ {
    52  		var letter = d.Pop()
    53  		if letter != letters[len(letters)-1-i] {
    54  			t.Errorf("Expected %s, got %s", letters[len(letters)-1-i], letter)
    55  		}
    56  	}
    57  
    58  	if d.Len() != len(letters)/2 {
    59  		t.Errorf("Expected length %d, got %d", len(letters)/2, d.Len())
    60  	}
    61  
    62  	t.Log(d)
    63  }