blob: 3b25c9da3918d034aee631ea5e73a13124c89cf0 [file] [log] [blame]
Roland Levillain66ce1732014-10-23 16:38:33 +01001/*
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
17// Note that $opt$ is a marker for the optimizing compiler to ensure
18// it does compile the method.
19public class Main {
20
21 public static void expectEquals(int expected, int result) {
22 if (expected != result) {
23 throw new Error("Expected: " + expected + ", found: " + result);
24 }
25 }
26
27 public static void expectEquals(long expected, long result) {
28 if (expected != result) {
29 throw new Error("Expected: " + expected + ", found: " + result);
30 }
31 }
32
33 public static void expectEquals(float expected, float result) {
34 if (expected != result) {
35 throw new Error("Expected: " + expected + ", found: " + result);
36 }
37 }
38
39 public static void expectEquals(double expected, double result) {
40 if (expected != result) {
41 throw new Error("Expected: " + expected + ", found: " + result);
42 }
43 }
44
45 public static void expectApproxEquals(float a, float b, float maxDelta) {
46 boolean aproxEquals = (a > b)
47 ? ((a - b) < maxDelta)
48 : ((b - a) < maxDelta);
49 if (!aproxEquals) {
50 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta);
51 }
52 }
53
54 public static void expectApproxEquals(double a, double b, double maxDelta) {
55 boolean aproxEquals = (a > b)
56 ? ((a - b) < maxDelta)
57 : ((b - a) < maxDelta);
58 if (!aproxEquals) {
59 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta);
60 }
61 }
62
63 public static void expectNaN(float a) {
64 if (a == a) {
65 throw new Error("Expected NaN: " + a);
66 }
67 }
68
69 public static void expectNaN(double a) {
70 if (a == a) {
71 throw new Error("Expected NaN: " + a);
72 }
73 }
74
75 public static void main(String[] args) {
76 negInt();
77 $opt$InplaceNegOneInt(1);
78
79 negLong();
80 $opt$InplaceNegOneLong(1L);
81 }
82
83 private static void negInt() {
84 expectEquals(-1, $opt$NegInt(1));
85 expectEquals(1, $opt$NegInt(-1));
86 expectEquals(0, $opt$NegInt(0));
87 expectEquals(51, $opt$NegInt(-51));
88 expectEquals(-51, $opt$NegInt(51));
89 expectEquals(2147483647, $opt$NegInt(-2147483647)); // (2^31 - 1)
90 expectEquals(-2147483647, $opt$NegInt(2147483647)); // -(2^31 - 1)
91 // From the Java 7 SE Edition specification:
92 // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4
93 //
94 // For integer values, negation is the same as subtraction from
95 // zero. The Java programming language uses two's-complement
96 // representation for integers, and the range of two's-complement
97 // values is not symmetric, so negation of the maximum negative
98 // int or long results in that same maximum negative number.
99 // Overflow occurs in this case, but no exception is thrown.
100 // For all integer values x, -x equals (~x)+1.''
101 expectEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31)
102 }
103
104 private static void $opt$InplaceNegOneInt(int a) {
105 a = -a;
106 expectEquals(-1, a);
107 }
108
109 private static void negLong() {
110 expectEquals(-1L, $opt$NegLong(1L));
111 expectEquals(1L, $opt$NegLong(-1L));
112 expectEquals(0L, $opt$NegLong(0L));
113 expectEquals(51L, $opt$NegLong(-51L));
114 expectEquals(-51L, $opt$NegLong(51L));
115 expectEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // (2^63 - 1)
116 expectEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // -(2^63 - 1)
117 // See comment in negInt().
118 expectEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63)
119 }
120
121 private static void $opt$InplaceNegOneLong(long a) {
122 a = -a;
123 expectEquals(-1L, a);
124 }
125
126 static int $opt$NegInt(int a){
127 return -a;
128 }
129
130 static long $opt$NegLong(long a){
131 return -a;
132 }
133}