#!/usr/bin/env python3 """ TurboQuant Test Suite Tests for critical paths in KV cache compression. Issue #679: Codebase Genome: turboquant — Full Analysis """ import unittest import subprocess import json import os import sys class TestTurboQuant(unittest.TestCase): """Test TurboQuant implementation.""" def test_repo_structure(self): """Verify expected files exist.""" required_files = [ "llama-turbo.h", "llama-turbo.cpp", "ggml-metal-turbo.metal", "README.md", "GENOME.md" ] for filename in required_files: filepath = os.path.join(os.path.dirname(__file__), "..", filename) self.assertTrue(os.path.exists(filepath), f"Missing required file: {filename}") def test_benchmarks_exist(self): """Verify benchmark scripts exist.""" benchmark_files = [ "benchmarks/run_benchmarks.py", "benchmarks/run_perplexity.py", "benchmarks/run_long_session.py" ] for filename in benchmark_files: filepath = os.path.join(os.path.dirname(__file__), "..", filename) self.assertTrue(os.path.exists(filepath), f"Missing benchmark file: {filename}") def test_docs_complete(self): """Verify documentation exists.""" doc_files = [ "docs/PROJECT_STATUS.md", "profiles/README.md" ] for filename in doc_files: filepath = os.path.join(os.path.dirname(__file__), "..", filename) self.assertTrue(os.path.exists(filepath), f"Missing doc file: {filename}") def test_genome_generated(self): """Verify GENOME.md was generated.""" genome_path = os.path.join(os.path.dirname(__file__), "..", "GENOME.md") self.assertTrue(os.path.exists(genome_path), "GENOME.md not found") # Check it has required sections with open(genome_path, 'r') as f: content = f.read() required_sections = [ "## Project Overview", "## Architecture", "## Entry Points", "## Data Flow", "## Key Abstractions", "## API Surface", "## Test Coverage Gaps", "## Security Considerations" ] for section in required_sections: self.assertIn(section, content, f"GENOME.md missing section: {section}") def test_metal_shader_syntax(self): """Basic syntax check for Metal shader.""" shader_path = os.path.join(os.path.dirname(__file__), "..", "ggml-metal-turbo.metal") with open(shader_path, 'r') as f: content = f.read() # Check for key functions self.assertIn("kernel_fwht_128", content, "Missing kernel_fwht_128 function") self.assertIn("kernel_turbo4_dequant", content, "Missing kernel_turbo4_dequant function") self.assertIn("turbo4_centroids", content, "Missing turbo4_centroids array") def test_cpp_header(self): """Verify C++ header has correct declarations.""" header_path = os.path.join(os.path.dirname(__file__), "..", "llama-turbo.h") with open(header_path, 'r') as f: content = f.read() # Check for function declarations self.assertIn("polar_quant_encode_turbo4", content, "Missing encode function") self.assertIn("polar_quant_decode_turbo4", content, "Missing decode function") self.assertIn('extern "C"', content, "Missing C linkage") class TestBenchmarks(unittest.TestCase): """Test benchmark infrastructure.""" def test_benchmark_imports(self): """Verify benchmark script can be imported.""" benchmark_path = os.path.join(os.path.dirname(__file__), "..", "benchmarks", "run_benchmarks.py") # Check file exists self.assertTrue(os.path.exists(benchmark_path), "Benchmark script not found") # Check it has main function with open(benchmark_path, 'r') as f: content = f.read() self.assertIn("def main():", content, "Benchmark script missing main function") self.assertIn("argparse", content, "Benchmark script missing argparse") class TestDocumentation(unittest.TestCase): """Test documentation completeness.""" def test_readme_sections(self): """Verify README has required sections.""" readme_path = os.path.join(os.path.dirname(__file__), "..", "README.md") with open(readme_path, 'r') as f: content = f.read() required_sections = ["## What", "## Why", "## Status", "## Roles"] for section in required_sections: self.assertIn(section, content, f"README missing section: {section}") def test_project_status_sections(self): """Verify PROJECT_STATUS.md has required sections.""" status_path = os.path.join(os.path.dirname(__file__), "..", "docs", "PROJECT_STATUS.md") with open(status_path, 'r') as f: content = f.read() # Check for key findings self.assertIn("73%", content, "Missing 73% savings metric") self.assertIn("PolarQuant", content, "Missing PolarQuant references") self.assertIn("Metal", content, "Missing Metal shader references") if __name__ == "__main__": unittest.main()