1 package com.android.dx.merge;
2 
3 import com.android.dex.Dex;
4 import com.android.dex.DexIndexOverflowException;
5 
6 import java.io.File;
7 import java.util.Arrays;
8 import java.util.Random;
9 
10 /**
11  * This test tries to merge given dex files at random, a first pass at 2 by 2, followed by
12  * a second pass doing multi-way merges.
13  */
14 public class MergeTest {
15 
16   private static final int NUMBER_OF_TRIES = 1000;
17 
main(String[] args)18   public static void main(String[] args) throws Throwable {
19 
20     Random random = new Random();
21     for (int pass = 0; pass < 2; pass++) {
22       for (int i = 0; i < NUMBER_OF_TRIES; i++) {
23         // On the first pass only do 2-way merges, then do from 3 to 10 way merges
24         // but not more to avoid dex index overflow.
25         int numDex = pass == 0 ? 2 : random.nextInt(8) + 3;
26 
27         String[] fileNames = new String[numDex]; // only for the error message
28         try {
29           Dex[] dexesToMerge = new Dex[numDex];
30           for (int j = 0; j < numDex; j++) {
31             String fileName = args[random.nextInt(args.length)];
32             fileNames[j] = fileName;
33             dexesToMerge[j] = new Dex(new File(fileName));
34           }
35           new DexMerger(dexesToMerge, CollisionPolicy.KEEP_FIRST).merge();
36         } catch (DexIndexOverflowException e) {
37           // ignore index overflow
38         } catch (Throwable t) {
39           System.err.println(
40                   "Problem merging those dexes: " + Arrays.toString(fileNames));
41           throw t;
42         }
43       }
44     }
45   }
46 }
47