|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +def run_cargo(): |
| 6 | + """Compileaza si ruleaza cargo pentru a verifica daca exista erori.""" |
| 7 | + print("Compiling Rust project...") |
| 8 | + result = subprocess.run( |
| 9 | + ["cargo", "build"], |
| 10 | + stdout=subprocess.PIPE, |
| 11 | + stderr=subprocess.PIPE, |
| 12 | + text=True |
| 13 | + ) |
| 14 | + # Afiseaza warning-uri si erori |
| 15 | + print(result.stdout) |
| 16 | + print(result.stderr) |
| 17 | + if result.returncode != 0: |
| 18 | + print("Eroare la compilare! Oprire program.") |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | +def run_test(db_file, test_idx): |
| 22 | + """Ruleaza un singur test si returneaza True daca trece, False altfel.""" |
| 23 | + input_file = Path(f"../tests/input/test{test_idx}.in") |
| 24 | + output_dir = Path("../tests/output-rust-method") |
| 25 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 26 | + output_file = output_dir / f"{db_file.stem}.test{test_idx}.out" |
| 27 | + ref_file = Path(f"../tests/ref/{db_file.stem}.test{test_idx}.ref") |
| 28 | + |
| 29 | + # Ruleaza aplicatia Rust |
| 30 | + with input_file.open("r") as fin, output_file.open("w") as fout: |
| 31 | + result = subprocess.run( |
| 32 | + ["cargo", "run", "--", str(db_file)], |
| 33 | + stdin=fin, |
| 34 | + stdout=fout, |
| 35 | + stderr=subprocess.PIPE, |
| 36 | + text=True |
| 37 | + ) |
| 38 | + if result.returncode != 0: |
| 39 | + print(f"Eroare la rularea testului {test_idx} cu {db_file.name}") |
| 40 | + print(result.stderr) |
| 41 | + return False |
| 42 | + |
| 43 | + # Compara output cu fisierul de referinta |
| 44 | + diff_result = subprocess.run( |
| 45 | + ["diff", str(output_file), str(ref_file)], |
| 46 | + stdout=subprocess.PIPE, |
| 47 | + text=True |
| 48 | + ) |
| 49 | + if diff_result.returncode == 0: |
| 50 | + print(f"Test {test_idx} cu {db_file.name}: OK") |
| 51 | + return True |
| 52 | + else: |
| 53 | + print(f"Test {test_idx} cu {db_file.name}: FAILED") |
| 54 | + return False |
| 55 | + |
| 56 | +def main(): |
| 57 | + run_cargo() |
| 58 | + db_files = [Path("../tests/db/small.db"), Path("../tests/db/big.db")] |
| 59 | + total_score = 0 |
| 60 | + points_per_test = 3.5 |
| 61 | + for db_file in db_files: |
| 62 | + print(f"\n--- Testare pentru {db_file.name} ---") |
| 63 | + for i in range(1, 11): |
| 64 | + if run_test(db_file, i): |
| 65 | + total_score += points_per_test |
| 66 | + |
| 67 | + print(f"\nScor total: {total_score}/70") |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + main() |
0 commit comments