# Path: chopsticks/pencode.py
# def pencode(obj):
#     """Encode the given Python primitive structure, returning a byte string."""
#     p = Pencoder()
#     p._pencode(obj, p.backrefs, p.out)
#     return p.getvalue()
# 
# def pdecode(buf):
#     """Decode a pencoded byte string to a structure."""
#     return PDecoder().decode(buf)


import math
import pytest
from hypothesis import example, given, strategies
from chopsticks.pencode import pencode, pdecode
ascii_binary = strategies.builds(lambda s: s.encode('ascii'), ascii_text)

immutables = strategies.recursive(
    ascii_binary |
    strategies.booleans() |
    strategies.floats(allow_nan=False) |
    strategies.integers() |
    strategies.none() |
    strategies.text() |
    strategies.tuples(),
    lambda children: (
        strategies.frozensets(children) |
        strategies.tuples(children)
    ),
)

mutables = strategies.recursive(
    strategies.dictionaries(immutables, immutables) |
    strategies.lists(immutables) |
    strategies.sets(immutables),
    lambda children: (
        strategies.dictionaries(immutables, children) |
        strategies.lists(children)
    ),
)

def assert_roundtrip(obj):
    """Assert that we can successfully round-trip the given object."""
    buf = pencode(obj)
    assert isinstance(buf, bytes)

# Path: chopsticks/pencode.py
# def pencode(obj):
#     """Encode the given Python primitive structure, returning a byte string."""
#     p = Pencoder()
#     p._pencode(obj, p.backrefs, p.out)
#     return p.getvalue()
# 
# def pdecode(buf):
#     """Decode a pencoded byte string to a structure."""
#     return PDecoder().decode(buf)


# Path: tests/test_pencode.py
import math
import pytest
from hypothesis import example, given, strategies
from chopsticks.pencode import pencode, pdecode

ascii_binary = strategies.builds(lambda s: s.encode('ascii'), ascii_text)

immutables = strategies.recursive(
    ascii_binary |
    strategies.booleans() |
    strategies.floats(allow_nan=False) |
    strategies.integers() |
    strategies.none() |
    strategies.text() |
    strategies.tuples(),
    lambda children: (
        strategies.frozensets(children) |
        strategies.tuples(children)
    ),
)

mutables = strategies.recursive(
    strategies.dictionaries(immutables, immutables) |
    strategies.lists(immutables) |
    strategies.sets(immutables),
    lambda children: (
        strategies.dictionaries(immutables, children) |
        strategies.lists(children)
    ),
)

def assert_roundtrip(obj):
    """Assert that we can successfully round-trip the given object."""
    buf = pencode(obj)
    assert isinstance(buf, bytes)

    obj2 = pdecode(buf)