github.com/matrixorigin/matrixone@v1.2.0/pkg/util/list/dequeue_test.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package list
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestPushBack(t *testing.T) {
    24  	q := New[int]()
    25  	q.PushBack(1)
    26  	assert.Equal(t, 1, q.Len())
    27  }
    28  
    29  func TestPopFront(t *testing.T) {
    30  	q := New[int]()
    31  	q.PushBack(1)
    32  	q.PushBack(2)
    33  	v := q.PopFront()
    34  	assert.Equal(t, 1, v.Value)
    35  	assert.Equal(t, 2, q.MustFront().Value)
    36  }
    37  
    38  func TestPopBack(t *testing.T) {
    39  	q := New[int]()
    40  	q.PushBack(1)
    41  	q.PushBack(2)
    42  	v := q.PopBack()
    43  	assert.Equal(t, 2, v.Value)
    44  	assert.Equal(t, 1, q.MustFront().Value)
    45  	assert.Equal(t, 1, q.MustBack().Value)
    46  }
    47  
    48  func TestRemove(t *testing.T) {
    49  	q := New[int]()
    50  	q.Remove(q.PushBack(1))
    51  	assert.Equal(t, 0, q.Len())
    52  }
    53  
    54  func TestIter(t *testing.T) {
    55  	q := New[int]()
    56  	q.PushBack(0)
    57  	q.PushBack(1)
    58  	q.PushBack(2)
    59  
    60  	var values []int
    61  	q.Iter(0, func(i int) bool {
    62  		values = append(values, i)
    63  		return true
    64  	})
    65  	assert.Equal(t, []int{0, 1, 2}, values)
    66  
    67  	values = values[:0]
    68  	q.Iter(0, func(i int) bool {
    69  		values = append(values, i)
    70  		return false
    71  	})
    72  	assert.Equal(t, []int{0}, values)
    73  
    74  	values = values[:0]
    75  	q.Iter(1, func(i int) bool {
    76  		values = append(values, i)
    77  		return true
    78  	})
    79  	assert.Equal(t, []int{1, 2}, values)
    80  }
    81  
    82  func TestInsertBefore(t *testing.T) {
    83  	q := New[int]()
    84  	q.PushBack(0)
    85  	e := q.PushBack(2)
    86  	q.PushBack(3)
    87  	q.InsertBefore(1, e)
    88  
    89  	v := -1
    90  	q.Iter(1, func(i int) bool {
    91  		v = i
    92  		return false
    93  	})
    94  	assert.Equal(t, 1, v)
    95  }
    96  
    97  func TestInsertAfter(t *testing.T) {
    98  	q := New[int]()
    99  	e := q.PushBack(0)
   100  	q.PushBack(2)
   101  	q.PushBack(3)
   102  	q.InsertAfter(1, e)
   103  
   104  	v := -1
   105  	q.Iter(1, func(i int) bool {
   106  		v = i
   107  		return false
   108  	})
   109  	assert.Equal(t, 1, v)
   110  }
   111  
   112  func TestMoveToFront(t *testing.T) {
   113  	q := New[int]()
   114  	q.PushBack(0)
   115  	q.PushBack(1)
   116  	e := q.PushBack(2)
   117  	q.MoveToFront(e)
   118  
   119  	var values []int
   120  	q.Iter(0, func(i int) bool {
   121  		values = append(values, i)
   122  		return true
   123  	})
   124  	assert.Equal(t, []int{2, 0, 1}, values)
   125  }
   126  
   127  func TestMoveToBack(t *testing.T) {
   128  	q := New[int]()
   129  	e := q.PushBack(0)
   130  	q.PushBack(1)
   131  	q.PushBack(2)
   132  	q.MoveToBack(e)
   133  
   134  	var values []int
   135  	q.Iter(0, func(i int) bool {
   136  		values = append(values, i)
   137  		return true
   138  	})
   139  	assert.Equal(t, []int{1, 2, 0}, values)
   140  }
   141  
   142  func TestMoveBefore(t *testing.T) {
   143  	q := New[int]()
   144  	e0 := q.PushBack(0)
   145  	e1 := q.PushBack(1)
   146  	q.PushBack(2)
   147  
   148  	q.MoveBefore(e1, e0)
   149  
   150  	var values []int
   151  	q.Iter(0, func(i int) bool {
   152  		values = append(values, i)
   153  		return true
   154  	})
   155  	assert.Equal(t, []int{1, 0, 2}, values)
   156  }
   157  
   158  func TestMoveAfter(t *testing.T) {
   159  	q := New[int]()
   160  	e0 := q.PushBack(0)
   161  	e1 := q.PushBack(1)
   162  	q.PushBack(2)
   163  
   164  	q.MoveAfter(e0, e1)
   165  
   166  	var values []int
   167  	q.Iter(0, func(i int) bool {
   168  		values = append(values, i)
   169  		return true
   170  	})
   171  	assert.Equal(t, []int{1, 0, 2}, values)
   172  }
   173  
   174  func TestTruncate(t *testing.T) {
   175  	q := New[int]()
   176  	for i := 0; i < 10; i++ {
   177  		q.PushBack(i)
   178  	}
   179  
   180  	q.Truncate(q.Len())
   181  	assert.Equal(t, 10, q.Len())
   182  
   183  	q.Truncate(q.Len() + 1)
   184  	assert.Equal(t, 10, q.Len())
   185  
   186  	q.Truncate(1)
   187  	assert.Equal(t, 1, q.Len())
   188  	v, ok := q.Front()
   189  	assert.True(t, ok)
   190  	i := 0
   191  	for e := v; e != nil; e = e.Next() {
   192  		assert.Equal(t, i, e.Value)
   193  		i++
   194  	}
   195  	v, _ = q.Back()
   196  	for e := v; e != nil; e = e.Prev() {
   197  		i--
   198  		assert.Equal(t, i, e.Value)
   199  	}
   200  
   201  	q.Truncate(0)
   202  	assert.Equal(t, 0, q.Len())
   203  	_, ok = q.Front()
   204  	assert.False(t, ok)
   205  	_, ok = q.Back()
   206  	assert.False(t, ok)
   207  }
   208  
   209  func TestDrain(t *testing.T) {
   210  	// case 1: drain 5,10 from 0-9
   211  	q := New[int]()
   212  	for i := 0; i < 10; i++ {
   213  		q.PushBack(i)
   214  	}
   215  	drained := q.Drain(5, 10)
   216  
   217  	assert.Equal(t, 5, q.Len())
   218  	v, ok := q.Front()
   219  	assert.True(t, ok)
   220  	i := 0
   221  	for e := v; e != nil; e = e.Next() {
   222  		assert.Equal(t, i, e.Value)
   223  		i++
   224  	}
   225  	v, _ = q.Back()
   226  	for e := v; e != nil; e = e.Prev() {
   227  		i--
   228  		assert.Equal(t, i, e.Value)
   229  	}
   230  
   231  	v, ok = drained.Front()
   232  	assert.Equal(t, 5, drained.Len())
   233  	assert.True(t, ok)
   234  	i = 5
   235  	for e := v; e != nil; e = e.Next() {
   236  		assert.Equal(t, i, e.Value)
   237  		i++
   238  	}
   239  	v, _ = drained.Back()
   240  	for e := v; e != nil; e = e.Prev() {
   241  		i--
   242  		assert.Equal(t, i, e.Value)
   243  	}
   244  
   245  	// case 2: drain 5,9 from 0-9
   246  	q.Clear()
   247  	for i := 0; i < 10; i++ {
   248  		q.PushBack(i)
   249  	}
   250  	drained = q.Drain(5, 9)
   251  
   252  	assert.Equal(t, 6, q.Len())
   253  
   254  	i = 0
   255  	values := []int{0, 1, 2, 3, 4, 9}
   256  	v, ok = q.Front()
   257  	assert.True(t, ok)
   258  	for e := v; e != nil; e = e.Next() {
   259  		assert.Equal(t, values[i], e.Value)
   260  		i++
   261  	}
   262  	v, _ = q.Back()
   263  	for e := v; e != nil; e = e.Prev() {
   264  		i--
   265  		assert.Equal(t, values[i], e.Value)
   266  	}
   267  
   268  	v, ok = drained.Front()
   269  	assert.Equal(t, 4, drained.Len())
   270  	assert.True(t, ok)
   271  	i = 5
   272  	for e := v; e != nil; e = e.Next() {
   273  		assert.Equal(t, i, e.Value)
   274  		i++
   275  	}
   276  	v, _ = drained.Back()
   277  	for e := v; e != nil; e = e.Prev() {
   278  		i--
   279  		assert.Equal(t, i, e.Value)
   280  	}
   281  
   282  	// case 3: drain 0,5 from 0-9
   283  	q.Clear()
   284  	for i := 0; i < 10; i++ {
   285  		q.PushBack(i)
   286  	}
   287  	drained = q.Drain(0, 5)
   288  
   289  	assert.Equal(t, 5, q.Len())
   290  	i = 5
   291  	v, ok = q.Front()
   292  	assert.True(t, ok)
   293  	for e := v; e != nil; e = e.Next() {
   294  		assert.Equal(t, i, e.Value)
   295  		i++
   296  	}
   297  	v, _ = q.Back()
   298  	for e := v; e != nil; e = e.Prev() {
   299  		i--
   300  		assert.Equal(t, i, e.Value)
   301  	}
   302  
   303  	v, ok = drained.Front()
   304  	assert.Equal(t, 5, drained.Len())
   305  	assert.True(t, ok)
   306  	i = 0
   307  	for e := v; e != nil; e = e.Next() {
   308  		assert.Equal(t, i, e.Value)
   309  		i++
   310  	}
   311  	v, _ = drained.Back()
   312  	for e := v; e != nil; e = e.Prev() {
   313  		i--
   314  		assert.Equal(t, i, e.Value)
   315  	}
   316  
   317  	// case 3: drain 0,9 from 0-9
   318  	q.Clear()
   319  	for i := 0; i < 10; i++ {
   320  		q.PushBack(i)
   321  	}
   322  	drained = q.Drain(0, 10)
   323  	assert.Equal(t, 0, q.Len())
   324  	_, ok = q.Front()
   325  	assert.False(t, ok)
   326  	_, ok = q.Back()
   327  	assert.False(t, ok)
   328  
   329  	assert.Equal(t, 10, drained.Len())
   330  	v, ok = drained.Front()
   331  	assert.True(t, ok)
   332  	i = 0
   333  	for e := v; e != nil; e = e.Next() {
   334  		assert.Equal(t, i, e.Value)
   335  		i++
   336  	}
   337  	v, _ = drained.Back()
   338  	for e := v; e != nil; e = e.Prev() {
   339  		i--
   340  		assert.Equal(t, i, e.Value)
   341  	}
   342  }