github.com/m4gshm/gollections@v0.0.10/collection/immutable/map_/test/map_test.go (about)

     1  package test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/m4gshm/gollections/collection/immutable"
     8  	"github.com/m4gshm/gollections/collection/immutable/map_"
     9  	"github.com/m4gshm/gollections/k"
    10  	"github.com/m4gshm/gollections/slice"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_Map_Iterate(t *testing.T) {
    15  	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"))
    16  
    17  	assert.Equal(t, 4, dict.Len())
    18  	assert.Equal(t, 4, len(dict.Map()))
    19  
    20  	expectedK := slice.Of(1, 2, 3, 4)
    21  	expectedV := slice.Of("1", "2", "3", "4")
    22  
    23  	keys := make([]int, 0)
    24  	values := make([]string, 0)
    25  
    26  	it := dict.Head()
    27  	for key, val, ok := it.Next(); 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 = dict.Keys().Slice()
    38  	values = dict.Values().Slice()
    39  	sort.Ints(keys)
    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_Iterate_Keys(t *testing.T) {
    46  	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"))
    47  	assert.Equal(t, 4, len(dict.Map()))
    48  
    49  	expectedK := slice.Of(1, 2, 3, 4)
    50  
    51  	keys := []int{}
    52  	for it, key, ok := dict.K().First(); ok; key, ok = it.Next() {
    53  		keys = append(keys, key)
    54  	}
    55  
    56  	sort.Ints(keys)
    57  	assert.Equal(t, expectedK, keys)
    58  
    59  }
    60  
    61  func Test_Map_Iterate_Values(t *testing.T) {
    62  	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"))
    63  	assert.Equal(t, 4, len(ordered.Map()))
    64  
    65  	expectedV := slice.Of("1", "2", "3", "4")
    66  
    67  	values := []string{}
    68  	for it, val, ok := ordered.V().First(); ok; val, ok = it.Next() {
    69  		values = append(values, val)
    70  	}
    71  
    72  	sort.Strings(values)
    73  	assert.Equal(t, expectedV, values)
    74  }
    75  
    76  func Test_Map_Zero(t *testing.T) {
    77  	var m immutable.Map[string, string]
    78  
    79  	m.Contains("")
    80  
    81  	out := m.Map()
    82  	assert.Equal(t, 0, len(out))
    83  
    84  	e := m.IsEmpty()
    85  	assert.True(t, e)
    86  
    87  	head, _, _, ok := m.First()
    88  	assert.False(t, ok)
    89  	_, _, ok = head.Next()
    90  	assert.False(t, ok)
    91  
    92  	head = m.Head()
    93  	_, _, ok = head.Next()
    94  	assert.False(t, ok)
    95  
    96  	_, ok = m.Get("")
    97  	assert.False(t, ok)
    98  
    99  	m.For(nil)
   100  	m.ForEach(nil)
   101  	m.Track(nil)
   102  	m.TrackEach(nil)
   103  
   104  	m.Filter(nil)
   105  	m.FilterKey(nil)
   106  	m.FilterValue(nil)
   107  
   108  	m.Values().For(nil)
   109  	m.Values().ForEach(nil)
   110  	m.ConvertValue(nil).Track(nil)
   111  	m.ConvertValue(nil).TrackEach(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).TrackEach(nil)
   119  	m.ConvertKey(nil).Filter(nil).FilterKey(nil)
   120  	m.ConvertKey(nil).Filter(nil).FilterValue(nil)
   121  	m.Convert(nil)
   122  
   123  	m.Sort(nil).For(nil)
   124  	m.Sort(nil).ForEach(nil)
   125  
   126  	m.StableSort(nil).For(nil)
   127  	m.StableSort(nil).ForEach(nil)
   128  }