Skip to content

Commit

Permalink
fix(interval_size_extension): address simplified compound interval si…
Browse files Browse the repository at this point in the history
…zes (#95)
  • Loading branch information
albertms10 committed May 7, 2023
1 parent 8bfad2e commit 02af652
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/src/interval/interval_size_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ extension IntervalSizeExtension on int {
/// 13.simplified == 6
/// (-9).simplified == -2
/// 8.simplified == 8
/// (-22).simplified == -8
/// ```
int get simplified {
assert(this != 0, 'Size must be non-zero');
final sizeAbs = abs();

return isCompound ? (abs().nModExcludeZero(8) + 1) * sign : this;
return isCompound
? ((sizeAbs + sizeAbs ~/ 8).nModExcludeZero(8)) * sign
: this;
}

/// Returns the inverted of this [Interval.size].
Expand Down
8 changes: 8 additions & 0 deletions test/src/interval/interval_size_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ void main() {
});

test('should return the simplified Interval size', () {
expect((-22).simplified, -8);
expect((-17).simplified, -3);
expect((-16).simplified, -2);
expect((-15).simplified, -8);
expect((-13).simplified, -6);
expect((-10).simplified, -3);
expect((-8).simplified, -8);
Expand All @@ -170,6 +174,10 @@ void main() {
expect(8.simplified, 8);
expect(10.simplified, 3);
expect(13.simplified, 6);
expect(15.simplified, 8);
expect(16.simplified, 2);
expect(17.simplified, 3);
expect(22.simplified, 8);
});
});

Expand Down

0 comments on commit 02af652

Please sign in to comment.