Files
sdk/tests/language/operator/integer_division_by_zero_test.dart
Lasse R.H. Nielsen 2c36db9b0a Deprecate IntegerDivisionByZeroException.
Deprecates the class and makes it implement `Error` (more specifically
`UnsupportedError`).
Code throwing `IntegerDivisionByZeroException` should be migrated to
throwing `unsupportedError` directly, code catching the exception class
should start catching `UnsupportedError` immediately (or reconsider why
they are catching at all).

Integer division by zero also covers other ways that a double division
can give a non-number result (any infinity or NaN result of the division prior to calling `truncate()` on the result, will cause `~/` to throw).

Fixes #46776

Bug: https://github.com/dart-lang/sdk/issues/46776
Change-Id: Idf2657153dd16542e72c6ba921f587dd9fc9032a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208643
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
2021-10-19 16:07:52 +00:00

50 lines
2.0 KiB
Dart

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test integer division by zero.
// Test that results before and after optimization are the same.
// VMOptions=--optimization-counter-threshold=10 --no-use-osr --no-background-compilation
import "package:expect/expect.dart";
num divBy(num a, num b) => a ~/ b;
main() {
// Dividing integers by zero is an error.
Expect.throws<Error>(() => divBy(1, 0));
Expect.throws<Error>(() => divBy(0, 0));
// Dividing doubles by zero is an error (result is never finite).
Expect.throws<Error>(() => divBy(1.0, 0));
Expect.throws<Error>(() => divBy(1, 0.0));
Expect.throws<Error>(() => divBy(1, -0.0));
Expect.throws<Error>(() => divBy(1.0, 0.0));
// Double division yielding infinity is an error, even when not dividing
// by zero.
Expect.throws<Error>(() => divBy(double.maxFinite, 0.5));
Expect.throws<Error>(() => divBy(1, double.minPositive));
Expect.throws<Error>(() => divBy(double.infinity, 2.0));
Expect.throws<Error>(() => divBy(-double.maxFinite, 0.5));
Expect.throws<Error>(() => divBy(-1, double.minPositive));
Expect.throws<Error>(() => divBy(-double.infinity, 2.0));
// Double division yielding NaN is an error.
Expect.throws<Error>(() => divBy(0.0, 0.0));
Expect.throws<Error>(() => divBy(double.infinity, double.infinity));
Expect.throws<Error>(() => divBy(-0.0, 0.0));
Expect.throws<Error>(() => divBy(-double.infinity, double.infinity));
// Truncating division containing a double truncates to max integer
// on non-web.
num one = 1;
if (one is! double) {
var minInt = -0x8000000000000000;
var maxInt = minInt - 1;
Expect.isTrue(maxInt > 0);
// Not on web.
Expect.equals(divBy(double.maxFinite, 2), maxInt);
Expect.equals(divBy(-double.maxFinite, 2), minInt);
Expect.equals(divBy(maxInt, 0.25), maxInt);
Expect.equals(divBy(minInt, 0.25), minInt);
}
}