Better errors for pkg/fixnum

Replace `new ArgumentError(x)` with `new ArgumentError.value(x)`.
Throw RangeError for bad radix arguments.

R=cbracken@google.com

Review URL: https://codereview.chromium.org//1184183006.
This commit is contained in:
Stephen Adams 2015-06-19 12:35:07 -07:00
parent 408a2923a2
commit 7926e2630f
5 changed files with 60 additions and 44 deletions

View file

@ -57,15 +57,18 @@ class Int32 implements IntX {
}
}
static int _validateRadix(int radix) {
if (2 <= radix && radix <= 36) return radix;
throw new RangeError.range(radix, 2, 36, 'radix');
}
/**
* Parses a [String] in a given [radix] between 2 and 16 and returns an
* [Int32].
*/
// TODO(rice) - Make this faster by converting several digits at once.
static Int32 parseRadix(String s, int radix) {
if ((radix <= 1) || (radix > 16)) {
throw new ArgumentError("Bad radix: $radix");
}
_validateRadix(radix);
Int32 x = ZERO;
for (int i = 0; i < s.length; i++) {
int c = s.codeUnitAt(i);
@ -348,12 +351,12 @@ class Int32 implements IntX {
int numberOfTrailingZeros() => _numberOfTrailingZeros(_i);
Int32 toSigned(int width) {
if (width < 1 || width > 32) throw new ArgumentError(width);
if (width < 1 || width > 32) throw new RangeError.range(width, 1, 32);
return new Int32(_i.toSigned(width));
}
Int32 toUnsigned(int width) {
if (width < 0 || width > 32) throw new ArgumentError(width);
if (width < 0 || width > 32) throw new RangeError.range(width, 0, 32);
return new Int32(_i.toUnsigned(width));
}

View file

@ -70,10 +70,7 @@ class Int64 implements IntX {
* [Int64].
*/
static Int64 parseRadix(String s, int radix) {
if ((radix <= 1) || (radix > 36)) {
throw new ArgumentError("Bad radix: $radix");
}
return _parseRadix(s, radix);
return _parseRadix(s, Int32._validateRadix(radix));
}
static Int64 _parseRadix(String s, int radix) {
@ -210,15 +207,15 @@ class Int64 implements IntX {
// Returns the [Int64] representation of the specified value. Throws
// [ArgumentError] for non-integer arguments.
static Int64 _promote(val) {
if (val is Int64) {
return val;
} else if (val is int) {
return new Int64(val);
} else if (val is Int32) {
return val.toInt64();
static Int64 _promote(value) {
if (value is Int64) {
return value;
} else if (value is int) {
return new Int64(value);
} else if (value is Int32) {
return value.toInt64();
}
throw new ArgumentError(val);
throw new ArgumentError.value(value);
}
Int64 operator +(other) {
@ -353,7 +350,7 @@ class Int64 implements IntX {
Int64 operator <<(int n) {
if (n < 0) {
throw new ArgumentError(n);
throw new ArgumentError.value(n);
}
n &= 63;
@ -377,7 +374,7 @@ class Int64 implements IntX {
Int64 operator >>(int n) {
if (n < 0) {
throw new ArgumentError(n);
throw new ArgumentError.value(n);
}
n &= 63;
@ -420,7 +417,7 @@ class Int64 implements IntX {
Int64 shiftRightUnsigned(int n) {
if (n < 0) {
throw new ArgumentError(n);
throw new ArgumentError.value(n);
}
n &= 63;
@ -583,7 +580,7 @@ class Int64 implements IntX {
}
Int64 toSigned(int width) {
if (width < 1 || width > 64) throw new ArgumentError(width);
if (width < 1 || width > 64) throw new RangeError.range(width, 1, 64);
if (width > _BITS01) {
return Int64._masked(_l, _m, _h.toSigned(width - _BITS01));
} else if (width > _BITS) {
@ -600,7 +597,7 @@ class Int64 implements IntX {
}
Int64 toUnsigned(int width) {
if (width < 0 || width > 64) throw new ArgumentError(width);
if (width < 0 || width > 64) throw new RangeError.range(width, 0, 64);
if (width > _BITS01) {
int h = _h.toUnsigned(width - _BITS01);
return Int64._masked(_l, _m, h);
@ -684,10 +681,7 @@ class Int64 implements IntX {
}
String toRadixString(int radix) {
if ((radix <= 1) || (radix > 36)) {
throw new ArgumentError("Bad radix: $radix");
}
return _toRadixString(radix);
return _toRadixString(Int32._validateRadix(radix));
}
String _toRadixString(int radix) {

View file

@ -1,5 +1,5 @@
name: fixnum
version: 0.10.1
version: 0.10.2
author: Dart Team <misc@dartlang.org>
description: Library for 32- and 64-bit fixed size integers.
homepage: http://www.dartlang.org

View file

@ -273,8 +273,8 @@ void main() {
expect(Int32.MIN_VALUE.toSigned(32), Int32.MIN_VALUE);
expect(Int32.MAX_VALUE.toSigned(31), -Int32.ONE);
expect(Int32.MIN_VALUE.toSigned(31), Int32.ZERO);
expect(() => Int32.ONE.toSigned(0), throws);
expect(() => Int32.ONE.toSigned(33), throws);
expect(() => Int32.ONE.toSigned(0), throwsRangeError);
expect(() => Int32.ONE.toSigned(33), throwsRangeError);
});
test("toUnsigned", () {
expect(Int32.ONE.toUnsigned(1), Int32.ONE);
@ -283,8 +283,8 @@ void main() {
expect(Int32.MIN_VALUE.toUnsigned(32), Int32.MIN_VALUE);
expect(Int32.MAX_VALUE.toUnsigned(31), Int32.MAX_VALUE);
expect(Int32.MIN_VALUE.toUnsigned(31), Int32.ZERO);
expect(() => Int32.ONE.toUnsigned(-1), throws);
expect(() => Int32.ONE.toUnsigned(33), throws);
expect(() => Int32.ONE.toUnsigned(-1), throwsRangeError);
expect(() => Int32.ONE.toUnsigned(33), throwsRangeError);
});
test("toDouble", () {
expect(new Int32(17).toDouble(), same(17.0));

View file

@ -7,6 +7,15 @@ import 'package:fixnum/fixnum.dart';
import 'package:unittest/unittest.dart';
void main() {
argumentErrorTest(name, op, [receiver = Int64.ONE]) {
throwsArgumentErrorMentioning(substring) =>
throwsA((e) => e is ArgumentError && '$e'.contains(substring));
expect(() => op(receiver, null), throwsArgumentErrorMentioning('null'));
expect(() => op(receiver, 'foo'), throwsArgumentErrorMentioning(r'"foo"'));
}
group("is-tests", () {
test("isEven", () {
expect((-Int64.ONE).isEven, false);
@ -68,6 +77,7 @@ void main() {
expect(n3 + n4, new Int64(-11110));
expect(n5 + n6, new Int64.fromInts(0x89ab89ab, 0xcdeff011));
expect(Int64.MAX_VALUE + 1, Int64.MIN_VALUE);
argumentErrorTest("+", (a, b) => a + b);
});
test("-", () {
@ -76,6 +86,7 @@ void main() {
expect(n3 - n4, new Int64(8642));
expect(n5 - n6, new Int64.fromInts(0x9abd2345, 0x89ab6789));
expect(Int64.MIN_VALUE - 1, Int64.MAX_VALUE);
argumentErrorTest("-", (a, b) => a - b);
});
test("unary -", () {
@ -113,6 +124,7 @@ void main() {
expect(Int64.MIN_VALUE * new Int64(2), Int64.ZERO);
expect(Int64.MIN_VALUE * new Int64(1), Int64.MIN_VALUE);
expect(Int64.MIN_VALUE * new Int64(-1), Int64.MIN_VALUE);
argumentErrorTest("*", (a, b) => a * b);
});
test("~/", () {
@ -185,7 +197,7 @@ void main() {
expect(Int64.MIN_VALUE ~/ new Int64(1), Int64.MIN_VALUE);
expect(Int64.MIN_VALUE ~/ new Int64(-1), Int64.MIN_VALUE);
expect(() => new Int64(17) ~/ Int64.ZERO, throws);
expect(() => new Int64(17) ~/ null, throwsArgumentError);
argumentErrorTest("~/", (a, b) => a ~/ b);
});
test("%", () {
@ -225,6 +237,7 @@ void main() {
new Int64(-0x12345678.remainder(0x22)));
expect(new Int32(0x12345678).remainder(new Int64(0x22)),
new Int64(0x12345678.remainder(0x22)));
argumentErrorTest("%", (a, b) => a % b);
});
test("clamp", () {
@ -268,7 +281,7 @@ void main() {
expect(largePosPlusOne < largePos, false);
expect(Int64.MIN_VALUE < Int64.MAX_VALUE, true);
expect(Int64.MAX_VALUE < Int64.MIN_VALUE, false);
expect(() => new Int64(17) < null, throwsArgumentError);
argumentErrorTest("<", (a, b) => a < b);
});
test("<=", () {
@ -287,7 +300,7 @@ void main() {
expect(largePosPlusOne <= largePos, false);
expect(Int64.MIN_VALUE <= Int64.MAX_VALUE, true);
expect(Int64.MAX_VALUE <= Int64.MIN_VALUE, false);
expect(() => new Int64(17) <= null, throwsArgumentError);
argumentErrorTest("<=", (a, b) => a <= b);
});
test("==", () {
@ -323,7 +336,7 @@ void main() {
expect(largePosPlusOne >= largePos, true);
expect(Int64.MIN_VALUE >= Int64.MAX_VALUE, false);
expect(Int64.MAX_VALUE >= Int64.MIN_VALUE, true);
expect(() => new Int64(17) >= null, throwsArgumentError);
argumentErrorTest(">=", (a, b) => a >= b);
});
test(">", () {
@ -344,7 +357,7 @@ void main() {
expect(Int64.ZERO > Int64.MIN_VALUE, true);
expect(Int64.MIN_VALUE > Int64.MAX_VALUE, false);
expect(Int64.MAX_VALUE > Int64.MIN_VALUE, true);
expect(() => new Int64(17) > null, throwsArgumentError);
argumentErrorTest(">", (a, b) => a > b);
});
});
@ -360,6 +373,7 @@ void main() {
expect(n3 & n2, new Int64(8708));
expect(n4 & n5, new Int64(0x1034) << 32);
expect(() => n1 & null, throwsArgumentError);
argumentErrorTest("&", (a, b) => a & b);
});
test("|", () {
@ -367,6 +381,7 @@ void main() {
expect(n3 | n2, new Int64(-66));
expect(n4 | n5, new Int64(0x9a76) << 32);
expect(() => n1 | null, throwsArgumentError);
argumentErrorTest("|", (a, b) => a | b);
});
test("^", () {
@ -374,6 +389,7 @@ void main() {
expect(n3 ^ n2, new Int64(-8774));
expect(n4 ^ n5, new Int64(0x8a42) << 32);
expect(() => n1 ^ null, throwsArgumentError);
argumentErrorTest("^", (a, b) => a ^ b);
});
test("~", () {
@ -523,8 +539,8 @@ void main() {
expect(Int64.MIN_VALUE.toSigned(64), Int64.MIN_VALUE);
expect(Int64.MAX_VALUE.toSigned(63), -Int64.ONE);
expect(Int64.MIN_VALUE.toSigned(63), Int64.ZERO);
expect(() => Int64.ONE.toSigned(0), throws);
expect(() => Int64.ONE.toSigned(65), throws);
expect(() => Int64.ONE.toSigned(0), throwsRangeError);
expect(() => Int64.ONE.toSigned(65), throwsRangeError);
});
test("toUnsigned", () {
expect((Int64.ONE << 44).toUnsigned(45), Int64.ONE << 44);
@ -537,8 +553,8 @@ void main() {
expect(Int64.MIN_VALUE.toUnsigned(64), Int64.MIN_VALUE);
expect(Int64.MAX_VALUE.toUnsigned(63), Int64.MAX_VALUE);
expect(Int64.MIN_VALUE.toUnsigned(63), Int64.ZERO);
expect(() => Int64.ONE.toUnsigned(-1), throws);
expect(() => Int64.ONE.toUnsigned(65), throws);
expect(() => Int64.ONE.toUnsigned(-1), throwsRangeError);
expect(() => Int64.ONE.toUnsigned(65), throwsRangeError);
});
test("toDouble", () {
expect(new Int64(0).toDouble(), same(0.0));
@ -658,9 +674,6 @@ void main() {
checkInt(4294967296);
checkInt(-4294967295);
checkInt(-4294967296);
expect(() => Int64.parseRadix('xyzzy', -1), throwsArgumentError);
expect(() => Int64.parseRadix('plugh', 10),
throwsA(new isInstanceOf<FormatException>()));
});
test("parseRadix", () {
@ -674,6 +687,11 @@ void main() {
check("9223372036854775807", 10, "9223372036854775807");
// Overflow during parsing.
check("9223372036854775808", 10, "-9223372036854775808");
expect(() => Int64.parseRadix('0', 1), throwsRangeError);
expect(() => Int64.parseRadix('0', 37), throwsRangeError);
expect(() => Int64.parseRadix('xyzzy', -1), throwsRangeError);
expect(() => Int64.parseRadix('xyzzy', 10), throwsFormatException);
});
test("parseRadixN", () {
@ -748,7 +766,8 @@ void main() {
expect(Int64.MAX_VALUE.toRadixString(13), "10b269549075433c37");
expect(Int64.MAX_VALUE.toRadixString(14), "4340724c6c71dc7a7");
expect(Int64.MAX_VALUE.toRadixString(15), "160e2ad3246366807");
expect(Int64.MAX_VALUE.toRadixString(16), "7fffffffffffffff");
expect(() => Int64.ZERO.toRadixString(1), throwsRangeError);
expect(() => Int64.ZERO.toRadixString(37), throwsRangeError);
});
});
}