github.com/duomi520/utils@v0.0.0-20240430123446-e03a4cddd6ec/queue_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"log"
     5  	"testing"
     6  )
     7  
     8  type lockListTemp struct {
     9  	i int
    10  }
    11  
    12  func (l lockListTemp) equal(obj any) bool {
    13  	return l.i == obj.(lockListTemp).i
    14  }
    15  func TestCopyOnWriteList(t *testing.T) {
    16  	l := NewCopyOnWriteList()
    17  	for i := 0; i < 10; i++ {
    18  		l.Add(lockListTemp{i})
    19  	}
    20  	var tests = []struct {
    21  		arg    int
    22  		result []int
    23  	}{
    24  		{5, []int{0, 1, 2, 3, 4, 6, 7, 8, 9}},
    25  		{2, []int{0, 1, 3, 4, 6, 7, 8, 9}},
    26  		{9, []int{0, 1, 3, 4, 6, 7, 8}},
    27  		{0, []int{1, 3, 4, 6, 7, 8}},
    28  		{10, []int{1, 3, 4, 6, 7, 8}},
    29  	}
    30  	for _, v := range tests {
    31  		l.Remove(lockListTemp{v.arg}.equal)
    32  		temp := l.List()
    33  		if len(v.result) != len(l.List()) {
    34  			log.Fatalln(v, temp)
    35  		}
    36  		for i := range temp {
    37  			if v.result[i] != temp[i].(lockListTemp).i {
    38  				log.Fatalln(i, v, temp)
    39  			}
    40  		}
    41  	}
    42  }
    43  func BenchmarkCopyOnWriteList(b *testing.B) {
    44  	l := NewCopyOnWriteList()
    45  	for i := 0; i < 10; i++ {
    46  		l.Add(lockListTemp{i})
    47  	}
    48  	for i := 0; i < b.N; i++ {
    49  		_ = l.List()
    50  	}
    51  }