Randomize order of program classes in debug.

*** Demo only, DO NOT MERGE ***

BUG=

Change-Id: I69b2ab1e2811bbd3ac40b7ae120bf06b4040fcc4
diff --git a/src/main/java/com/android/tools/r8/graph/DexApplication.java b/src/main/java/com/android/tools/r8/graph/DexApplication.java
index af586db..f472085 100644
--- a/src/main/java/com/android/tools/r8/graph/DexApplication.java
+++ b/src/main/java/com/android/tools/r8/graph/DexApplication.java
@@ -26,6 +26,7 @@
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.Hashtable;
 import java.util.List;
@@ -80,6 +81,17 @@
     return classMap;
   }
 
+  // Reorder classes randomly. Note that the order of classes in program or library
+  // class collections should not matter for compilation of valid code and when running
+  // with assertions enabled we reorder the classes randomly to catch possible issues.
+  // Also note that the order may add to non-determinism in reporting errors for invalid
+  // code, but this non-determinism exists even with the same order of classes since we
+  // may process classes concurrently and fail-fast on the first error.
+  private <T> boolean reorderClasses(List<T> classes) {
+    Collections.shuffle(classes);
+    return true;
+  }
+
   public Iterable<DexProgramClass> classes() {
     List<DexProgramClass> result = new ArrayList<>();
     // Note: we ignore lazy class collection because it
@@ -89,6 +101,7 @@
         result.add(clazz.asProgramClass());
       }
     }
+    assert reorderClasses(result);
     return result;
   }
 
@@ -100,6 +113,7 @@
         result.add(clazz.asLibraryClass());
       }
     }
+    assert reorderClasses(result);
     return result;
   }
 
diff --git a/src/test/java/com/android/tools/r8/shaking/TreeShakingSpecificTest.java b/src/test/java/com/android/tools/r8/shaking/TreeShakingSpecificTest.java
index f5b2b1a..8f46572 100644
--- a/src/test/java/com/android/tools/r8/shaking/TreeShakingSpecificTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/TreeShakingSpecificTest.java
@@ -11,13 +11,16 @@
 import com.android.tools.r8.R8Command;
 import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.errors.CompilationError;
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
 import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
@@ -95,7 +98,11 @@
     actualMapping = new String(Files.readAllBytes(outputmapping), StandardCharsets.UTF_8);
     String refMapping = new String(Files.readAllBytes(
         Paths.get(EXAMPLES_DIR, "shaking1", "print-mapping.ref")), StandardCharsets.UTF_8);
-    Assert.assertEquals(refMapping, actualMapping);
+    Assert.assertEquals(sorted(refMapping), sorted(actualMapping));
   }
 
+  private static String sorted(String str) {
+    return new BufferedReader(new StringReader(str))
+        .lines().sorted().collect(Collectors.joining("\n"));
+  }
 }