github.com/dubbogo/gost@v1.14.0/bytes/slice_pool_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxbytes
    19  
    20  import (
    21  	"math/rand"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func init() {
    27  	rand.Seed(time.Now().UnixNano())
    28  }
    29  
    30  func intRange(min, max int) int {
    31  	return rand.Intn(max-min) + min
    32  }
    33  
    34  func intN(n int) int {
    35  	return rand.Intn(n) + 1
    36  }
    37  
    38  func ExampleGetBytes() {
    39  	str := "hello, world"
    40  	// Obtain a buffer from the pool.
    41  	bufPtr := GetBytes(len(str))
    42  	defer PutBytes(bufPtr)
    43  	buf := *bufPtr
    44  	copy(buf, []byte(str))
    45  	if string(buf) != str {
    46  		panic("wrong slice buffer content!!")
    47  	}
    48  }
    49  
    50  func TestSlicePoolSmallBytes(t *testing.T) {
    51  	pool := NewSlicePool()
    52  
    53  	for i := 0; i < 1024; i++ {
    54  		size := intN(1 << minShift)
    55  		bp := pool.Get(size)
    56  
    57  		if cap(*bp) != 1<<minShift {
    58  			t.Errorf("Expect get the %d bytes from pool, but got %d", size, cap(*bp))
    59  		}
    60  
    61  		// Puts the bytes to pool
    62  		pool.Put(bp)
    63  	}
    64  }
    65  
    66  func TestSlicePoolMediumBytes(t *testing.T) {
    67  	pool := NewSlicePool()
    68  
    69  	for i := minShift; i < maxShift; i++ {
    70  		size := intRange((1<<uint(i))+1, 1<<uint(i+1))
    71  		bp := pool.Get(size)
    72  
    73  		if cap(*bp) != 1<<uint(i+1) {
    74  			t.Errorf("Expect get the slab size (%d) from pool, but got %d", 1<<uint(i+1), cap(*bp))
    75  		}
    76  
    77  		// Puts the bytes to pool
    78  		pool.Put(bp)
    79  	}
    80  }
    81  
    82  func TestSlicePoolLargeBytes(t *testing.T) {
    83  	pool := NewSlicePool()
    84  
    85  	for i := 0; i < 1024; i++ {
    86  		size := 1<<maxShift + intN(i+1)
    87  		bp := pool.Get(size)
    88  
    89  		if cap(*bp) != size {
    90  			t.Errorf("Expect get the %d bytes from pool, but got %d", size, cap(*bp))
    91  		}
    92  
    93  		// Puts the bytes to pool
    94  		pool.Put(bp)
    95  	}
    96  }