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

     1  package esync
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"math/rand"
     6  	"testing"
     7  )
     8  
     9  func TestAtomicFloat32(t *testing.T) {
    10  	var a AtomicFloat32
    11  
    12  	for i := 0; i < 100; i++ {
    13  		f := rand.Float32()
    14  		a.Set(f)
    15  		assert.True(t, a.Get() == f)
    16  		assert.True(t, a.Swap(0) == f, a.Get() == 0)
    17  	}
    18  	multiRun(10, func() {
    19  		for i := 0; i < 1000000; i++ {
    20  			a.Set(123.45)
    21  			_ = a.Swap(1111111.1)
    22  			a.Get()
    23  		}
    24  	})
    25  	t.Log(a.String())
    26  }
    27  
    28  // BenchmarkAtomicFloat32_Set-12    	188348179	         6.284 ns/op
    29  func BenchmarkAtomicFloat32_Set(b *testing.B) {
    30  	var a AtomicFloat32
    31  	for i := 0; i < b.N; i++ {
    32  		a.Set(float32(i))
    33  	}
    34  }
    35  
    36  // BenchmarkAtomicFloat32_Get-12    	1000000000	         0.5498 ns/op
    37  func BenchmarkAtomicFloat32_Get(b *testing.B) {
    38  	var a AtomicFloat32
    39  	for i := 0; i < b.N; i++ {
    40  		_ = a.Get()
    41  	}
    42  }
    43  
    44  // BenchmarkAtomicFloat32_Swap-12    	189897183	         6.165 ns/op
    45  func BenchmarkAtomicFloat32_Swap(b *testing.B) {
    46  	var a AtomicFloat32
    47  	for i := 0; i < b.N; i++ {
    48  		_ = a.Swap(float32(i))
    49  	}
    50  }