-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathmobius_test.py
More file actions
117 lines (87 loc) · 4.08 KB
/
mobius_test.py
File metadata and controls
117 lines (87 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Import the mobius function
import sys
import unittest
# For importing from different folders
# OBS: This is supposed to be done with automated testing, hence relative to folder we want to import from
sys.path.append("Algorithms/math/mobius")
# If run from local:
# sys.path.append('../../Algorithms/math/mobius')
from mobius import mobius
class TestMobius(unittest.TestCase):
"""Test cases for the Mobius function."""
def test_mobius_one(self):
"""Test μ(1) = 1 (special case, zero prime factors)."""
self.assertEqual(mobius(1), 1)
def test_mobius_prime_two(self):
"""Test μ(2) = -1 (single prime factor)."""
self.assertEqual(mobius(2), -1)
def test_mobius_prime_three(self):
"""Test μ(3) = -1 (single prime factor)."""
self.assertEqual(mobius(3), -1)
def test_mobius_prime_large(self):
"""Test μ(17) = -1 (single prime factor - larger prime)."""
self.assertEqual(mobius(17), -1)
def test_mobius_two_primes(self):
"""Test μ(6) = 1 (two distinct prime factors: 2 × 3)."""
self.assertEqual(mobius(6), 1)
def test_mobius_two_primes_alternative(self):
"""Test μ(10) = 1 (two distinct prime factors: 2 × 5)."""
self.assertEqual(mobius(10), 1)
def test_mobius_three_primes(self):
"""Test μ(30) = -1 (three distinct prime factors: 2 × 3 × 5)."""
self.assertEqual(mobius(30), -1)
def test_mobius_three_primes_alternative(self):
"""Test μ(42) = -1 (three distinct prime factors: 2 × 3 × 7)."""
self.assertEqual(mobius(42), -1)
def test_mobius_four_primes(self):
"""Test μ(210) = 1 (four distinct prime factors: 2 × 3 × 5 × 7)."""
self.assertEqual(mobius(210), 1)
def test_mobius_five_primes(self):
"""Test μ(2310) = -1 (five distinct prime factors: 2 × 3 × 5 × 7 × 11)."""
self.assertEqual(mobius(2310), -1)
def test_mobius_squared_prime_four(self):
"""Test μ(4) = 0 (4 = 2², has squared factor)."""
self.assertEqual(mobius(4), 0)
def test_mobius_squared_prime_nine(self):
"""Test μ(9) = 0 (9 = 3², has squared factor)."""
self.assertEqual(mobius(9), 0)
def test_mobius_squared_factor_twelve(self):
"""Test μ(12) = 0 (12 = 2² × 3, has squared factor)."""
self.assertEqual(mobius(12), 0)
def test_mobius_squared_factor_eighteen(self):
"""Test μ(18) = 0 (18 = 2 × 3², has squared factor)."""
self.assertEqual(mobius(18), 0)
def test_mobius_squared_factor_large(self):
"""Test μ(72) = 0 (72 = 2³ × 3², has squared factors)."""
self.assertEqual(mobius(72), 0)
def test_mobius_squared_factor_hundred(self):
"""Test μ(100) = 0 (100 = 2² × 5², has squared factors)."""
self.assertEqual(mobius(100), 0)
def test_mobius_square_free_fifteen(self):
"""Test μ(15) = 1 (15 = 3 × 5, two distinct primes)."""
self.assertEqual(mobius(15), 1)
def test_mobius_square_free_thirtyfive(self):
"""Test μ(35) = 1 (35 = 5 × 7, two distinct primes)."""
self.assertEqual(mobius(35), 1)
def test_mobius_square_free_oneohfive(self):
"""Test μ(105) = -1 (105 = 3 × 5 × 7, three distinct primes)."""
self.assertEqual(mobius(105), -1)
def test_mobius_invalid_zero(self):
"""Test that μ(0) raises ValueError."""
with self.assertRaises(ValueError):
mobius(0)
def test_mobius_invalid_negative(self):
"""Test that μ(-5) raises ValueError."""
with self.assertRaises(ValueError):
mobius(-5)
def test_mobius_invalid_float(self):
"""Test that μ(3.5) raises ValueError."""
with self.assertRaises(ValueError):
mobius(3.5)
def test_mobius_invalid_string(self):
"""Test that μ('10') raises ValueError."""
with self.assertRaises(ValueError):
mobius('10')
if __name__ == "__main__":
print("Running Mobius function tests:")
unittest.main()