Skip to main content

Property-based testing utilities for finite automata and context-free grammars.

Project description

pbt4automata

Tests Python Version

pbt4automata provides tools to construct finite automata and context-free grammars in Python and validate their behavior with property-based tests powered by Hypothesis.

Installation

pip install -e .

Examples

Deterministic Finite Automata

Testing a DFA against a rule

The following example tests a DFA that accepts all strings that contain the substring "010" or "100".

from pbt4automata import DFA

automaton = DFA(
    states=["q0", "q1", "q2", "q3", "q4", "q5"],
    alphabet="01",
    transition_function={
        ("q0", "0"): "q1",
        ("q0", "1"): "q4",
        ("q1", "0"): "q1",
        ("q1", "1"): "q2",
        ("q2", "0"): "q3",
        ("q2", "1"): "q4",
        ("q3", "0"): "q3",
        ("q3", "1"): "q3",
        ("q4", "0"): "q5",
        ("q4", "1"): "q4",
        ("q5", "0"): "q3",
        ("q5", "1"): "q2",
    },
    start_state="q0",
    accept_states=["q3"],
)

result = automaton.test("[01]*(010|100)[01]*")

if result is True:
    print("Success!")
else:
    print("Counterexample:", result)

You can also pass a function of type Callable[[str], bool] to automaton.test(...) instead of a regex.

Testing two DFAs for equivalence

from pbt4automata import Automaton, DFA

automata1 = DFA(
    ...
)

automata2 = DFA(
    ...
)

result = Automaton.test_equivalence(automata1, automata2)

if result is True:
    print("Success!")
else:
    print("Counterexample:", repr(result))

Context-Free Grammars

Testing a CFG against a rule

The following example tests a CFG in Chomsky Normal Form that generates all non-empty strings of balanced parentheses.

from pbt4automata import CNF

cnf = CNF(
    terminals="()",
    nonterminals="SLRX",
    productions={
        "S": ["LX", "SS"],
        "L": ["("],
        "R": [")"],
        "X": ["SR", ")"]
    },
    start_symbol="S"
)

def check_balance(input_string: str) -> bool:
    if input_string == "":
        return False
    s = 0
    for c in input_string:
        if c == "(":
            s += 1
        elif c == ")":
            s -= 1
        if s < 0:
            return False
    return s == 0

result = cnf.test(check_balance)

if result is True:
    print("Success!")
else:
    print("Counterexample:", result)

Development

Install dev dependencies and run tests:

pip install -e ".[dev]"
pytest -v

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pbt4automata-0.1.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pbt4automata-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file pbt4automata-0.1.1.tar.gz.

File metadata

  • Download URL: pbt4automata-0.1.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pbt4automata-0.1.1.tar.gz
Algorithm Hash digest
SHA256 526679d2212426f4e63e91aaf1ce518dcd729550f58e4ee41f4bfb7a7b7d632f
MD5 cc5f3e3df56f28ee821d143793689eb6
BLAKE2b-256 66604f56360928d8c035f2565243738cae24c6486fe43e3f7c01e249c037cf05

See more details on using hashes here.

Provenance

The following attestation bundles were made for pbt4automata-0.1.1.tar.gz:

Publisher: python-publish.yml on mrigankpawagi/pbt4automata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pbt4automata-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pbt4automata-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pbt4automata-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3cd13b8cc33314a8430e1ca1683c66c6751e44d431e0260529b2eeff40016ef9
MD5 485ba4bd82531fea27b56f309b0ac2ed
BLAKE2b-256 bdc9172aa22bd93b6814ef3a729951b28be8adb0ba8be61814c334d5ab55de04

See more details on using hashes here.

Provenance

The following attestation bundles were made for pbt4automata-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on mrigankpawagi/pbt4automata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page