github.com/yaoapp/kun@v0.9.0/maps/stranysync_test.go (about)

     1  package maps
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/yaoapp/kun/interfaces"
     9  )
    10  
    11  func TestStrAnySyncMake(t *testing.T) {
    12  
    13  	m1 := SyncOf(map[string]interface{}{
    14  		"foo": "bar",
    15  		"nested": map[string]interface{}{
    16  			"foo": "bar",
    17  		},
    18  	})
    19  	assert.Equal(t, "bar", m1.Get("foo"))
    20  	assert.Equal(t, map[string]interface{}{"foo": "bar"}, m1.Get("nested"))
    21  
    22  	assert.IsType(t, MapStrAnySync{}, MakeSync())
    23  	assert.IsType(t, MapStrAnySync{}, MakeMapSync())
    24  	assert.IsType(t, MapStrAnySync{}, MakeMapStrSync())
    25  	assert.IsType(t, MapStrAnySync{}, MakeStrSync())
    26  	assert.IsType(t, MapStrAnySync{}, MakeStrAnySync())
    27  	assert.IsType(t, MapStrAnySync{}, MakeMapStrAnySync())
    28  	assert.IsType(t, MapStrAnySync{}, SyncOf(map[string]interface{}{"foo": "bar"}))
    29  	assert.IsType(t, MapStrAnySync{}, MapSyncOf(map[string]interface{}{"foo": "bar"}))
    30  	assert.IsType(t, MapStrAnySync{}, MapStrSyncOf(map[string]interface{}{"foo": "bar"}))
    31  	assert.IsType(t, MapStrAnySync{}, StrSyncOf(map[string]interface{}{"foo": "bar"}))
    32  	assert.IsType(t, MapStrAnySync{}, StrAnySyncOf(map[string]interface{}{"foo": "bar"}))
    33  	assert.IsType(t, MapStrAnySync{}, MapStrAnySyncOf(map[string]interface{}{"foo": "bar"}))
    34  }
    35  
    36  func TestStrAnySyncSetBasic(t *testing.T) {
    37  	basic, _, _, _, _ := prepareTestingData()
    38  	m := MakeMapStrAnySync()
    39  	for key, value := range basic {
    40  		m.Set(key, value)
    41  	}
    42  	if assert.Equal(t, 16, m.Len(), "The length of map should be 16") {
    43  		checkBaiscValues(t, m)
    44  	}
    45  }
    46  
    47  func TestStrAnySyncSetBasicConcurrent(t *testing.T) {
    48  	basic, _, _, _, _ := prepareTestingData()
    49  	m := MakeMapStrAnySync()
    50  	var wg sync.WaitGroup
    51  	for key, value := range basic {
    52  		wg.Add(1)
    53  		go func(k string, v interface{}) {
    54  			defer wg.Done()
    55  			m.Set(k, v)
    56  		}(key, value)
    57  	}
    58  	wg.Wait()
    59  	if assert.Equal(t, 16, m.Len(), "The length of map should be 16") {
    60  		checkBaiscValues(t, m)
    61  	}
    62  }
    63  
    64  func TestStrAnySyncSetAll(t *testing.T) {
    65  	_, _, _, _, all := prepareTestingData()
    66  	m := MakeMapStrAnySync()
    67  	for key, value := range all {
    68  		m.Set(key, value)
    69  	}
    70  	if assert.Equal(t, 22, m.Len(), "The length of map should be 22") {
    71  		checkBaiscValues(t, m)
    72  		checkArrayValues(t, m)
    73  		checkSliceValues(t, m)
    74  		checkMapValues(t, m)
    75  		checkNestedValues(t, m)
    76  		checkStructValues(t, m)
    77  	}
    78  }
    79  
    80  func TestStrAnySyncSetAllConcurrent(t *testing.T) {
    81  	_, _, _, _, all := prepareTestingData()
    82  	m := MakeMapStrAnySync()
    83  	var wg sync.WaitGroup
    84  	for key, value := range all {
    85  		wg.Add(1)
    86  		go func(k string, v interface{}) {
    87  			defer wg.Done()
    88  			m.Set(k, v)
    89  		}(key, value)
    90  	}
    91  	wg.Wait()
    92  	if assert.Equal(t, 22, m.Len(), "The length of map should be 22") {
    93  		checkBaiscValues(t, m)
    94  		checkArrayValues(t, m)
    95  		checkSliceValues(t, m)
    96  		checkMapValues(t, m)
    97  		checkNestedValues(t, m)
    98  		checkStructValues(t, m)
    99  	}
   100  }
   101  
   102  func TestStrAnySyncKeys(t *testing.T) {
   103  	_, _, _, _, all := prepareTestingData()
   104  	keys := SyncOf(all).Keys()
   105  	assert.Equal(t, []string{
   106  		"arrayint64",
   107  		"bool",
   108  		"byte",
   109  		"float32",
   110  		"float64",
   111  		"int",
   112  		"int16",
   113  		"int32",
   114  		"int64",
   115  		"int8",
   116  		"mapint64",
   117  		"nested",
   118  		"nested2",
   119  		"sliceint64",
   120  		"string",
   121  		"struct",
   122  		"uint",
   123  		"uint16",
   124  		"uint32",
   125  		"uint64",
   126  		"uint8",
   127  		"uintptr"}, keys)
   128  }
   129  
   130  func TestStrAnySyncKeysValues(t *testing.T) {
   131  	_, _, _, _, all := prepareTestingData()
   132  	m := SyncOf(all)
   133  	keys := m.Keys()
   134  	values := m.Values()
   135  	if assert.Equal(t, 22, len(keys), "The length of map should be 21") {
   136  		for i := 0; i < 22; i++ {
   137  			key := keys[i]
   138  			value := values[i]
   139  			assert.Equal(t, value, m.Get(key))
   140  		}
   141  	}
   142  }
   143  
   144  func TestStrAnySyncRange(t *testing.T) {
   145  	_, _, _, _, all := prepareTestingData()
   146  	m := SyncOf(all)
   147  	values := []interface{}{}
   148  	m.Range(func(key string, value interface{}) bool {
   149  		values = append(values, value)
   150  		return true
   151  	})
   152  	assert.Equal(t, m.Len(), len(values))
   153  
   154  	values = []interface{}{}
   155  	m.Range(func(key string, value interface{}) bool {
   156  		values = append(values, value)
   157  		return false
   158  	})
   159  	assert.Equal(t, 1, len(values))
   160  }
   161  
   162  func TestStrAnySyncFlatten(t *testing.T) {
   163  	_, _, _, _, all := prepareTestingData()
   164  	flatten := SyncOf(all).Flatten()
   165  	if assert.Equal(t, 62, flatten.Len(), "The length of map should be 62") {
   166  		values := flatten.Values()
   167  		for i, key := range flatten.Keys() {
   168  			assert.Equal(t, values[i], flatten.Get(key))
   169  		}
   170  	}
   171  }
   172  
   173  func TestStrAnySyncHas(t *testing.T) {
   174  	_, _, _, _, all := prepareTestingData()
   175  	flatten := SyncOf(all).Flatten()
   176  	keys := flatten.Keys()
   177  	if assert.Equal(t, 62, len(keys), "The length of keys should be 62") {
   178  		for _, key := range keys {
   179  			assert.True(t, flatten.Has(key))
   180  		}
   181  	}
   182  	assert.False(t, flatten.Has("not_existed_key"))
   183  }
   184  
   185  func TestStrSyncAnyDel(t *testing.T) {
   186  	_, _, _, _, all := prepareTestingData()
   187  	m := SyncOf(all)
   188  	keys := m.Keys()
   189  	if assert.Equal(t, 22, len(keys), "The length of keys should be 22") {
   190  		for _, key := range keys {
   191  			m.Del(key)
   192  		}
   193  	}
   194  	assert.Equal(t, 0, m.Len())
   195  }
   196  
   197  func TestStrAnySyncGetAndDel(t *testing.T) {
   198  	_, _, _, _, all := prepareTestingData()
   199  	m := SyncOf(all)
   200  	keys := m.Keys()
   201  	valuesBefore := m.Values()
   202  	valuesAfter := []interface{}{}
   203  	if assert.Equal(t, 22, len(keys), "The length of keys should be 22") {
   204  		for _, key := range keys {
   205  			valuesAfter = append(valuesAfter, m.GetAndDel(key))
   206  		}
   207  	}
   208  	assert.Equal(t, 0, m.Len())
   209  	assert.Equal(t, 0, len(m.Values()))
   210  	assert.Equal(t, valuesBefore, valuesAfter)
   211  	assert.Nil(t, m.GetAndDel("not-exists"))
   212  }
   213  
   214  func TestStrAnySyncGetOrSet(t *testing.T) {
   215  	_, _, _, _, all := prepareTestingData()
   216  	m := SyncOf(all)
   217  	keys := m.Keys()
   218  	valuesBefore := m.Values()
   219  	valuesAfter := []interface{}{}
   220  	if assert.Equal(t, 22, len(keys), "The length of keys should be 22") {
   221  		for _, key := range keys {
   222  			valuesAfter = append(valuesAfter, m.GetOrSet(key, "getorset"))
   223  		}
   224  	}
   225  	assert.Equal(t, 22, m.Len())
   226  	assert.Equal(t, valuesBefore, valuesAfter)
   227  
   228  	value := m.GetOrSet("new-key", "getorset")
   229  	assert.Equal(t, 23, m.Len())
   230  	assert.Equal(t, value, "getorset")
   231  	assert.Equal(t, value, m.Get("new-key"))
   232  }
   233  
   234  func TestStrAnySyncIsEmpty(t *testing.T) {
   235  	_, _, _, _, all := prepareTestingData()
   236  	m := SyncOf(all)
   237  	assert.False(t, m.IsEmpty())
   238  
   239  	keys := m.Keys()
   240  	if assert.Equal(t, 22, len(keys), "The length of keys should be 22") {
   241  		for _, key := range keys {
   242  			m.Del(key)
   243  		}
   244  	}
   245  	assert.True(t, m.IsEmpty())
   246  }
   247  
   248  func TestStrAnySyncMerge(t *testing.T) {
   249  	basic, array, slice, _, _ := prepareTestingData()
   250  	m := SyncOf(basic)
   251  
   252  	var new interfaces.MapStr = SyncOf(array)
   253  	m.Merge(new)
   254  	if assert.Equal(t, 16, m.Len(), "The length of keys should be 22") {
   255  		assert.Equal(t, int64(64), m.Dot().Get("int64.0"))
   256  		assert.Equal(t, int64(64), m.Dot().Get("int64.1"))
   257  	}
   258  
   259  	var new2 interfaces.MapStr = Of(slice)
   260  	m.Merge(new, new2)
   261  	if assert.Equal(t, 16, m.Len(), "The length of keys should be 22") {
   262  		assert.Equal(t, int64(64), m.Dot().Get("int64.0"))
   263  		assert.Equal(t, int64(64), m.Dot().Get("int64.1"))
   264  		assert.Equal(t, int64(64), m.Dot().Get("int64.2"))
   265  	}
   266  }