github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/tracers/internal/util_test.go (about)

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