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

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