Skip to content

test: improve coverage for BaseUint arithmetic operators (81%) #521

@tcoratger

Description

@tcoratger

Summary

`src/lean_spec/types/uint.py` has 81% test coverage. While `test_uint.py` exists, many arithmetic and bitwise operator overloads lack tests — particularly reverse operators, type error paths, and edge cases.

Coverage stats:

File Statements Covered Coverage
`types/uint.py` 197 30 uncovered 81%

Uncovered lines: 210, 218, 225, 230, 237, 242, 249, 254, 261, 266, 273, 279, 286–288, 293, 301–302, 312, 317, 322, 327, 332, 341–342, 351–352, 369, 375, 404

What needs testing

Reverse arithmetic operators

Each of these returns `Self` and must reject non-BaseUint operands:

  • `radd`: `Uint64(2) + Uint64(3)` when left operand defers
  • `rsub`: Reverse subtraction
  • `rmul`: Reverse multiplication
  • `rfloordiv`: Reverse floor division
  • `rmod`: Reverse modulo

Exponentiation

  • `pow`: `Uint64(2) ** Uint64(10)` returns `Uint64(1024)`
  • `pow` with modulo: `pow(Uint64(2), Uint64(10), Uint64(100))` returns `Uint64(24)`
  • `rpow`: Reverse exponentiation
  • Type errors: Non-int exponent raises TypeError

Divmod

  • `divmod`: `divmod(Uint64(10), Uint64(3))` returns `(Uint64(3), Uint64(1))`
  • `rdivmod`: Reverse divmod
  • Type errors: Non-BaseUint raises TypeError

Bitwise operators (reverse)

  • `rand`: Reverse AND
  • `ror`: Reverse OR
  • `rxor`: Reverse XOR

Shift operators

  • `lshift`: `Uint64(1) << 3`
  • `rlshift`: Reverse left shift
  • `rshift`: `Uint64(8) >> 2`
  • `rrshift`: Reverse right shift
  • Bool rejected: `Uint64(1) << True` raises TypeError

Comparison operators

  • `lt`, `le`, `gt`, `ge`: With valid BaseUint operands
  • Type errors: Comparing with non-BaseUint raises TypeError

`index`

  • Slicing: `[0:Uint64(3)]` works correctly (returns `int(self)`)

Why this matters

  • Type safety: Every operator must reject non-BaseUint operands to prevent silent type coercion bugs in spec code
  • Overflow protection: Results must be validated against the bit range
  • Spec correctness: These types are used throughout the spec for slots, epochs, balances — arithmetic bugs have cascading effects

How to test

Running tests with coverage

```bash
uv run pytest tests/lean_spec/types/test_uint.py -v \
--cov=src/lean_spec/types/uint --cov-report=term-missing
```

Target: ≥95% line coverage.

Test file location

```
tests/lean_spec/types/test_uint.py (extend existing)
```

Testing tips

  • Use `Uint64` as the primary test type (most common in spec)
  • Test cross-type operations: `Uint64(1) + Uint32(2)` should work (both are BaseUint)
  • Test type error paths with `pytest.raises(TypeError)`
  • For reverse operators, you may need to use a plain BaseUint subclass as left operand
  • `index` is testable via slicing: `list(range(10))[Uint64(3):Uint64(7)]`

Using Claude Code subagents

1. `code-tester` agent — Generate the tests

Extend tests in `tests/lean_spec/types/test_uint.py`. Cover all reverse arithmetic operators (radd, rsub, rmul, rfloordiv, rmod), pow with and without modulo, rpow, divmod and rdivmod, reverse bitwise operators, shift operators with bool rejection, all comparison operators with type errors, and index via slicing.

Workflow

  1. Group tests by operator category (arithmetic, bitwise, comparison)
  2. Test both happy paths and type error rejection
  3. Verify results are correct type (Self, not raw int)
  4. Run `uvx tox -e all-checks` to pass all quality checks

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerstestsScope: Changes to the spec tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions