github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/mutable/map_/test/map_test.go (about)

     1  package test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/collection/mutable"
    10  	"github.com/m4gshm/gollections/collection/mutable/map_"
    11  	"github.com/m4gshm/gollections/collection/mutable/ordered"
    12  	"github.com/m4gshm/gollections/convert/as"
    13  	"github.com/m4gshm/gollections/k"
    14  	"github.com/m4gshm/gollections/op"
    15  	"github.com/m4gshm/gollections/slice"
    16  )
    17  
    18  func Test_Map_Iterate(t *testing.T) {
    19  	unordered := map_.Of(k.V(1, "1"), k.V(1, "1"), k.V(2, "2"), k.V(4, "4"), k.V(3, "3"), k.V(1, "1"))
    20  	assert.Equal(t, 4, len(unordered.Map()))
    21  
    22  	expectedK := slice.Of(1, 2, 3, 4)
    23  	expectedV := slice.Of("1", "2", "3", "4")
    24  
    25  	keys := make([]int, 0)
    26  	values := make([]string, 0)
    27  	for it, key, val, ok := unordered.First(); ok; key, val, ok = it.Next() {
    28  		keys = append(keys, key)
    29  		values = append(values, val)
    30  	}
    31  
    32  	sort.Ints(keys)
    33  	sort.Strings(values)
    34  	assert.Equal(t, expectedK, keys)
    35  	assert.Equal(t, expectedV, values)
    36  
    37  	keys = unordered.Keys().Slice()
    38  	sort.Ints(keys)
    39  	values = unordered.Values().Slice()
    40  	sort.Strings(values)
    41  	assert.Equal(t, slice.Of(1, 2, 3, 4), keys)
    42  	assert.Equal(t, slice.Of("1", "2", "3", "4"), values)
    43  }
    44  
    45  func Test_Map_IterateOverRange(t *testing.T) {
    46  	unordered := map_.Of(k.V(1, "1"), k.V(1, "1"), k.V(2, "2"), k.V(4, "4"), k.V(3, "3"), k.V(1, "1"))
    47  	assert.Equal(t, 4, len(unordered.Map()))
    48  
    49  	expectedK := slice.Of(1, 2, 3, 4)
    50  	expectedV := slice.Of("1", "2", "3", "4")
    51  
    52  	keys := make([]int, 0)
    53  	values := make([]string, 0)
    54  	for key, val := range *unordered {
    55  		keys = append(keys, key)
    56  		values = append(values, val)
    57  	}
    58  
    59  	sort.Ints(keys)
    60  	sort.Strings(values)
    61  	assert.Equal(t, expectedK, keys)
    62  	assert.Equal(t, expectedV, values)
    63  
    64  	keys = unordered.Keys().Slice()
    65  	sort.Ints(keys)
    66  	values = unordered.Values().Slice()
    67  	sort.Strings(values)
    68  	assert.Equal(t, slice.Of(1, 2, 3, 4), keys)
    69  	assert.Equal(t, slice.Of("1", "2", "3", "4"), values)
    70  }
    71  
    72  func Test_Map_Add(t *testing.T) {
    73  	d := map_.New[int, string](4)
    74  	s := d.SetNew(1, "1")
    75  	assert.Equal(t, s, true)
    76  
    77  	d.Set(2, "2")
    78  	d.Set(4, "4")
    79  	d.Set(3, "3")
    80  
    81  	s = d.SetNew(1, "11")
    82  	assert.Equal(t, s, false)
    83  
    84  	keys := d.Keys().Slice()
    85  	sort.Ints(keys)
    86  	values := d.Values().Slice()
    87  	sort.Strings(values)
    88  	assert.Equal(t, slice.Of(1, 2, 3, 4), keys)
    89  	assert.Equal(t, slice.Of("1", "2", "3", "4"), values)
    90  }
    91  
    92  func Test_Map_Nil(t *testing.T) {
    93  	var m *mutable.Map[string, string]
    94  
    95  	m.Set("a", "A")
    96  	assert.False(t, m.SetNew("b", "B"))
    97  
    98  	m.SetMap(nil)
    99  	m.SetMap(map_.Of(k.V("d", "D")))
   100  
   101  	out := m.Map()
   102  	assert.Equal(t, 0, len(out))
   103  
   104  	m.Delete("b")
   105  	var nilKeys []string
   106  	m.Delete(nilKeys...)
   107  	m.DeleteOne("a")
   108  
   109  	e := m.IsEmpty()
   110  	assert.True(t, e)
   111  
   112  	head, _, _, ok := m.First()
   113  	assert.False(t, ok)
   114  
   115  	head = m.Head()
   116  	_, _, ok = head.Next()
   117  	assert.False(t, ok)
   118  
   119  	m.Reduce(nil)
   120  	m.Convert(nil).Track(nil)
   121  	m.ConvertKey(nil).Filter(nil)
   122  	m.ConvertValue(nil).Filter(nil)
   123  	m.Filter(nil).Convert(nil).Track(nil)
   124  
   125  	m.Keys().For(nil)
   126  	m.Keys().ForEach(nil)
   127  	m.Values().For(nil)
   128  	m.Values().ForEach(nil)
   129  	m.Values().Convert(nil).For(nil)
   130  	m.Values().Filter(nil).ForEach(nil)
   131  }
   132  
   133  func Test_Map_Zero(t *testing.T) {
   134  	var m mutable.Map[string, string]
   135  
   136  	m.Set("a", "A")
   137  	assert.True(t, m.SetNew("b", "B"))
   138  	assert.False(t, m.SetNew("b", "B"))
   139  
   140  	m.SetMap(nil)
   141  	m.SetMap(map_.Of(k.V("d", "D")))
   142  
   143  	out := m.Map()
   144  	assert.Equal(t, 3, len(out))
   145  
   146  	m.Delete("b")
   147  	var nilKeys []string
   148  	m.Delete(nilKeys...)
   149  	m.DeleteOne("a")
   150  
   151  	e := m.IsEmpty()
   152  	assert.False(t, e)
   153  
   154  	l := m.Len()
   155  	assert.Equal(t, 1, l)
   156  
   157  	head, k, v, ok := m.First()
   158  	assert.True(t, ok)
   159  	assert.Equal(t, "d", k)
   160  	assert.Equal(t, "D", v)
   161  
   162  	head = m.Head()
   163  	_, _, ok = head.Next()
   164  	assert.True(t, ok)
   165  
   166  	m.Reduce(func(k1, v1, k2, v2 string) (string, string) { return k1 + k2, v1 + v2 })
   167  	m.Convert(func(s1, s2 string) (string, string) { return s1, s2 }).Track(func(_, _ string) error { return nil })
   168  	m.Filter(func(_, _ string) bool { return true }).Convert(func(s1, s2 string) (string, string) { return s1, s2 }).Track(func(_, _ string) error { return nil })
   169  
   170  	m.Keys().For(func(_ string) error { return nil })
   171  	m.Keys().ForEach(func(_ string) {})
   172  	m.Values().For(func(_ string) error { return nil })
   173  	m.Values().ForEach(func(_ string) {})
   174  	m.Values().Convert(as.Is[string]).For(func(_ string) error { return nil })
   175  	m.Values().Filter(func(_ string) bool { return true }).ForEach(func(_ string) {})
   176  }
   177  
   178  func Test_Map_new(t *testing.T) {
   179  	var m = new(mutable.Map[string, string])
   180  
   181  	m.Set("a", "A")
   182  	assert.True(t, m.SetNew("b", "B"))
   183  	assert.False(t, m.SetNew("b", "B"))
   184  
   185  	m.SetMap(nil)
   186  	m.SetMap(map_.Of(k.V("d", "D")))
   187  
   188  	out := m.Map()
   189  	assert.Equal(t, 3, len(out))
   190  
   191  	m.Delete("b")
   192  	var nilKeys []string
   193  	m.Delete(nilKeys...)
   194  	m.DeleteOne("a")
   195  
   196  	e := m.IsEmpty()
   197  	assert.False(t, e)
   198  
   199  	l := m.Len()
   200  	assert.Equal(t, 1, l)
   201  
   202  	head, k, v, ok := m.First()
   203  	assert.True(t, ok)
   204  	assert.Equal(t, "d", k)
   205  	assert.Equal(t, "D", v)
   206  
   207  	head = m.Head()
   208  	_, _, ok = head.Next()
   209  	assert.True(t, ok)
   210  
   211  	m.Reduce(func(k1, v1, k2, v2 string) (string, string) { return k1 + k2, v1 + v2 })
   212  	m.Convert(func(s1, s2 string) (string, string) { return s1, s2 }).Track(func(_, _ string) error { return nil })
   213  
   214  	m.Filter(func(_, _ string) bool { return true }).Convert(func(s1, s2 string) (string, string) { return s1, s2 }).Track(func(_, _ string) error { return nil })
   215  
   216  	m.Keys().For(func(_ string) error { return nil })
   217  	m.Keys().ForEach(func(_ string) {})
   218  	m.Values().For(func(_ string) error { return nil })
   219  	m.Values().ForEach(func(_ string) {})
   220  	m.Values().Convert(as.Is[string]).For(func(_ string) error { return nil })
   221  	m.Values().Filter(func(_ string) bool { return true }).ForEach(func(_ string) {})
   222  }
   223  
   224  func Test_Map_Sort(t *testing.T) {
   225  	var m = new(mutable.Map[int, string])
   226  
   227  	m.Set(5, "5")
   228  	m.Set(4, "4")
   229  	m.Set(-8, "-8")
   230  	m.Set(10, "10")
   231  
   232  	o := m.Sort(op.Compare)
   233  
   234  	assert.Equal(t, ordered.NewMap(k.V(-8, "-8"), k.V(4, "4"), k.V(5, "5"), k.V(10, "10")), o)
   235  	assert.NotSame(t, m, o)
   236  }