github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/arena/arena_test.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package arena
    15  
    16  import (
    17  	"testing"
    18  )
    19  
    20  func TestSimpleArenaAllocator(t *testing.T) {
    21  	arena := NewAllocator(1000)
    22  	slice := arena.Alloc(10)
    23  	if arena.off != 10 {
    24  		t.Error("off not match, expect 10 bug got", arena.off)
    25  	}
    26  
    27  	if len(slice) != 0 || cap(slice) != 10 {
    28  		t.Error("slice length or cap not match")
    29  	}
    30  
    31  	slice = arena.Alloc(20)
    32  	if arena.off != 30 {
    33  		t.Error("off not match, expect 30 bug got", arena.off)
    34  	}
    35  
    36  	if len(slice) != 0 || cap(slice) != 20 {
    37  		t.Error("slice length or cap not match")
    38  	}
    39  
    40  	slice = arena.Alloc(1024)
    41  	if arena.off != 30 {
    42  		t.Error("off not match, expect 30 bug got", arena.off)
    43  	}
    44  
    45  	if len(slice) != 0 || cap(slice) != 1024 {
    46  		t.Error("slice length or cap not match")
    47  	}
    48  
    49  	slice = arena.AllocWithLen(2, 10)
    50  	if arena.off != 40 {
    51  		t.Error("off not match, expect 40 bug got", arena.off)
    52  	}
    53  
    54  	if len(slice) != 2 || cap(slice) != 10 {
    55  		t.Error("slice length or cap not match")
    56  	}
    57  
    58  	arena.Reset()
    59  	if arena.off != 0 || cap(arena.arena) != 1000 {
    60  		t.Error("off or cap not match")
    61  	}
    62  }
    63  
    64  func TestStdAllocator(t *testing.T) {
    65  	slice := StdAllocator.Alloc(20)
    66  	if len(slice) != 0 {
    67  		t.Error("length not match")
    68  	}
    69  
    70  	if cap(slice) != 20 {
    71  		t.Error("cap not match")
    72  	}
    73  
    74  	slice = StdAllocator.AllocWithLen(10, 20)
    75  	if len(slice) != 10 {
    76  		t.Error("length not match")
    77  	}
    78  
    79  	if cap(slice) != 20 {
    80  		t.Error("cap not match")
    81  	}
    82  }