Skip to content

avg test#392

Merged
thedavidmeister merged 1 commit into
mainfrom
2025-08-06-avg
Aug 6, 2025
Merged

avg test#392
thedavidmeister merged 1 commit into
mainfrom
2025-08-06-avg

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Aug 6, 2025

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • New Features

    • Re-enabled the arithmetic average (mean) operation, allowing users to compute the average of two numbers.
  • Refactor

    • Updated the average operation to use more precise decimal floating point arithmetic for improved accuracy.
  • Tests

    • Enhanced tests for the average operation to cover a wider range of floating point inputs and scenarios.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 6, 2025

Walkthrough

The changes re-enable and refactor the avg (arithmetic average) operation in the interpreter's standard operations library. The avg operation is now integrated using decimal floating point arithmetic, with updates to its library, metadata, function pointers, and tests to support the new numeric representation and operand handling.

Changes

Cohort / File(s) Change Summary
Standard Ops Integration
src/lib/op/LibAllStandardOps.sol
Re-enables the avg operation: imports LibOpAvg, increments the standard ops count, and adds avg to authoring metadata, operand handler, integrity, and opcode function pointer arrays.
Avg Operation Refactor
src/lib/op/math/LibOpAvg.sol
Refactors LibOpAvg to use decimal floating point arithmetic (Float and LibDecimalFloat) instead of fixed-point (UD60x18). Updates function signatures to use OperandV2 and StackItem. The average calculation now uses floating point math.
Avg Operation Tests
test/src/lib/op/math/LibOpAvg.t.sol
Updates tests for avg to use floating point input construction, bounding, and verification. Test functions now use OperandV2, Float, and StackItem types. Test logic is adapted for floating point representation and new run/integrity signatures.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Interpreter
  participant LibAllStandardOps
  participant LibOpAvg

  Caller->>Interpreter: Requests execution of "avg" opcode
  Interpreter->>LibAllStandardOps: Looks up function pointer for "avg"
  LibAllStandardOps->>LibOpAvg: Calls run(InterpreterState, OperandV2, stackTop)
  LibOpAvg->>LibDecimalFloat: Loads and averages two Float values
  LibOpAvg-->>Interpreter: Returns result on stack
  Interpreter-->>Caller: Returns averaged value
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15–20 minutes

Possibly related PRs

  • linear growth #377: Re-enables and refactors the linear-growth opcode using floating-point arithmetic, following a similar pattern for opcode integration and arithmetic updates.
  • 2025 07 13 max #359: Re-enables the max opcode with floating-point adaptations, modifying the same arrays and constants in LibAllStandardOps.sol for opcode management.
  • wip on frac #384: Re-enables and updates the frac opcode and LibOpFrac library to use decimal floating point arithmetic, closely paralleling the changes for the avg opcode.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 70953d0 and 184f85f.

⛔ Files ignored due to path filters (3)
  • src/generated/Rainterpreter.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterExpressionDeployer.pointers.sol is excluded by !**/generated/**
  • src/generated/RainterpreterParser.pointers.sol is excluded by !**/generated/**
📒 Files selected for processing (3)
  • src/lib/op/LibAllStandardOps.sol (6 hunks)
  • src/lib/op/math/LibOpAvg.sol (1 hunks)
  • test/src/lib/op/math/LibOpAvg.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
📚 Learning: in rain interpreter stack operations like libopevery, when the output position (stacktop) is set to ...
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#381
File: src/lib/op/logic/LibOpEvery.sol:24-46
Timestamp: 2025-07-27T22:56:57.928Z
Learning: In Rain interpreter stack operations like LibOpEvery, when the output position (stackTop) is set to coincide with an input item's position on the stack, explicit writing may not be needed if the desired output value is already at that position. The function can return the pointer to that position directly, leveraging the existing stack layout.

Applied to files:

  • src/lib/op/math/LibOpAvg.sol
📚 Learning: in multiplication overflow detection tests like libopuint256multest, when performing sequential mult...
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

Applied to files:

  • test/src/lib/op/math/LibOpAvg.t.sol
  • src/lib/op/LibAllStandardOps.sol
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-artifacts)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (16)
src/lib/op/LibAllStandardOps.sol (6)

74-74: LGTM!

The LibOpAvg import is correctly uncommented to support the re-enabled avg operation.


115-115: LGTM!

The constant is correctly incremented from 66 to 67 to account for the addition of the avg operation.


272-272: LGTM!

The metadata entry for avg is properly formatted with an accurate description.


467-468: LGTM!

The operand handler correctly uses handleOperandDisallowed, which is appropriate for the avg operation that doesn't require operand configuration.


603-603: LGTM!

The integrity function pointer is correctly added for the avg operation in the proper alphabetical position.


714-714: LGTM!

The runtime function pointer is correctly added for the avg operation in the proper alphabetical position.

src/lib/op/math/LibOpAvg.sol (4)

4-15: LGTM!

The imports and library setup correctly transition from fixed-point to floating-point arithmetic using the appropriate types and dependencies.


16-19: LGTM!

The integrity function correctly specifies 2 inputs and 1 output for the average operation and uses the updated OperandV2 type.


21-37: LGTM!

The run function correctly implements arithmetic average using floating-point operations. The algorithm (add two values, divide by 2) is mathematically sound and the stack manipulation follows established patterns.


39-54: LGTM!

The reference function correctly implements the same average algorithm as the run function, with proper type conversions for the testing interface.

test/src/lib/op/math/LibOpAvg.t.sol (6)

4-10: LGTM!

The imports are correctly updated to support testing the new floating-point implementation of the avg operation.


11-17: LGTM!

The integrity test correctly verifies that the avg operation requires 2 inputs and produces 1 output.


19-42: LGTM!

The runtime test provides comprehensive fuzzing coverage with proper input bounds and validates consistency between the main and reference implementations.


44-55: LGTM!

The evaluation tests cover various input combinations with mathematically correct expected results properly encoded as floating-point values.


57-72: LGTM!

The error handling tests properly validate that the operation fails appropriately with incorrect numbers of inputs or outputs.


74-78: LGTM!

The operand disallowance test correctly validates that the avg operation rejects operand usage, consistent with its configuration in the standard ops library.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2025-08-06-avg

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@thedavidmeister thedavidmeister merged commit dad1487 into main Aug 6, 2025
11 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 7, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 3, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant