Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Unified Diff: pkg/fixnum/test/int_32_test.dart

Issue 24388004: Added bitLength, clamp(), toDouble(), toSigned(), toUnsigned(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use string literals in place of doubles where precision is exceeded Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/fixnum/lib/src/intx.dart ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/fixnum/test/int_32_test.dart
diff --git a/pkg/fixnum/test/int_32_test.dart b/pkg/fixnum/test/int_32_test.dart
index 1fcaed05c0774054e17c2b309cb731a34e53d917..e34f714cb82b29f79d8674dee5d9c8a190645f65 100644
--- a/pkg/fixnum/test/int_32_test.dart
+++ b/pkg/fixnum/test/int_32_test.dart
@@ -7,6 +7,50 @@ import 'package:fixnum/fixnum.dart';
import 'package:unittest/unittest.dart';
void main() {
+ group("isX tests", () {
+ test("isEven", () {
+ expect((-Int32.ONE).isEven, false);
+ expect(Int32.ZERO.isEven, true);
+ expect(Int32.ONE.isEven, false);
+ expect(Int32.TWO.isEven, true);
+ });
+ test("isMaxValue", () {
+ expect(Int32.MIN_VALUE.isMaxValue, false);
+ expect(Int32.ZERO.isMaxValue, false);
+ expect(Int32.MAX_VALUE.isMaxValue, true);
+ });
+ test("isMinValue", () {
+ expect(Int32.MIN_VALUE.isMinValue, true);
+ expect(Int32.ZERO.isMinValue, false);
+ expect(Int32.MAX_VALUE.isMinValue, false);
+ });
+ test("isNegative", () {
+ expect(Int32.MIN_VALUE.isNegative, true);
+ expect(Int32.ZERO.isNegative, false);
+ expect(Int32.ONE.isNegative, false);
+ });
+ test("isOdd", () {
+ expect((-Int32.ONE).isOdd, true);
+ expect(Int32.ZERO.isOdd, false);
+ expect(Int32.ONE.isOdd, true);
+ expect(Int32.TWO.isOdd, false);
+ });
+ test("isZero", () {
+ expect(Int32.MIN_VALUE.isZero, false);
+ expect(Int32.ZERO.isZero, true);
+ expect(Int32.MAX_VALUE.isZero, false);
+ });
+ test("bitLength", () {
+ expect(new Int32(-2).bitLength, 1);
+ expect((-Int32.ONE).bitLength, 0);
+ expect(Int32.ZERO.bitLength, 0);
+ expect(Int32.ONE.bitLength, 1);
+ expect(new Int32(2).bitLength, 2);
+ expect(Int32.MAX_VALUE.bitLength, 31);
+ expect(Int32.MIN_VALUE.bitLength, 31);
+ });
+ });
+
group("arithmetic operators", () {
Int32 n1 = new Int32(1234);
Int32 n2 = new Int32(9876);
@@ -85,6 +129,26 @@ void main() {
new Int32(0x12345678.remainder(0x22)));
expect(() => new Int32(17).remainder(null), throws);
});
+
+ test("clamp", () {
+ Int32 val = new Int32(17);
+ expect(val.clamp(20, 30), new Int32(20));
+ expect(val.clamp(10, 20), new Int32(17));
+ expect(val.clamp(10, 15), new Int32(15));
+
+ expect(val.clamp(new Int32(20), new Int32(30)), new Int32(20));
+ expect(val.clamp(new Int32(10), new Int32(20)), new Int32(17));
+ expect(val.clamp(new Int32(10), new Int32(15)), new Int32(15));
+
+ expect(val.clamp(new Int64(20), new Int64(30)), new Int32(20));
+ expect(val.clamp(new Int64(10), new Int64(20)), new Int32(17));
+ expect(val.clamp(new Int64(10), new Int64(15)), new Int32(15));
+ expect(val.clamp(Int64.MIN_VALUE, new Int64(30)), new Int32(17));
+ expect(val.clamp(new Int64(10), Int64.MAX_VALUE), new Int32(17));
+
+ expect(() => val.clamp(1, 'b'), throwsA(isArgumentError));
+ expect(() => val.clamp('a', 1), throwsA(isArgumentError));
+ });
});
group("comparison operators", () {
@@ -201,13 +265,43 @@ void main() {
});
});
- group("type conversions", () {
- expect(new Int32(17).toInt(), 17);
- expect(new Int32(-17).toInt(), -17);
- expect(new Int32(17).toInt32(), new Int32(17));
- expect(new Int32(-17).toInt32(), new Int32(-17));
- expect(new Int32(17).toInt64(), new Int64(17));
- expect(new Int32(-17).toInt64(), new Int64(-17));
+ group("conversions", () {
+ test("toSigned", () {
+ expect(Int32.ONE.toSigned(2), Int32.ONE);
+ expect(Int32.ONE.toSigned(1), -Int32.ONE);
+ expect(Int32.MAX_VALUE.toSigned(32), Int32.MAX_VALUE);
+ 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);
+ });
+ test("toUnsigned", () {
+ expect(Int32.ONE.toUnsigned(1), Int32.ONE);
+ expect(Int32.ONE.toUnsigned(0), Int32.ZERO);
+ expect(Int32.MAX_VALUE.toUnsigned(32), Int32.MAX_VALUE);
+ 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);
+ });
+ test("toDouble", () {
+ expect(new Int32(17).toDouble(), same(17.0));
+ expect(new Int32(-17).toDouble(), same(-17.0));
+ });
+ test("toInt", () {
+ expect(new Int32(17).toInt(), 17);
+ expect(new Int32(-17).toInt(), -17);
+ });
+ test("toInt32", () {
+ expect(new Int32(17).toInt32(), new Int32(17));
+ expect(new Int32(-17).toInt32(), new Int32(-17));
+ });
+ test("toInt64", () {
+ expect(new Int32(17).toInt64(), new Int64(17));
+ expect(new Int32(-17).toInt64(), new Int64(-17));
+ });
});
group("string representation", () {
« no previous file with comments | « pkg/fixnum/lib/src/intx.dart ('k') | pkg/fixnum/test/int_64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698