forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_exponents.py
121 lines (97 loc) · 2.87 KB
/
test_exponents.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pytest
from hypothesis import example, given, settings
from hypothesis import strategies as st
from vyper.codegen.expr import calculate_largest_base, calculate_largest_power
@pytest.mark.fuzzing
@pytest.mark.parametrize("power", range(2, 255))
def test_exp_uint256(get_contract, assert_tx_failed, power):
code = f"""
@external
def foo(a: uint256) -> uint256:
return a ** {power}
"""
max_base = calculate_largest_base(power, 256, False)
assert max_base ** power < 2 ** 256
assert (max_base + 1) ** power >= 2 ** 256
c = get_contract(code)
c.foo(max_base)
assert_tx_failed(lambda: c.foo(max_base + 1))
@pytest.mark.fuzzing
@pytest.mark.parametrize("power", range(2, 127))
def test_exp_int128(get_contract, assert_tx_failed, power):
code = f"""
@external
def foo(a: int128) -> int128:
return a ** {power}
"""
max_base = calculate_largest_base(power, 128, True)
assert -(2 ** 127) <= max_base ** power < 2 ** 127
assert -(2 ** 127) <= (-max_base) ** power < 2 ** 127
assert not -(2 ** 127) <= (max_base + 1) ** power < 2 ** 127
assert not -(2 ** 127) <= (-(max_base + 1)) ** power < 2 ** 127
c = get_contract(code)
c.foo(max_base)
c.foo(-max_base)
assert_tx_failed(lambda: c.foo(max_base + 1))
assert_tx_failed(lambda: c.foo(-max_base - 1))
@pytest.mark.fuzzing
@given(a=st.integers(min_value=2, max_value=2 ** 256 - 1))
# 8 bits
@example(a=2 ** 7)
@example(a=2 ** 7 - 1)
# 16 bits
@example(a=2 ** 15)
@example(a=2 ** 15 - 1)
# 32 bits
@example(a=2 ** 31)
@example(a=2 ** 31 - 1)
# 64 bits
@example(a=2 ** 63)
@example(a=2 ** 63 - 1)
# 128 bits
@example(a=2 ** 127)
@example(a=2 ** 127 - 1)
# 256 bits
@example(a=2 ** 256 - 1)
@settings(max_examples=200, deadline=1000)
def test_max_exp(get_contract, assert_tx_failed, a):
code = f"""
@external
def foo(b: uint256) -> uint256:
return {a} ** b
"""
c = get_contract(code)
max_power = calculate_largest_power(a, 256, False)
assert a ** max_power < 2 ** 256
assert a ** (max_power + 1) >= 2 ** 256
c.foo(max_power)
assert_tx_failed(lambda: c.foo(max_power + 1))
@pytest.mark.fuzzing
@given(a=st.integers(min_value=2, max_value=2 ** 127 - 1))
# 8 bits
@example(a=2 ** 7)
@example(a=2 ** 7 - 1)
# 16 bits
@example(a=2 ** 15)
@example(a=2 ** 15 - 1)
# 32 bits
@example(a=2 ** 31)
@example(a=2 ** 31 - 1)
# 64 bits
@example(a=2 ** 63)
@example(a=2 ** 63 - 1)
# 128 bits
@example(a=2 ** 127 - 1)
@settings(max_examples=200, deadline=1000)
def test_max_exp_int128(get_contract, assert_tx_failed, a):
code = f"""
@external
def foo(b: int128) -> int128:
return {a} ** b
"""
c = get_contract(code)
max_power = calculate_largest_power(a, 128, True)
assert -(2 ** 127) <= a ** max_power < 2 ** 127
assert not -(2 ** 127) <= a ** (max_power + 1) < 2 ** 127
c.foo(max_power)
assert_tx_failed(lambda: c.foo(max_power + 1))