1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker // Note that $opt$ is a marker for the optimizing compiler to test 18*795d594fSAndroid Build Coastguard Worker // it does compile the method. 19*795d594fSAndroid Build Coastguard Worker public class DivTest { 20*795d594fSAndroid Build Coastguard Worker expectEquals(int expected, int result)21*795d594fSAndroid Build Coastguard Worker public static void expectEquals(int expected, int result) { 22*795d594fSAndroid Build Coastguard Worker if (expected != result) { 23*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + result); 24*795d594fSAndroid Build Coastguard Worker } 25*795d594fSAndroid Build Coastguard Worker } 26*795d594fSAndroid Build Coastguard Worker expectEquals(long expected, long result)27*795d594fSAndroid Build Coastguard Worker public static void expectEquals(long expected, long result) { 28*795d594fSAndroid Build Coastguard Worker if (expected != result) { 29*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + result); 30*795d594fSAndroid Build Coastguard Worker } 31*795d594fSAndroid Build Coastguard Worker } 32*795d594fSAndroid Build Coastguard Worker expectEquals(float expected, float result)33*795d594fSAndroid Build Coastguard Worker public static void expectEquals(float expected, float result) { 34*795d594fSAndroid Build Coastguard Worker if (expected != result) { 35*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + result); 36*795d594fSAndroid Build Coastguard Worker } 37*795d594fSAndroid Build Coastguard Worker } 38*795d594fSAndroid Build Coastguard Worker expectEquals(double expected, double result)39*795d594fSAndroid Build Coastguard Worker public static void expectEquals(double expected, double result) { 40*795d594fSAndroid Build Coastguard Worker if (expected != result) { 41*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + expected + ", found: " + result); 42*795d594fSAndroid Build Coastguard Worker } 43*795d594fSAndroid Build Coastguard Worker } 44*795d594fSAndroid Build Coastguard Worker expectApproxEquals(float a, float b)45*795d594fSAndroid Build Coastguard Worker public static void expectApproxEquals(float a, float b) { 46*795d594fSAndroid Build Coastguard Worker float maxDelta = 0.00001F; 47*795d594fSAndroid Build Coastguard Worker boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta); 48*795d594fSAndroid Build Coastguard Worker if (!aproxEquals) { 49*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + a + ", found: " + b 50*795d594fSAndroid Build Coastguard Worker + ", with delta: " + maxDelta + " " + (a - b)); 51*795d594fSAndroid Build Coastguard Worker } 52*795d594fSAndroid Build Coastguard Worker } 53*795d594fSAndroid Build Coastguard Worker expectApproxEquals(double a, double b)54*795d594fSAndroid Build Coastguard Worker public static void expectApproxEquals(double a, double b) { 55*795d594fSAndroid Build Coastguard Worker double maxDelta = 0.00001D; 56*795d594fSAndroid Build Coastguard Worker boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta); 57*795d594fSAndroid Build Coastguard Worker if (!aproxEquals) { 58*795d594fSAndroid Build Coastguard Worker throw new Error("Expected: " + a + ", found: " 59*795d594fSAndroid Build Coastguard Worker + b + ", with delta: " + maxDelta + " " + (a - b)); 60*795d594fSAndroid Build Coastguard Worker } 61*795d594fSAndroid Build Coastguard Worker } 62*795d594fSAndroid Build Coastguard Worker expectNaN(float a)63*795d594fSAndroid Build Coastguard Worker public static void expectNaN(float a) { 64*795d594fSAndroid Build Coastguard Worker if (a == a) { 65*795d594fSAndroid Build Coastguard Worker throw new Error("Expected NaN: " + a); 66*795d594fSAndroid Build Coastguard Worker } 67*795d594fSAndroid Build Coastguard Worker } 68*795d594fSAndroid Build Coastguard Worker expectNaN(double a)69*795d594fSAndroid Build Coastguard Worker public static void expectNaN(double a) { 70*795d594fSAndroid Build Coastguard Worker if (a == a) { 71*795d594fSAndroid Build Coastguard Worker throw new Error("Expected NaN: " + a); 72*795d594fSAndroid Build Coastguard Worker } 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(int value)75*795d594fSAndroid Build Coastguard Worker public static void expectDivisionByZero(int value) { 76*795d594fSAndroid Build Coastguard Worker try { 77*795d594fSAndroid Build Coastguard Worker $opt$Div(value, 0); 78*795d594fSAndroid Build Coastguard Worker throw new Error("Expected RuntimeException when dividing by 0"); 79*795d594fSAndroid Build Coastguard Worker } catch (java.lang.RuntimeException e) { 80*795d594fSAndroid Build Coastguard Worker } 81*795d594fSAndroid Build Coastguard Worker try { 82*795d594fSAndroid Build Coastguard Worker $opt$DivZero(value); 83*795d594fSAndroid Build Coastguard Worker throw new Error("Expected RuntimeException when dividing by 0"); 84*795d594fSAndroid Build Coastguard Worker } catch (java.lang.RuntimeException e) { 85*795d594fSAndroid Build Coastguard Worker } 86*795d594fSAndroid Build Coastguard Worker } 87*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(long value)88*795d594fSAndroid Build Coastguard Worker public static void expectDivisionByZero(long value) { 89*795d594fSAndroid Build Coastguard Worker try { 90*795d594fSAndroid Build Coastguard Worker $opt$Div(value, 0L); 91*795d594fSAndroid Build Coastguard Worker throw new Error("Expected RuntimeException when dividing by 0"); 92*795d594fSAndroid Build Coastguard Worker } catch (java.lang.RuntimeException e) { 93*795d594fSAndroid Build Coastguard Worker } 94*795d594fSAndroid Build Coastguard Worker try { 95*795d594fSAndroid Build Coastguard Worker $opt$DivZero(value); 96*795d594fSAndroid Build Coastguard Worker throw new Error("Expected RuntimeException when dividing by 0"); 97*795d594fSAndroid Build Coastguard Worker } catch (java.lang.RuntimeException e) { 98*795d594fSAndroid Build Coastguard Worker } 99*795d594fSAndroid Build Coastguard Worker } 100*795d594fSAndroid Build Coastguard Worker main()101*795d594fSAndroid Build Coastguard Worker public static void main() { 102*795d594fSAndroid Build Coastguard Worker divInt(); 103*795d594fSAndroid Build Coastguard Worker divLong(); 104*795d594fSAndroid Build Coastguard Worker divFloat(); 105*795d594fSAndroid Build Coastguard Worker divDouble(); 106*795d594fSAndroid Build Coastguard Worker } 107*795d594fSAndroid Build Coastguard Worker divInt()108*795d594fSAndroid Build Coastguard Worker private static void divInt() { 109*795d594fSAndroid Build Coastguard Worker expectEquals(2, $opt$DivConst(6)); 110*795d594fSAndroid Build Coastguard Worker expectEquals(2, $opt$Div(6, 3)); 111*795d594fSAndroid Build Coastguard Worker expectEquals(6, $opt$Div(6, 1)); 112*795d594fSAndroid Build Coastguard Worker expectEquals(-2, $opt$Div(6, -3)); 113*795d594fSAndroid Build Coastguard Worker expectEquals(1, $opt$Div(4, 3)); 114*795d594fSAndroid Build Coastguard Worker expectEquals(-1, $opt$Div(4, -3)); 115*795d594fSAndroid Build Coastguard Worker expectEquals(5, $opt$Div(23, 4)); 116*795d594fSAndroid Build Coastguard Worker expectEquals(-5, $opt$Div(-23, 4)); 117*795d594fSAndroid Build Coastguard Worker 118*795d594fSAndroid Build Coastguard Worker expectEquals(-Integer.MAX_VALUE, $opt$Div(Integer.MAX_VALUE, -1)); 119*795d594fSAndroid Build Coastguard Worker expectEquals(Integer.MIN_VALUE, $opt$Div(Integer.MIN_VALUE, -1)); // overflow 120*795d594fSAndroid Build Coastguard Worker expectEquals(-1073741824, $opt$Div(Integer.MIN_VALUE, 2)); 121*795d594fSAndroid Build Coastguard Worker 122*795d594fSAndroid Build Coastguard Worker expectEquals(0, $opt$Div(0, Integer.MAX_VALUE)); 123*795d594fSAndroid Build Coastguard Worker expectEquals(0, $opt$Div(0, Integer.MIN_VALUE)); 124*795d594fSAndroid Build Coastguard Worker 125*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(0); 126*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(1); 127*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(Integer.MAX_VALUE); 128*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(Integer.MIN_VALUE); 129*795d594fSAndroid Build Coastguard Worker } 130*795d594fSAndroid Build Coastguard Worker divLong()131*795d594fSAndroid Build Coastguard Worker private static void divLong() { 132*795d594fSAndroid Build Coastguard Worker expectEquals(2L, $opt$DivConst(6L)); 133*795d594fSAndroid Build Coastguard Worker expectEquals(2L, $opt$Div(6L, 3L)); 134*795d594fSAndroid Build Coastguard Worker expectEquals(6L, $opt$Div(6L, 1L)); 135*795d594fSAndroid Build Coastguard Worker expectEquals(-2L, $opt$Div(6L, -3L)); 136*795d594fSAndroid Build Coastguard Worker expectEquals(1L, $opt$Div(4L, 3L)); 137*795d594fSAndroid Build Coastguard Worker expectEquals(-1L, $opt$Div(4L, -3L)); 138*795d594fSAndroid Build Coastguard Worker expectEquals(5L, $opt$Div(23L, 4L)); 139*795d594fSAndroid Build Coastguard Worker expectEquals(-5L, $opt$Div(-23L, 4L)); 140*795d594fSAndroid Build Coastguard Worker 141*795d594fSAndroid Build Coastguard Worker expectEquals(-Integer.MAX_VALUE, $opt$Div(Integer.MAX_VALUE, -1L)); 142*795d594fSAndroid Build Coastguard Worker expectEquals(2147483648L, $opt$Div(Integer.MIN_VALUE, -1L)); 143*795d594fSAndroid Build Coastguard Worker expectEquals(-1073741824L, $opt$Div(Integer.MIN_VALUE, 2L)); 144*795d594fSAndroid Build Coastguard Worker 145*795d594fSAndroid Build Coastguard Worker expectEquals(-Long.MAX_VALUE, $opt$Div(Long.MAX_VALUE, -1L)); 146*795d594fSAndroid Build Coastguard Worker expectEquals(Long.MIN_VALUE, $opt$Div(Long.MIN_VALUE, -1L)); // overflow 147*795d594fSAndroid Build Coastguard Worker 148*795d594fSAndroid Build Coastguard Worker expectEquals(11111111111111L, $opt$Div(33333333333333L, 3L)); 149*795d594fSAndroid Build Coastguard Worker expectEquals(3L, $opt$Div(33333333333333L, 11111111111111L)); 150*795d594fSAndroid Build Coastguard Worker 151*795d594fSAndroid Build Coastguard Worker expectEquals(0L, $opt$Div(0L, Long.MAX_VALUE)); 152*795d594fSAndroid Build Coastguard Worker expectEquals(0L, $opt$Div(0L, Long.MIN_VALUE)); 153*795d594fSAndroid Build Coastguard Worker 154*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(0L); 155*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(1L); 156*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(Long.MAX_VALUE); 157*795d594fSAndroid Build Coastguard Worker expectDivisionByZero(Long.MIN_VALUE); 158*795d594fSAndroid Build Coastguard Worker } 159*795d594fSAndroid Build Coastguard Worker divFloat()160*795d594fSAndroid Build Coastguard Worker private static void divFloat() { 161*795d594fSAndroid Build Coastguard Worker expectApproxEquals(1.6666666F, $opt$Div(5F, 3F)); 162*795d594fSAndroid Build Coastguard Worker expectApproxEquals(0F, $opt$Div(0F, 3F)); 163*795d594fSAndroid Build Coastguard Worker expectApproxEquals(-0.3333333F, $opt$Div(1F, -3F)); 164*795d594fSAndroid Build Coastguard Worker expectApproxEquals(4F, $opt$Div(-12F, -3F)); 165*795d594fSAndroid Build Coastguard Worker expectApproxEquals(0.5, $opt$Div(0.1F, 0.2F)); 166*795d594fSAndroid Build Coastguard Worker expectApproxEquals(-2.5F, $opt$Div(-0.5F, 0.2F)); 167*795d594fSAndroid Build Coastguard Worker 168*795d594fSAndroid Build Coastguard Worker expectEquals(0F, $opt$Div(0F, Float.POSITIVE_INFINITY)); 169*795d594fSAndroid Build Coastguard Worker expectEquals(0F, $opt$Div(11F, Float.POSITIVE_INFINITY)); 170*795d594fSAndroid Build Coastguard Worker expectEquals(0F, $opt$Div(0F, Float.NEGATIVE_INFINITY)); 171*795d594fSAndroid Build Coastguard Worker expectEquals(0F, $opt$Div(11F, Float.NEGATIVE_INFINITY)); 172*795d594fSAndroid Build Coastguard Worker 173*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(0F, 0F)); 174*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NaN, 11F)); 175*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(-11F, Float.NaN)); 176*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); 177*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY)); 178*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)); 179*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); 180*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NaN, Float.NEGATIVE_INFINITY)); 181*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.NaN)); 182*795d594fSAndroid Build Coastguard Worker 183*795d594fSAndroid Build Coastguard Worker expectEquals(Float.POSITIVE_INFINITY, $opt$Div(3F, 0F)); 184*795d594fSAndroid Build Coastguard Worker expectEquals(Float.NEGATIVE_INFINITY, $opt$Div(-3F, 0F)); 185*795d594fSAndroid Build Coastguard Worker expectEquals(Float.POSITIVE_INFINITY, $opt$Div(Float.MAX_VALUE, Float.MIN_VALUE)); 186*795d594fSAndroid Build Coastguard Worker expectEquals(Float.NEGATIVE_INFINITY, $opt$Div(-Float.MAX_VALUE, Float.MIN_VALUE)); 187*795d594fSAndroid Build Coastguard Worker } 188*795d594fSAndroid Build Coastguard Worker divDouble()189*795d594fSAndroid Build Coastguard Worker private static void divDouble() { 190*795d594fSAndroid Build Coastguard Worker expectApproxEquals(1.6666666D, $opt$Div(5D, 3D)); 191*795d594fSAndroid Build Coastguard Worker expectApproxEquals(0D, $opt$Div(0D, 3D)); 192*795d594fSAndroid Build Coastguard Worker expectApproxEquals(-0.3333333D, $opt$Div(1D, -3D)); 193*795d594fSAndroid Build Coastguard Worker expectApproxEquals(4D, $opt$Div(-12D, -3D)); 194*795d594fSAndroid Build Coastguard Worker expectApproxEquals(0.5, $opt$Div(0.1D, 0.2D)); 195*795d594fSAndroid Build Coastguard Worker expectApproxEquals(-2.5D, $opt$Div(-0.5D, 0.2D)); 196*795d594fSAndroid Build Coastguard Worker 197*795d594fSAndroid Build Coastguard Worker expectEquals(0D, $opt$Div(0D, Float.POSITIVE_INFINITY)); 198*795d594fSAndroid Build Coastguard Worker expectEquals(0D, $opt$Div(11D, Float.POSITIVE_INFINITY)); 199*795d594fSAndroid Build Coastguard Worker expectEquals(0D, $opt$Div(0D, Float.NEGATIVE_INFINITY)); 200*795d594fSAndroid Build Coastguard Worker expectEquals(0D, $opt$Div(11D, Float.NEGATIVE_INFINITY)); 201*795d594fSAndroid Build Coastguard Worker 202*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(0D, 0D)); 203*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NaN, 11D)); 204*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(-11D, Float.NaN)); 205*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); 206*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY)); 207*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)); 208*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); 209*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.NaN, Float.NEGATIVE_INFINITY)); 210*795d594fSAndroid Build Coastguard Worker expectNaN($opt$Div(Float.POSITIVE_INFINITY, Float.NaN)); 211*795d594fSAndroid Build Coastguard Worker 212*795d594fSAndroid Build Coastguard Worker expectEquals(Float.POSITIVE_INFINITY, $opt$Div(3D, 0D)); 213*795d594fSAndroid Build Coastguard Worker expectEquals(Float.NEGATIVE_INFINITY, $opt$Div(-3D, 0D)); 214*795d594fSAndroid Build Coastguard Worker expectEquals(Float.POSITIVE_INFINITY, $opt$Div(Float.MAX_VALUE, Float.MIN_VALUE)); 215*795d594fSAndroid Build Coastguard Worker expectEquals(Float.NEGATIVE_INFINITY, $opt$Div(-Float.MAX_VALUE, Float.MIN_VALUE)); 216*795d594fSAndroid Build Coastguard Worker } 217*795d594fSAndroid Build Coastguard Worker $opt$Div(int a, int b)218*795d594fSAndroid Build Coastguard Worker static int $opt$Div(int a, int b) { 219*795d594fSAndroid Build Coastguard Worker return a / b; 220*795d594fSAndroid Build Coastguard Worker } 221*795d594fSAndroid Build Coastguard Worker $opt$DivZero(int a)222*795d594fSAndroid Build Coastguard Worker static int $opt$DivZero(int a) { 223*795d594fSAndroid Build Coastguard Worker return a / 0; 224*795d594fSAndroid Build Coastguard Worker } 225*795d594fSAndroid Build Coastguard Worker 226*795d594fSAndroid Build Coastguard Worker // Division by literals != 0 should not generate checks. $opt$DivConst(int a)227*795d594fSAndroid Build Coastguard Worker static int $opt$DivConst(int a) { 228*795d594fSAndroid Build Coastguard Worker return a / 3; 229*795d594fSAndroid Build Coastguard Worker } 230*795d594fSAndroid Build Coastguard Worker $opt$DivConst(long a)231*795d594fSAndroid Build Coastguard Worker static long $opt$DivConst(long a) { 232*795d594fSAndroid Build Coastguard Worker return a / 3L; 233*795d594fSAndroid Build Coastguard Worker } 234*795d594fSAndroid Build Coastguard Worker $opt$Div(long a, long b)235*795d594fSAndroid Build Coastguard Worker static long $opt$Div(long a, long b) { 236*795d594fSAndroid Build Coastguard Worker return a / b; 237*795d594fSAndroid Build Coastguard Worker } 238*795d594fSAndroid Build Coastguard Worker $opt$DivZero(long a)239*795d594fSAndroid Build Coastguard Worker static long $opt$DivZero(long a) { 240*795d594fSAndroid Build Coastguard Worker return a / 0L; 241*795d594fSAndroid Build Coastguard Worker } 242*795d594fSAndroid Build Coastguard Worker $opt$Div(float a, float b)243*795d594fSAndroid Build Coastguard Worker static float $opt$Div(float a, float b) { 244*795d594fSAndroid Build Coastguard Worker return a / b; 245*795d594fSAndroid Build Coastguard Worker } 246*795d594fSAndroid Build Coastguard Worker $opt$Div(double a, double b)247*795d594fSAndroid Build Coastguard Worker static double $opt$Div(double a, double b) { 248*795d594fSAndroid Build Coastguard Worker return a / b; 249*795d594fSAndroid Build Coastguard Worker } 250*795d594fSAndroid Build Coastguard Worker } 251