-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_python.py
103 lines (78 loc) · 3.01 KB
/
test_python.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
import re
import pytest
from eth_account import Account
from src.main import (calculate_rarity_score, is_palindrome,
to_checksum_address, verify_address)
def test_rarity_score():
test_cases = [
("0x0000000000000000", 0.125), # all zeros should have low score
("0x1234567890abcdef", 1.0), # all different chars should have high score
("0xaaaaaaaaaaaaaaaa", 0.125), # all same chars should have low score
]
for address, expected_score in test_cases:
score = calculate_rarity_score(address[2:]) # remove 0x prefix
assert abs(score - expected_score) < 0.01, f"Failed for {address}"
def test_palindrome_check():
test_cases = [
("1221", True),
("abba", True),
("1234", False),
("12345", False),
]
for test_str, expected in test_cases:
assert is_palindrome(test_str) == expected, f"Failed for {test_str}"
def test_checksum_address():
test_cases = [
(
"0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed",
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD",
),
(
"0x0000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000",
),
]
for input_addr, expected in test_cases:
result = to_checksum_address(input_addr)
assert result == expected, f"Failed for {input_addr}"
def test_address_verification():
# create a test account
account = Account.create()
address = account.address
private_key = account._private_key.hex()
assert verify_address(address, private_key), "Address verification failed"
def test_pattern_matching():
patterns = [
(r"^0x[0-9a-fA-F]{4}0000[0-9a-fA-F]{36}$", "0x12340000abcdef"),
(
r"^0x[0-9a-fA-F]*(1234|2345|3456|4567|5678|6789)[0-9a-fA-F]*$",
"0x1234abcdef",
),
(r"^0x[0-9a-fA-F]*(DEADBEEF|BADDCAFE|1337BEEF)[0-9a-fA-F]*$", "0xDEADBEEF"),
]
for pattern, test_str in patterns:
assert re.match(
pattern, test_str
), f"Pattern {pattern} failed to match {test_str}"
@pytest.mark.parametrize("num_attempts", [1000, 5000, 10000])
def test_address_generation_performance(benchmark, num_attempts):
def generate_addresses():
for _ in range(num_attempts):
Account.create()
result = benchmark(generate_addresses)
assert result.stats.total_time < num_attempts * 0.1 # expected time per address
def test_concurrent_generation():
import threading
from concurrent.futures import ThreadPoolExecutor
found = threading.Event()
addresses = []
def generate_address():
if not found.is_set():
account = Account.create()
addresses.append(account.address)
found.set()
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(generate_address) for _ in range(4)]
for future in futures:
future.result()
assert addresses, "No addresses were generated"