github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/memcam/memcam_test.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 memcam 15 16 import ( 17 "testing" 18 19 . "github.com/whtcorpsinc/check" 20 ) 21 22 func TestT(t *testing.T) { 23 TestingT(t) 24 } 25 26 func TestSimpleMemCamSlabPredictor(t *testing.T) { 27 memcam := NewSlabPredictor(1000) 28 slice := memcam.Alloc(10) 29 if memcam.off != 10 { 30 t.Error("off not match, expect 10 bug got", memcam.off) 31 } 32 33 if len(slice) != 0 || cap(slice) != 10 { 34 t.Error("slice length or cap not match") 35 } 36 37 slice = memcam.Alloc(20) 38 if memcam.off != 30 { 39 t.Error("off not match, expect 30 bug got", memcam.off) 40 } 41 42 if len(slice) != 0 || cap(slice) != 20 { 43 t.Error("slice length or cap not match") 44 } 45 46 slice = memcam.Alloc(1024) 47 if memcam.off != 30 { 48 t.Error("off not match, expect 30 bug got", memcam.off) 49 } 50 51 if len(slice) != 0 || cap(slice) != 1024 { 52 t.Error("slice length or cap not match") 53 } 54 55 slice = memcam.AllocWithLen(2, 10) 56 if memcam.off != 40 { 57 t.Error("off not match, expect 40 bug got", memcam.off) 58 } 59 60 if len(slice) != 2 || cap(slice) != 10 { 61 t.Error("slice length or cap not match") 62 } 63 64 memcam.Reset() 65 if memcam.off != 0 || cap(memcam.memcam) != 1000 { 66 t.Error("off or cap not match") 67 } 68 } 69 70 func TestStdSlabPredictor(t *testing.T) { 71 slice := StdSlabPredictor.Alloc(20) 72 if len(slice) != 0 { 73 t.Error("length not match") 74 } 75 76 if cap(slice) != 20 { 77 t.Error("cap not match") 78 } 79 80 slice = StdSlabPredictor.AllocWithLen(10, 20) 81 if len(slice) != 10 { 82 t.Error("length not match") 83 } 84 85 if cap(slice) != 20 { 86 t.Error("cap not match") 87 } 88 }