github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/list/singly_linkedlist_ginkgo_test.go (about)

     1  package list
     2  
     3  import (
     4  	"container/list"
     5  	"github.com/stretchr/testify/assert"
     6  	"testing"
     7  )
     8  
     9  func TestSinglyLinkedList(t *testing.T) {
    10  	l1 := NewSinglyLinkedList[int]()
    11  	assert.NotNil(t, l1)
    12  	elements := l1.AppendValue(1, 2, 3)
    13  	assert.Equal(t, int64(3), l1.Len())
    14  	l1.ForEach(func(idx int64, e NodeElement[int]) {
    15  		t.Logf("node element: %v\n", e.GetValue())
    16  	})
    17  
    18  	l2 := list.New()
    19  	l2.PushBack(1)
    20  	_l2_e2 := l2.PushBack(2)
    21  	l2.PushBack(3)
    22  	assert.Equal(t, int64(3), int64(l2.Len()))
    23  
    24  	l1Itr := l1.(*singlyLinkedList[int]).getRootHead()
    25  	l2Itr := l2.Front()
    26  	for l2Itr != nil {
    27  		assert.Equal(t, l1Itr.GetValue(), l2Itr.Value)
    28  		l2Itr = l2Itr.Next()
    29  		l1Itr = l1Itr.GetNext()
    30  	}
    31  
    32  	l1.Remove(elements[1])
    33  	l2.Remove(_l2_e2)
    34  	assert.Equal(t, int64(2), l1.Len())
    35  	l1Itr = l1.(*singlyLinkedList[int]).getRootHead()
    36  	l2Itr = l2.Front()
    37  	for l2Itr != nil {
    38  		assert.Equal(t, l1Itr.GetValue(), l2Itr.Value)
    39  		l2Itr = l2Itr.Next()
    40  		l1Itr = l1Itr.GetNext()
    41  	}
    42  
    43  }