import junit.framework.TestCase; import org.infinispan.commons.hash.MurmurHash3; import org.infinispan.util.TimSort; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; /* * Copyright (C) 2012 eXo Platform SAS. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ /** * @author Nicolas Filotto * @version $Id$ * */ public class TestSort extends TestCase { private static final Object[] sorted = {new MyClass("eba5a419c0a8010335b4955eb93af680"), new MyClass("eba5a419c0a8010360a883f7e28db72a[http://www.jcp.org/jcr/1.0]primaryType:1PROPERTY"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("eba5a419c0a8010335b4955eb93af680"), new MyClass("eba5a419c0a8010346b8b39050146684"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("00exo0jcr0root0uuid0000000000000[]reindex_test:1NODE"), new MyClass("eba5a419c0a80103093445685cb11408"), new MyClass("eba5a419c0a8010335b4955eb93af680[http://www.jcp.org/jcr/1.0]primaryType:1PROPERTY"), new MyClass("eba5a419c0a80103093445685cb11408"), new MyClass("00exo0jcr0root0uuid0000000000000"), new MyClass("eba5a419c0a80103093445685cb11408[http://www.jcp.org/jcr/1.0]primaryType:1PROPERTY"), new MyClass("eba5a416c0a801031df214fd06357c25"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("eba5a419c0a8010335b4955eb93af680[]sub1.1:1NODE"), new MyClass("eba5a419c0a80103093445685cb11408"), new MyClass("eba5a419c0a8010335b4955eb93af680"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("eba5a419c0a8010362ba69dd7c08b9db"), new MyClass("eba5a419c0a8010335b4955eb93af680"), new MyClass("eba5a416c0a801037fc94ebc3a5d4c9f"), new MyClass("eba5a419c0a8010360a883f7e28db72a[]sub1:1NODE"), new MyClass("eba5a419c0a80103093445685cb11408"), new MyClass("eba5a416c0a801031df214fd06357c25"), new MyClass("00exo0jcr0root0uuid0000000000000"), new MyClass("eba5a416c0a801031df214fd06357c25"), new MyClass("eba5a416c0a801031df214fd06357c25"), new MyClass("eba5a419c0a80103093445685cb11408"), new MyClass("eba5a419c0a8010360a883f7e28db72a"), new MyClass("eba5a416c0a801031df214fd06357c25"), new MyClass("eba5a419c0a80103661be6cb1b767b0c")}; public void testSortWithSimpleComparator() throws Exception { Comparator keyComparator = new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable)o1).compareTo(o2); } }; testSort(keyComparator); } public void testSortWithMurmurHash3() throws Exception { Comparator keyComparator = new Comparator() { private final MurmurHash3 hash = new MurmurHash3(); public int compare(Object o1, Object o2) { return hash.hash(o1) - hash.hash(o2); } }; testSort(keyComparator); } private static void testSort(Comparator keyComparator) { for (int i = 0; i < 1000000; i++) { List values = Arrays.asList(sorted); Collections.shuffle(values); try { TimSort.sort(values.toArray(new Object[]{}), keyComparator); } catch (Exception e) { System.err.println("Failed with :"); for (Object o : values) { System.err.println(o); } fail("It fails with at least the array printed into the console, with the following error message: " + e.getMessage()); } } } public static class MyClass implements Comparable { private final String value; protected MyClass(String value) { super(); this.value = value; } /** * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((value == null) ? 0 : value.hashCode()); return result; } /** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MyClass other = (MyClass)obj; if (value == null) { if (other.value != null) return false; } else if (!value.equals(other.value)) return false; return true; } public String toString() { return value; } /** * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(MyClass myClass) { return value.compareTo(myClass.value); } } }