"""
Unit tests for f0_make_randomvalues function block.
Tests random number generation, CSV I/O, and function notation output.
"""
import os
import tempfile
from pathlib import Path
from lib import (
generate_random_numbers,
save_data_to_csv,
load_data_from_csv,
display_data,
write_function_notation
)
def test_generate_random_numbers():
"""Test UT-001: Default random number generation (5 numbers)."""
print("Running UT-001: Default random number generation...")
data = generate_random_numbers(5)
assert len(data) == 5, f"Expected 5 numbers, got {len(data)}"
assert all(0 <= x <= 999 for x in data), "All numbers should be in range 0-999"
print(f"✓ Generated 5 random numbers: {data}")
def test_custom_count_random_numbers():
"""Test UT-002: Custom count random number generation."""
print("\nRunning UT-002: Custom count random number generation...")
counts = [1, 10, 100]
for count in counts:
data = generate_random_numbers(count)
assert len(data) == count, f"Expected {count} numbers, got {len(data)}"
assert all(0 <= x <= 999 for x in data), "All numbers should be in range 0-999"
print(f"✓ Generated {count} random numbers successfully")
def test_csv_save_and_load():
"""Test UT-003: CSV save and load operations."""
print("\nRunning UT-003: CSV save and load...")
with tempfile.TemporaryDirectory() as tmpdir:
filepath = Path(tmpdir) / "test_data.csv"
test_data = [123, 456, 789, 12, 345]
# Save data
save_data_to_csv(test_data, str(filepath))
assert filepath.exists(), "CSV file should be created"
print(f"✓ Saved data to CSV: {test_data}")
# Load data
loaded_data = load_data_from_csv(str(filepath))
assert loaded_data == test_data, f"Data mismatch: {loaded_data} != {test_data}"
print(f"✓ Loaded data from CSV: {loaded_data}")
print("✓ Data integrity verified")
def test_function_notation_output():
"""Test UT-004: Function notation output."""
print("\nRunning UT-004: Function notation output...")
with tempfile.TemporaryDirectory() as tmpdir:
filepath = Path(tmpdir) / "test_func.txt"
# Write function notation
write_function_notation(str(filepath))
assert filepath.exists(), "Function notation file should be created"
# Verify content
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
assert content == 'y=f0()', f"Expected 'y=f0()', got '{content}'"
print(f"✓ Function notation verified: {content}")
def test_display_data():
"""Test data display functionality."""
print("\nTesting data display functionality...")
test_data = [42, 123, 999]
print("Expected output:")
print("data")
for value in test_data:
print(value)
print("\nActual output:")
display_data(test_data)
print("✓ Display function executed successfully")
def run_all_tests():
"""Run all unit tests."""
print("=" * 60)
print("f0_make_randomvalues Unit Tests")
print("=" * 60)
try:
test_generate_random_numbers()
test_custom_count_random_numbers()
test_csv_save_and_load()
test_function_notation_output()
test_display_data()
print("\n" + "=" * 60)
print("All tests passed successfully! ✓")
print("=" * 60)
return 0
except AssertionError as e:
print(f"\n✗ Test failed: {e}")
return 1
except Exception as e:
print(f"\n✗ Unexpected error: {e}")
return 1
if __name__ == '__main__':
import sys
sys.exit(run_all_tests())