From: <gca...@us...> - 2010-05-28 12:49:40
|
Revision: 2583 http://openutils.svn.sourceforge.net/openutils/?rev=2583&view=rev Author: gcatania Date: 2010-05-28 12:49:34 +0000 (Fri, 28 May 2010) Log Message: ----------- TEST-1 added sort asserts, unit tests Modified Paths: -------------- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java Modified: trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java =================================================================== --- trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-05-28 10:33:58 UTC (rev 2582) +++ trunk/openutils-testing/src/main/java/it/openutils/testing/collections/CollectionAssert.java 2010-05-28 12:49:34 UTC (rev 2583) @@ -28,8 +28,10 @@ import it.openutils.testing.AssertFormatter; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -231,6 +233,195 @@ } /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code e1.compareTo(e2) == 0} are + * found, the assertion will fail + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, boolean strict, String message) + throws AssertionError, NullPointerException + { + assertNotNull(c, message); + if (c.size() < 2) + { + return; + } + Iterator<T> i = c.iterator(); + T previous = i.next(); + do + { + T next = i.next(); + int compare = next.compareTo(previous); + if (compare < 0) + { + throw assertionFailed( + message, + "{0} is not sorted: element {1} is greater than element {2} but occurs first.", + capitalizedTypeName(c), + previous, + next); + } + if (strict && compare == 0) + { + throw assertionFailed( + message, + "{0} is not strictly sorted: element {1} compares equally to element {2}.", + capitalizedTypeName(c), + previous, + next); + } + previous = next; + } + while (i.hasNext()); + + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code e1.compareTo(e2) == 0} are + * found, the assertion will fail + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, boolean strict) throws AssertionError, + NullPointerException + { + assertSorted(c, strict, null); + } + + /** + * Asserts that a collection is loosely sorted, i.e that for every two subsequent elements {@code e1} and {@code e2} + * in the collection, {@code e1.compareTo(e2) >=0} is true. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c, String message) throws AssertionError, + NullPointerException + { + assertSorted(c, false, message); + } + + /** + * Asserts that a collection is loosely sorted, i.e that for every two subsequent elements {@code e1} and {@code e2} + * in the collection, {@code e1.compareTo(e2) >=0} is true. + * @param <T> the type of elements in the collection, must be comparable + * @param c the collection + * @throws AssertionError if assertion fails + * @throws NullPointerException if any element in the input collection is null + */ + public static <T extends Comparable<T>> void assertSorted(Collection<T> c) throws AssertionError, + NullPointerException + { + assertSorted(c, false, null); + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code comparator.compare(e1, e2) == + * 0} are found, the assertion will fail + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, boolean strict, String message) + throws AssertionError + { + assertNotNull(c, message); + if (c.size() < 2) + { + return; + } + Iterator<T> i = c.iterator(); + T previous = i.next(); + do + { + T next = i.next(); + int compare = comparator.compare(previous, next); + if (compare < 0) + { + throw assertionFailed( + message, + "{0} is not sorted according to {3}: element {1} is greater than element {2} but occurs first.", + capitalizedTypeName(c), + previous, + next, + comparator); + } + if (strict && compare == 0) + { + throw assertionFailed( + message, + "{0} is not strictly sorted according to {3}: element {1} compares equally to element {2}.", + capitalizedTypeName(c), + previous, + next, + comparator); + } + previous = next; + } + while (i.hasNext()); + + } + + /** + * Asserts that a collection is sorted. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param strict if true and two elements {@code e1} and {@code e2} such that {@code comparator.compare(e1, e2) == + * 0} are found, the assertion will fail + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, boolean strict) + throws AssertionError + { + assertSorted(c, comparator, strict, null); + } + + /** + * Asserts that a collection is loosely sorted, i.e. that for every two subsequent elements {@code e1} and {@code + * e2} in the collection, {@code comparator.compare(e1, e2) >= 0} is true. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @param message a custom error message + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator, String message) + throws AssertionError + { + assertSorted(c, comparator, false, message); + } + + /** + * Asserts that a collection is loosely sorted, i.e. that for every two subsequent elements {@code e1} and {@code + * e2} in the collection, {@code comparator.compare(e1, e2) >= 0} is true. + * @param <T> the type of elements in the collection + * @param c the collection + * @param comparator defines an ordering on the collection's element + * @throws AssertionError if assertion fails + * @throws any exception thrown by {@code comparator} when comparing elements of the input collection + */ + public static <T> void assertSorted(Collection<T> c, Comparator<T> comparator) throws AssertionError + { + assertSorted(c, comparator, false, null); + } + + /** * internal utility method to retrieve the occurrences of distinct objects in a collection. * @param c the collection * @return a map between objects and their occurrences in the collection Modified: trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java =================================================================== --- trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-28 10:33:58 UTC (rev 2582) +++ trunk/openutils-testing/src/test/java/it/openutils/testing/collections/CollectionAssertTest.java 2010-05-28 12:49:34 UTC (rev 2583) @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.LinkedList; import java.util.Stack; @@ -43,18 +44,27 @@ public class CollectionAssertTest { - private static final Collection< ? > empty = Collections.EMPTY_LIST; + private final Collection< ? > empty = Collections.EMPTY_LIST; - private static final Collection< ? > singleton = Collections.singleton("alone"); + private final Collection< ? > singleton = Collections.singleton("alone"); - private static final Collection<Object> couple = new Stack<Object>(); + private final Collection<Object> couple = new Stack<Object>(); - private static final Collection<Object> tripleWithNull = new HashSet<Object>(3, 1f); + private final Collection<Object> tripleWithNull = new HashSet<Object>(3, 1f); - private static final Collection<Object> quadrupleWithRepeats = new LinkedList<Object>(); + private final Collection<Object> quadrupleWithRepeats = new LinkedList<Object>(); - static + private final Comparator<String> secondCharComparator = new Comparator<String>() { + + public int compare(String o1, String o2) + { + return new Character(o2.charAt(1)).compareTo(o1.charAt(1)); + } + }; + + public CollectionAssertTest() + { couple.add("first"); couple.add("second"); tripleWithNull.add("first"); @@ -216,4 +226,61 @@ CollectionAssert.assertEquals(c("one", "two", "three", null, "three"), quadrupleWithRepeats); } + @Test + public void testAssertSortedSuccess() + { + CollectionAssert.assertSorted(Collections.<Integer> emptyList(), true); + CollectionAssert.assertSorted(c(23), true); + CollectionAssert.assertSorted(c(11, 32, 77, 99), true); + CollectionAssert.assertSorted(c(-8d, 3d, 4d, 4d, 77.3d), false); + CollectionAssert.assertSorted(c("abacus", "banana", "choir", "dandy", "dedalus", "zebra"), true); + CollectionAssert.assertSorted(c("", "a", "aa", "z", "zz"), true); + CollectionAssert.assertSorted(c("", "a", "a", "z", "zz"), false); + + CollectionAssert.assertSorted(c("z2a", "a3z", "341", "y59"), secondCharComparator, true); + CollectionAssert.assertSorted(c("z2a", "a3z", "331", "y59"), secondCharComparator, false); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNull() + { + CollectionAssert.assertSorted((Collection<String>) null, true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted1() + { + CollectionAssert.assertSorted(c(11, 32, 99, 77), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted2() + { + CollectionAssert.assertSorted(c("abacus", "banana", "dandy", "choir", "dedalus", "zebra"), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotSorted3() + { + CollectionAssert.assertSorted(c("z2a", "a3z", "311", "y59"), secondCharComparator, false); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict1() + { + CollectionAssert.assertSorted(c(1, 2, 3, 4, 5, 5, 6, 7, 8), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict2() + { + CollectionAssert.assertSorted(c("abacus", "banana", "choir", "dandy", "dandy", "dedalus", "zebra"), true); + } + + @Test(expectedExceptions = AssertionError.class) + public void testAssertSortedFailsForNotStrict3() + { + CollectionAssert.assertSorted(c("z2a", "a3z", "321", "y59"), secondCharComparator, false); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |