1 /*
2  * Copyright (C) 2018 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 import java.math.BigInteger;
18 
19 // This is motivated by the assumption that BigInteger allocates malloc memory
20 // underneath. That's true (in 2018) on Android.
21 
22 public class Main {
main(String[] args)23   public static void main(String[] args) throws Exception {
24     final int nIters = 20_000;  // Presumed < 1_000_000.
25     final BigInteger big2_20 = BigInteger.valueOf(1024*1024); // 2^20
26     BigInteger huge = BigInteger.valueOf(1).shiftLeft(4_000_000);  // ~0.5MB
27     for (int i = 0; i < nIters; ++i) {  // 10 GB total
28       huge = huge.add(BigInteger.ONE);
29     }
30     if (huge.bitLength() != 4_000_001) {
31       System.out.println("Wrong answer length: " + huge.bitLength());
32     } else if (huge.mod(big2_20).compareTo(BigInteger.valueOf(nIters)) != 0) {
33       System.out.println("Wrong answer: ..." + huge.mod(big2_20));
34     } else {
35       System.out.println("Test complete");
36     }
37   }
38 }
39