github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/internal/util_test.go (about) 1 // Copyright 2024 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 internal 18 19 import ( 20 "testing" 21 22 "github.com/ethereum/go-ethereum/core/vm" 23 ) 24 25 func TestMemCopying(t *testing.T) { 26 for i, tc := range []struct { 27 memsize int64 28 offset int64 29 size int64 30 wantErr string 31 wantSize int 32 }{ 33 {0, 0, 100, "", 100}, // Should pad up to 100 34 {0, 100, 0, "", 0}, // No need to pad (0 size) 35 {100, 50, 100, "", 100}, // Should pad 100-150 36 {100, 50, 5, "", 5}, // Wanted range fully within memory 37 {100, -50, 0, "offset or size must not be negative", 0}, // Error 38 {0, 1, 1024*1024 + 1, "reached limit for padding memory slice: 1048578", 0}, // Error 39 {10, 0, 1024*1024 + 100, "reached limit for padding memory slice: 1048666", 0}, // Error 40 41 } { 42 mem := vm.NewMemory() 43 mem.Resize(uint64(tc.memsize)) 44 cpy, err := GetMemoryCopyPadded(mem.Data(), tc.offset, tc.size) 45 if want := tc.wantErr; want != "" { 46 if err == nil { 47 t.Fatalf("test %d: want '%v' have no error", i, want) 48 } 49 if have := err.Error(); want != have { 50 t.Fatalf("test %d: want '%v' have '%v'", i, want, have) 51 } 52 continue 53 } 54 if err != nil { 55 t.Fatalf("test %d: unexpected error: %v", i, err) 56 } 57 if want, have := tc.wantSize, len(cpy); have != want { 58 t.Fatalf("test %d: want %v have %v", i, want, have) 59 } 60 } 61 }