github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/immutable/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/immutable"
    10  	"github.com/m4gshm/gollections/collection/immutable/map_"
    11  	"github.com/m4gshm/gollections/collection/immutable/ordered"
    12  	"github.com/m4gshm/gollections/k"
    13  	"github.com/m4gshm/gollections/op"
    14  	"github.com/m4gshm/gollections/slice"
    15  )
    16  
    17  func Test_Map_Iterate(t *testing.T) {
    18  	dict := 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"))
    19  
    20  	assert.Equal(t, 4, dict.Len())
    21  	assert.Equal(t, 4, len(dict.Map()))
    22  
    23  	expectedK := slice.Of(1, 2, 3, 4)
    24  	expectedV := slice.Of("1", "2", "3", "4")
    25  
    26  	keys := make([]int, 0)
    27  	values := make([]string, 0)
    28  
    29  	it := dict.Head()
    30  	for key, val, ok := it.Next(); ok; key, val, ok = it.Next() {
    31  		keys = append(keys, key)
    32  		values = append(values, val)
    33  	}
    34  
    35  	sort.Ints(keys)
    36  	sort.Strings(values)
    37  	assert.Equal(t, expectedK, keys)
    38  	assert.Equal(t, expectedV, values)
    39  
    40  	keys = dict.Keys().Slice()
    41  	values = dict.Values().Slice()
    42  	sort.Ints(keys)
    43  	sort.Strings(values)
    44  	assert.Equal(t, slice.Of(1, 2, 3, 4), keys)
    45  	assert.Equal(t, slice.Of("1", "2", "3", "4"), values)
    46  }
    47  
    48  func Test_Map_Iterate_Keys(t *testing.T) {
    49  	dict := 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"))
    50  	assert.Equal(t, 4, len(dict.Map()))
    51  
    52  	expectedK := slice.Of(1, 2, 3, 4)
    53  
    54  	keys := []int{}
    55  	for it, key, ok := dict.K().First(); ok; key, ok = it.Next() {
    56  		keys = append(keys, key)
    57  	}
    58  
    59  	sort.Ints(keys)
    60  	assert.Equal(t, expectedK, keys)
    61  
    62  }
    63  
    64  func Test_Map_Iterate_Values(t *testing.T) {
    65  	ordered := 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"))
    66  	assert.Equal(t, 4, len(ordered.Map()))
    67  
    68  	expectedV := slice.Of("1", "2", "3", "4")
    69  
    70  	values := []string{}
    71  	for it, val, ok := ordered.V().First(); ok; val, ok = it.Next() {
    72  		values = append(values, val)
    73  	}
    74  
    75  	sort.Strings(values)
    76  	assert.Equal(t, expectedV, values)
    77  }
    78  
    79  func Test_Map_Zero(t *testing.T) {
    80  	var m immutable.Map[string, string]
    81  
    82  	m.Contains("")
    83  
    84  	out := m.Map()
    85  	assert.Equal(t, 0, len(out))
    86  
    87  	e := m.IsEmpty()
    88  	assert.True(t, e)
    89  
    90  	head, _, _, ok := m.First()
    91  	assert.False(t, ok)
    92  	_, _, ok = head.Next()
    93  	assert.False(t, ok)
    94  
    95  	head = m.Head()
    96  	_, _, ok = head.Next()
    97  	assert.False(t, ok)
    98  
    99  	_, ok = m.Get("")
   100  	assert.False(t, ok)
   101  
   102  	m.Track(nil)
   103  	m.TrackEach(nil)
   104  
   105  	m.Filter(nil)
   106  	m.FilterKey(nil)
   107  	m.FilterValue(nil)
   108  
   109  	m.Values().For(nil)
   110  	m.Values().ForEach(nil)
   111  	m.ConvertValue(nil).Track(nil)
   112  	m.ConvertValue(nil).Filter(nil).FilterKey(nil)
   113  	m.ConvertValue(nil).Filter(nil).FilterValue(nil)
   114  
   115  	m.Keys().For(nil)
   116  	m.Keys().ForEach(nil)
   117  	m.ConvertKey(nil).Track(nil)
   118  	m.ConvertKey(nil).Filter(nil).FilterKey(nil)
   119  	m.ConvertKey(nil).Filter(nil).FilterValue(nil)
   120  	m.Convert(nil)
   121  
   122  	m.Sort(nil).Track(nil)
   123  	m.Sort(nil).TrackEach(nil)
   124  
   125  	m.StableSort(nil).Track(nil)
   126  	m.StableSort(nil).TrackEach(nil)
   127  }
   128  
   129  func Test_Map_Sort(t *testing.T) {
   130  	var m = map_.Of(k.V(5, "5"), k.V(4, "4"), k.V(-8, "-8"), k.V(10, "10"))
   131  	o := m.Sort(op.Compare)
   132  
   133  	expected := ordered.NewMap(k.V(-8, "-8"), k.V(4, "4"), k.V(5, "5"), k.V(10, "10"))
   134  
   135  	assert.Equal(t, expected, o)
   136  	assert.NotSame(t, m, o)
   137  }