#!/usr/bin/env python3
"""
Test the optimized find implementation
"""

import time
from cutysoup import CutySoup

# Test HTML 
html = """
<html>
<body>
    <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <p class="text">Some text</p>
        <span class="highlight">Highlight</span>
    </div>
    <div class="footer">
        <p>Footer text</p>
    </div>
</body>
</html>
"""

soup = CutySoup(html)

def time_operation(func, name, iterations=10):
    times = []
    for _ in range(iterations):
        start = time.perf_counter()
        result = func()
        end = time.perf_counter()
        times.append((end - start) * 1000)
    
    avg_time = sum(times) / len(times)
    result_count = len(result) if result else 0
    print(f"{name}: {avg_time:.3f}ms ({result_count} results)")
    return avg_time, result_count

print("🧪 Testing Optimized Find Implementation")
print("=" * 50)

# Test basic tag search
time_operation(lambda: soup.find_all('div'), "find_all('div')")
time_operation(lambda: soup.find_all('p'), "find_all('p')")

# Test class searches - fix the parameter format
time_operation(lambda: soup.find_all('div', class_='item'), "find_all('div', class_='item')")
time_operation(lambda: soup.find_all(class_='item'), "find_all(class_='item')")

# Test complex searches
time_operation(lambda: soup.find_all('p', class_='text'), "find_all('p', class_='text')")

# Test that results are correct
print("\n🔍 Correctness Check:")
divs = soup.find_all('div')
print(f"Total divs: {len(divs)}")
for i, div in enumerate(divs):
    print(f"  div[{i}]: class='{div.get('class', [])}', text='{div.get_text().strip()}'")

items = soup.find_all('div', class_='item')
print(f"Div items: {len(items)}")
for i, item in enumerate(items):
    print(f"  item[{i}]: {item.get_text().strip()}")

# Test edge cases
time_operation(lambda: soup.find_all('nonexistent'), "find_all('nonexistent')")
time_operation(lambda: soup.find_all(class_='nonexistent'), "find_all(class_='nonexistent')")