blob: 7404b9b28d5f01e011c15823d64d0e0ea46c6074 [file] [log] [blame]
Ningsheng Jiana262f772014-11-25 16:48:07 +08001/*
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
17public class Main {
18
19 static int imax = Integer.MAX_VALUE;
20 static int imin = Integer.MIN_VALUE;
21 static long lmax = Long.MAX_VALUE;
22 static long lmin = Long.MIN_VALUE;
23 static CA ca;
24
25 public static void expectEquals(long expected, long result) {
26 if (expected != result) {
27 throw new Error("Expected: " + expected + ", found: " + result);
28 }
29 }
30
31 public static void expectEquals(int expected, int result) {
32 if (expected != result) {
33 throw new Error("Expected: " + expected + ", found: " + result);
34 }
35 }
36
37 public static void test_int() {
38 int result = 0;
39 int a = imax;
40 int b = imin;
41 int c = 10;
42 int d = c;
43 int tmp = 0;
44 int [] ia = new int[5];
45 for (int i = 0; i < 100; i++) {
46 tmp = i*c;
47 result += i*i;
48 result = i - tmp;
49 }
50 expectEquals(result, -891);
51
52 result = c*c + (result - c);
53 expectEquals(result, -801);
54
55 result = a + a*a;
56 expectEquals(result, -2147483648);
57
58 result = b + b*b;
59 expectEquals(result, -2147483648);
60
61 result = b - a*a;
62 expectEquals(result, 2147483647);
63
64 result = d*d;
65 d++;
66 result += result;
67 expectEquals(result, 200);
68
69 result = c*c;
70 tmp++;
71 result += result;
72 expectEquals(result, 200);
73
74 result = 0;
75 try {
76 result = c*c;
77 ia[c] = d; // array out of bound.
78 result += d;
79 } catch (Exception e) {
80 }
81 expectEquals(result, 100);
82
83 CA obj = new CA();
84 result = a*c + obj.ia;
85 expectEquals(result, 2);
86
87 result = 0;
88 obj = ca;
89 try {
90 result = a*c;
91 tmp = obj.ia;
92 result = result + tmp;
93 } catch (Exception e) {
94 }
95 expectEquals(result, -10);
96 }
97
98 public static void test_long() {
99 long result = 0;
100 long a = lmax;
101 long b = lmin;
102 long c = 10;
103 long d = c;
104 long tmp = 0;
105 int [] ia = new int[5];
106 for (long i = 0; i < 100; i++) {
107 tmp = i*c;
108 result += i*i;
109 result = i - tmp;
110 }
111 expectEquals(result, -891L);
112
113 result = c*c + (result - c);
114 expectEquals(result, -801L);
115
116 result = a + a*a;
117 expectEquals(result, -9223372036854775808L);
118
119 result = b + b*b;
120 expectEquals(result, -9223372036854775808L);
121
122 result = b - a*a;
123 expectEquals(result, 9223372036854775807L);
124
125 result = d*d;
126 d++;
127 result += result;
128 expectEquals(result, 200L);
129
130 result = c*c;
131 tmp++;
132 result += result;
133 expectEquals(result, 200L);
134
135 result = 0;
136 int index = 10;
137 try {
138 result = c*c;
139 ia[index] = 10; // array out of bound.
140 result += d;
141 } catch (Exception e) {
142 }
143 expectEquals(result, 100L);
144
145 CA obj = new CA();
146 result = a*c + obj.la;
147 expectEquals(result, 113L);
148
149 result = 0;
150 obj = ca;
151 try {
152 result = a*c;
153 tmp = obj.la;
154 result = result + tmp;
155 } catch (Exception e) {
156 }
157 expectEquals(result, -10L);
158 }
159
160 public static void main(String[] args) {
161 test_int();
162 test_long();
163 System.out.println("Done!");
164 }
165
166}
167
168class CA {
169 public int ia = 12;
170 public long la = 123L;
171}