Files
Ein Anderssono 54d2fecee0 Initial commit: Pi calculation benchmark with 34 languages
- Added implementations for: bash, brainfuck, c, cpp, crystal, csharp, d, dart, elixir, erlang, fortran, go, haskell, java, javascript, julia, kotlin, objective-c, scala, typescript, lua, nim, odin, perl, php, python, r, ruby, rust, swift, zig, assembly, vimscript, wolfram
- All implementations use Machin's formula: π/4 = 4*arctan(1/5) - arctan(1/239)
- Build system with ./build.sh, test system with ./test.sh
- Performance testing with ./run_all.sh
- Comprehensive README.md explaining performance differences
- Test framework verifies correctness against known π values
2026-04-23 00:26:18 +02:00

123 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Unit tests for Python pi calculation
"""
import subprocess
import sys
SCRIPT_DIR = "/Users/einand/Code/test/python"
SCRIPT = f"{SCRIPT_DIR}/print_hej.py"
def run_test(decimals, expected_prefix):
"""Run the program and check output"""
result = subprocess.run(
["python3", SCRIPT, str(decimals)],
capture_output=True,
text=True,
timeout=30
)
output = result.stdout.strip()
if output.startswith(expected_prefix):
print(f"✓ PASS: {decimals} decimaler - {output[:50]}...")
return True
else:
print(f"✗ FAIL: {decimals} decimaler")
print(f" Förväntade: {expected_prefix}...")
print(f" Fick: {output[:100]}")
return False
def main():
print("=== Python Pi-beräkning Unit Tester ===")
print()
tests_passed = 0
tests_failed = 0
# Test 1: 10 decimaler
print("Test 1: 10 decimaler")
if run_test(10, "3.1415926535"):
tests_passed += 1
else:
tests_failed += 1
print()
# Test 2: 5 decimaler
print("Test 2: 5 decimaler")
if run_test(5, "3.14159"):
tests_passed += 1
else:
tests_failed += 1
print()
# Test 3: 1 decimal
print("Test 3: 1 decimal")
if run_test(1, "3.1"):
tests_passed += 1
else:
tests_failed += 1
print()
# Test 4: 100 decimaler
print("Test 4: 100 decimaler")
expected_100 = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
if run_test(100, expected_100):
tests_passed += 1
else:
tests_failed += 1
print()
# Test 5: Default (100 decimaler)
print("Test 5: Default (100 decimaler)")
result = subprocess.run(
["python3", SCRIPT],
capture_output=True,
text=True,
timeout=30
)
output = result.stdout.strip()
if output.startswith(expected_100):
print(f"✓ PASS: Default - {output[:50]}...")
tests_passed += 1
else:
print(f"✗ FAIL: Default")
print(f" Förväntade: {expected_100}...")
print(f" Fick: {output[:100]}")
tests_failed += 1
print()
# Test 6: Ogiltig input
print("Test 6: Ogiltig input (ska använda default 100)")
result = subprocess.run(
["python3", SCRIPT, "invalid"],
capture_output=True,
text=True,
timeout=30
)
output = result.stdout.strip()
if output.startswith(expected_100):
print(f"✓ PASS: Ogiltig input hanterad korrekt")
tests_passed += 1
else:
print(f"✗ FAIL: Ogiltig input")
print(f" Förväntade: {expected_100}...")
print(f" Fick: {output[:100]}")
tests_failed += 1
print()
# Summary
print("=== Sammanfattning ===")
print(f"Passerade: {tests_passed}")
print(f"Misslyckade: {tests_failed}")
if tests_failed == 0:
print("✓ Alla tester passerade!")
sys.exit(0)
else:
print("✗ Några tester misslyckades")
sys.exit(1)
if __name__ == "__main__":
main()