github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/esync/sync_atomic_uint_test.go (about)

     1  package esync
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"testing"
     6  )
     7  
     8  func TestAtomicUint16(t *testing.T) {
     9  	var num AtomicUint16
    10  
    11  	assert.True(t, num.Inc() == 1)
    12  	assert.True(t, num.Add(2) == 3)
    13  	num.Set(100)
    14  	assert.True(t, num.Swap(200) == 100)
    15  	assert.True(t, num.Get() == 200)
    16  
    17  	num.Set(0)
    18  	for i := 1; i < 200000; i++ {
    19  		assert.True(t, num.Add(1) == uint16(i&0xFFFF))
    20  	}
    21  	multiRun(5, func() {
    22  		for i := 0; i < 200000; i++ {
    23  			num.Set(100)
    24  			_ = num.Swap(200)
    25  			num.Get()
    26  		}
    27  	})
    28  }
    29  
    30  // BenchmarkAtomicUint16-12    	255484669	         4.639 ns/op
    31  func BenchmarkAtomicUint16(b *testing.B) {
    32  	var num AtomicUint16
    33  	for i := 0; i < b.N; i++ {
    34  		num.Add(i)
    35  	}
    36  }