Skip to content

Commit 688cb4b

Browse files
authored
LogicValue.ofRadixString returning 0 on bad input instead of throwing (#617)
1 parent 7f74489 commit 688cb4b

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

lib/src/values/logic_value.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,8 @@ abstract class LogicValue implements Comparable<LogicValue> {
757757
/// in chunks of 4 digits.
758758
///
759759
/// If the format of then length/radix-encoded string is not completely parsed
760-
/// an exception will be thrown. This can be caused by illegal characters
760+
/// a [LogicValueConstructionException] will be thrown. This can be caused by
761+
/// illegal characters
761762
/// in the string or too long of a value string.
762763
///
763764
/// Strings created by [toRadixString] are parsed by [ofRadixString].
@@ -911,12 +912,10 @@ abstract class LogicValue implements Comparable<LogicValue> {
911912
lastPos = pos;
912913
}
913914
return logicValList.rswizzle();
914-
} else {
915-
throw LogicValueConstructionException(
916-
'Invalid LogicValue string $valueString');
917915
}
918916
}
919-
return LogicValue.zero;
917+
throw LogicValueConstructionException(
918+
'Invalid LogicValue string $valueString');
920919
}
921920

922921
/// Compares this to `other`.

test/logic_value_test.dart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,7 @@ void main() {
20572057
LogicValue.ofRadixString(lv.toRadixString(radix: i)), equals(lv));
20582058
}
20592059
});
2060+
20602061
test('radixString roundTrip with leading zeros', () {
20612062
final lv = LogicValue.ofBigInt(BigInt.from(737481838713847), 61);
20622063
for (final i in [2, 4, 8, 10, 16]) {
@@ -2066,6 +2067,7 @@ void main() {
20662067
equals(lv));
20672068
}
20682069
});
2070+
20692071
test('radixString roundTrip zero corner case', () {
20702072
final lv = LogicValue.ofBigInt(BigInt.from(0), 61);
20712073
for (final i in [2, 4, 8, 10, 16]) {
@@ -2077,6 +2079,7 @@ void main() {
20772079
equals(lv));
20782080
}
20792081
});
2082+
20802083
test('radixString binary expansion', () {
20812084
final lv = LogicValue.ofRadixString("12'b10z111011z00");
20822085
expect(lv.toRadixString(radix: 16), equals("12'h<10z1>d<1z00>"));
@@ -2115,22 +2118,44 @@ void main() {
21152118
}
21162119
try {
21172120
lv.toRadixString(sepChar: 'q');
2121+
fail('Should throw a LogicValueConstructionException');
21182122
} on Exception catch (e) {
21192123
expect(e, isA<LogicValueConversionException>());
21202124
}
21212125
try {
21222126
lv.toRadixString(radix: 14);
2127+
fail('Should throw a LogicValueConstructionException');
21232128
} on Exception catch (e) {
21242129
expect(e, isA<LogicValueConversionException>());
21252130
}
21262131
});
2132+
21272133
test('radixString space separators', () {
21282134
final lv = LogicValue.ofRadixString("10'b10 0010 0111", sepChar: ' ');
21292135
expect(lv.toInt(), equals(551));
21302136
});
2137+
2138+
test('radixString bad input', () {
2139+
try {
2140+
LogicValue.ofRadixString('something');
2141+
fail('Should throw a LogicValueConstructionException');
2142+
} on Exception catch (e) {
2143+
expect(e, isA<LogicValueConstructionException>());
2144+
}
2145+
});
2146+
test('radixString bad input with fake length', () {
2147+
try {
2148+
LogicValue.ofRadixString("10'bsomething");
2149+
fail('Should throw a LogicValueConstructionException');
2150+
} on Exception catch (e) {
2151+
expect(e, isA<LogicValueConstructionException>());
2152+
}
2153+
});
2154+
21312155
test('radixString bad separator', () {
21322156
try {
21332157
LogicValue.ofRadixString("10'b10 0010_0111");
2158+
fail('Should throw a LogicValueConstructionException');
21342159
} on Exception catch (e) {
21352160
expect(e, isA<LogicValueConstructionException>());
21362161
}
@@ -2139,19 +2164,24 @@ void main() {
21392164
test('radixString illegal separator', () {
21402165
try {
21412166
LogicValue.ofRadixString("10'b10q0010q0111", sepChar: 'q');
2167+
fail('Should throw a LogicValueConstructionException');
21422168
} on Exception catch (e) {
21432169
expect(e, isA<LogicValueConstructionException>());
21442170
}
21452171
});
21462172

21472173
test('radixString bad length', () {
21482174
try {
2149-
LogicValue.ofRadixString("10'b10_0010_0111_0000");
2175+
LogicValue.ofRadixString("10'b10_0010_0111_0001");
2176+
fail('Should throw a LogicValueConstructionException');
21502177
} on Exception catch (e) {
21512178
expect(e, isA<LogicValueConstructionException>());
21522179
}
2153-
// Try the shortest possible input
2154-
LogicValue.ofRadixString("10'b");
2180+
});
2181+
2182+
test('radixString shortest possible length', () {
2183+
final lv = LogicValue.ofRadixString("10'b");
2184+
expect(lv, equals(LogicValue.ofInt(0, 10)));
21552185
});
21562186

21572187
test('radixString leading Z', () {
@@ -2165,6 +2195,7 @@ void main() {
21652195
LogicValue.ofRadixString(lv.toRadixString(radix: i)), equals(lv));
21662196
}
21672197
});
2198+
21682199
test('radixString decimal case', () {
21692200
{
21702201
final lv = LogicValue.ofRadixString("12'bzz_zzz1_1011");
@@ -2195,6 +2226,7 @@ void main() {
21952226
LogicValue.ofRadixString(lv.toRadixString(radix: i)), equals(lv));
21962227
}
21972228
});
2229+
21982230
test('radixString: slide set bits along entire word', () {
21992231
final random = Random(5);
22002232

0 commit comments

Comments
 (0)