github.com/immesys/bw2bc@v1.1.0/core/block_cache_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/core/types" 25 ) 26 27 func newChain(size int) (chain []*types.Block) { 28 var parentHash common.Hash 29 for i := 0; i < size; i++ { 30 head := &types.Header{ParentHash: parentHash, Number: big.NewInt(int64(i))} 31 block := types.NewBlock(head, nil, nil, nil) 32 chain = append(chain, block) 33 parentHash = block.Hash() 34 } 35 return chain 36 } 37 38 func insertChainCache(cache *BlockCache, chain []*types.Block) { 39 for _, block := range chain { 40 cache.Push(block) 41 } 42 } 43 44 func TestNewBlockCache(t *testing.T) { 45 chain := newChain(3) 46 cache := NewBlockCache(2) 47 insertChainCache(cache, chain) 48 49 if cache.hashes[0] != chain[1].Hash() { 50 t.Error("oldest block incorrect") 51 } 52 } 53 54 func TestInclusion(t *testing.T) { 55 chain := newChain(3) 56 cache := NewBlockCache(3) 57 insertChainCache(cache, chain) 58 59 for _, block := range chain { 60 if b := cache.Get(block.Hash()); b == nil { 61 t.Errorf("getting %x failed", block.Hash()) 62 } 63 } 64 } 65 66 func TestDeletion(t *testing.T) { 67 chain := newChain(3) 68 cache := NewBlockCache(3) 69 insertChainCache(cache, chain) 70 71 cache.Delete(chain[1].Hash()) 72 73 if cache.Has(chain[1].Hash()) { 74 t.Errorf("expected %x not to be included") 75 } 76 }