Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame^] | 1 | // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package backport; |
| 6 | |
| 7 | import com.android.tools.r8.TestParameters; |
| 8 | import com.android.tools.r8.TestRuntime.CfVm; |
| 9 | import com.android.tools.r8.desugar.backports.AbstractBackportTest; |
| 10 | import com.android.tools.r8.utils.AndroidApiLevel; |
| 11 | import org.junit.runner.RunWith; |
| 12 | import org.junit.runners.Parameterized; |
| 13 | import org.junit.runners.Parameterized.Parameters; |
| 14 | |
| 15 | @RunWith(Parameterized.class) |
| 16 | public final class ShortBackportJava9Test extends AbstractBackportTest { |
| 17 | @Parameters(name = "{0}") |
| 18 | public static Iterable<?> data() { |
| 19 | return getTestParameters() |
| 20 | .withCfRuntimesStartingFromIncluding(CfVm.JDK9) |
| 21 | .withDexRuntimes() |
| 22 | .withAllApiLevelsAlsoForCf() |
| 23 | .build(); |
| 24 | } |
| 25 | |
| 26 | public ShortBackportJava9Test(TestParameters parameters) { |
| 27 | super(parameters, Short.class, ShortBackportJava9Main.class); |
| 28 | |
| 29 | // Short.compareUnsigned added in API 31. |
| 30 | registerTarget(AndroidApiLevel.S, 16); |
| 31 | } |
| 32 | |
| 33 | public static class ShortBackportJava9Main { |
| 34 | private static final byte MIN_UNSIGNED_VALUE = (short) 0; |
| 35 | private static final byte MAX_UNSIGNED_VALUE = (short) -1; |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | testCompareUnsigned(); |
| 39 | } |
| 40 | |
| 41 | private static void testCompareUnsigned() { |
| 42 | assertTrue(Short.compareUnsigned(MIN_UNSIGNED_VALUE, MIN_UNSIGNED_VALUE) == 0); |
| 43 | assertTrue(Short.compareUnsigned(MIN_UNSIGNED_VALUE, Short.MAX_VALUE) < 0); |
| 44 | assertTrue(Short.compareUnsigned(MIN_UNSIGNED_VALUE, Short.MIN_VALUE) < 0); |
| 45 | assertTrue(Short.compareUnsigned(MIN_UNSIGNED_VALUE, MAX_UNSIGNED_VALUE) < 0); |
| 46 | |
| 47 | assertTrue(Short.compareUnsigned(Short.MAX_VALUE, MIN_UNSIGNED_VALUE) > 0); |
| 48 | assertTrue(Short.compareUnsigned(Short.MAX_VALUE, Short.MAX_VALUE) == 0); |
| 49 | assertTrue(Short.compareUnsigned(Short.MAX_VALUE, Short.MIN_VALUE) < 0); |
| 50 | assertTrue(Short.compareUnsigned(Short.MAX_VALUE, MAX_UNSIGNED_VALUE) < 0); |
| 51 | |
| 52 | assertTrue(Short.compareUnsigned(Short.MIN_VALUE, MIN_UNSIGNED_VALUE) > 0); |
| 53 | assertTrue(Short.compareUnsigned(Short.MIN_VALUE, Short.MAX_VALUE) > 0); |
| 54 | assertTrue(Short.compareUnsigned(Short.MIN_VALUE, Short.MIN_VALUE) == 0); |
| 55 | assertTrue(Short.compareUnsigned(Short.MIN_VALUE, MAX_UNSIGNED_VALUE) < 0); |
| 56 | |
| 57 | assertTrue(Short.compareUnsigned(MAX_UNSIGNED_VALUE, MIN_UNSIGNED_VALUE) > 0); |
| 58 | assertTrue(Short.compareUnsigned(MAX_UNSIGNED_VALUE, Short.MAX_VALUE) > 0); |
| 59 | assertTrue(Short.compareUnsigned(MAX_UNSIGNED_VALUE, Short.MIN_VALUE) > 0); |
| 60 | assertTrue(Short.compareUnsigned(MAX_UNSIGNED_VALUE, MAX_UNSIGNED_VALUE) == 0); |
| 61 | } |
| 62 | |
| 63 | private static void assertTrue(boolean value) { |
| 64 | if (!value) { |
| 65 | throw new AssertionError("Expected <true> but was <false>"); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |