Skip to content

Commit

Permalink
Fix coverage issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
matanlurey committed Apr 15, 2024
1 parent 2770d20 commit f390112
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
9 changes: 1 addition & 8 deletions lib/gambit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,6 @@ final class _WeightedIndexDistribution extends Distribution<int> {
);
}
final normalized = List.of(weights.map((e) => e / total));
if (normalized.isEmpty) {
throw ArgumentError.value(
weights,
'weights',
'Cannot be empty.',
);
}
return _WeightedIndexDistribution._(normalized);
}

Expand All @@ -365,7 +358,7 @@ final class _WeightedIndexDistribution extends Distribution<int> {
int sample(Random random) {
final value = random.nextDouble();
var sum = 0.0;
for (var i = 0; i < _weights.length; i++) {
for (var i = 0; i < _weights.length - 1; i++) {
sum += _weights[i];
if (value < sum) {
return i;
Expand Down
21 changes: 21 additions & 0 deletions lib/src/dice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ final class SingleDiceResult extends DiceResult {
}

/// Pool of multiple dice.
///
/// ## Equality
///
/// Two pools of multiple dice are equal if they have the same number of dice
/// and the same dice:
///
/// ```dart
/// print(MultipleDice(3, Dice(6)) == MultipleDice(3, Dice(6))); // true
/// print(MultipleDice(3, Dice(6)) == MultipleDice(3, Dice(20))); // false
/// ```
@immutable
final class MultipleDice extends Distribution<MultipleDiceResult> {
/// Creates a pool of [count] dice with [sides] number of faces.
Expand Down Expand Up @@ -167,6 +177,17 @@ final class MultipleDice extends Distribution<MultipleDiceResult> {
List.generate(count, (_) => dice.sample(random).value),
);
}

@override
bool operator ==(Object other) {
return other is MultipleDice && count == other.count && dice == other.dice;
}

@override
int get hashCode => Object.hash(count, dice);

@override
String toString() => '$count$dice';
}

/// Result of a pool of multiple dice.
Expand Down
51 changes: 51 additions & 0 deletions test/gambit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ void main() {
check(() => FixedRandom.never().nextDouble()).throws<StateError>();
});

test('requires non-emtpy values', () {
check(() => FixedRandom.terminal([])).throws<ArgumentError>();
check(() => FixedRandom.repeat([])).throws<ArgumentError>();
});

test('terminal uses pre-defined values, then throws', () {
final random = FixedRandom.terminal([0.5, 0.75]);
check(random.nextDouble()).equals(0.5);
Expand All @@ -22,6 +27,19 @@ void main() {
check(random.nextDouble()).equals(0.5);
});

test('also works with integers and booleans', () {
final random = FixedRandom.normal(4);
check(random.nextInt(4)).equals(0);
check(random.nextInt(4)).equals(1);
check(random.nextInt(4)).equals(2);
check(random.nextInt(4)).equals(3);

check(random.nextBool()).isTrue();
check(random.nextBool()).isTrue();
check(random.nextBool()).isFalse();
check(random.nextBool()).isFalse();
});

test('normal uses a normal distribution', () {
final random = FixedRandom.normal(3);
check(List.generate(100, (_) => random.nextInt(3)))
Expand Down Expand Up @@ -134,4 +152,37 @@ void main() {
check(string).has(pattern.hasMatch, 'pattern').isTrue();
}
});

group('Dice', () {
test('use an existing dice', () {
final random = FixedRandom.normal(3);
check(d6.sample(random).toString()).equals('1');
check(d6.sample(random).toString(verbose: true)).equals('3 (1d6)');
});

test('create a custom dice', () {
final random = FixedRandom.normal(3);
final $3d7 = Dice(7) * 3;
check('${$3d7}').equals('3d7');
check($3d7).equals(MultipleDice(3, Dice(7)));
check($3d7)
.has((d) => d.hashCode, 'hashCode')
.equals(MultipleDice(3, Dice(7)).hashCode);
check($3d7.sample(random).toString()).equals('9');
check($3d7.sample(random).toString(verbose: true)).equals('9 (3d7)');
});

test('dice pools must be at least 1', () {
check(() => Dice(6) * 0).throws<ArgumentError>();
});

test('require dice to have at least 1 side', () {
check(() => Dice(0)).throws<ArgumentError>();
});

test('dice implement == and hashCode', () {
check(d6).equals(Dice(6));
check(d6).has((d) => d.hashCode, 'hashCode').equals(Dice(6).hashCode);
});
});
}

0 comments on commit f390112

Please sign in to comment.