MCPcopy Create free account
hub / github.com/CPChain/chain / TestInputFixedArrayAndVariableInputLength

Function TestInputFixedArrayAndVariableInputLength

accounts/abi/abi_test.go:368–548  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

366}
367
368func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
369 const definition = `[
370 { "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
371 { "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
372 { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
373 { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
374 { "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] }
375 ]`
376
377 abi, err := JSON(strings.NewReader(definition))
378 if err != nil {
379 t.Error(err)
380 }
381
382 // test string, fixed array uint256[2]
383 strin := "hello world"
384 arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
385 fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin)
386 if err != nil {
387 t.Error(err)
388 }
389
390 // generate expected output
391 offset := make([]byte, 32)
392 offset[31] = 96
393 length := make([]byte, 32)
394 length[31] = byte(len(strin))
395 strvalue := common.RightPadBytes([]byte(strin), 32)
396 arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32)
397 arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32)
398 exp := append(offset, arrinvalue1...)
399 exp = append(exp, arrinvalue2...)
400 exp = append(exp, append(length, strvalue...)...)
401
402 // ignore first 4 bytes of the output. This is the function identifier
403 fixedArrStrPack = fixedArrStrPack[4:]
404 if !bytes.Equal(fixedArrStrPack, exp) {
405 t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack)
406 }
407
408 // test byte array, fixed array uint256[2]
409 bytesin := []byte(strin)
410 arrin = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
411 fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin)
412 if err != nil {
413 t.Error(err)
414 }
415
416 // generate expected output
417 offset = make([]byte, 32)
418 offset[31] = 96
419 length = make([]byte, 32)
420 length[31] = byte(len(strin))
421 strvalue = common.RightPadBytes([]byte(strin), 32)
422 arrinvalue1 = common.LeftPadBytes(arrin[0].Bytes(), 32)
423 arrinvalue2 = common.LeftPadBytes(arrin[1].Bytes(), 32)
424 exp = append(offset, arrinvalue1...)
425 exp = append(exp, arrinvalue2...)

Callers

nothing calls this directly

Calls 6

JSONFunction · 0.85
U256Function · 0.85
ErrorMethod · 0.65
BytesMethod · 0.65
EqualMethod · 0.65
PackMethod · 0.45

Tested by

no test coverage detected