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

     1  package vector
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/m4gshm/gollections/collection/mutable"
     9  	"github.com/m4gshm/gollections/collection/mutable/vector"
    10  	"github.com/m4gshm/gollections/loop"
    11  
    12  	"github.com/m4gshm/gollections/op"
    13  	"github.com/m4gshm/gollections/slice"
    14  	"github.com/m4gshm/gollections/slice/range_"
    15  	"github.com/m4gshm/gollections/walk/group"
    16  )
    17  
    18  func Test_Vector_From(t *testing.T) {
    19  	set := vector.From(loop.Of(1, 1, 2, 2, 3, 4, 3, 2, 1))
    20  	assert.Equal(t, slice.Of(1, 1, 2, 2, 3, 4, 3, 2, 1), set.Slice())
    21  }
    22  
    23  func Test_VectorIterate(t *testing.T) {
    24  	expected := slice.Of(1, 2, 3, 4)
    25  	v := vector.Of(1, 2, 3, 4)
    26  	result := make([]int, v.Len())
    27  	i := 0
    28  	for it := v.Head(); it.HasNext(); {
    29  		result[i] = it.GetNext()
    30  		i++
    31  	}
    32  	assert.Equal(t, expected, result)
    33  }
    34  
    35  func Test_VectorIterate2(t *testing.T) {
    36  	expected := slice.Of(1, 2, 3, 4)
    37  	v := vector.Of(1, 2, 3, 4)
    38  	result := make([]int, v.Len())
    39  	i := 0
    40  	for it, v, ok := v.First(); ok; v, ok = it.Next() {
    41  		result[i] = v
    42  		i++
    43  	}
    44  	assert.Equal(t, expected, result)
    45  }
    46  
    47  func Test_VectorIterateOverRange(t *testing.T) {
    48  	expected := slice.Of(1, 2, 3, 4)
    49  	v := vector.Of(1, 2, 3, 4)
    50  	result := make([]int, v.Len())
    51  	for i, val := range *v {
    52  		result[i] = val
    53  		i++
    54  	}
    55  	assert.Equal(t, expected, result)
    56  }
    57  
    58  func Test_VectorReverseIteration(t *testing.T) {
    59  	expected := slice.Of(4, 3, 2, 1)
    60  	v := vector.Of(1, 2, 3, 4)
    61  	result := make([]int, v.Len())
    62  	i := 0
    63  	for it := v.Tail(); it.HasPrev(); {
    64  		result[i] = it.GetPrev()
    65  		i++
    66  	}
    67  	assert.Equal(t, expected, result)
    68  }
    69  
    70  func Test_Vector_Sort(t *testing.T) {
    71  	var (
    72  		elements = vector.Of(3, 1, 5, 6, 8, 0, -2)
    73  		sorted   = elements.Sort(op.Compare)
    74  	)
    75  	assert.Equal(t, vector.Of(-2, 0, 1, 3, 5, 6, 8), sorted)
    76  	assert.Equal(t, vector.Of(-2, 0, 1, 3, 5, 6, 8), elements)
    77  	assert.Same(t, elements, sorted)
    78  }
    79  
    80  func Test_Vector_SortStructByField(t *testing.T) {
    81  	var (
    82  		anonymous = &user{"Anonymous", 0}
    83  		cherlie   = &user{"Cherlie", 25}
    84  		alise     = &user{"Alise", 20}
    85  		bob       = &user{"Bob", 19}
    86  
    87  		elements     = vector.Of(anonymous, cherlie, alise, bob)
    88  		sortedByName = vector.Sort(elements.Clone(), (*user).Name)
    89  		sortedByAge  = vector.Sort(elements.Clone(), (*user).Age)
    90  	)
    91  	assert.Equal(t, vector.Of(alise, anonymous, bob, cherlie), sortedByName)
    92  	assert.Equal(t, vector.Of(anonymous, bob, alise, cherlie), sortedByAge)
    93  }
    94  
    95  func Test_Vector_Nil(t *testing.T) {
    96  	var vec *mutable.Vector[int]
    97  	var nils []int
    98  	vec.Add(1, 2, 3)
    99  	vec.Add(nils...)
   100  	vec.AddOne(4)
   101  	vec.AddAll(vec)
   102  
   103  	vec.Delete(1, 2, 3)
   104  	vec.Delete(nils...)
   105  	vec.DeleteOne(4)
   106  
   107  	vec.IsEmpty()
   108  	vec.Len()
   109  
   110  	vec.For(nil)
   111  	vec.ForEach(nil)
   112  	vec.Track(nil)
   113  	vec.TrackEach(nil)
   114  
   115  	assert.Equal(t, nils, vec.Slice())
   116  
   117  	head := vec.Head()
   118  	assert.False(t, head.HasNext())
   119  	assert.False(t, head.HasPrev())
   120  
   121  	_, ok := head.Get()
   122  	assert.False(t, ok)
   123  	_, ok = head.Next()
   124  	assert.False(t, ok)
   125  	head.Size()
   126  
   127  	tail := vec.Tail()
   128  	assert.False(t, tail.HasNext())
   129  	assert.False(t, tail.HasPrev())
   130  
   131  	_, ok = tail.Get()
   132  	assert.False(t, ok)
   133  	_, ok = tail.Next()
   134  	assert.False(t, ok)
   135  	tail.Size()
   136  }
   137  
   138  func Test_Vector_Zero(t *testing.T) {
   139  	var vec mutable.Vector[string]
   140  
   141  	var nilValues []string
   142  	vec.Add("a", "b", "c")
   143  	vec.Add(nilValues...)
   144  	vec.AddOne("d")
   145  	vec.AddAll(&vec)
   146  
   147  	vec.Delete(0, 1, 2)
   148  	var nilIndexes []int
   149  	vec.Delete(nilIndexes...)
   150  	vec.DeleteOne(0)
   151  
   152  	e := vec.IsEmpty()
   153  	assert.False(t, e)
   154  
   155  	l := vec.Len()
   156  	assert.Equal(t, 4, l)
   157  
   158  	vec.For(func(_ string) error { return nil })
   159  	vec.ForEach(func(_ string) {})
   160  	vec.Track(func(_ int, _ string) error { return nil })
   161  	vec.TrackEach(func(_ int, _ string) {})
   162  
   163  	assert.Equal(t, slice.Of("a", "b", "c", "d"), vec.Slice())
   164  
   165  	head := vec.Head()
   166  	assert.True(t, head.HasNext())
   167  	assert.False(t, head.HasPrev())
   168  
   169  	_, ok := head.Get()
   170  	assert.False(t, ok)
   171  	fv, ok := head.Next()
   172  	assert.True(t, ok)
   173  	assert.Equal(t, "a", fv)
   174  	c := head.Size()
   175  	assert.Equal(t, 4, c)
   176  
   177  	tail := vec.Tail()
   178  	assert.False(t, tail.HasNext())
   179  	assert.True(t, tail.HasPrev())
   180  
   181  	_, ok = tail.Get()
   182  	assert.False(t, ok)
   183  	tv, ok := tail.Prev()
   184  	assert.True(t, ok)
   185  	assert.Equal(t, "d", tv)
   186  	c = tail.Size()
   187  	assert.Equal(t, 4, c)
   188  }
   189  
   190  func Test_Vector_new(t *testing.T) {
   191  	var vec = new(mutable.Vector[string])
   192  
   193  	var nilValues []string
   194  	vec.Add("a", "b", "c")
   195  	vec.Add(nilValues...)
   196  	vec.AddOne("d")
   197  	vec.AddAll(vec)
   198  
   199  	vec.Delete(0, 1, 2)
   200  	var nilIndexes []int
   201  	vec.Delete(nilIndexes...)
   202  	vec.DeleteOne(0)
   203  
   204  	e := vec.IsEmpty()
   205  	assert.False(t, e)
   206  
   207  	l := vec.Len()
   208  	assert.Equal(t, 4, l)
   209  
   210  	vec.For(func(_ string) error { return nil })
   211  	vec.ForEach(func(_ string) {})
   212  	vec.Track(func(_ int, _ string) error { return nil })
   213  	vec.TrackEach(func(_ int, _ string) {})
   214  
   215  	assert.Equal(t, slice.Of("a", "b", "c", "d"), vec.Slice())
   216  
   217  	head := vec.Head()
   218  	assert.True(t, head.HasNext())
   219  	assert.False(t, head.HasPrev())
   220  
   221  	_, ok := head.Get()
   222  	assert.False(t, ok)
   223  	fv, ok := head.Next()
   224  	assert.True(t, ok)
   225  	assert.Equal(t, "a", fv)
   226  	c := head.Size()
   227  	assert.Equal(t, 4, c)
   228  
   229  	tail := vec.Tail()
   230  	assert.False(t, tail.HasNext())
   231  	assert.True(t, tail.HasPrev())
   232  
   233  	_, ok = tail.Get()
   234  	assert.False(t, ok)
   235  	tv, ok := tail.Prev()
   236  	assert.True(t, ok)
   237  	assert.Equal(t, "d", tv)
   238  	c = tail.Size()
   239  	assert.Equal(t, 4, c)
   240  }
   241  
   242  func Test_Vector_AddAllOfSelf(t *testing.T) {
   243  	vec := vector.Of(1, 2, 3)
   244  	vec.AddAll(vec)
   245  	assert.Equal(t, vector.Of(1, 2, 3, 1, 2, 3), vec)
   246  }
   247  
   248  type user struct {
   249  	name string
   250  	age  int
   251  }
   252  
   253  func (u *user) Name() string { return u.name }
   254  func (u *user) Age() int     { return u.age }
   255  
   256  func Test_Vector_AddAndDelete(t *testing.T) {
   257  	vec := vector.NewCap[int](0)
   258  	vec.Add(range_.Closed(0, 1000)...)
   259  	deleted := false
   260  	for i := vec.Head(); i.HasNext(); {
   261  		deleted = i.DeleteNext()
   262  	}
   263  	assert.Equal(t, deleted, true)
   264  	assert.True(t, vec.IsEmpty())
   265  
   266  	vec.Add(range_.Closed(0, 10000)...)
   267  	for i := vec.Tail(); i.HasPrev(); {
   268  		deleted = i.DeletePrev()
   269  	}
   270  	assert.Equal(t, deleted, true)
   271  	assert.True(t, vec.IsEmpty())
   272  }
   273  
   274  func Test_Vector_Add(t *testing.T) {
   275  	vec := vector.NewCap[int](0)
   276  	vec.Add(1, 1, 2, 4, 3, 1)
   277  	assert.Equal(t, slice.Of(1, 1, 2, 4, 3, 1), vec.Slice())
   278  	vec.Add(1)
   279  	assert.Equal(t, slice.Of(1, 1, 2, 4, 3, 1, 1), vec.Slice())
   280  }
   281  
   282  func Test_Vector_AddAll(t *testing.T) {
   283  	vec := vector.NewCap[int](0)
   284  	vec.AddAll(vector.Of(1, 1, 2, 4, 3, 1))
   285  	assert.Equal(t, slice.Of(1, 1, 2, 4, 3, 1), vec.Slice())
   286  	vec.AddAll(vector.Of(1))
   287  	assert.Equal(t, slice.Of(1, 1, 2, 4, 3, 1, 1), vec.Slice())
   288  }
   289  
   290  func Test_Vector_Add_And_Iterate(t *testing.T) {
   291  	vec := vector.NewCap[int](0)
   292  	it, v, ok := vec.First()
   293  	//no a first element
   294  	assert.False(t, ok)
   295  	//no more elements
   296  	assert.False(t, it.HasNext())
   297  	vec.Add(1)
   298  	//exists one more
   299  	assert.True(t, it.HasNext())
   300  	v, ok = it.Get()
   301  	//but the cursor points out of the range
   302  	assert.False(t, ok)
   303  	//starts itaration
   304  	v, ok = it.Next()
   305  	//success
   306  	assert.True(t, ok)
   307  	assert.Equal(t, 1, v)
   308  	//no more
   309  	assert.False(t, it.HasNext())
   310  	//no prev
   311  	assert.False(t, it.HasPrev())
   312  	//only the current one
   313  	v, ok = it.Get()
   314  	assert.True(t, ok)
   315  	assert.Equal(t, 1, v)
   316  }
   317  
   318  func Test_Vector_Delete_And_Iterate(t *testing.T) {
   319  	vec := vector.Of(2)
   320  	it, v, ok := vec.First()
   321  	//success
   322  	assert.True(t, ok)
   323  	assert.Equal(t, 2, v)
   324  
   325  	//no more
   326  	assert.False(t, it.HasNext())
   327  	//no prev
   328  	assert.False(t, it.HasPrev())
   329  
   330  	//only the current one
   331  	v, ok = it.Get()
   332  	assert.True(t, ok)
   333  	assert.Equal(t, 2, v)
   334  
   335  	it.Delete()
   336  
   337  	//no the current one
   338  	_, ok = it.Get()
   339  	assert.False(t, ok)
   340  
   341  	//no more
   342  	assert.False(t, it.HasNext())
   343  	//no prev
   344  	assert.False(t, it.HasPrev())
   345  
   346  	assert.True(t, vec.IsEmpty())
   347  
   348  	// add values to vector
   349  	vec.Add(1, 3)
   350  
   351  	//it must to point before the first
   352  	_, ok = it.Get()
   353  	assert.False(t, ok)
   354  	assert.True(t, it.HasNext())
   355  	assert.False(t, it.HasPrev())
   356  
   357  	v, ok = it.Next()
   358  	assert.True(t, ok)
   359  	assert.Equal(t, 1, v)
   360  
   361  	assert.True(t, it.HasNext())
   362  	assert.False(t, it.HasPrev())
   363  
   364  	v, ok = it.Next()
   365  	assert.True(t, ok)
   366  	assert.Equal(t, 3, v)
   367  
   368  	assert.False(t, it.HasNext())
   369  	assert.True(t, it.HasPrev())
   370  
   371  	v, ok = it.Prev()
   372  	assert.True(t, ok)
   373  	assert.Equal(t, 1, v)
   374  
   375  	assert.True(t, it.HasNext())
   376  	assert.False(t, it.HasPrev())
   377  
   378  	//delete the first one
   379  	it.Delete()
   380  
   381  	//second must remains
   382  	assert.False(t, it.HasNext())
   383  	assert.False(t, it.HasPrev())
   384  	v, ok = it.Get()
   385  	assert.True(t, ok)
   386  	assert.Equal(t, 3, v)
   387  
   388  	assert.Equal(t, []int{3}, vec.Slice())
   389  }
   390  
   391  func Test_Vector_DeleteOne(t *testing.T) {
   392  	vec := vector.Of("1", "1", "2", "4", "3", "1")
   393  	vec.DeleteOne(3)
   394  	assert.Equal(t, slice.Of("1", "1", "2", "3", "1"), vec.Slice())
   395  	r := vec.DeleteActualOne(5)
   396  	assert.Equal(t, r, false)
   397  }
   398  
   399  func Test_Vector_DeleteMany(t *testing.T) {
   400  	vec := vector.Of("0", "1", "2", "3", "4", "5", "6")
   401  	vec.Delete(3, 0, 5)
   402  	assert.Equal(t, slice.Of("1", "2", "4", "6"), vec.Slice())
   403  	r := vec.DeleteActual(5, 4)
   404  	assert.Equal(t, r, false)
   405  }
   406  
   407  func Test_Vector_DeleteManyFromTail(t *testing.T) {
   408  	vec := vector.Of("0", "1", "2", "3", "4", "5", "6")
   409  	vec.Delete(4, 5, 6)
   410  	assert.Equal(t, slice.Of("0", "1", "2", "3"), vec.Slice())
   411  }
   412  
   413  func Test_Vector_DeleteManyFromHead(t *testing.T) {
   414  	vec := vector.Of("0", "1", "2", "3", "4", "5", "6")
   415  	vec.Delete(0, 1, 2)
   416  	assert.Equal(t, slice.Of("3", "4", "5", "6"), vec.Slice())
   417  }
   418  
   419  func Test_Vector_DeleteManyFromMiddle(t *testing.T) {
   420  	vec := vector.Of("0", "1", "2", "3", "4", "5", "6")
   421  	vec.Delete(4, 3)
   422  	assert.Equal(t, slice.Of("0", "1", "2", "5", "6"), vec.Slice())
   423  }
   424  
   425  func Test_Vector_Set(t *testing.T) {
   426  	vec := vector.Of("1", "1", "2", "4", "3", "1")
   427  	vec.Set(10, "11")
   428  	assert.Equal(t, slice.Of("1", "1", "2", "4", "3", "1", "", "", "", "", "11"), vec.Slice())
   429  }
   430  
   431  func Test_Vector_DeleteByIterator(t *testing.T) {
   432  	vec := vector.Of(1, 1, 2, 4, 3, 1)
   433  	iterator := vec.Head()
   434  
   435  	i := 0
   436  	var v int
   437  	var ok bool
   438  	for v, ok = iterator.Next(); ok; v, ok = iterator.Next() {
   439  		i++
   440  		iterator.Delete()
   441  	}
   442  
   443  	_, _ = v, ok
   444  
   445  	assert.Equal(t, 6, i)
   446  	assert.Equal(t, 0, len(vec.Slice()))
   447  }
   448  
   449  func Test_Vector_DeleteByIterator_Reverse(t *testing.T) {
   450  	vec := vector.Of(1, 1, 2, 4, 3, 1)
   451  	iterator := vec.Tail()
   452  
   453  	i := 0
   454  	var v int
   455  	var ok bool
   456  	for v, ok = iterator.Prev(); ok; v, ok = iterator.Prev() {
   457  		i++
   458  		iterator.Delete()
   459  	}
   460  
   461  	_, _ = v, ok
   462  
   463  	assert.Equal(t, 6, i)
   464  	assert.Equal(t, 0, len(vec.Slice()))
   465  }
   466  
   467  func Test_Vector_FilterMapReduce(t *testing.T) {
   468  	s := vector.Of(1, 1, 2, 4, 3, 4).Filter(func(i int) bool { return i%2 == 0 }).Convert(func(i int) int { return i * 2 }).Reduce(op.Sum[int])
   469  	assert.Equal(t, 20, s)
   470  }
   471  
   472  func Test_Vector_Group(t *testing.T) {
   473  	groups := group.Of(vector.Of(0, 1, 1, 2, 4, 3, 1, 6, 7), func(e int) bool { return e%2 == 0 })
   474  
   475  	assert.Equal(t, len(groups), 2)
   476  	assert.Equal(t, []int{1, 1, 3, 1, 7}, groups[false])
   477  	assert.Equal(t, []int{0, 2, 4, 6}, groups[true])
   478  }