github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_unit_tree_map_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with gm file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gmap"
    13  	"github.com/wangyougui/gf/v2/frame/g"
    14  	"github.com/wangyougui/gf/v2/internal/json"
    15  	"github.com/wangyougui/gf/v2/test/gtest"
    16  	"github.com/wangyougui/gf/v2/util/gconv"
    17  	"github.com/wangyougui/gf/v2/util/gutil"
    18  )
    19  
    20  func Test_TreeMap_Var(t *testing.T) {
    21  	gtest.C(t, func(t *gtest.T) {
    22  		var m gmap.TreeMap
    23  		m.SetComparator(gutil.ComparatorString)
    24  		m.Set("key1", "val1")
    25  		t.Assert(m.Keys(), []interface{}{"key1"})
    26  
    27  		t.Assert(m.Get("key1"), "val1")
    28  		t.Assert(m.Size(), 1)
    29  		t.Assert(m.IsEmpty(), false)
    30  
    31  		t.Assert(m.GetOrSet("key2", "val2"), "val2")
    32  		t.Assert(m.SetIfNotExist("key2", "val2"), false)
    33  
    34  		t.Assert(m.SetIfNotExist("key3", "val3"), true)
    35  
    36  		t.Assert(m.Remove("key2"), "val2")
    37  		t.Assert(m.Contains("key2"), false)
    38  
    39  		t.AssertIN("key3", m.Keys())
    40  		t.AssertIN("key1", m.Keys())
    41  		t.AssertIN("val3", m.Values())
    42  		t.AssertIN("val1", m.Values())
    43  
    44  		m.Flip()
    45  		t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
    46  
    47  		m.Clear()
    48  		t.Assert(m.Size(), 0)
    49  		t.Assert(m.IsEmpty(), true)
    50  	})
    51  }
    52  
    53  func Test_TreeMap_Basic(t *testing.T) {
    54  	gtest.C(t, func(t *gtest.T) {
    55  		m := gmap.NewTreeMap(gutil.ComparatorString)
    56  		m.Set("key1", "val1")
    57  		t.Assert(m.Keys(), []interface{}{"key1"})
    58  
    59  		t.Assert(m.Get("key1"), "val1")
    60  		t.Assert(m.Size(), 1)
    61  		t.Assert(m.IsEmpty(), false)
    62  
    63  		t.Assert(m.GetOrSet("key2", "val2"), "val2")
    64  		t.Assert(m.SetIfNotExist("key2", "val2"), false)
    65  
    66  		t.Assert(m.SetIfNotExist("key3", "val3"), true)
    67  
    68  		t.Assert(m.Remove("key2"), "val2")
    69  		t.Assert(m.Contains("key2"), false)
    70  
    71  		t.AssertIN("key3", m.Keys())
    72  		t.AssertIN("key1", m.Keys())
    73  		t.AssertIN("val3", m.Values())
    74  		t.AssertIN("val1", m.Values())
    75  
    76  		m.Flip()
    77  		t.Assert(m.Map(), map[interface{}]interface{}{"val3": "key3", "val1": "key1"})
    78  
    79  		m.Clear()
    80  		t.Assert(m.Size(), 0)
    81  		t.Assert(m.IsEmpty(), true)
    82  
    83  		m2 := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
    84  		t.Assert(m2.Map(), map[interface{}]interface{}{1: 1, "key1": "val1"})
    85  	})
    86  }
    87  
    88  func Test_TreeMap_Set_Fun(t *testing.T) {
    89  	gtest.C(t, func(t *gtest.T) {
    90  		m := gmap.NewTreeMap(gutil.ComparatorString)
    91  		m.GetOrSetFunc("fun", getValue)
    92  		m.GetOrSetFuncLock("funlock", getValue)
    93  		t.Assert(m.Get("funlock"), 3)
    94  		t.Assert(m.Get("fun"), 3)
    95  		m.GetOrSetFunc("fun", getValue)
    96  		t.Assert(m.SetIfNotExistFunc("fun", getValue), false)
    97  		t.Assert(m.SetIfNotExistFuncLock("funlock", getValue), false)
    98  	})
    99  }
   100  
   101  func Test_TreeMap_Batch(t *testing.T) {
   102  	gtest.C(t, func(t *gtest.T) {
   103  		m := gmap.NewTreeMap(gutil.ComparatorString)
   104  		m.Sets(map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
   105  		t.Assert(m.Map(), map[interface{}]interface{}{1: 1, "key1": "val1", "key2": "val2", "key3": "val3"})
   106  		m.Removes([]interface{}{"key1", 1})
   107  		t.Assert(m.Map(), map[interface{}]interface{}{"key2": "val2", "key3": "val3"})
   108  	})
   109  }
   110  
   111  func Test_TreeMap_Iterator(t *testing.T) {
   112  	gtest.C(t, func(t *gtest.T) {
   113  		expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
   114  		m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
   115  		m.Iterator(func(k interface{}, v interface{}) bool {
   116  			t.Assert(expect[k], v)
   117  			return true
   118  		})
   119  		// 断言返回值对遍历控制
   120  		i := 0
   121  		j := 0
   122  		m.Iterator(func(k interface{}, v interface{}) bool {
   123  			i++
   124  			return true
   125  		})
   126  		m.Iterator(func(k interface{}, v interface{}) bool {
   127  			j++
   128  			return false
   129  		})
   130  		t.Assert(i, 2)
   131  		t.Assert(j, 1)
   132  	})
   133  
   134  	gtest.C(t, func(t *gtest.T) {
   135  		expect := map[interface{}]interface{}{1: 1, "key1": "val1"}
   136  		m := gmap.NewTreeMapFrom(gutil.ComparatorString, expect)
   137  		for i := 0; i < 10; i++ {
   138  			m.IteratorAsc(func(k interface{}, v interface{}) bool {
   139  				t.Assert(expect[k], v)
   140  				return true
   141  			})
   142  		}
   143  		j := 0
   144  		for i := 0; i < 10; i++ {
   145  			m.IteratorAsc(func(k interface{}, v interface{}) bool {
   146  				j++
   147  				return false
   148  			})
   149  		}
   150  		t.Assert(j, 10)
   151  	})
   152  }
   153  
   154  func Test_TreeMap_Clone(t *testing.T) {
   155  	gtest.C(t, func(t *gtest.T) {
   156  		// clone 方法是深克隆
   157  		m := gmap.NewTreeMapFrom(gutil.ComparatorString, map[interface{}]interface{}{1: 1, "key1": "val1"})
   158  		m_clone := m.Clone()
   159  		m.Remove(1)
   160  		// 修改原 map,clone 后的 map 不影响
   161  		t.AssertIN(1, m_clone.Keys())
   162  
   163  		m_clone.Remove("key1")
   164  		// 修改clone map,原 map 不影响
   165  		t.AssertIN("key1", m.Keys())
   166  	})
   167  }
   168  
   169  func Test_TreeMap_Json(t *testing.T) {
   170  	// Marshal
   171  	gtest.C(t, func(t *gtest.T) {
   172  		data := g.MapAnyAny{
   173  			"k1": "v1",
   174  			"k2": "v2",
   175  		}
   176  		m1 := gmap.NewTreeMapFrom(gutil.ComparatorString, data)
   177  		b1, err1 := json.Marshal(m1)
   178  		b2, err2 := json.Marshal(gconv.Map(data))
   179  		t.Assert(err1, err2)
   180  		t.Assert(b1, b2)
   181  	})
   182  	// Unmarshal
   183  	gtest.C(t, func(t *gtest.T) {
   184  		data := g.MapAnyAny{
   185  			"k1": "v1",
   186  			"k2": "v2",
   187  		}
   188  		b, err := json.Marshal(gconv.Map(data))
   189  		t.AssertNil(err)
   190  
   191  		m := gmap.NewTreeMap(gutil.ComparatorString)
   192  		err = json.UnmarshalUseNumber(b, m)
   193  		t.AssertNil(err)
   194  		t.Assert(m.Get("k1"), data["k1"])
   195  		t.Assert(m.Get("k2"), data["k2"])
   196  	})
   197  	gtest.C(t, func(t *gtest.T) {
   198  		data := g.MapAnyAny{
   199  			"k1": "v1",
   200  			"k2": "v2",
   201  		}
   202  		b, err := json.Marshal(gconv.Map(data))
   203  		t.AssertNil(err)
   204  
   205  		var m gmap.TreeMap
   206  		err = json.UnmarshalUseNumber(b, &m)
   207  		t.AssertNil(err)
   208  		t.Assert(m.Get("k1"), data["k1"])
   209  		t.Assert(m.Get("k2"), data["k2"])
   210  	})
   211  }
   212  
   213  func TestTreeMap_UnmarshalValue(t *testing.T) {
   214  	type V struct {
   215  		Name string
   216  		Map  *gmap.TreeMap
   217  	}
   218  	// JSON
   219  	gtest.C(t, func(t *gtest.T) {
   220  		var v *V
   221  		err := gconv.Struct(map[string]interface{}{
   222  			"name": "john",
   223  			"map":  []byte(`{"k1":"v1","k2":"v2"}`),
   224  		}, &v)
   225  		t.AssertNil(err)
   226  		t.Assert(v.Name, "john")
   227  		t.Assert(v.Map.Size(), 2)
   228  		t.Assert(v.Map.Get("k1"), "v1")
   229  		t.Assert(v.Map.Get("k2"), "v2")
   230  	})
   231  	// Map
   232  	gtest.C(t, func(t *gtest.T) {
   233  		var v *V
   234  		err := gconv.Struct(map[string]interface{}{
   235  			"name": "john",
   236  			"map": g.Map{
   237  				"k1": "v1",
   238  				"k2": "v2",
   239  			},
   240  		}, &v)
   241  		t.AssertNil(err)
   242  		t.Assert(v.Name, "john")
   243  		t.Assert(v.Map.Size(), 2)
   244  		t.Assert(v.Map.Get("k1"), "v1")
   245  		t.Assert(v.Map.Get("k2"), "v2")
   246  	})
   247  }