Main.java revision 946e143941d456a4ec666f7f54719c65c5aa3f5d
12e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain/* 22e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * Copyright (C) 2014 The Android Open Source Project 32e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * 42e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * Licensed under the Apache License, Version 2.0 (the "License"); 52e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * you may not use this file except in compliance with the License. 62e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * You may obtain a copy of the License at 72e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * 82e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * http://www.apache.org/licenses/LICENSE-2.0 92e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * 102e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * Unless required by applicable law or agreed to in writing, software 112e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * distributed under the License is distributed on an "AS IS" BASIS, 122e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * See the License for the specific language governing permissions and 142e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain * limitations under the License. 152e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain */ 162e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 172e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain// Note that $opt$ is a marker for the optimizing compiler to ensure 182e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain// it does compile the method. 192e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillainpublic class Main { 202e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 213dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertEquals(int expected, int result) { 222e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain if (expected != result) { 232e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain throw new Error("Expected: " + expected + ", found: " + result); 242e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 252e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 262e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 273dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertEquals(long expected, long result) { 282e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain if (expected != result) { 292e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain throw new Error("Expected: " + expected + ", found: " + result); 302e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 312e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 322e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 333dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertEquals(float expected, float result) { 343dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain if (expected != result) { 353dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain throw new Error("Expected: " + expected + ", found: " + result); 363dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 373dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 383dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 393dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertEquals(double expected, double result) { 403dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain if (expected != result) { 413dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain throw new Error("Expected: " + expected + ", found: " + result); 423dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 433dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 443dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 453dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertIsNaN(float result) { 463dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain if (!Float.isNaN(result)) { 473dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain throw new Error("Expected NaN: " + result); 483dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 493dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 503dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 513dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain public static void assertIsNaN(double result) { 523dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain if (!Double.isNaN(result)) { 533dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain throw new Error("Expected NaN: " + result); 543dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 553dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 563dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 572e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain public static void main(String[] args) { 582e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain negInt(); 592e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain $opt$InplaceNegOneInt(1); 602e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 612e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain negLong(); 622e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain $opt$InplaceNegOneLong(1L); 633dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 643dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain negFloat(); 653dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain negDouble(); 662e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 672e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 682e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain private static void negInt() { 693dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1, $opt$NegInt(1)); 703dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(1, $opt$NegInt(-1)); 713dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0, $opt$NegInt(0)); 723dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(51, $opt$NegInt(-51)); 733dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-51, $opt$NegInt(51)); 74946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(2147483647, $opt$NegInt(-2147483647)); // -(2^31 - 1) 75946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(-2147483647, $opt$NegInt(2147483647)); // 2^31 - 1 762e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // From the Java 7 SE Edition specification: 772e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4 782e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // 792e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // For integer values, negation is the same as subtraction from 802e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // zero. The Java programming language uses two's-complement 812e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // representation for integers, and the range of two's-complement 822e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // values is not symmetric, so negation of the maximum negative 832e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // int or long results in that same maximum negative number. 842e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // Overflow occurs in this case, but no exception is thrown. 852e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // For all integer values x, -x equals (~x)+1.'' 863dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31) 872e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 882e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 892e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain private static void $opt$InplaceNegOneInt(int a) { 902e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain a = -a; 913dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1, a); 922e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 932e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 942e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain private static void negLong() { 953dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1L, $opt$NegLong(1L)); 963dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(1L, $opt$NegLong(-1L)); 973dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0L, $opt$NegLong(0L)); 983dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(51L, $opt$NegLong(-51L)); 993dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-51L, $opt$NegLong(51L)); 1003dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 101946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(2147483647L, $opt$NegLong(-2147483647L)); // -(2^31 - 1) 102946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(-2147483647L, $opt$NegLong(2147483647L)); // (2^31 - 1) 103946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(2147483648L, $opt$NegLong(-2147483648L)); // -(2^31) 104946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(-2147483648L, $opt$NegLong(2147483648L)); // 2^31 1053dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 106946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // -(2^63 - 1) 107946e143941d456a4ec666f7f54719c65c5aa3f5dRoland Levillain assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // 2^63 - 1 1082e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // See remark regarding the negation of the maximum negative 1092e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain // (long) value in negInt(). 1103dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63) 1112e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 1122e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 1132e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain private static void $opt$InplaceNegOneLong(long a) { 1142e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain a = -a; 1153dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1L, a); 1163dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 1173dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1183dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain private static void negFloat() { 1193dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1F, $opt$NegFloat(1F)); 1203dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(1F, $opt$NegFloat(-1F)); 1213dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0F, $opt$NegFloat(0F)); 1223dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(51F, $opt$NegFloat(-51F)); 1233dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-51F, $opt$NegFloat(51F)); 1243dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1253dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-0.1F, $opt$NegFloat(0.1F)); 1263dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0.1F, $opt$NegFloat(-0.1F)); 1273dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(343597.38362F, $opt$NegFloat(-343597.38362F)); 1283dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-343597.38362F, $opt$NegFloat(343597.38362F)); 1293dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1303dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Float.MIN_NORMAL, $opt$NegFloat(Float.MIN_NORMAL)); 1313dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Float.MIN_NORMAL, $opt$NegFloat(-Float.MIN_NORMAL)); 1323dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Float.MIN_VALUE, $opt$NegFloat(Float.MIN_VALUE)); 1333dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Float.MIN_VALUE, $opt$NegFloat(-Float.MIN_VALUE)); 1343dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Float.MAX_VALUE, $opt$NegFloat(Float.MAX_VALUE)); 1353dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Float.MAX_VALUE, $opt$NegFloat(-Float.MAX_VALUE)); 1363dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1373dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Float.NEGATIVE_INFINITY, $opt$NegFloat(Float.POSITIVE_INFINITY)); 1383dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Float.POSITIVE_INFINITY, $opt$NegFloat(Float.NEGATIVE_INFINITY)); 1393dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertIsNaN($opt$NegFloat(Float.NaN)); 1403dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 1413dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1423dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain private static void negDouble() { 1433dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-1D, $opt$NegDouble(1D)); 1443dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(1D, $opt$NegDouble(-1D)); 1453dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0D, $opt$NegDouble(0D)); 1463dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(51D, $opt$NegDouble(-51D)); 1473dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-51D, $opt$NegDouble(51D)); 1483dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1493dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-0.1D, $opt$NegDouble(0.1D)); 1503dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(0.1D, $opt$NegDouble(-0.1D)); 1513dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(343597.38362D, $opt$NegDouble(-343597.38362D)); 1523dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-343597.38362D, $opt$NegDouble(343597.38362D)); 1533dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1543dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Double.MIN_NORMAL, $opt$NegDouble(Double.MIN_NORMAL)); 1553dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Double.MIN_NORMAL, $opt$NegDouble(-Double.MIN_NORMAL)); 1563dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Double.MIN_VALUE, $opt$NegDouble(Double.MIN_VALUE)); 1573dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Double.MIN_VALUE, $opt$NegDouble(-Double.MIN_VALUE)); 1583dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(-Double.MAX_VALUE, $opt$NegDouble(Double.MAX_VALUE)); 1593dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Double.MAX_VALUE, $opt$NegDouble(-Double.MAX_VALUE)); 1603dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1613dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Double.NEGATIVE_INFINITY, $opt$NegDouble(Double.POSITIVE_INFINITY)); 1623dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertEquals(Double.POSITIVE_INFINITY, $opt$NegDouble(Double.NEGATIVE_INFINITY)); 1633dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain assertIsNaN($opt$NegDouble(Double.NaN)); 1642e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 1652e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 1662e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain static int $opt$NegInt(int a){ 1672e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain return -a; 1682e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 1692e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain 1702e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain static long $opt$NegLong(long a){ 1712e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain return -a; 1722e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain } 1733dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1743dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain static float $opt$NegFloat(float a){ 1753dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain return -a; 1763dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 1773dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain 1783dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain static double $opt$NegDouble(double a){ 1793dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain return -a; 1803dbcb38a8b2237b0da290ae35dc0caab3cb47b3dRoland Levillain } 1812e07b4f0a84a7968b4690c2b1be2e2f75cc6fa8eRoland Levillain} 182