Skip to content

fix(maths): validate non-positive dimensions and torus radius in Volume - #7770

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/volume-input-validation
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/volume-input-validation

Conversation

@spidervirus

Copy link
Copy Markdown
Contributor
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Description

In com.thealgorithms.maths.Volume:
Unlike Area.java, which checks all input dimensions and throws IllegalArgumentException for values $\le 0$, the methods in Volume.java did not validate inputs.
Non-positive and negative dimensions (e.g. negative radius, negative height) produced invalid, negative, or contradictory volumes without error. In addition, volumeTorus(majorRadius, minorRadius) did not verify that majorRadius >= minorRadius.

Fix

  • Added input validation for all shapes throwing IllegalArgumentException when dimensions (radius, height, width, length, side, base area, semi-axes) are $\le 0$, matching the established convention in Area.java.
  • Added check ensuring majorRadius >= minorRadius for torus volume calculation.
  • Added comprehensive unit tests in VolumeTest asserting IllegalArgumentException on non-positive and invalid inputs for each shape.

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.48%. Comparing base (e79b0f1) to head (0a395f2).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7770      +/-   ##
============================================
+ Coverage     82.42%   82.48%   +0.05%     
- Complexity     8508     8536      +28     
============================================
  Files           847      847              
  Lines         26289    26335      +46     
  Branches       5133     5144      +11     
============================================
+ Hits          21670    21722      +52     
+ Misses         3828     3824       -4     
+ Partials        791      789       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@spidervirus
spidervirus force-pushed the fix/volume-input-validation branch from 50189d0 to 0a395f2 Compare November 19, 2026 11:55
@spidervirus
spidervirus marked this pull request as ready for review November 19, 2026 11:55
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.

2 participants