Skip to content

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

Merged
DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
spidervirus:fix/volume-input-validation
Nov 29, 2026
Merged

DenizAltunkapan merged 2 commits 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.51%. Comparing base (3bb4459) to head (9bcc612).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7770      +/-   ##
============================================
+ Coverage     82.49%   82.51%   +0.01%     
- Complexity     8526     8548      +22     
============================================
  Files           847      847              
  Lines         26305    26349      +44     
  Branches       5140     5150      +10     
============================================
+ Hits          21700    21741      +41     
  Misses         3822     3822              
- Partials        783      786       +3     

☔ 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
@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) November 29, 2026 14:40
@DenizAltunkapan
DenizAltunkapan merged commit a7b6674 into TheAlgorithms:master Nov 29, 2026
7 checks passed
@spidervirus
spidervirus deleted the fix/volume-input-validation branch November 29, 2026 19:59
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.

3 participants