|
| 1 | +import unittest |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +class TestProgressionRecognizer(unittest.TestCase): |
| 7 | + def setUp(self): |
| 8 | + self.script_path = os.path.join("math", "AP-GP-AGP-HP-Recognizer", "AP-GP-AGP-HP-Recognizer.py") |
| 9 | + |
| 10 | + def run_script_with_input(self, user_input): |
| 11 | + process = subprocess.Popen( |
| 12 | + [sys.executable, self.script_path], |
| 13 | + stdin=subprocess.PIPE, |
| 14 | + stdout=subprocess.PIPE, |
| 15 | + stderr=subprocess.PIPE, |
| 16 | + text=True |
| 17 | + ) |
| 18 | + stdout, stderr = process.communicate(input=user_input) |
| 19 | + return stdout, stderr |
| 20 | + |
| 21 | + def test_ap_sequence(self): |
| 22 | + # Choice 1 for Recognize sequence type, then the AP sequence, then 2 to exit |
| 23 | + user_input = "1\n2, 4, 6, 8\n2\n" |
| 24 | + stdout, _ = self.run_script_with_input(user_input) |
| 25 | + self.assertIn("AP (common difference d = 2)", stdout) |
| 26 | + |
| 27 | + def test_gp_sequence(self): |
| 28 | + user_input = "1\n3, 6, 12, 24\n2\n" |
| 29 | + stdout, _ = self.run_script_with_input(user_input) |
| 30 | + self.assertIn("GP (common ratio r = 2)", stdout) |
| 31 | + |
| 32 | + def test_hp_sequence(self): |
| 33 | + user_input = "1\n6, 3, 2, 1.5\n2\n" |
| 34 | + stdout, _ = self.run_script_with_input(user_input) |
| 35 | + self.assertIn("HP (reciprocal AP difference = 0.166667)", stdout) |
| 36 | + |
| 37 | + def test_agp_sequence(self): |
| 38 | + user_input = "1\n2, 8, 24, 64\n2\n" |
| 39 | + stdout, _ = self.run_script_with_input(user_input) |
| 40 | + self.assertIn("AGP (repetition ratio r = 2)", stdout) |
| 41 | + |
| 42 | + def test_non_agp_zero_sequence(self): |
| 43 | + # Sequence like [0, 1, 0, 0] should not be identified as AGP with ratio 0 |
| 44 | + user_input = "1\n0, 1, 0, 0\n2\n" |
| 45 | + stdout, _ = self.run_script_with_input(user_input) |
| 46 | + self.assertNotIn("AGP (repetition ratio r = 0)", stdout) |
| 47 | + self.assertIn("Not AP, GP, AGP, or HP for the given values", stdout) |
| 48 | + |
| 49 | + def test_valid_agp_zero_sequence(self): |
| 50 | + # Sequence like [5, 0, 0, 0] is a valid AGP with ratio 0 |
| 51 | + user_input = "1\n5, 0, 0, 0\n2\n" |
| 52 | + stdout, _ = self.run_script_with_input(user_input) |
| 53 | + self.assertIn("AGP (repetition ratio r = 0)", stdout) |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + unittest.main() |
0 commit comments