"""Isolate Figure 3 line 30; not a CART simulator or a workload benchmark."""
from fractions import Fraction
import json


def relax_filter(capacity, t1, b1, target):
    """Check the size condition for an already referenced S page in T1."""
    for value in (capacity, t1, b1):
        if type(value) is not int:
            raise ValueError("capacity and list lengths must be integers")
    if capacity < 1 or not 1 <= t1 <= capacity or not 0 <= b1 <= capacity:
        raise ValueError("invalid pre-eviction list lengths")
    if type(target) not in (int, str, Fraction):
        raise ValueError("target must be an integer or exact rational")
    try:
        p = Fraction(target)
    except (ValueError, ZeroDivisionError) as error:
        raise ValueError("invalid target") from error
    if not 0 <= p <= capacity:
        raise ValueError("target outside cache capacity")
    return t1 >= min(p + 1, b1)


def examples():
    inputs = [(4, 2, 4, "2"), (4, 3, 4, "2"), (4, 2, 2, "2"),
              (4, 1, 0, "4"), (4, 3, 4, "5/2"), (4, 4, 4, "5/2")]
    return {
        "schemaVersion": 1,
        "evidenceType": "constructed exact-arithmetic predicate inputs",
        "source": "FAST 2004, Bansal and Modha, Figure 3 line 30",
        "scope": "Referenced S page rotating within T1; size predicate only.",
        "limitations": "Not a complete CART simulator. No reachability, hit-ratio, latency or concurrency result.",
        "cases": [
            {"capacityPages": c, "t1Pages": n, "b1Entries": b, "targetExact": p,
             "relaxFilter": relax_filter(c, n, b, p)}
            for c, n, b, p in inputs
        ],
    }


if __name__ == "__main__":
    print(json.dumps(examples(), indent=2))
