Skip to content

NumberTheory, Sequences, Statistics on double[], math and string property tests - #9

Merged
diegourban merged 5 commits into
masterfrom
math-cleanup
Nov 6, 2026
Merged

diegourban merged 5 commits into
masterfrom
math-cleanup

Conversation

@diegourban

Copy link
Copy Markdown
Owner

Last of the three library-redesign PRs. Algorithms are moved or retyped as they are; no method bodies were rewritten.

Math

  • Algebra → NumberTheory (git rename): gcd (gcdLoop, gcdRecursive), lcm, isPrime and prime factorisation.
  • The new Sequences class holds fibonacci, fibonacciSimpleBinetFormula and fibonacciBinetFormula, moved unchanged together with their tests.
  • Statistics now takes double[] (mode returns double). Only the types changed. Rainfall now uses Statistics.mean.
  • Measurement and Geometry keep all their methods (Q10).

Property tests (jqwik)

  • NumberTheory: the two gcds agree; gcd divides both numbers; lcm × gcd = a × b; the lcm of a list is a multiple of every element; isPrime agrees with trial division; prime factors multiply back to n, are all prime and come out sorted; primeFactorsSet holds the distinct factors.
  • Sequences: checked exhaustively against an exact BigInteger reference over the ranges the Javadoc states (fibonacci ≤ 46, simple Binet ≤ 67, BigInteger Binet ≤ 71). All three claims hold.
  • Statistics: the mean lies within [min, max]; the median is the middle of the sorted data; the mode is one of the values.
  • StringUtils: isPalindrome agrees with reversing the string; mirrored strings are palindromes; shuffles are anagrams, case-insensitively; isAnagram agrees with letter counts. Inputs are ASCII letters, since both methods compare char by char.

Checked that the properties catch real bugs: breaking gcdLoop's loop condition made 4 properties fail.

Not changed, but worth knowing

  • Statistics.median sorts the array it's given in place, so the caller's data gets reordered.
  • Statistics.mean sums naively, so values near Double.MAX_VALUE overflow to Infinity.

Test plan

  • ./mvnw verify: 581 tests, coverage floors met
  • CI green

🤖 Generated with Claude Code

diegourban and others added 3 commits November 6, 2026 15:42
gcd, lcm, primality and prime factorisation stay in NumberTheory;
fibonacci, fibonacciSimpleBinetFormula and fibonacciBinetFormula move
unchanged to the new math.Sequences, with their tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mean, median, mode and meanAbsoluteDeviation now take double[] (mode
returns double); method bodies are unchanged apart from the types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- NumberTheory: gcdLoop and gcdRecursive agree; gcd divides both; lcm *
  gcd = a * b; lcm of a list is a multiple of every element; isPrime
  agrees with trial division; prime factors multiply back to n, are
  prime and sorted; primeFactorsSet holds the distinct factors
- Sequences (exhaustive): each Fibonacci variant matches an exact
  BigInteger reference over the range its Javadoc claims (46, 67, 71)
- Statistics: mean within [min, max]; median is the sorted middle;
  mode is one of the values
- StringUtils: isPalindrome agrees with reversing; mirrored strings are
  palindromes; shuffles are anagrams (case-insensitive); isAnagram
  agrees with letter counts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 78 complexity · 0 duplication

Metric Results
Complexity 78
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

⚠️ Resolve issues before merging

Two blocking correctness gaps remain: the Binet Fibonacci implementations accept invalid negative indices, and Statistics methods fail or return NaN for empty arrays. These affect documented API behavior and should be addressed before merging. The Sequences implementation also has a precision-risk suggestion involving BigDecimal construction.

Test suggestions

  • Verify both gcd implementations agree and the result divides both positive inputs.
  • Verify two-value and list lcm behavior, including the gcd/lcm product invariant.
  • Verify primality against trial division, including values near Integer.MAX_VALUE.
  • Verify prime factors multiply to the input, are prime and sorted, and that the factor set is distinct.
  • Verify all Fibonacci implementations against an exact reference over their documented ranges.
  • Verify mean bounds, median ordering behavior, and mode membership for double[] data.
  • Verify palindrome and anagram properties, including shuffled and case-insensitive inputs.
  • Verify Statistics behavior for empty arrays, especially median and mean absolute deviation.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify Statistics behavior for empty arrays, especially median and mean absolute deviation.

Make this review better

TIP Add a ticket key to the branch or title so the reviewer can check the PR against it. Or tell us which ticket tracker you use here.
TIP Add Codacy review.md instructions or an AGENTS.md so the reviewer knows your conventions.

TIP How was this review? Give us feedback

* @return the Average Absolute Deviation from data
*/
public static double meanAbsoluteDeviation(int[] data) {
public static double meanAbsoluteDeviation(double[] data) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Define and apply an explicit empty-array result before dividing by data.length.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

* @return
*/
public static double median(int[] data) {
public static double median(double[] data) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Handle data.length == 0 before indexing, using the intended empty-data contract.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

Comment on lines +58 to +64
public static double fibonacciSimpleBinetFormula(int n) {
if (n == 0) {
return 0;
}
final double phi = (1 + Math.sqrt(5)) / 2;
final double sqrt5 = Math.sqrt(5);
return ((Math.pow(phi, n) - Math.pow(-phi, -n)) / sqrt5);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Add the same negative-index validation used by fibonacci before evaluating either Binet formula.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

@codecov-commenter

codecov-commenter commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.09%. Comparing base (7553bca) to head (7a66409).

Additional details and impacted files
@@             Coverage Diff              @@
##             master       #9      +/-   ##
============================================
+ Coverage     92.41%   94.09%   +1.68%     
- Complexity      260      264       +4     
============================================
  Files            23       24       +1     
  Lines           593      593              
  Branches         94       94              
============================================
+ Hits            548      558      +10     
+ Misses           26       17       -9     
+ Partials         19       18       -1     

☔ 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.

diegourban and others added 2 commits November 6, 2026 16:01
Clears Codacy/PMD AvoidDecimalLiteralsInBigDecimalConstructor (High).
Results are unchanged: SequencesPropertyTest checks every n in 0..71
against an exact BigInteger reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sequences only has static methods, so it gets a private constructor
(JaCoCo leaves private empty constructors out of coverage), and
fibonacci(-1) is now tested to throw with its message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diegourban
diegourban merged commit 0237e3d into master Nov 6, 2026
2 checks passed
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