github.com/easierway/concurrent_map@v1.0.0/concurrent_map_test.go (about)

     1  package concurrent_map
     2  
     3  import (
     4  	"math/rand"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestBasicOPs(t *testing.T) {
    11  	for i := 0; i < 10; i++ {
    12  		testV := rand.Intn(1000)
    13  		m := CreateConcurrentMap(99)
    14  		v, ok := m.Get(StrKey("Hello"))
    15  		if v != nil || ok != false {
    16  			t.Error("init/get failed")
    17  		}
    18  		m.Set(StrKey("Hello"), testV)
    19  		v, ok = m.Get(StrKey("Hello"))
    20  		if v.(int) != testV || ok != true {
    21  			t.Error("set/get failed.")
    22  		}
    23  		m.Del(StrKey("Hello"))
    24  		v, ok = m.Get(StrKey("Hello"))
    25  		if v != nil || ok != false {
    26  			t.Error("del failed")
    27  		}
    28  	}
    29  }
    30  
    31  func TestInt64KeyBasicOPs(t *testing.T) {
    32  	for i := 0; i < 10; i++ {
    33  		testV := rand.Int63n(1024)
    34  		cm := CreateConcurrentMap(99)
    35  		var key int64 = 1023
    36  		v, ok := cm.Get(I64Key(key))
    37  		if v != nil || ok != false {
    38  			t.Error("init/get failed")
    39  		}
    40  		cm.Set(I64Key(key), testV)
    41  		v, ok = cm.Get(I64Key(key))
    42  		if v.(int64) != testV || ok != true {
    43  			t.Error("set/get failed.")
    44  		}
    45  		cm.Del(I64Key(key))
    46  		v, ok = cm.Get(I64Key(key))
    47  		if v != nil || ok != false {
    48  			t.Error("del failed")
    49  		}
    50  	}
    51  }
    52  
    53  func TestInCurrentEnv(t *testing.T) {
    54  	m := CreateConcurrentMap(99)
    55  	go func() {
    56  
    57  		for i := 0; i < 100; i++ {
    58  			if v, ok := m.Get(StrKey(strconv.Itoa(i))); ok {
    59  				if v != i*i {
    60  					t.Error("Fail")
    61  				}
    62  			} else {
    63  				if v != nil {
    64  					t.Error("Fail")
    65  				}
    66  			}
    67  
    68  		}
    69  
    70  	}()
    71  	go func() {
    72  		for i := 0; i < 100; i++ {
    73  			m.Set(StrKey(strconv.Itoa(i)), i*i)
    74  		}
    75  
    76  	}()
    77  	time.Sleep(time.Second * 1)
    78  }