blob: 5eefb65b0d5061d815ff23fcb68ac7f6079ca317 [file] [log] [blame]
clementbera0bdd90f2019-07-06 11:15:23 +02001// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5package backport;
6
7public class MathBackportJava9Main {
8
9 public static void main(String[] args) {
10 testMultiplyExactLongInt();
11 testFloorDivLongInt();
12 testFloorModLongInt();
13 }
14
15 public static void testMultiplyExactLongInt() {
16 assertEquals(8L, Math.multiplyExact(2L, 4));
17 assertEquals(Long.MAX_VALUE, Math.multiplyExact(Long.MAX_VALUE, 1));
18 assertEquals(Long.MIN_VALUE, Math.multiplyExact(Long.MIN_VALUE / 2L, 2));
19 try {
20 throw new AssertionError(Math.multiplyExact(Long.MAX_VALUE, 2));
21 } catch (ArithmeticException expected) {
22 }
23 try {
24 throw new AssertionError(Math.multiplyExact(Long.MIN_VALUE, 2));
25 } catch (ArithmeticException expected) {
26 }
27 }
28
29 public static void testFloorDivLongInt() {
30 assertEquals(1L, Math.floorDiv(4L, 4));
31 assertEquals(1L, Math.floorDiv(-4L, -4));
32 assertEquals(-1L, Math.floorDiv(-4L, 4));
33 assertEquals(-1L, Math.floorDiv(4L, -4));
34
35 assertEquals(1L, Math.floorDiv(4L, 3));
36 assertEquals(1L, Math.floorDiv(-4L, -3));
37 assertEquals(-2L, Math.floorDiv(-4L, 3));
38 assertEquals(-2L, Math.floorDiv(4L, -3));
39
40 // Spec edge case: result is actually MAX_VALUE+1 which becomes MIN_VALUE.
41 assertEquals(Long.MIN_VALUE, Math.floorDiv(Long.MIN_VALUE, -1));
42 }
43
44 public static void testFloorModLongInt() {
45 assertEquals(0, Math.floorMod(4L, 4));
46 assertEquals(0, Math.floorMod(-4L, -4));
47 assertEquals(0, Math.floorMod(-4L, 4));
48 assertEquals(0, Math.floorMod(4L, -4));
49
50 assertEquals(1, Math.floorMod(4L, 3));
51 assertEquals(-1, Math.floorMod(-4L, -3));
52 assertEquals(2, Math.floorMod(-4L, 3));
53 assertEquals(-2, Math.floorMod(4L, -3));
54 }
55
56 private static void assertEquals(int expected, int actual) {
57 if (expected != actual) {
58 throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>');
59 }
60 }
61
62 private static void assertEquals(long expected, long actual) {
63 if (expected != actual) {
64 throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>');
65 }
66 }
67}