Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roland Levillain | c2abe2f | 2015-08-03 15:20:02 +0100 | [diff] [blame^] | 17 | // Note that $opt$ is a marker for the optimizing compiler to test |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 18 | // it does compile the method. |
| 19 | public class Main { |
| 20 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 21 | public static void assertEquals(int expected, int result) { |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 22 | if (expected != result) { |
| 23 | throw new Error("Expected: " + expected + ", found: " + result); |
| 24 | } |
| 25 | } |
| 26 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 27 | public static void assertEquals(long expected, long result) { |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 28 | if (expected != result) { |
| 29 | throw new Error("Expected: " + expected + ", found: " + result); |
| 30 | } |
| 31 | } |
| 32 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 33 | public static void assertEquals(float expected, float result) { |
| 34 | if (expected != result) { |
| 35 | throw new Error("Expected: " + expected + ", found: " + result); |
| 36 | } |
| 37 | } |
| 38 | |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 39 | public static void assertEquals(String expected, float result) { |
| 40 | if (!expected.equals(new Float(result).toString())) { |
| 41 | throw new Error("Expected: " + expected + ", found: " + result); |
| 42 | } |
| 43 | } |
| 44 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 45 | public static void assertEquals(double expected, double result) { |
| 46 | if (expected != result) { |
| 47 | throw new Error("Expected: " + expected + ", found: " + result); |
| 48 | } |
| 49 | } |
| 50 | |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 51 | public static void assertEquals(String expected, double result) { |
| 52 | if (!expected.equals(new Double(result).toString())) { |
| 53 | throw new Error("Expected: " + expected + ", found: " + result); |
| 54 | } |
| 55 | } |
| 56 | |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 57 | public static void assertIsNaN(float result) { |
| 58 | if (!Float.isNaN(result)) { |
| 59 | throw new Error("Expected NaN: " + result); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void assertIsNaN(double result) { |
| 64 | if (!Double.isNaN(result)) { |
| 65 | throw new Error("Expected NaN: " + result); |
| 66 | } |
| 67 | } |
| 68 | |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 69 | public static void main(String[] args) { |
| 70 | negInt(); |
| 71 | $opt$InplaceNegOneInt(1); |
| 72 | |
| 73 | negLong(); |
| 74 | $opt$InplaceNegOneLong(1L); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 75 | |
| 76 | negFloat(); |
| 77 | negDouble(); |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private static void negInt() { |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 81 | assertEquals(-1, $opt$NegInt(1)); |
| 82 | assertEquals(1, $opt$NegInt(-1)); |
| 83 | assertEquals(0, $opt$NegInt(0)); |
| 84 | assertEquals(51, $opt$NegInt(-51)); |
| 85 | assertEquals(-51, $opt$NegInt(51)); |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 86 | assertEquals(2147483647, $opt$NegInt(-2147483647)); // -(2^31 - 1) |
| 87 | assertEquals(-2147483647, $opt$NegInt(2147483647)); // 2^31 - 1 |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 88 | // From the Java 7 SE Edition specification: |
| 89 | // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4 |
| 90 | // |
| 91 | // For integer values, negation is the same as subtraction from |
| 92 | // zero. The Java programming language uses two's-complement |
| 93 | // representation for integers, and the range of two's-complement |
| 94 | // values is not symmetric, so negation of the maximum negative |
| 95 | // int or long results in that same maximum negative number. |
| 96 | // Overflow occurs in this case, but no exception is thrown. |
| 97 | // For all integer values x, -x equals (~x)+1.'' |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 98 | assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31) |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private static void $opt$InplaceNegOneInt(int a) { |
| 102 | a = -a; |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 103 | assertEquals(-1, a); |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private static void negLong() { |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 107 | assertEquals(-1L, $opt$NegLong(1L)); |
| 108 | assertEquals(1L, $opt$NegLong(-1L)); |
| 109 | assertEquals(0L, $opt$NegLong(0L)); |
| 110 | assertEquals(51L, $opt$NegLong(-51L)); |
| 111 | assertEquals(-51L, $opt$NegLong(51L)); |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 112 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 113 | assertEquals(2147483647L, $opt$NegLong(-2147483647L)); // -(2^31 - 1) |
| 114 | assertEquals(-2147483647L, $opt$NegLong(2147483647L)); // (2^31 - 1) |
| 115 | assertEquals(2147483648L, $opt$NegLong(-2147483648L)); // -(2^31) |
| 116 | assertEquals(-2147483648L, $opt$NegLong(2147483648L)); // 2^31 |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 117 | |
Roland Levillain | 946e143 | 2014-11-11 17:35:19 +0000 | [diff] [blame] | 118 | assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // -(2^63 - 1) |
| 119 | assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // 2^63 - 1 |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 120 | // See remark regarding the negation of the maximum negative |
| 121 | // (long) value in negInt(). |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 122 | assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63) |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private static void $opt$InplaceNegOneLong(long a) { |
| 126 | a = -a; |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 127 | assertEquals(-1L, a); |
| 128 | } |
| 129 | |
| 130 | private static void negFloat() { |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 131 | assertEquals("-0.0", $opt$NegFloat(0F)); |
| 132 | assertEquals("0.0", $opt$NegFloat(-0F)); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 133 | assertEquals(-1F, $opt$NegFloat(1F)); |
| 134 | assertEquals(1F, $opt$NegFloat(-1F)); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 135 | assertEquals(51F, $opt$NegFloat(-51F)); |
| 136 | assertEquals(-51F, $opt$NegFloat(51F)); |
| 137 | |
| 138 | assertEquals(-0.1F, $opt$NegFloat(0.1F)); |
| 139 | assertEquals(0.1F, $opt$NegFloat(-0.1F)); |
| 140 | assertEquals(343597.38362F, $opt$NegFloat(-343597.38362F)); |
| 141 | assertEquals(-343597.38362F, $opt$NegFloat(343597.38362F)); |
| 142 | |
| 143 | assertEquals(-Float.MIN_NORMAL, $opt$NegFloat(Float.MIN_NORMAL)); |
| 144 | assertEquals(Float.MIN_NORMAL, $opt$NegFloat(-Float.MIN_NORMAL)); |
| 145 | assertEquals(-Float.MIN_VALUE, $opt$NegFloat(Float.MIN_VALUE)); |
| 146 | assertEquals(Float.MIN_VALUE, $opt$NegFloat(-Float.MIN_VALUE)); |
| 147 | assertEquals(-Float.MAX_VALUE, $opt$NegFloat(Float.MAX_VALUE)); |
| 148 | assertEquals(Float.MAX_VALUE, $opt$NegFloat(-Float.MAX_VALUE)); |
| 149 | |
| 150 | assertEquals(Float.NEGATIVE_INFINITY, $opt$NegFloat(Float.POSITIVE_INFINITY)); |
| 151 | assertEquals(Float.POSITIVE_INFINITY, $opt$NegFloat(Float.NEGATIVE_INFINITY)); |
| 152 | assertIsNaN($opt$NegFloat(Float.NaN)); |
| 153 | } |
| 154 | |
| 155 | private static void negDouble() { |
Roland Levillain | 5368c21 | 2014-11-27 15:03:41 +0000 | [diff] [blame] | 156 | assertEquals("-0.0", $opt$NegDouble(0D)); |
| 157 | assertEquals("0.0", $opt$NegDouble(-0D)); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 158 | assertEquals(-1D, $opt$NegDouble(1D)); |
| 159 | assertEquals(1D, $opt$NegDouble(-1D)); |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 160 | assertEquals(51D, $opt$NegDouble(-51D)); |
| 161 | assertEquals(-51D, $opt$NegDouble(51D)); |
| 162 | |
| 163 | assertEquals(-0.1D, $opt$NegDouble(0.1D)); |
| 164 | assertEquals(0.1D, $opt$NegDouble(-0.1D)); |
| 165 | assertEquals(343597.38362D, $opt$NegDouble(-343597.38362D)); |
| 166 | assertEquals(-343597.38362D, $opt$NegDouble(343597.38362D)); |
| 167 | |
| 168 | assertEquals(-Double.MIN_NORMAL, $opt$NegDouble(Double.MIN_NORMAL)); |
| 169 | assertEquals(Double.MIN_NORMAL, $opt$NegDouble(-Double.MIN_NORMAL)); |
| 170 | assertEquals(-Double.MIN_VALUE, $opt$NegDouble(Double.MIN_VALUE)); |
| 171 | assertEquals(Double.MIN_VALUE, $opt$NegDouble(-Double.MIN_VALUE)); |
| 172 | assertEquals(-Double.MAX_VALUE, $opt$NegDouble(Double.MAX_VALUE)); |
| 173 | assertEquals(Double.MAX_VALUE, $opt$NegDouble(-Double.MAX_VALUE)); |
| 174 | |
| 175 | assertEquals(Double.NEGATIVE_INFINITY, $opt$NegDouble(Double.POSITIVE_INFINITY)); |
| 176 | assertEquals(Double.POSITIVE_INFINITY, $opt$NegDouble(Double.NEGATIVE_INFINITY)); |
| 177 | assertIsNaN($opt$NegDouble(Double.NaN)); |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int $opt$NegInt(int a){ |
| 181 | return -a; |
| 182 | } |
| 183 | |
| 184 | static long $opt$NegLong(long a){ |
| 185 | return -a; |
| 186 | } |
Roland Levillain | 3dbcb38 | 2014-10-28 17:30:07 +0000 | [diff] [blame] | 187 | |
| 188 | static float $opt$NegFloat(float a){ |
| 189 | return -a; |
| 190 | } |
| 191 | |
| 192 | static double $opt$NegDouble(double a){ |
| 193 | return -a; |
| 194 | } |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 195 | } |