github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/sets/set_fixed_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package setutils_test
     4  
     5  import (
     6  	setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
     7  	"github.com/iwind/TeaGo/assert"
     8  	"github.com/iwind/TeaGo/rands"
     9  	"testing"
    10  )
    11  
    12  func TestNewFixedSet(t *testing.T) {
    13  	var a = assert.NewAssertion(t)
    14  
    15  	{
    16  		var set = setutils.NewFixedSet(0)
    17  		set.Push(1)
    18  		set.Push(2)
    19  		set.Push(2)
    20  		a.IsTrue(set.Size() == 2)
    21  		a.IsTrue(set.Has(1))
    22  		a.IsTrue(set.Has(2))
    23  	}
    24  
    25  	{
    26  		var set = setutils.NewFixedSet(1)
    27  		set.Push(1)
    28  		set.Push(2)
    29  		set.Push(3)
    30  		a.IsTrue(set.Size() == 1)
    31  		a.IsFalse(set.Has(1))
    32  		a.IsTrue(set.Has(3))
    33  		a.IsFalse(set.Has(4))
    34  	}
    35  }
    36  
    37  func TestFixedSet_Reset(t *testing.T) {
    38  	var a = assert.NewAssertion(t)
    39  
    40  	var set = setutils.NewFixedSet(3)
    41  	set.Push(1)
    42  	set.Push(2)
    43  	set.Push(3)
    44  	set.Reset()
    45  	a.IsTrue(set.Size() == 0)
    46  }
    47  
    48  func BenchmarkFixedSet_Has(b *testing.B) {
    49  	var count = 1_000_000
    50  	var set = setutils.NewFixedSet(count)
    51  	for i := 0; i < count; i++ {
    52  		set.Push(i)
    53  	}
    54  
    55  	b.ResetTimer()
    56  
    57  	b.RunParallel(func(pb *testing.PB) {
    58  		for pb.Next() {
    59  			set.Has(rands.Int(0, 100_000))
    60  		}
    61  	})
    62  }