MCPcopy Create free account
hub / github.com/NethermindEth/cairo-vm-go / TestMemoryEncodingDecoding

Function TestMemoryEncodingDecoding

pkg/vm/vm_test.go:1292–1340  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1290}
1291
1292func TestMemoryEncodingDecoding(t *testing.T) {
1293 memory := []*f.Element{
1294 new(f.Element).SetUint64(4),
1295 new(f.Element).SetUint64(15),
1296 nil,
1297 nil,
1298 new(f.Element).SetUint64(8),
1299 nil,
1300 new(f.Element).SetUint64(2),
1301 }
1302
1303 encodedMemory := EncodeMemory(memory)
1304
1305 // the array size depends on the ammount of non nil elements
1306 // it stores (addres, felt) encoded in little endian in a consecutive way
1307 expected := make([]byte, 4*(8+32))
1308
1309 //first element
1310 binary.LittleEndian.PutUint64(expected[0:8], 0)
1311 f.LittleEndian.PutElement((*[32]byte)(expected[8:40]), *new(f.Element).SetUint64(4))
1312 //second element
1313 binary.LittleEndian.PutUint64(expected[40:48], 1)
1314 f.LittleEndian.PutElement((*[32]byte)(expected[48:80]), *new(f.Element).SetUint64(15))
1315 //third element
1316 binary.LittleEndian.PutUint64(expected[80:88], 4)
1317 f.LittleEndian.PutElement((*[32]byte)(expected[88:120]), *new(f.Element).SetUint64(8))
1318 //fourth element
1319 binary.LittleEndian.PutUint64(expected[120:128], 6)
1320 f.LittleEndian.PutElement((*[32]byte)(expected[128:160]), *new(f.Element).SetUint64(2))
1321
1322 require.Equal(
1323 t,
1324 len(expected),
1325 len(encodedMemory),
1326 )
1327 require.Equal(
1328 t,
1329 expected,
1330 encodedMemory,
1331 )
1332
1333 // testing decoding
1334 decodedMemory := DecodeMemory(encodedMemory)
1335 require.Equal(
1336 t,
1337 memory,
1338 decodedMemory,
1339 )
1340}
1341
1342// ==============
1343// Util Functions

Callers

nothing calls this directly

Calls 3

EncodeMemoryFunction · 0.85
DecodeMemoryFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected