test/java/util/Comparators/BasicTest.java

Print this page
rev 6853 : 8010279: java.util.Stream.min/max((Comparator)null) is not consistent in throwing (unspecified) NPE


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8001667
  27  * @run testng ComparatorsTest
  28  */
  29 
  30 import java.util.Comparator;
  31 import java.util.Comparators;
  32 import java.util.AbstractMap;
  33 import java.util.Map;
  34 import org.testng.annotations.Test;
  35 
  36 import java.util.function.Function;
  37 import java.util.function.ToIntFunction;
  38 import java.util.function.ToLongFunction;
  39 import java.util.function.ToDoubleFunction;
  40 
  41 import static org.testng.Assert.assertEquals;
  42 import static org.testng.Assert.assertTrue;
  43 import static org.testng.Assert.assertSame;
  44 
  45 /**
  46  * Unit tests for helper methods in Comparators
  47  */
  48 @Test(groups = "unit")
  49 public class ComparatorsTest {
  50     private static class Thing {
  51         public final int intField;
  52         public final long longField;
  53         public final double doubleField;
  54         public final String stringField;
  55 
  56         private Thing(int intField, long longField, double doubleField, String stringField) {
  57             this.intField = intField;
  58             this.longField = longField;
  59             this.doubleField = doubleField;
  60             this.stringField = stringField;
  61         }
  62 
  63         public int getIntField() {
  64             return intField;
  65         }
  66 
  67         public long getLongField() {
  68             return longField;
  69         }


  78     }
  79 
  80     private final int[] intValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  81     private final long[] longValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  82     private final double[] doubleValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  83     private final String[] stringValues = { "a", "a", "b", "b", "c", "c", "d", "d", "e", "e" };
  84     private final int[] comparisons = { 0, -1, 0, -1, 0, -1, 0, -1, 0 };
  85 
  86     private<T> void assertComparisons(T[] things, Comparator<T> comp, int[] comparisons) {
  87         for (int i=0; i<comparisons.length; i++) {
  88             assertEquals(comparisons.length + 1, things.length);
  89             assertEquals(comparisons[i], comp.compare(things[i], things[i+1]));
  90             assertEquals(-comparisons[i], comp.compare(things[i+1], things[i]));
  91         }
  92     }
  93 
  94     public void testIntComparator() {
  95         Thing[] things = new Thing[intValues.length];
  96         for (int i=0; i<intValues.length; i++)
  97             things[i] = new Thing(intValues[i], 0L, 0.0, null);
  98         Comparator<Thing> comp = Comparators.comparing(new ToIntFunction<ComparatorsTest.Thing>() {
  99             @Override
 100             public int applyAsInt(Thing thing) {
 101                 return thing.getIntField();
 102             }
 103         });
 104 
 105         assertComparisons(things, comp, comparisons);
 106     }
 107 
 108     public void testLongComparator() {
 109         Thing[] things = new Thing[longValues.length];
 110         for (int i=0; i<longValues.length; i++)
 111             things[i] = new Thing(0, longValues[i], 0.0, null);
 112         Comparator<Thing> comp = Comparators.comparing(new ToLongFunction<ComparatorsTest.Thing>() {
 113             @Override
 114             public long applyAsLong(Thing thing) {
 115                 return thing.getLongField();
 116             }
 117         });
 118 
 119         assertComparisons(things, comp, comparisons);
 120     }
 121 
 122     public void testDoubleComparator() {
 123         Thing[] things = new Thing[doubleValues.length];
 124         for (int i=0; i<doubleValues.length; i++)
 125             things[i] = new Thing(0, 0L, doubleValues[i], null);
 126         Comparator<Thing> comp = Comparators.comparing(new ToDoubleFunction<ComparatorsTest.Thing>() {
 127             @Override
 128             public double applyAsDouble(Thing thing) {
 129                 return thing.getDoubleField();
 130             }
 131         });
 132 
 133         assertComparisons(things, comp, comparisons);
 134     }
 135 
 136     public void testComparing() {
 137         Thing[] things = new Thing[doubleValues.length];
 138         for (int i=0; i<doubleValues.length; i++)
 139             things[i] = new Thing(0, 0L, 0.0, stringValues[i]);
 140         Comparator<Thing> comp = Comparators.comparing(new Function<Thing, String>() {
 141             @Override
 142             public String apply(Thing thing) {
 143                 return thing.getStringField();
 144             }
 145         });
 146 




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8001667
  27  * @run testng BasicTest
  28  */
  29 
  30 import java.util.Comparator;
  31 import java.util.Comparators;
  32 import java.util.AbstractMap;
  33 import java.util.Map;
  34 import org.testng.annotations.Test;
  35 
  36 import java.util.function.Function;
  37 import java.util.function.ToIntFunction;
  38 import java.util.function.ToLongFunction;
  39 import java.util.function.ToDoubleFunction;
  40 
  41 import static org.testng.Assert.assertEquals;
  42 import static org.testng.Assert.assertTrue;
  43 import static org.testng.Assert.assertSame;
  44 
  45 /**
  46  * Unit tests for helper methods in Comparators
  47  */
  48 @Test(groups = "unit")
  49 public class BasicTest {
  50     private static class Thing {
  51         public final int intField;
  52         public final long longField;
  53         public final double doubleField;
  54         public final String stringField;
  55 
  56         private Thing(int intField, long longField, double doubleField, String stringField) {
  57             this.intField = intField;
  58             this.longField = longField;
  59             this.doubleField = doubleField;
  60             this.stringField = stringField;
  61         }
  62 
  63         public int getIntField() {
  64             return intField;
  65         }
  66 
  67         public long getLongField() {
  68             return longField;
  69         }


  78     }
  79 
  80     private final int[] intValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  81     private final long[] longValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  82     private final double[] doubleValues = { -2, -2, -1, -1, 0, 0, 1, 1, 2, 2 };
  83     private final String[] stringValues = { "a", "a", "b", "b", "c", "c", "d", "d", "e", "e" };
  84     private final int[] comparisons = { 0, -1, 0, -1, 0, -1, 0, -1, 0 };
  85 
  86     private<T> void assertComparisons(T[] things, Comparator<T> comp, int[] comparisons) {
  87         for (int i=0; i<comparisons.length; i++) {
  88             assertEquals(comparisons.length + 1, things.length);
  89             assertEquals(comparisons[i], comp.compare(things[i], things[i+1]));
  90             assertEquals(-comparisons[i], comp.compare(things[i+1], things[i]));
  91         }
  92     }
  93 
  94     public void testIntComparator() {
  95         Thing[] things = new Thing[intValues.length];
  96         for (int i=0; i<intValues.length; i++)
  97             things[i] = new Thing(intValues[i], 0L, 0.0, null);
  98         Comparator<Thing> comp = Comparators.comparing(new ToIntFunction<BasicTest.Thing>() {
  99             @Override
 100             public int applyAsInt(Thing thing) {
 101                 return thing.getIntField();
 102             }
 103         });
 104 
 105         assertComparisons(things, comp, comparisons);
 106     }
 107 
 108     public void testLongComparator() {
 109         Thing[] things = new Thing[longValues.length];
 110         for (int i=0; i<longValues.length; i++)
 111             things[i] = new Thing(0, longValues[i], 0.0, null);
 112         Comparator<Thing> comp = Comparators.comparing(new ToLongFunction<BasicTest.Thing>() {
 113             @Override
 114             public long applyAsLong(Thing thing) {
 115                 return thing.getLongField();
 116             }
 117         });
 118 
 119         assertComparisons(things, comp, comparisons);
 120     }
 121 
 122     public void testDoubleComparator() {
 123         Thing[] things = new Thing[doubleValues.length];
 124         for (int i=0; i<doubleValues.length; i++)
 125             things[i] = new Thing(0, 0L, doubleValues[i], null);
 126         Comparator<Thing> comp = Comparators.comparing(new ToDoubleFunction<BasicTest.Thing>() {
 127             @Override
 128             public double applyAsDouble(Thing thing) {
 129                 return thing.getDoubleField();
 130             }
 131         });
 132 
 133         assertComparisons(things, comp, comparisons);
 134     }
 135 
 136     public void testComparing() {
 137         Thing[] things = new Thing[doubleValues.length];
 138         for (int i=0; i<doubleValues.length; i++)
 139             things[i] = new Thing(0, 0L, 0.0, stringValues[i]);
 140         Comparator<Thing> comp = Comparators.comparing(new Function<Thing, String>() {
 141             @Override
 142             public String apply(Thing thing) {
 143                 return thing.getStringField();
 144             }
 145         });
 146