Files
python-bpf/tests/property/test_int_operators.py
Pragyansh Chaturvedi 32c1afff36 Tests: Make the integer properties cover every type on every run
Review of the first version of the suite found four holes.

- The conversion sites returned the converted value, which shows its
  bits but not the type it was stored as. The untyped_local site passed
  over a real bug that way. Each site now reads the value back through
  an expression whose result depends on its sign and width.
- With the type drawn at random, 25 examples per site left a single type
  with about 3. A deliberately broken c_bool conversion went undetected
  under some seeds. The conversion property is now parametrized by
  destination type and the operator property by left operand type, a
  sixteenth of the profile's examples each.
- The global_init site was a strict xfail. Hypothesis shrank its
  expected failures, which took 115 of the property's 187 seconds. Its
  two bugs are now pinned in test_known_gaps.py, and the site steps
  around them.
- Gaps are now stepped around by drawing only inputs that avoid them,
  not by assume(). Filtering tripped Hypothesis's filter_too_much health
  check.

untyped-copy-keeps-narrow-type is renamed untyped-copy-keeps-source-type.
`t = a` keeps a's type for any source that is not 64-bit, c_uint32 and
c_int32 included, not only narrow ones.

Under the ci profile, each of these deliberate compiler bugs fails a
property:
- >> always logical
- % always unsigned
- / always unsigned
- equal-width mixed signs going signed
- promotion keeping an unsigned sign
- widening always by zero-extension
- c_bool conversion by truncation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 21:55:59 +05:30

56 lines
2.1 KiB
Python

"""
Every binary operator on every pair of operand types: PythonBPF computes what
clang computes.
test_int_expressions finds bugs in how operations combine, but random trees
rarely put, say, a negative dividend under a signed `%` and then return the
result undisturbed (about 3 programs in 600 did). This property is the
enumerated complement: `a0 op a1` for each operator, left type and right type,
with Hypothesis choosing the operand values, the edge values of each type
first. Shift counts are drawn in range, since an out-of-range count is
undefined in C.
"""
import pytest
from hypothesis import assume, given, settings
from hypothesis import strategies as st
from tests.property.cints import (
ALL_TYPES,
ARITH_OPS,
COMPARE_OPS,
DIV_OPS,
SHIFT_OPS,
promote,
)
from tests.property.harness import CLANG, check_against_clang
from tests.property.known_gaps import gap_of
from tests.property.programs import Bin, ExpressionProgram, Var, values_of
pytestmark = pytest.mark.skipif(CLANG is None, reason="clang is not installed")
TYPES = st.sampled_from(ALL_TYPES)
# Parametrized over the left operand's type as well as the operator, so every
# type gets examples on every run (153 parameters, a sixteenth of the
# profile's examples each).
@pytest.mark.parametrize("left_ty", ALL_TYPES, ids=lambda t: t.name)
@pytest.mark.parametrize("op", ARITH_OPS + DIV_OPS + SHIFT_OPS + COMPARE_OPS)
@settings(max_examples=max(5, settings.default.max_examples // 16))
@given(right_ty=TYPES, data=st.data())
def test_binary_operator_matches_clang(op, left_ty, right_ty, data):
left = data.draw(values_of(left_ty), label="left")
if op in SHIFT_OPS:
count = st.integers(0, min(promote(left_ty).bits - 1, right_ty.max))
right = data.draw(count, label="right")
else:
right = data.draw(values_of(right_ty), label="right")
program = ExpressionProgram(
[("a0", left_ty, left), ("a1", right_ty, right)],
[],
Bin(op, Var("a0", left_ty), Var("a1", right_ty)),
)
assume(gap_of(program) is None)
check_against_clang(program.program(), program.expected)