blob: 46bd3c6ac517200324b09fcb8061823fe25c1bdb [file] [log] [blame]
Calin Juravlebacfec32014-11-14 15:54:36 +00001/*
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 public static void expectEquals(int expected, int result) {
20 if (expected != result) {
21 throw new Error("Expected: " + expected + ", found: " + result);
22 }
23 }
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 expectDivisionByZero(int value) {
32 try {
33 $opt$Rem(value, 0);
34 throw new Error("Expected RuntimeException when modulo by 0");
35 } catch (java.lang.RuntimeException e) {
36 }
37 try {
38 $opt$RemZero(value);
39 throw new Error("Expected RuntimeException when modulo by 0");
40 } catch (java.lang.RuntimeException e) {
41 }
42 }
43
44 public static void expectDivisionByZero(long value) {
45 try {
46 $opt$Rem(value, 0L);
47 throw new Error("Expected RuntimeException when modulo by 0");
48 } catch (java.lang.RuntimeException e) {
49 }
50 try {
51 $opt$RemZero(value);
52 throw new Error("Expected RuntimeException when modulo by 0");
53 } catch (java.lang.RuntimeException e) {
54 }
55 }
56
57 public static void main(String[] args) {
58 rem();
59 }
60
61 public static void rem() {
62 remInt();
63 remLong();
64 }
65
66 private static void remInt() {
67 expectEquals(2, $opt$RemConst(6));
68 expectEquals(2, $opt$Rem(6, 4));
69 expectEquals(2, $opt$Rem(6, -4));
70 expectEquals(0, $opt$Rem(6, 3));
71 expectEquals(0, $opt$Rem(6, -3));
72 expectEquals(0, $opt$Rem(6, 1));
73 expectEquals(0, $opt$Rem(6, -1));
74 expectEquals(-1, $opt$Rem(-7, 3));
75 expectEquals(-1, $opt$Rem(-7, -3));
76 expectEquals(0, $opt$Rem(6, 6));
77 expectEquals(0, $opt$Rem(-6, -6));
78 expectEquals(7, $opt$Rem(7, 9));
79 expectEquals(7, $opt$Rem(7, -9));
80 expectEquals(-7, $opt$Rem(-7, 9));
81 expectEquals(-7, $opt$Rem(-7, -9));
82
83 expectEquals(0, $opt$Rem(Integer.MAX_VALUE, 1));
84 expectEquals(0, $opt$Rem(Integer.MAX_VALUE, -1));
85 expectEquals(0, $opt$Rem(Integer.MIN_VALUE, 1));
86 expectEquals(0, $opt$Rem(Integer.MIN_VALUE, -1)); // no overflow
87 expectEquals(-1, $opt$Rem(Integer.MIN_VALUE, Integer.MAX_VALUE));
88 expectEquals(Integer.MAX_VALUE, $opt$Rem(Integer.MAX_VALUE, Integer.MIN_VALUE));
89
90 expectEquals(0, $opt$Rem(0, 7));
91 expectEquals(0, $opt$Rem(0, Integer.MAX_VALUE));
92 expectEquals(0, $opt$Rem(0, Integer.MIN_VALUE));
93
94 expectDivisionByZero(0);
95 expectDivisionByZero(1);
96 expectDivisionByZero(5);
97 expectDivisionByZero(Integer.MAX_VALUE);
98 expectDivisionByZero(Integer.MIN_VALUE);
99 }
100
101 private static void remLong() {
102 expectEquals(2L, $opt$RemConst(6L));
103 expectEquals(2L, $opt$Rem(6L, 4L));
104 expectEquals(2L, $opt$Rem(6L, -4L));
105 expectEquals(0L, $opt$Rem(6L, 3L));
106 expectEquals(0L, $opt$Rem(6L, -3L));
107 expectEquals(0L, $opt$Rem(6L, 1L));
108 expectEquals(0L, $opt$Rem(6L, -1L));
109 expectEquals(-1L, $opt$Rem(-7L, 3L));
110 expectEquals(-1L, $opt$Rem(-7L, -3L));
111 expectEquals(0L, $opt$Rem(6L, 6L));
112 expectEquals(0L, $opt$Rem(-6L, -6L));
113 expectEquals(7L, $opt$Rem(7L, 9L));
114 expectEquals(7L, $opt$Rem(7L, -9L));
115 expectEquals(-7L, $opt$Rem(-7L, 9L));
116 expectEquals(-7L, $opt$Rem(-7L, -9L));
117
118 expectEquals(0L, $opt$Rem(Integer.MAX_VALUE, 1L));
119 expectEquals(0L, $opt$Rem(Integer.MAX_VALUE, -1L));
120 expectEquals(0L, $opt$Rem(Integer.MIN_VALUE, 1L));
121 expectEquals(0L, $opt$Rem(Integer.MIN_VALUE, -1L)); // no overflow
122 expectEquals(-1L, $opt$Rem(Integer.MIN_VALUE, Integer.MAX_VALUE));
123 expectEquals(Integer.MAX_VALUE, $opt$Rem(Integer.MAX_VALUE, Integer.MIN_VALUE));
124
125 expectEquals(0L, $opt$Rem(0L, 7L));
126 expectEquals(0L, $opt$Rem(0L, Integer.MAX_VALUE));
127 expectEquals(0L, $opt$Rem(0L, Integer.MIN_VALUE));
128
129 expectDivisionByZero(0L);
130 expectDivisionByZero(1L);
131 expectDivisionByZero(5L);
132 expectDivisionByZero(Integer.MAX_VALUE);
133 expectDivisionByZero(Integer.MIN_VALUE);
134 }
135
136 static int $opt$Rem(int a, int b) {
137 return a % b;
138 }
139
140 static int $opt$RemZero(int a) {
141 return a % 0;
142 }
143
144 // Modulo by literals != 0 should not generate checks.
145 static int $opt$RemConst(int a) {
146 return a % 4;
147 }
148
149 static long $opt$RemConst(long a) {
150 return a % 4L;
151 }
152
153 static long $opt$Rem(long a, long b) {
154 return a % b;
155 }
156
157 static long $opt$RemZero(long a) {
158 return a % 0L;
159 }
160}