Tadashi G. Takaoka | 9f240ad | 2017-08-31 13:08:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.settingslib.inputmethod; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.List; |
| 24 | import java.util.function.Function; |
| 25 | import java.util.function.ToIntBiFunction; |
| 26 | |
| 27 | /** |
| 28 | * Utility class to assert {@link Comparable} objects. |
| 29 | */ |
| 30 | final class ComparableUtils { |
| 31 | |
| 32 | private ComparableUtils() { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Checks whether specified items have ascending ordering. |
| 37 | * |
| 38 | * @param items objects to be checked Comparable contracts. |
| 39 | * @param compareTo function to compare two objects as {@link Comparable#compareTo(Object)}. |
| 40 | * @param name function to extract name of an object. |
| 41 | * @param <T> type that implements {@link Comparable}. |
| 42 | */ |
| 43 | static <T> void assertAscendingOrdering(final List<T> items, |
| 44 | final ToIntBiFunction<T, T> compareTo, final Function<T, String> name) { |
| 45 | for (int i = 1; i < items.size(); i++) { |
| 46 | final T x = items.get(i - 1); |
| 47 | final T y = items.get(i); |
| 48 | assertTrue(name.apply(x) + " is less than " + name.apply(y), |
| 49 | compareTo.applyAsInt(x, y) < 0); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Checks whether specified items have the same ordering. |
| 55 | * |
| 56 | * @param items objects to be checked equality. |
| 57 | * @param compareTo function to compare two objects as {@link Comparable#compareTo(Object)}. |
| 58 | * @param name function to extract name of an object. |
| 59 | * @param <T> type that implements {@link Comparable}. |
| 60 | */ |
| 61 | static <T> void assertSameOrdering(final Collection<T> items, |
| 62 | final ToIntBiFunction<T, T> compareTo, final Function<T, String> name) { |
| 63 | for (final T x : items) { |
| 64 | for (final T y : items) { |
| 65 | assertTrue(name.apply(x) + " is equal to " + name.apply(y), |
| 66 | compareTo.applyAsInt(x, y) == 0); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks whether a {@link Comparable} type complies with Comparable contracts. |
| 73 | * <ul> |
| 74 | * <li>Ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.</li> |
| 75 | * <li>Ensure that the relation is transitive: |
| 76 | * (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.</li> |
| 77 | * <li>Ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), |
| 78 | * for all z.</li> |
| 79 | * </ul> |
| 80 | * |
| 81 | * @param items objects to be checked Comparable contracts. |
| 82 | * @param compareTo function to compare two objects as {@link Comparable#compareTo(Object)}. |
| 83 | * @param name function to extract name of an object. |
| 84 | * @param <T> type that implements {@link Comparable}. |
| 85 | */ |
| 86 | static <T> void assertComparableContracts(final Collection<T> items, |
| 87 | final ToIntBiFunction<T, T> compareTo, final Function<T, String> name) { |
| 88 | for (final T x : items) { |
| 89 | final String nameX = name.apply(x); |
| 90 | assertTrue("Reflective: " + nameX + " is equal to itself", |
| 91 | compareTo.applyAsInt(x, x) == 0); |
| 92 | for (final T y : items) { |
| 93 | final String nameY = name.apply(y); |
| 94 | assertEquals("Asymmetric: " + nameX + " and " + nameY, |
| 95 | Integer.signum(compareTo.applyAsInt(x, y)), |
| 96 | -Integer.signum(compareTo.applyAsInt(y, x))); |
| 97 | for (final T z : items) { |
| 98 | final String nameZ = name.apply(z); |
| 99 | if (compareTo.applyAsInt(x, y) > 0 && compareTo.applyAsInt(y, z) > 0) { |
| 100 | assertTrue("Transitive: " + nameX + " is greater than " + nameY |
| 101 | + " and " + nameY + " is greater than " + nameZ |
| 102 | + " then " + nameX + " is greater than " + nameZ, |
| 103 | compareTo.applyAsInt(x, z) > 0); |
| 104 | } |
| 105 | if (compareTo.applyAsInt(x, y) == 0) { |
| 106 | assertEquals("Transitive: " + nameX + " and " + nameY + " is same " |
| 107 | + " then both return the same result for " + nameZ, |
| 108 | Integer.signum(compareTo.applyAsInt(x, z)), |
| 109 | Integer.signum(compareTo.applyAsInt(y, z))); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |