1 package com.tonbeller.wcf.utils; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import junit.framework.TestCase; 9 10 14 public class IndexOfComparatorTest extends TestCase { 15 public void testIndexOfComparator() { 16 assertEquals("abcd", toString(asList("abcd"))); 17 check("abcd", "abcd", "abcd"); 18 check("dcba", "abcd", "abcd"); 19 check("xbcy", "abcd", "bcxy"); 20 check("", "", ""); 21 check("abc", "", "abc"); 22 check("", "abc", ""); 23 check("dcba", "", "dcba"); 24 check("dcba", "a", "adcb"); 25 check("dcba", "ab", "abdc"); 26 check("dcba", "abc", "abcd"); 27 check("dcba", "abcd", "abcd"); 28 } 29 30 private void check(String s1, String s2, String s3) { 31 List l1 = asList(s1); 32 List l2 = asList(s2); 33 IndexOfComparator c = new IndexOfComparator(l2); 34 Collections.sort(l1, c); 35 assertEquals(s3, toString(l1)); 36 } 37 38 List asList(String s) { 39 List l = new ArrayList (); 40 char[] c = s.toCharArray(); 41 for (int i = 0;i < c.length; i++) 42 l.add(new String ("" + c[i])); 43 return l; 44 } 45 46 String toString(List l) { 47 StringBuffer sb = new StringBuffer (); 48 for (Iterator it = l.iterator(); it.hasNext();) 49 sb.append(it.next()); 50 return sb.toString(); 51 } 52 53 } 54 | Popular Tags |