|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import math |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | +from hypothesis import given |
| 8 | +from hypothesis import strategies as st |
| 9 | + |
| 10 | +from fuzzylogic.classes import Domain |
| 11 | +from fuzzylogic.functions import singleton |
| 12 | + |
| 13 | +# --------------------------------------------------------------------------- |
| 14 | +# Basic Unit Tests |
| 15 | +# --------------------------------------------------------------------------- |
| 16 | + |
| 17 | + |
| 18 | +def test_singleton_membership(): |
| 19 | + """Test that a singleton returns 1.0 exactly at its specified location and 0 elsewhere.""" |
| 20 | + s = singleton(500) |
| 21 | + # Exact hit yields 1.0 |
| 22 | + assert s(500) == 1.0 |
| 23 | + # Any other value yields 0.0 |
| 24 | + assert s(499.999) == 0.0 |
| 25 | + assert s(500.1) == 0.0 |
| 26 | + |
| 27 | + |
| 28 | +def test_singleton_center_of_gravity(): |
| 29 | + """Test that the center_of_gravity always returns the singleton’s location.""" |
| 30 | + for c in [0, 250, 500, 750, 1000]: |
| 31 | + s = singleton(c) |
| 32 | + assert s.center_of_gravity() == c, f"Expected COG {c}, got {s.center_of_gravity()}" |
| 33 | + |
| 34 | + |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +# Domain Integration Tests |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | + |
| 39 | + |
| 40 | +def test_singleton_with_domain(): |
| 41 | + """ |
| 42 | + Test that a SingletonSet assigned to a domain yields the expected membership |
| 43 | + array, containing a spike at the correct position. |
| 44 | + """ |
| 45 | + D = Domain("D", 0, 1000) |
| 46 | + s = singleton(500) |
| 47 | + s.domain = D |
| 48 | + |
| 49 | + arr = s.array() |
| 50 | + points = D.range |
| 51 | + |
| 52 | + assert 500 in points, "Domain should contain 500 exactly." |
| 53 | + idx = points[500] |
| 54 | + |
| 55 | + np.testing.assert_almost_equal(arr[idx], 1.0) |
| 56 | + np.testing.assert_almost_equal(arr.sum(), 1.0) |
| 57 | + |
| 58 | + |
| 59 | +@given(c=st.integers(min_value=0, max_value=1000)) |
| 60 | +def test_singleton_property_center(c: int): |
| 61 | + """ |
| 62 | + Property-based test: For any integer c in [0, 1000], a singleton defined at c |
| 63 | + (and assigned to an appropriately discretized Domain) has a center-of-gravity equal to c. |
| 64 | + """ |
| 65 | + D = Domain("D", 0, 1000) |
| 66 | + s = singleton(c) |
| 67 | + s.domain = D |
| 68 | + assert s.center_of_gravity() == c |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# Fuzzy Operation Integration Tests |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | +def test_singleton_union(): |
| 77 | + """ |
| 78 | + Test that the fuzzy union (OR) of two disjoint singleton sets creates a fuzzy set |
| 79 | + containing two spikes – one at each singleton location. |
| 80 | + """ |
| 81 | + D = Domain("D", 0, 1000) |
| 82 | + s1 = singleton(500) |
| 83 | + s2 = singleton(600) |
| 84 | + s1.domain = D |
| 85 | + s2.domain = D |
| 86 | + |
| 87 | + union_set = s1 | s2 |
| 88 | + union_set.domain = D |
| 89 | + |
| 90 | + arr = union_set.array() |
| 91 | + points = D.range |
| 92 | + |
| 93 | + assert 500 in points and 600 in points |
| 94 | + idx_500 = points[500] |
| 95 | + idx_600 = points[600] |
| 96 | + np.testing.assert_almost_equal(arr[idx_500], 1.0) |
| 97 | + np.testing.assert_almost_equal(arr[idx_600], 1.0) |
| 98 | + |
| 99 | + np.testing.assert_almost_equal(arr.sum(), 2.0) |
| 100 | + |
| 101 | + |
| 102 | +# --------------------------------------------------------------------------- |
| 103 | +# Differential / Regression Testing with Defuzzification |
| 104 | +# --------------------------------------------------------------------------- |
| 105 | + |
| 106 | + |
| 107 | +def test_singleton_defuzzification(): |
| 108 | + """ |
| 109 | + Test that when a singleton is used in defuzzification (via center_of_gravity), |
| 110 | + the exact spike value is returned regardless of discrete sampling issues. |
| 111 | + """ |
| 112 | + s = singleton(500.1) |
| 113 | + assert math.isclose(s.center_of_gravity(), 500.1, rel_tol=1e-9) |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# Performance |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | + |
| 121 | +def test_singleton_performance(): |
| 122 | + """ |
| 123 | + A basic performance test to ensure that evaluating a singleton over a large domain |
| 124 | + remains efficient. |
| 125 | + """ |
| 126 | + D = Domain("D", 0, 1000, res=0.0001) # A large domain |
| 127 | + D.s = singleton(500) |
| 128 | + time_taken = pytest.importorskip("timeit").timeit(lambda: D.s.array(), number=10) |
| 129 | + assert time_taken < 1, "Performance slowed down unexpectedly." |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + pytest.main([__file__, "-v"]) |
0 commit comments