Skip to content

Commit

Permalink
Add support for equality checking with don't-cares in LogicValue (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmetis authored Mar 7, 2023
1 parent 023fc58 commit 7813e4c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/src/values/logic_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,41 @@ abstract class LogicValue {
].swizzle();
}

/// Checks if `this` is equal to [other], except ignoring bits of
/// which are not valid.
///
/// Returns `true` if each bit of `this` which [isValid] is equal
/// to each bit of [other] which [isValid].
///
/// For example:
/// ```dart
/// // Returns false
/// LogicValue.ofString('1010xz').equalsWithDontCare(
/// LogicValue.ofString('10111x'));
///
/// // Returns true
/// LogicValue.ofString('10x111').equalsWithDontCare(
/// LogicValue.ofString('10111x'));
///
/// // Returns false
/// LogicValue.ofString('10x1z1').equalsWithDontCare(
/// LogicValue.ofString('10101x'));
/// ```
bool equalsWithDontCare(LogicValue other) {
if (width == other.width) {
for (var i = 0; i < width; i++) {
if (!this[i].isValid || !other[i].isValid) {
continue;
} else if (this[i] != other[i]) {
return false;
}
}
return true;
} else {
return false;
}
}

/// Returns new [LogicValue] replicated [multiplier] times. An exception will
/// be thrown in case the multiplier is <1
LogicValue replicate(int multiplier) {
Expand Down
27 changes: 26 additions & 1 deletion test/logic_value_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ void main() {
});
});
group('comparison operations', () {
test('equalsWithDontCare', () {
expect(
// == not equal
LogicValue.ofString('1010xz')
.equalsWithDontCare(LogicValue.ofString('10111x')),
equals(false));
expect(
// == equal
LogicValue.ofString('1010xz')
.equalsWithDontCare(LogicValue.ofString('101z1x')),
equals(true));
expect(
// == not equal
LogicValue.ofString('10x1z1')
.equalsWithDontCare(LogicValue.ofString('10101x')),
equals(false));
expect(
//
LogicValue.ofString('10x1z1')
.equalsWithDontCare(LogicValue.ofString('10101x')),
equals(false));
expect(
LogicValue.ofString('10x1z1')
.equalsWithDontCare(LogicValue.ofString('101x11')),
equals(true));
});
test('equality', () {
expect(
// == equal
Expand Down Expand Up @@ -740,7 +766,6 @@ void main() {
equals(LogicValue.ofBigInt(BigInt.from(3), 512)));
});
});

group('FilledLogicValue', () {
test('overrides', () {
expect(
Expand Down

0 comments on commit 7813e4c

Please sign in to comment.