forked from kkrt-labs/kakarot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bytes.py
72 lines (61 loc) · 3.04 KB
/
test_bytes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pytest
from tests.utils.uint256 import int_to_uint256
PRIME = 0x800000000000011000000000000000000000000000000000000000000000001
class TestBytes:
class TestFeltToAscii:
@pytest.mark.parametrize("n", [0, 10, 1234, 0xFFFFFF])
def test_should_return_ascii(self, cairo_run, n):
output = cairo_run("test__felt_to_ascii", n=n)
assert str(n) == bytes(output).decode()
class TestFeltToBytesLittle:
@pytest.mark.parametrize("n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1])
def test_should_return_bytes(self, cairo_run, n):
output = cairo_run("test__felt_to_bytes_little", n=n)
res = bytes(output)
assert bytes.fromhex(f"{n:x}".rjust(len(res) * 2, "0"))[::-1] == res
class TestFeltToBytes:
@pytest.mark.parametrize("n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1])
def test_should_return_bytes(self, cairo_run, n):
output = cairo_run("test__felt_to_bytes", n=n)
res = bytes(output)
assert bytes.fromhex(f"{n:x}".rjust(len(res) * 2, "0")) == res
class TestFeltToBytes20:
@pytest.mark.parametrize("n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1])
def test_should_return_bytes20(self, cairo_run, n):
output = cairo_run("test__felt_to_bytes20", n=n)
assert f"{n:064x}"[-40:] == bytes(output).hex()
class TestUint256ToBytesLittle:
@pytest.mark.parametrize(
"n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1, 2**256 - 1]
)
def test_should_return_bytes(self, cairo_run, n):
output = cairo_run("test__uint256_to_bytes_little", n=int_to_uint256(n))
res = bytes(output)
assert bytes.fromhex(f"{n:x}".rjust(len(res) * 2, "0"))[::-1] == res
class TestUint256ToBytes:
@pytest.mark.parametrize(
"n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1, 2**256 - 1]
)
def test_should_return_bytes(self, cairo_run, n):
output = cairo_run("test__uint256_to_bytes", n=int_to_uint256(n))
res = bytes(output)
assert bytes.fromhex(f"{n:x}".rjust(len(res) * 2, "0")) == res
class TestUint256ToBytes32:
@pytest.mark.parametrize(
"n", [0, 10, 1234, 0xFFFFFF, 2**128, PRIME - 1, 2**256 - 1]
)
def test_should_return_bytes(self, cairo_run, n):
output = cairo_run("test__uint256_to_bytes32", n=int_to_uint256(n))
assert bytes.fromhex(f"{n:064x}") == bytes(output)
class TestBytesToBytes8LittleEndian:
@pytest.mark.parametrize("bytes_len", list(range(20)))
def test_should_return_bytes8(self, cairo_run, bytes_len):
bytes_array = list(range(bytes_len))
bytes8_little_endian = [
int(bytes(bytes_array[i : i + 8][::-1]).hex(), 16)
for i in range(0, len(bytes_array), 8)
]
output = cairo_run(
"test__bytes_to_bytes8_little_endian", bytes=list(range(bytes_len))
)
assert bytes8_little_endian == output