-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_human_id.py
More file actions
94 lines (69 loc) · 3 KB
/
Copy pathtest_human_id.py
File metadata and controls
94 lines (69 loc) · 3 KB
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
import re
import pytest
from eval_protocol.human_id import generate_id, num_combinations
def test_generate_id_basic_format():
"""Test that generate_id produces the expected adjective-noun-NN format"""
id_str = generate_id(index=0)
# Should match pattern: adjective-noun-NNNNNN where NNNNNN is 000000-999999
assert re.match(r"^[a-z]+-[a-z]+-\d{6}$", id_str)
# Test a few specific indices to ensure deterministic behavior
assert generate_id(index=0) == "other-time-000000"
assert generate_id(index=1) == "other-time-000001"
assert generate_id(index=99) == "other-time-000099"
assert generate_id(index=100) == "other-time-000100"
def test_generate_id_index_mapping():
"""Test that index mapping works correctly"""
# Test number cycling (0-99)
for i in range(100):
id_str = generate_id(index=i)
expected_num = f"{i:06d}"
assert id_str.endswith(f"-{expected_num}")
assert id_str.startswith("other-time-")
# Test noun advancement after 1000000 numbers
id_1000000 = generate_id(index=1000000)
assert id_1000000.startswith("other-year-000000")
# Test adjective advancement (after all nouns * 1000000)
# This will depend on dictionary size, so let's test the pattern
from eval_protocol.human_id import dictionary
nouns_count = len(dictionary.nouns)
adjective_boundary = nouns_count * 1000000
id_at_boundary = generate_id(index=adjective_boundary)
# Should have advanced to the next adjective
assert not id_at_boundary.startswith("other-")
def test_generate_id_index_out_of_range():
"""Test that invalid indices raise appropriate errors"""
total = num_combinations()
assert total > 0
# Last valid index should work
generate_id(index=total - 1)
# First invalid index should raise error
with pytest.raises(ValueError):
generate_id(index=total)
# Negative index should raise error
with pytest.raises(ValueError):
generate_id(index=-1)
def test_generate_id_seed_stability():
"""Test that same seed produces same ID"""
a = generate_id(seed=1234)
b = generate_id(seed=1234)
assert a == b
# Without index, default produces separator '-' and at least 3 components
c = generate_id()
assert re.match(r"^[a-z]+-[a-z]+-\d{6}$", c)
def test_generate_id_seed_with_index():
"""Test that seed affects index-based generation deterministically"""
x = generate_id(index=42, seed=1)
y = generate_id(index=42, seed=999)
z = generate_id(index=42, seed=1)
# Same seed should produce same result
assert x == z
# Different seeds should produce different results
assert x != y
# All should follow the correct format
assert re.match(r"^[a-z]+-[a-z]+-\d{6}$", x)
assert re.match(r"^[a-z]+-[a-z]+-\d{6}$", y)
def test_generate_id_random_format():
"""Test that random generation (no index) produces correct format"""
for _ in range(10):
id_str = generate_id()
assert re.match(r"^[a-z]+-[a-z]+-\d{6}$", id_str)