github.com/blend/go-sdk@v1.20220411.3/collections/linked_list_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func Test_LinkedList(t *testing.T) {
    17  	a := assert.New(t)
    18  
    19  	q := NewLinkedList()
    20  	a.Nil(q.head)
    21  	a.Nil(q.tail)
    22  	a.Empty(q.Contents())
    23  	a.Nil(q.Dequeue())
    24  	a.Equal(q.head, q.tail)
    25  	a.Nil(q.Peek())
    26  	a.Nil(q.PeekBack())
    27  	a.Equal(0, q.Len())
    28  
    29  	q.Enqueue("foo")
    30  	a.NotNil(q.head)
    31  	a.Nil(q.head.Previous)
    32  	a.NotNil(q.tail)
    33  	a.Nil(q.tail.Previous)
    34  	a.Equal(q.head, q.tail)
    35  	a.Equal(1, q.Len())
    36  	a.Equal("foo", q.Peek())
    37  	a.Equal("foo", q.PeekBack())
    38  
    39  	q.Enqueue("bar")
    40  	a.NotNil(q.head)
    41  	a.NotNil(q.head.Previous)
    42  	a.Nil(q.head.Previous.Previous)
    43  	a.Equal(q.head.Previous, q.tail)
    44  	a.NotNil(q.tail)
    45  	a.NotNil(q.tail.Next)
    46  	a.NotEqual(q.head, q.tail)
    47  	a.Equal(2, q.Len())
    48  	a.Equal("foo", q.Peek())
    49  	a.Equal("bar", q.PeekBack())
    50  
    51  	q.Enqueue("baz")
    52  	a.NotNil(q.head)
    53  	a.NotNil(q.head.Previous)
    54  	a.NotNil(q.head.Previous.Previous)
    55  	a.Nil(q.head.Previous.Previous.Previous)
    56  	a.Equal(q.head.Previous.Previous, q.tail)
    57  	a.NotNil(q.tail)
    58  	a.NotEqual(q.head, q.tail)
    59  	a.Equal(3, q.Len())
    60  	a.Equal("foo", q.Peek())
    61  	a.Equal("baz", q.PeekBack())
    62  
    63  	q.Enqueue("fizz")
    64  	a.NotNil(q.head)
    65  	a.NotNil(q.head.Previous)
    66  	a.NotNil(q.head.Previous.Previous)
    67  	a.NotNil(q.head.Previous.Previous.Previous)
    68  	a.Nil(q.head.Previous.Previous.Previous.Previous)
    69  	a.Equal(q.head.Previous.Previous.Previous, q.tail)
    70  	a.NotNil(q.tail)
    71  	a.NotEqual(q.head, q.tail)
    72  	a.Equal(4, q.Len())
    73  	a.Equal("foo", q.Peek())
    74  	a.Equal("fizz", q.PeekBack())
    75  
    76  	values := q.Contents()
    77  	a.Len(values, 4)
    78  	a.Equal("foo", values[0])
    79  	a.Equal("bar", values[1])
    80  	a.Equal("baz", values[2])
    81  	a.Equal("fizz", values[3])
    82  
    83  	shouldBeFoo := q.Dequeue()
    84  	a.Equal("foo", shouldBeFoo)
    85  	a.NotNil(q.head)
    86  	a.NotNil(q.head.Previous)
    87  	a.NotNil(q.head.Previous.Previous)
    88  	a.Nil(q.head.Previous.Previous.Previous)
    89  	a.Equal(q.head.Previous.Previous, q.tail)
    90  	a.NotNil(q.tail)
    91  	a.NotEqual(q.head, q.tail)
    92  	a.Equal(q.Len(), 3)
    93  	a.Equal("bar", q.Peek())
    94  	a.Equal("fizz", q.PeekBack())
    95  
    96  	shouldBeBar := q.Dequeue()
    97  	a.Equal("bar", shouldBeBar)
    98  	a.NotNil(q.head)
    99  	a.NotNil(q.head.Previous)
   100  	a.Nil(q.head.Previous.Previous)
   101  	a.Equal(q.head.Previous, q.tail)
   102  	a.NotNil(q.tail)
   103  	a.NotEqual(q.head, q.tail)
   104  	a.Equal(2, q.Len())
   105  	a.Equal("baz", q.Peek())
   106  	a.Equal("fizz", q.PeekBack())
   107  
   108  	shouldBeBaz := q.Dequeue()
   109  	a.Equal("baz", shouldBeBaz)
   110  	a.NotNil(q.head)
   111  	a.Nil(q.head.Previous)
   112  	a.NotNil(q.tail)
   113  	a.Equal(q.head, q.tail)
   114  	a.Equal(1, q.Len())
   115  	a.Equal("fizz", q.Peek())
   116  	a.Equal("fizz", q.PeekBack())
   117  
   118  	shouldBeFizz := q.Dequeue()
   119  	a.Equal("fizz", shouldBeFizz)
   120  	a.Nil(q.head)
   121  	a.Nil(q.tail)
   122  	a.Nil(q.Peek())
   123  	a.Nil(q.PeekBack())
   124  	a.Equal(0, q.Len())
   125  
   126  	q.Enqueue("foo")
   127  	q.Enqueue("bar")
   128  	q.Enqueue("baz")
   129  	a.Equal(3, q.Len())
   130  	q.Clear()
   131  	a.Equal(0, q.Len())
   132  
   133  	q.Enqueue("foo")
   134  	q.Enqueue("bar")
   135  	q.Enqueue("baz")
   136  
   137  	var items []string
   138  	q.Each(func(v Any) {
   139  		items = append(items, v.(string))
   140  	})
   141  	a.Len(items, 3)
   142  	a.Equal("foo", items[0])
   143  	a.Equal("bar", items[1])
   144  	a.Equal("baz", items[2])
   145  	a.Equal(3, q.Len())
   146  
   147  	items = []string{}
   148  	q.Consume(func(v Any) {
   149  		items = append(items, v.(string))
   150  	})
   151  	a.Equal(0, q.Len())
   152  	a.Len(items, 3)
   153  	a.Equal("foo", items[0])
   154  	a.Equal("bar", items[1])
   155  	a.Equal("baz", items[2])
   156  
   157  	q.Enqueue("foo")
   158  	q.Enqueue("bar")
   159  	q.Enqueue("baz")
   160  	a.Equal(3, q.Len())
   161  
   162  	contents := q.Drain()
   163  	a.Len(contents, 3)
   164  	a.Equal(0, q.Len())
   165  }
   166  
   167  func Test_LinkedList_DequeueBack(t *testing.T) {
   168  	a := assert.New(t)
   169  
   170  	q := NewLinkedList()
   171  	q.Enqueue(1)
   172  	q.Enqueue(2)
   173  	q.Enqueue(3)
   174  	q.Enqueue(4)
   175  
   176  	a.Equal(4, q.DequeueBack())
   177  	a.Equal(3, q.DequeueBack())
   178  	a.Equal(2, q.DequeueBack())
   179  	a.Equal(1, q.DequeueBack())
   180  	a.Nil(q.DequeueBack())
   181  	a.Nil(q.DequeueBack())
   182  
   183  	q.Enqueue(1)
   184  	q.Enqueue(2)
   185  	q.Enqueue(3)
   186  	q.Enqueue(4)
   187  
   188  	a.Equal(4, q.DequeueBack())
   189  	a.Equal(3, q.DequeueBack())
   190  	a.Equal(2, q.DequeueBack())
   191  	a.Equal(1, q.DequeueBack())
   192  	a.Nil(q.DequeueBack())
   193  }