Skip to content

Tests: saturating ops coverage (§4) #522

Closed

Description

@ehartford

Tests: saturating ops coverage (§4)

Summary

Saturating arithmetic has Sema and LLVM codegen support, but the requirements campaign still needs focused tests for the §4.2.3 contract. Add behavior and compile-error coverage for explicit saturating operators, min/max clamping for signed and unsigned integers, and float rejection.

Spec

docs/with-specification.md:869-921 defines overflow behavior and saturating arithmetic. +|, -|, and *| clamp to numeric min/max on overflow, apply only to integer types, and reject floats. The same section covers overflow modes such as @[overflow(saturate)].

Current state

  • src/SemaCheck.w:4056 and src/SemaCheck.w:4212-4217 recognize saturating arithmetic operators and reject floating-point operands with "saturating arithmetic is not defined for floating-point types".
  • src/CodegenDispatch.w:1665-1668 selects LLVM signed/unsigned saturating intrinsics for add/sub; src/CodegenDispatch.w:1898-1904 routes explicit saturating ops and saturating overflow mode through that helper.
  • src/CodegenDispatch.w:2861 handles saturating integer negation under saturating overflow mode.
  • src/CCodegen.w:2752-2754 currently fails loudly for C backend saturating integer arithmetic, so new tests should be clear about the backend they exercise or expose that as an implementation follow-up.
  • Existing test/behavior/behav_overflow_explicit_modes.w:14-15 has one const u8 saturating-add smoke case; it is not enough for the requirement IDs in this issue.

Implementation plan

  1. Add behavior tests for explicit +|, -|, and *| across unsigned and signed integer widths.
  2. Add min-bound and max-bound cases: unsigned underflow to 0, unsigned overflow to max, signed positive overflow to max, signed negative overflow to min.
  3. Add tests for @[overflow(saturate)] if the attribute is already implemented for checked operators.
  4. Add compile-error tests for saturating float operations.
  5. If the normal test runner exercises the C backend for these tests, either implement C backend saturating arithmetic or mark the backend gap as a separate loud issue; do not hide it with a fallback.

Test plan

  • test/behavior/behav_saturating_ops.w:
    • u8: 250 +| 20 == 255, 3 -| 9 == 0, 20 *| 20 == 255.
    • i8: 120 +| 20 == 127, -120 -| 20 == -128, positive and negative multiply clamps.
    • one wider type (i32 or u32) to avoid only sub-byte coverage.
  • Compile-error tests for 1.0 +| 2.0, 1.0 -| 2.0, and 1.0 *| 2.0.
  • Attribute-mode test if @[overflow(saturate)] is accepted by the parser/Sema.
  • Run with build, with build :fixpoint, and with build :test.

Acceptance criteria

  • Explicit saturating add/sub/mul are tested for signed and unsigned integers.
  • Tests cover min and max clamping.
  • Floating-point saturating operators are compile errors.
  • Saturating overflow mode is covered or explicitly split if not implemented.
  • Any C backend incompatibility is surfaced as a real implementation issue, not skipped silently.

Dependencies

No test dependency. Potential implementation follow-up if C backend support is required by the selected test path.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.2.3.11, 4.2.3.12, 4.2.3.13, 4.2.3.9.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — saturating +|/-|/*| pinned by behav_saturating_ops (u8/i8/u32 clamps at bounds) + f64 rejection (err_saturating_float), ec03406. §4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions