github.com/theQRL/go-zond@v0.1.1/core/vm/memory_test.go (about) 1 package vm 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/theQRL/go-zond/common" 9 ) 10 11 func TestMemoryCopy(t *testing.T) { 12 // Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases 13 for i, tc := range []struct { 14 dst, src, len uint64 15 pre string 16 want string 17 }{ 18 { // MCOPY 0 32 32 - copy 32 bytes from offset 32 to offset 0. 19 0, 32, 32, 20 "0000000000000000000000000000000000000000000000000000000000000000 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 21 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 22 }, 23 24 { // MCOPY 0 0 32 - copy 32 bytes from offset 0 to offset 0. 25 0, 0, 32, 26 "0101010101010101010101010101010101010101010101010101010101010101", 27 "0101010101010101010101010101010101010101010101010101010101010101", 28 }, 29 { // MCOPY 0 1 8 - copy 8 bytes from offset 1 to offset 0 (overlapping). 30 0, 1, 8, 31 "000102030405060708 000000000000000000000000000000000000000000000000", 32 "010203040506070808 000000000000000000000000000000000000000000000000", 33 }, 34 { // MCOPY 1 0 8 - copy 8 bytes from offset 0 to offset 1 (overlapping). 35 1, 0, 8, 36 "000102030405060708 000000000000000000000000000000000000000000000000", 37 "000001020304050607 000000000000000000000000000000000000000000000000", 38 }, 39 // Tests below are not in the EIP, but maybe should be added 40 { // MCOPY 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds index(overlapping). 41 0xFFFFFFFFFFFF, 0xFFFFFFFFFFFF, 0, 42 "11", 43 "11", 44 }, 45 { // MCOPY 0xFFFFFFFFFFFF 0 0 - copy zero bytes from start of mem to out-of-bounds. 46 0xFFFFFFFFFFFF, 0, 0, 47 "11", 48 "11", 49 }, 50 { // MCOPY 0 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds to start of mem 51 0, 0xFFFFFFFFFFFF, 0, 52 "11", 53 "11", 54 }, 55 } { 56 m := NewMemory() 57 // Clean spaces 58 data := common.FromHex(strings.ReplaceAll(tc.pre, " ", "")) 59 // Set pre 60 m.Resize(uint64(len(data))) 61 m.Set(0, uint64(len(data)), data) 62 // Do the copy 63 m.Copy(tc.dst, tc.src, tc.len) 64 want := common.FromHex(strings.ReplaceAll(tc.want, " ", "")) 65 if have := m.store; !bytes.Equal(want, have) { 66 t.Errorf("case %d: want: %#x\nhave: %#x\n", i, want, have) 67 } 68 } 69 }