Refactor Jdk11TimeTest

- Split test
- avoid multiple compilation

Bug: b/270029504
Change-Id: I984b0bdca51b61f324bc122f22c7ef88e1520b9e
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeAbstractTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeAbstractTests.java
index 3e0dc37..63ccb30 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeAbstractTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeAbstractTests.java
@@ -33,6 +33,7 @@
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 import org.junit.BeforeClass;
@@ -43,6 +44,7 @@
 @RunWith(Parameterized.class)
 public abstract class Jdk11TimeAbstractTests extends DesugaredLibraryTestBase {
 
+  private static final int SPLIT = 2;
   private static final Path JDK_11_TCK_TEST_FILES_DIR =
       Paths.get(ToolHelper.JDK_11_TIME_TESTS_DIR).resolve("tck");
   private static final Path JDK_11_TIME_TEST_FILES_DIR =
@@ -211,7 +213,48 @@
         "test.java.time.chrono.TestUmmAlQuraChronology",
       };
 
-  void testTime(String[] toRun) throws Exception {
+  public String[] getFormatChronoSuccesses() {
+    List<String> allTests = new ArrayList<>();
+    Collections.addAll(allTests, FORMAT_CHRONO_SUCCESSES);
+    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
+      // Formatting issues starting from 12.
+      Collections.addAll(allTests, FORMAT_CHRONO_SUCCESSES_UP_TO_11);
+    }
+    return allTests.toArray(new String[0]);
+  }
+
+  public String[] getRawTemporalSuccesses() {
+    List<String> allTests = new ArrayList<>();
+    Collections.addAll(allTests, RAW_TEMPORAL_SUCCESSES);
+    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
+      // In 12 some ISO is supported that other versions do not support.
+      Collections.addAll(allTests, RAW_TEMPORAL_SUCCESSES_UP_TO_11);
+    }
+    // The bridge is always present with JDK11 due to partial desugaring between 26 and 33.
+    // On JDK8 the bridge is absent in between 26 and 33.
+    if (libraryDesugaringSpecification != JDK8
+        || !parameters.getApiLevel().betweenBothIncluded(AndroidApiLevel.O, AndroidApiLevel.Sv2)) {
+      Collections.addAll(allTests, RAW_TEMPORAL_SUCCESSES_IF_BRIDGE);
+    }
+    return allTests.toArray(new String[0]);
+  }
+
+  String[] split(String[] input, int index) {
+    return split(input, index, SPLIT);
+  }
+
+  private String[] split(String[] input, int index, int split) {
+    assert index >= 0 && index < split;
+    Arrays.sort(input);
+    int length = input.length;
+    int start = index * length / split + (index == 0 ? 0 : 1);
+    int last = (index + 1) * length / split;
+    return Arrays.copyOfRange(input, start, last);
+  }
+
+  void compileAndTestTime(String[] toRun) throws Exception {
+    // The compilation time is significantly higher than the test time, it is important to compile
+    // once and test multiple times on the same artifact for test performance.
     String verbosity = "2";
     DesugaredLibraryTestCompileResult<?> compileResult =
         testForDesugaredLibrary(
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono0Tests.java
similarity index 66%
copy from src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
copy to src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono0Tests.java
index 772308e..2e8655c 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono0Tests.java
@@ -1,11 +1,10 @@
-// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
 package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
 
 import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
 import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
 import org.junit.Test;
@@ -13,9 +12,9 @@
 import org.junit.runners.Parameterized;
 
 @RunWith(Parameterized.class)
-public class Jdk11TimeFormatChronoTests extends Jdk11TimeAbstractTests {
+public class Jdk11TimeFormatChrono0Tests extends Jdk11TimeAbstractTests {
 
-  public Jdk11TimeFormatChronoTests(
+  public Jdk11TimeFormatChrono0Tests(
       TestParameters parameters,
       LibraryDesugaringSpecification libraryDesugaringSpecification,
       CompilationSpecification compilationSpecification) {
@@ -24,10 +23,6 @@
 
   @Test
   public void testTime() throws Exception {
-    testTime(FORMAT_CHRONO_SUCCESSES);
-    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
-      // Formatting issues starting from 12.
-      testTime(FORMAT_CHRONO_SUCCESSES_UP_TO_11);
-    }
+    compileAndTestTime(split(getFormatChronoSuccesses(), 0));
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono1Tests.java
similarity index 66%
copy from src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
copy to src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono1Tests.java
index 772308e..6166599 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChrono1Tests.java
@@ -1,11 +1,10 @@
-// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
 package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
 
 import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
 import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
 import org.junit.Test;
@@ -13,9 +12,9 @@
 import org.junit.runners.Parameterized;
 
 @RunWith(Parameterized.class)
-public class Jdk11TimeFormatChronoTests extends Jdk11TimeAbstractTests {
+public class Jdk11TimeFormatChrono1Tests extends Jdk11TimeAbstractTests {
 
-  public Jdk11TimeFormatChronoTests(
+  public Jdk11TimeFormatChrono1Tests(
       TestParameters parameters,
       LibraryDesugaringSpecification libraryDesugaringSpecification,
       CompilationSpecification compilationSpecification) {
@@ -24,10 +23,6 @@
 
   @Test
   public void testTime() throws Exception {
-    testTime(FORMAT_CHRONO_SUCCESSES);
-    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
-      // Formatting issues starting from 12.
-      testTime(FORMAT_CHRONO_SUCCESSES_UP_TO_11);
-    }
+    compileAndTestTime(split(getFormatChronoSuccesses(), 1));
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal0Tests.java
similarity index 66%
copy from src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
copy to src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal0Tests.java
index 772308e..bce8804 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal0Tests.java
@@ -1,11 +1,10 @@
-// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
 package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
 
 import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
 import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
 import org.junit.Test;
@@ -13,9 +12,9 @@
 import org.junit.runners.Parameterized;
 
 @RunWith(Parameterized.class)
-public class Jdk11TimeFormatChronoTests extends Jdk11TimeAbstractTests {
+public class Jdk11TimeRawTemporal0Tests extends Jdk11TimeAbstractTests {
 
-  public Jdk11TimeFormatChronoTests(
+  public Jdk11TimeRawTemporal0Tests(
       TestParameters parameters,
       LibraryDesugaringSpecification libraryDesugaringSpecification,
       CompilationSpecification compilationSpecification) {
@@ -24,10 +23,6 @@
 
   @Test
   public void testTime() throws Exception {
-    testTime(FORMAT_CHRONO_SUCCESSES);
-    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
-      // Formatting issues starting from 12.
-      testTime(FORMAT_CHRONO_SUCCESSES_UP_TO_11);
-    }
+    compileAndTestTime(split(getRawTemporalSuccesses(), 0));
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal1Tests.java
similarity index 66%
rename from src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
rename to src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal1Tests.java
index 772308e..7e3459f 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeFormatChronoTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporal1Tests.java
@@ -1,11 +1,10 @@
-// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
 package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
 
 import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
 import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
 import org.junit.Test;
@@ -13,9 +12,9 @@
 import org.junit.runners.Parameterized;
 
 @RunWith(Parameterized.class)
-public class Jdk11TimeFormatChronoTests extends Jdk11TimeAbstractTests {
+public class Jdk11TimeRawTemporal1Tests extends Jdk11TimeAbstractTests {
 
-  public Jdk11TimeFormatChronoTests(
+  public Jdk11TimeRawTemporal1Tests(
       TestParameters parameters,
       LibraryDesugaringSpecification libraryDesugaringSpecification,
       CompilationSpecification compilationSpecification) {
@@ -24,10 +23,6 @@
 
   @Test
   public void testTime() throws Exception {
-    testTime(FORMAT_CHRONO_SUCCESSES);
-    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
-      // Formatting issues starting from 12.
-      testTime(FORMAT_CHRONO_SUCCESSES_UP_TO_11);
-    }
+    compileAndTestTime(split(getRawTemporalSuccesses(), 1));
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporalTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporalTests.java
deleted file mode 100644
index aa1f537..0000000
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeRawTemporalTests.java
+++ /dev/null
@@ -1,42 +0,0 @@
-// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
-
-import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK8;
-
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.ToolHelper.DexVm.Version;
-import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
-import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
-import com.android.tools.r8.utils.AndroidApiLevel;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-
-@RunWith(Parameterized.class)
-public class Jdk11TimeRawTemporalTests extends Jdk11TimeAbstractTests {
-
-  public Jdk11TimeRawTemporalTests(
-      TestParameters parameters,
-      LibraryDesugaringSpecification libraryDesugaringSpecification,
-      CompilationSpecification compilationSpecification) {
-    super(parameters, libraryDesugaringSpecification, compilationSpecification);
-  }
-
-  @Test
-  public void testTime() throws Exception {
-    testTime(RAW_TEMPORAL_SUCCESSES);
-    if (parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)) {
-      // In 12 some ISO is supported that other versions do not support.
-      testTime(RAW_TEMPORAL_SUCCESSES_UP_TO_11);
-    }
-    // The bridge is always present with JDK11 due to partial desugaring between 26 and 33.
-    // On JDK8 the bridge is absent in between 26 and 33.
-    if (libraryDesugaringSpecification != JDK8
-        || !parameters.getApiLevel().betweenBothIncluded(AndroidApiLevel.O, AndroidApiLevel.Sv2)) {
-      testTime(RAW_TEMPORAL_SUCCESSES_IF_BRIDGE);
-    }
-  }
-}