Skip to content

Commit

Permalink
adding a python script to translate large twos complement hex strings…
Browse files Browse the repository at this point in the history
… into integers, updating the ganache tests to use it (#347)
  • Loading branch information
ivoysey committed Jun 11, 2021
1 parent e911c9f commit 6b917fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
14 changes: 14 additions & 0 deletions bin/2sc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python

import sys

## takes a two's complement 256 as the only argument and returns it as a
## decimal

def twos_comp(val, bits):
"""compute the 2's complement of int value val"""
if (val & (1 << (bits - 1))) != 0:
val = val - (1 << bits)
return val

print(twos_comp(int(sys.argv[1],16),8*32))
8 changes: 8 additions & 0 deletions resources/tests/GanacheTests/PrimOpsSubNeg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"gas" : 30000000,
"gasprice" : "0x9184e72a000",
"startingeth" : 5000000,
"numaccts" : 1,
"testexp" : "primopssubneg()",
"expected" : "-15"
}
6 changes: 3 additions & 3 deletions travis_specific/ganache_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ do
continue
fi

# pull the result out of the JSON object, delete the quotes and leading 0x, and make it upper case
GOT=$(echo "$RESP" | jq '.result' | tr -d '"' | sed -e "s/^0x//" | tr '[:lower:]' '[:upper:]')
# pull the result out of the JSON object, removing the quotation marks
GOT=$(echo "$RESP" | jq '.result' | tr -d '"')
# use BC to convert it to decimal
GOT_DEC=$(echo "obase=10; ibase=16;$GOT" | bc)
GOT_DEC=$(python ../bin/2sc.py "$GOT")

# todo: extend JSON object with a decode field so that we can have expected values that aren't integers more easily
if [ "$GOT_DEC" == "$EXPECTED" ]
Expand Down

0 comments on commit 6b917fa

Please sign in to comment.