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