|
| 1 | +"""Unit tests for the mathf library.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from mathf import fibonacci, multiples_of |
| 5 | + |
| 6 | + |
| 7 | +class TestFibonacci: |
| 8 | + """Tests for the fibonacci generator function.""" |
| 9 | + |
| 10 | + def test_fibonacci_first_ten_numbers(self): |
| 11 | + """Test that fibonacci generates the correct first 10 numbers.""" |
| 12 | + fib = fibonacci() |
| 13 | + expected = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
| 14 | + actual = [next(fib) for _ in range(10)] |
| 15 | + assert actual == expected |
| 16 | + |
| 17 | + def test_fibonacci_is_generator(self): |
| 18 | + """Test that fibonacci returns a generator.""" |
| 19 | + fib = fibonacci() |
| 20 | + assert hasattr(fib, '__next__') |
| 21 | + assert hasattr(fib, '__iter__') |
| 22 | + |
| 23 | + def test_fibonacci_continues_correctly(self): |
| 24 | + """Test that fibonacci continues correctly after first few numbers.""" |
| 25 | + fib = fibonacci() |
| 26 | + # Skip first 5 numbers |
| 27 | + for _ in range(5): |
| 28 | + next(fib) |
| 29 | + # Test the next few |
| 30 | + assert next(fib) == 8 |
| 31 | + assert next(fib) == 13 |
| 32 | + assert next(fib) == 21 |
| 33 | + |
| 34 | + |
| 35 | +class TestMultiplesOf: |
| 36 | + """Tests for the multiples_of function.""" |
| 37 | + |
| 38 | + def test_multiples_of_basic_case(self): |
| 39 | + """Test basic functionality with multiples of 2.""" |
| 40 | + collection = [1, 2, 3, 4, 5, 6, 7, 8] |
| 41 | + result = list(multiples_of(collection, 2)) |
| 42 | + assert result == [2, 4, 6, 8] |
| 43 | + |
| 44 | + def test_multiples_of_three(self): |
| 45 | + """Test with multiples of 3.""" |
| 46 | + collection = [1, 2, 3, 6, 9, 10, 12, 15] |
| 47 | + result = list(multiples_of(collection, 3)) |
| 48 | + assert result == [3, 6, 9, 12, 15] |
| 49 | + |
| 50 | + def test_multiples_of_one(self): |
| 51 | + """Test with multiples of 1 (all numbers).""" |
| 52 | + collection = [1, 2, 3, 4, 5] |
| 53 | + result = list(multiples_of(collection, 1)) |
| 54 | + assert result == [1, 2, 3, 4, 5] |
| 55 | + |
| 56 | + def test_multiples_of_empty_collection(self): |
| 57 | + """Test with empty collection.""" |
| 58 | + result = list(multiples_of([], 2)) |
| 59 | + assert result == [] |
| 60 | + |
| 61 | + def test_multiples_of_no_matches(self): |
| 62 | + """Test when no numbers are multiples.""" |
| 63 | + collection = [1, 3, 5, 7] |
| 64 | + result = list(multiples_of(collection, 2)) |
| 65 | + assert result == [] |
| 66 | + |
| 67 | + def test_multiples_of_zero_raises_error(self): |
| 68 | + """Test that zero raises ValueError.""" |
| 69 | + with pytest.raises(ValueError, match='Number must be positive'): |
| 70 | + list(multiples_of([1, 2, 3], 0)) |
| 71 | + |
| 72 | + def test_multiples_of_negative_raises_error(self): |
| 73 | + """Test that negative numbers raise ValueError.""" |
| 74 | + with pytest.raises(ValueError, match='Number must be positive'): |
| 75 | + list(multiples_of([1, 2, 3], -5)) |
| 76 | + |
| 77 | + def test_multiples_of_is_generator(self): |
| 78 | + """Test that multiples_of returns a generator.""" |
| 79 | + result = multiples_of([1, 2, 3, 4], 2) |
| 80 | + assert hasattr(result, '__next__') |
| 81 | + assert hasattr(result, '__iter__') |
| 82 | + |
| 83 | + def test_multiples_of_with_large_numbers(self): |
| 84 | + """Test with larger numbers.""" |
| 85 | + collection = [100, 150, 200, 250, 300] |
| 86 | + result = list(multiples_of(collection, 50)) |
| 87 | + assert result == [100, 150, 200, 250, 300] |
| 88 | + |
| 89 | + def test_multiples_of_with_duplicates(self): |
| 90 | + """Test that duplicates in collection are preserved.""" |
| 91 | + collection = [2, 4, 2, 6, 4] |
| 92 | + result = list(multiples_of(collection, 2)) |
| 93 | + assert result == [2, 4, 2, 6, 4] |
0 commit comments