github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_pool_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestIteratorPool(t *testing.T) {
    11  	must := require.New(t)
    12  
    13  	pool := newIteratorPool()
    14  
    15  	f := func(cb func(it *Iterator)) {
    16  		it := pool.borrowIterator()
    17  		must.Nil(it.reader)
    18  		must.Nil(it.buffer)
    19  		must.Equal(0, it.offset)
    20  		must.Equal(0, it.head)
    21  		must.Equal(0, it.tail)
    22  		cb(it)
    23  		pool.returnIterator(it)
    24  		must.Nil(it.reader)
    25  		must.Nil(it.buffer)
    26  	}
    27  
    28  	f(func(it *Iterator) {
    29  		it.Reset(&stepByteReader{})
    30  	})
    31  
    32  	data := []byte("test")
    33  
    34  	f(func(it *Iterator) {
    35  		it.Reset(bytes.NewBuffer(data))
    36  		must.Nil(it.reader)
    37  		must.Equal(data, it.buffer)
    38  		must.Equal(0, it.offset)
    39  		must.Equal(0, it.head)
    40  		must.Equal(len(data), it.tail)
    41  	})
    42  
    43  	f(func(it *Iterator) {
    44  		it.ResetBytes(data)
    45  		must.Nil(it.reader)
    46  		must.Equal(data, it.buffer)
    47  		must.Equal(0, it.offset)
    48  		must.Equal(0, it.head)
    49  		must.Equal(len(data), it.tail)
    50  	})
    51  }