github.com/GoWebProd/gip@v0.0.0-20230623090727-b60d41d5d320/smap/map_test.go (about)

     1  package smap
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func TestMap(t *testing.T) {
     9  	var m Map[string, int]
    10  
    11  	i1 := 1
    12  	i2 := 2
    13  
    14  	m.Store("test", i1)
    15  	m.Store("test2", i2)
    16  
    17  	val, ok := m.Load("test")
    18  	if !ok {
    19  		t.Fatal("value not found in map")
    20  	}
    21  
    22  	if val != i1 {
    23  		t.Fatal("invalid value:", val)
    24  	}
    25  }
    26  
    27  func BenchmarkMap(b *testing.B) {
    28  	var m Map[string, int]
    29  
    30  	for i := 0; i < b.N; i++ {
    31  		m.Store("test", i)
    32  
    33  		val, ok := m.Load("test")
    34  		if !ok {
    35  			b.Fatal("value not found in map")
    36  		}
    37  
    38  		if val != i {
    39  			b.Fatal("invalid value:", val)
    40  		}
    41  
    42  		m.Delete("test")
    43  	}
    44  }
    45  
    46  func BenchmarkSyncMap(b *testing.B) {
    47  	var m sync.Map
    48  
    49  	i1 := 1
    50  
    51  	for i := 0; i < b.N; i++ {
    52  		m.Store("test", &i1)
    53  
    54  		val, ok := m.Load("test")
    55  		if !ok {
    56  			b.Fatal("value not found in map")
    57  		}
    58  
    59  		if *(val.(*int)) != i1 {
    60  			b.Fatal("invalid value:", *(val.(*int)))
    61  		}
    62  
    63  		m.Delete("test")
    64  	}
    65  }