Home | History | Annotate | Download | only in testing
      1 /*
      2  * Copyright (C) 2015 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.tv.testing;
     18 
     19 import static junit.framework.Assert.assertEquals;
     20 
     21 import java.util.ArrayList;
     22 import java.util.Arrays;
     23 import java.util.Collection;
     24 import java.util.Comparator;
     25 import java.util.List;
     26 
     27 /**
     28  * Tester for {@link Comparator} relationships between groups of T.
     29  *
     30  * <p>
     31  * To use, create a new {@link ComparatorTester} and add comparable groups
     32  * where each group contains objects that are
     33  * {@link Comparator#compare(Object, Object)} == 0 to each other.
     34  * Groups are added in order asserting that all earlier groups have compare < 0
     35  * for all later groups.
     36  *
     37  * <pre>{@code
     38  * ComparatorTester
     39  *     .withoutEqualsTest(String.CASE_INSENSITIVE_ORDER)
     40  *     .addComparableGroup("Hello", "HELLO")
     41  *     .addComparableGroup("World", "wORLD")
     42  *     .addComparableGroup("ZEBRA")
     43  *     .test();
     44  * }
     45  * </pre>
     46  *
     47  * @param <T> the type of objects to compare.
     48  */
     49 public class ComparatorTester<T> {
     50 
     51     private final List<List<T>> listOfGroups = new ArrayList<>();
     52 
     53     private final Comparator<T> comparator;
     54 
     55 
     56     public static <T> ComparatorTester<T> withoutEqualsTest(Comparator<T> comparator) {
     57         return new ComparatorTester<>(comparator);
     58     }
     59 
     60     private ComparatorTester(Comparator<T> comparator) {
     61         this.comparator = comparator;
     62     }
     63 
     64     @SafeVarargs
     65     public final ComparatorTester<T> addComparableGroup(T... items) {
     66         listOfGroups.add(Arrays.asList(items));
     67         return this;
     68     }
     69 
     70     public void test() {
     71         for (int i = 0; i < listOfGroups.size(); i++) {
     72             List<T> currentGroup = listOfGroups.get(i);
     73             for (int j = 0; j < i; j++) {
     74                 List<T> lhs = listOfGroups.get(j);
     75                 assertOrder(i, j, lhs, currentGroup);
     76             }
     77             assertZero(currentGroup);
     78             for (int j = i + 1; j < listOfGroups.size(); j++) {
     79                 List<T> rhs = listOfGroups.get(j);
     80                 assertOrder(i, j, currentGroup, rhs);
     81             }
     82         }
     83         //TODO: also test equals
     84     }
     85 
     86     private void assertOrder(int less, int more, List<T> lessGroup, List<T> moreGroup) {
     87         assertLess(less, more, lessGroup, moreGroup);
     88         assertMore(more, less, moreGroup, lessGroup);
     89     }
     90 
     91     private void assertLess(int left, int right, Collection<T> leftGroup,
     92             Collection<T> rightGroup) {
     93         int leftSub = 0;
     94         for (T leftItem : leftGroup) {
     95             int rightSub = 0;
     96             for (T rightItem : rightGroup) {
     97                 String leftName = "Item[" + left + "," + (leftSub++) + "]";
     98                 String rName = "Item[" + right + "," + (rightSub++) + "]";
     99                 assertEquals(leftName + " " + leftItem + " compareTo " + rName + " " + rightItem
    100                                 + " is <0", true, comparator.compare(leftItem, rightItem) < 0);
    101             }
    102         }
    103     }
    104 
    105     private void assertMore(int left, int right, Collection<T> leftGroup,
    106             Collection<T> rightGroup) {
    107         int leftSub = 0;
    108         for (T leftItem : leftGroup) {
    109             int rightSub = 0;
    110             for (T rightItem : rightGroup) {
    111                 String leftName = "Item[" + left + "," + (leftSub++) + "]";
    112                 String rName = "Item[" + right + "," + (rightSub++) + "]";
    113                 assertEquals(leftName + " " + leftItem + " compareTo " + rName + " " + rightItem
    114                                 + " is >0", true, comparator.compare(leftItem, rightItem) > 0);
    115             }
    116         }
    117     }
    118 
    119     private void assertZero(Collection<T> group) {
    120         // Test everything against everything in both directions, including against itself.
    121         for (T leftItem : group) {
    122             for (T rightItem : group) {
    123                 assertEquals(leftItem + " compareTo " + rightItem, 0,
    124                         comparator.compare(leftItem, rightItem));
    125             }
    126         }
    127     }
    128 }