Skip to content

Instantly share code, notes, and snippets.

@elbosso
Last active April 6, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbosso/98e4dd2ee817e83c1c4136f9c980f16c to your computer and use it in GitHub Desktop.
Save elbosso/98e4dd2ee817e83c1c4136f9c980f16c to your computer and use it in GitHub Desktop.
Abstract JUnit 4 Test class for implementations of the Java interface java.util.List
/*
* Copyright (c) 2020.
*
* Juergen Key. Alle Rechte vorbehalten.
*
* Weiterverbreitung und Verwendung in nichtkompilierter oder kompilierter Form,
* mit oder ohne Veraenderung, sind unter den folgenden Bedingungen zulaessig:
*
* 1. Weiterverbreitete nichtkompilierte Exemplare muessen das obige Copyright,
* die Liste der Bedingungen und den folgenden Haftungsausschluss im Quelltext
* enthalten.
* 2. Weiterverbreitete kompilierte Exemplare muessen das obige Copyright,
* die Liste der Bedingungen und den folgenden Haftungsausschluss in der
* Dokumentation und/oder anderen Materialien, die mit dem Exemplar verbreitet
* werden, enthalten.
* 3. Weder der Name des Autors noch die Namen der Beitragsleistenden
* duerfen zum Kennzeichnen oder Bewerben von Produkten, die von dieser Software
* abgeleitet wurden, ohne spezielle vorherige schriftliche Genehmigung verwendet
* werden.
*
* DIESE SOFTWARE WIRD VOM AUTOR UND DEN BEITRAGSLEISTENDEN OHNE
* JEGLICHE SPEZIELLE ODER IMPLIZIERTE GARANTIEN ZUR VERFUEGUNG GESTELLT, DIE
* UNTER ANDEREM EINSCHLIESSEN: DIE IMPLIZIERTE GARANTIE DER VERWENDBARKEIT DER
* SOFTWARE FUER EINEN BESTIMMTEN ZWECK. AUF KEINEN FALL IST DER AUTOR
* ODER DIE BEITRAGSLEISTENDEN FUER IRGENDWELCHE DIREKTEN, INDIREKTEN,
* ZUFAELLIGEN, SPEZIELLEN, BEISPIELHAFTEN ODER FOLGENDEN SCHAEDEN (UNTER ANDEREM
* VERSCHAFFEN VON ERSATZGUETERN ODER -DIENSTLEISTUNGEN; EINSCHRAENKUNG DER
* NUTZUNGSFAEHIGKEIT; VERLUST VON NUTZUNGSFAEHIGKEIT; DATEN; PROFIT ODER
* GESCHAEFTSUNTERBRECHUNG), WIE AUCH IMMER VERURSACHT UND UNTER WELCHER
* VERPFLICHTUNG AUCH IMMER, OB IN VERTRAG, STRIKTER VERPFLICHTUNG ODER
* UNERLAUBTE HANDLUNG (INKLUSIVE FAHRLAESSIGKEIT) VERANTWORTLICH, AUF WELCHEM
* WEG SIE AUCH IMMER DURCH DIE BENUTZUNG DIESER SOFTWARE ENTSTANDEN SIND, SOGAR,
* WENN SIE AUF DIE MOEGLICHKEIT EINES SOLCHEN SCHADENS HINGEWIESEN WORDEN SIND.
*
*/
package what.ever.floats.your.boat;
@org.junit.Rule
public org.junit.rules.TestName name = new org.junit.rules.TestName();
private T list;
private static java.util.Random rand;
@Ignore
@org.junit.Test(expected = IllegalArgumentException.class)
public void testExceptionIsThrown()
{
if(CLASS_LOGGER.isTraceEnabled())CLASS_LOGGER.trace(name.getMethodName());
throw new IllegalArgumentException("i want to see this!");
}
//region testing if exceptions are thrown
@org.junit.Test
public void npeTests()
{
try
{
list.toArray((java.lang.Object[])null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.forEach(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.replaceAll(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.removeIf(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.addAll(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.addAll(0,null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.removeAll(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.retainAll(null);
Assert.fail("expected NPE not thrown");
} catch (NullPointerException npe)
{
}
try
{
list.sort(null);
} catch (Throwable t)
{
t.printStackTrace();
Assert.fail("Exception not expected: " + t);
}
}
@org.junit.Test
public void aseTests()
{
try
{
list.toArray(new java.lang.String[0]);
Assert.fail("expected ArrayStoreException not thrown");
} catch (ArrayStoreException ase)
{
}
}
@Ignore
@org.junit.Test
public void cceTests()
{
try
{
//can not happen because of generics...
// list.add("this is a string");
Assert.fail("expected ClassCastException not thrown");
} catch (ClassCastException cce)
{
}
try
{
//can not happen because of generics...
// list.remove("this is a string");
Assert.fail("expected ClassCastException not thrown");
} catch (ClassCastException cce)
{
}
try
{
//can not happen because of generics...
// list.containsAll(Arrays.asList("this is a string"));
Assert.fail("expected ClassCastException not thrown");
} catch (ClassCastException cce)
{
}
try
{
//can not happen because of generics...
// list.addAll(Arrays.asList("this is a string"));
Assert.fail("expected ClassCastException not thrown");
} catch (ClassCastException cce)
{
}
}
@org.junit.Test
public void ioobeTests()
{
java.util.List l = Arrays.asList(11, 12, 13);
try
{
Assert.assertTrue(list.addAll(-2,l));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException ase)
{
}
try
{
Assert.assertTrue(list.addAll(list.size()+1,l));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException ase)
{
}
try
{
Assert.assertTrue(list.addAll(list.size()+2,l));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException ase)
{
}
try
{
list.get(-1);
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.get(list.size());
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.get(list.size()*2);
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.set(-1,java.lang.Integer.valueOf(1));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.set(list.size(),java.lang.Integer.valueOf(1));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.set(list.size()*2,java.lang.Integer.valueOf(1));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.add(-1,java.lang.Integer.valueOf(1));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.add(list.size()*2,java.lang.Integer.valueOf(1));
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.remove(-1);
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.remove(list.size());
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
list.remove(list.size()*2);
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
try
{
T ll=createInstance();
java.util.Collections.addAll(ll,11,12,13,14,15,16);
Collections.copy(ll,list);
Assert.fail("expected IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException npe)
{
}
}
//endregion testing if exceptions are thrown
//region (interface) list operations
@Test
public void toArrayTest()
{
java.lang.Object[] obja=list.toArray();
Assert.assertEquals(list.size(),obja.length);
for(int i=0;i<obja.length;++i)
{
Assert.assertNotNull(obja[i]);
Assert.assertNotNull(list.get(i));
Assert.assertEquals(list.get(i),obja[i]);
}
java.lang.Integer[] inta=new java.lang.Integer[0];
java.lang.Integer[] inta2=list.toArray(inta);
Assert.assertNotSame(inta,inta2);
Assert.assertEquals(list.size(),inta2.length);
for(int i=0;i<inta2.length;++i)
{
Assert.assertNotNull(inta2[i]);
Assert.assertNotNull(list.get(i));
Assert.assertEquals(list.get(i),inta2[i]);
}
inta=new java.lang.Integer[list.size()/2];
inta2=list.toArray(inta);
Assert.assertNotSame(inta,inta2);
Assert.assertEquals(list.size(),inta2.length);
for(int i=0;i<inta2.length;++i)
{
Assert.assertNotNull(inta2[i]);
Assert.assertNotNull(list.get(i));
Assert.assertEquals(list.get(i),inta2[i]);
}
inta=new java.lang.Integer[list.size()];
inta2=list.toArray(inta);
Assert.assertSame(inta,inta2);
Assert.assertEquals(list.size(),inta2.length);
for(int i=0;i<inta2.length;++i)
{
Assert.assertNotNull(inta2[i]);
Assert.assertNotNull(list.get(i));
Assert.assertEquals(list.get(i),inta2[i]);
}
inta=new java.lang.Integer[list.size()*2];
inta2=list.toArray(inta);
Assert.assertSame(inta,inta2);
Assert.assertEquals(list.size()*2,inta2.length);
Assert.assertNull(inta2[list.size()]);
for(int i=0;i<list.size();++i)
{
Assert.assertNotNull(inta2[i]);
Assert.assertNotNull(list.get(i));
Assert.assertEquals(list.get(i),inta2[i]);
}
}
@Test
public void containsTest()
{
Assert.assertTrue(list.contains(java.lang.Integer.valueOf(4)));
Assert.assertTrue(list.contains(4));
Assert.assertFalse(list.contains("This is a string"));
Assert.assertFalse(list.contains(java.lang.Integer.valueOf(14)));
Assert.assertFalse(list.contains(14));
Assert.assertFalse(list.contains(null));
}
@Test
public void containsAllTest()
{
java.util.List l=Arrays.asList(1,2,3);
Assert.assertTrue(list.containsAll(l));
l=Arrays.asList(1,2,3,14);
Assert.assertFalse(list.containsAll(l));
l=Arrays.asList(11,2,3);
Assert.assertFalse(list.containsAll(l));
l=Arrays.asList(11,2,3);
Assert.assertFalse(list.containsAll(l));
l=Arrays.asList(1,12,3);
Assert.assertFalse(list.containsAll(l));
l=new java.util.LinkedList();
l.add(null);
l.add(1);
Assert.assertFalse(list.containsAll(l));
}
@Test
public void addAllTest()
{
java.util.List l = Arrays.asList(11, 12, 13);
Assert.assertFalse(list.containsAll(l));
int old=list.size();
Assert.assertTrue(list.addAll(l));
Assert.assertEquals(old+l.size(),list.size());
Assert.assertTrue(list.containsAll(l));
int counter=0;
Assert.assertTrue(list.addAll(l));
for(int i=0;i<list.size();++i)
{
if(l.get(0).equals(list.get(i)))
++counter;
}
Assert.assertEquals(2,counter);
l = Arrays.asList("huhu",new java.util.Date());
Assert.assertFalse(list.containsAll(l));
//die nächsten beiden Assertions müssen true sein, weil Java Generics so funktionieren...
Assert.assertTrue(list.addAll(l));
Assert.assertTrue(list.containsAll(l));
}
@Test
public void addAllAtIndexTest()
{
java.util.List l = Arrays.asList(11, 12, 13);
Assert.assertFalse(list.containsAll(l));
int old=list.size();
Assert.assertTrue(list.addAll(3,l));
Assert.assertEquals(l.size()+old,list.size());
Assert.assertTrue(list.containsAll(l));
for(int i=0;i<l.size();++i)
{
Assert.assertEquals(l.get(i),list.get(3+i));
}
Assert.assertTrue(list.addAll(list.size(),l));
int counter=0;
for(int i=0;i<list.size();++i)
{
if(l.get(0).equals(list.get(i)))
++counter;
}
Assert.assertEquals(2,counter);
}
@Test
public void removeAllTest()
{
java.util.List l = Arrays.asList(1, 2, 3);
Assert.assertTrue(list.containsAll(l));
int old=list.size();
Assert.assertTrue(list.removeAll(l));
Assert.assertEquals(old-l.size(),list.size());
Assert.assertFalse(list.containsAll(l));
for(int i=0;i<l.size();++i)
{
Assert.assertFalse(list.contains(l.get(i)));
}
Assert.assertFalse(list.removeAll(l));
l = Arrays.asList("huhu",4,new java.util.Date());
old=list.size();
//das muss funktionieren, weil Java Generics so funktionieren...
Assert.assertTrue(list.removeAll(l));
Assert.assertEquals(old-1,list.size());
l = Arrays.asList(11, 12, 13);
Assert.assertFalse(list.removeAll(l));
}
@Test
public void retainAllTest()
{
java.util.List l = Arrays.asList(1, 2, 3,4,5,6,7);
Assert.assertTrue(list.containsAll(l));
Assert.assertTrue(list.retainAll(l));
Assert.assertEquals(l.size(),list.size());
Assert.assertTrue(list.containsAll(l));
Assert.assertFalse(list.retainAll(list));
l = Arrays.asList("huhu",4,new java.util.Date());
//das muss funktionieren, weil Java Generics so funktionieren...
Assert.assertTrue(list.retainAll(l));
Assert.assertEquals(1,list.size());
Assert.assertEquals(java.lang.Integer.valueOf(4),list.get(0));
l = Arrays.asList(11, 12, 13);
Assert.assertTrue(list.retainAll(l));
Assert.assertTrue(list.isEmpty());
}
@Test
public void equalsHashCodeTest()
{
java.util.List l1=Arrays.asList(0,1,2,3,4,5,6,7,8,9);
java.util.List l2=Arrays.asList(0,1,2,3,4,5,6,7,8,9,10);
java.util.List l3=Arrays.asList(1,2,3,4,5,6,7,8,9,0);
java.util.List l4=null;
java.util.List l5=java.util.Collections.emptyList();
Assert.assertTrue(list.equals(l1));
Assert.assertFalse(list.equals(l2));
Assert.assertFalse(list.equals(l3));
Assert.assertFalse(list.equals(l4));
Assert.assertFalse(list.equals(l5));
Assert.assertTrue(l1.equals(list));
Assert.assertFalse(l2.equals(list));
Assert.assertFalse(l3.equals(list));
//NPE! Assert.assertFalse(list.equals(l4));
Assert.assertFalse(l5.equals(list));
Assert.assertEquals(l1.hashCode(),list.hashCode());
Assert.assertNotEquals(l2.hashCode(),list.hashCode());
Assert.assertNotEquals(l3.hashCode(),list.hashCode());
//NPE! Assert.assertNotEquals(l4.hashCode(),list.hashCode());
Assert.assertNotEquals(l5.hashCode(),list.hashCode());
}
@Test
public void getTest()
{
for(int i=0;i<list.size();++i)
{
Assert.assertEquals(java.lang.Integer.valueOf(i),list.get(i));
}
}
@Test
public void setTest()
{
for(int i=0;i<list.size();++i)
{
Assert.assertEquals(java.lang.Integer.valueOf(i),list.get(i));
Assert.assertEquals(java.lang.Integer.valueOf(i),list.set(i,java.lang.Integer.valueOf(11)));
Assert.assertEquals(java.lang.Integer.valueOf(11),list.get(i));
}
}
@Test
public void addTest()
{
java.util.List before=new java.util.LinkedList(list);
Assert.assertTrue(list.add(java.lang.Integer.valueOf(list.size())));
Assert.assertEquals(list.get(list.size()-1),java.lang.Integer.valueOf(list.size()-1));
Assert.assertEquals(before,list.subList(0,list.size()-1));
Assert.assertEquals(before.size()+1,list.size());
list.add(0,java.lang.Integer.valueOf(-1));
for(int i=1;i<list.size();++i)
{
Assert.assertEquals(java.lang.Integer.valueOf(i-1),list.get(i));
}
Assert.assertEquals(java.lang.Integer.valueOf(-1),list.get(0));
list.add(list.size(), Integer.MAX_VALUE);
Assert.assertEquals((Object)Integer.MAX_VALUE,list.get(list.size()-1));
}
@Test
public void removeAtIndexTest()
{
int old=list.size();
list.add(java.lang.Integer.valueOf(2));
int counter=0;
for(int i=0;i<list.size();++i)
{
if(java.lang.Integer.valueOf(2).equals(list.get(i)))
++counter;
}
Assert.assertEquals(2,counter);
Assert.assertEquals(old+1,list.size());
Assert.assertFalse(list.remove(java.lang.Integer.valueOf(12)));
Assert.assertTrue(list.remove(java.lang.Integer.valueOf(2)));
Assert.assertEquals((java.lang.Object)java.lang.Integer.valueOf(3),list.get(2));
counter=0;
for(int i=0;i<list.size();++i)
{
if(java.lang.Integer.valueOf(2).equals(list.get(i)))
++counter;
}
Assert.assertEquals(1,counter);
}
@Test
public void isEmptyTest()
{
Assert.assertFalse(list.isEmpty()&&list.size()<1);
Assert.assertTrue(list.isEmpty()==false&&list.size()>0);
Assert.assertFalse(list.isEmpty());
list.clear();
Assert.assertTrue(list.isEmpty()&&list.size()<1);
Assert.assertFalse(list.isEmpty()==false&&list.size()>0);
Assert.assertTrue(list.isEmpty());
}
@Test
public void sizeTest()
{
Assert.assertTrue(list.size()>0);
list.clear();
Assert.assertEquals(0,list.size());
}
@Test
public void removeTest()
{
int listSize=list.size();
Assert.assertFalse(list.remove(null));
Assert.assertEquals(listSize,list.size());
Assert.assertFalse(list.remove(java.lang.Integer.valueOf(14)));
Assert.assertEquals(listSize,list.size());
Assert.assertTrue(list.remove(java.lang.Integer.valueOf(4)));
Assert.assertEquals(listSize-1,list.size());
Assert.assertEquals(java.lang.Integer.valueOf(5),list.get(4));
Assert.assertTrue(list.remove(java.lang.Integer.valueOf(7)));
Assert.assertEquals(listSize-2,list.size());
Assert.assertEquals(java.lang.Integer.valueOf(9),list.get(7));
}
@Test
public void indexOfTest()
{
list.addAll(new java.util.LinkedList(list));
Assert.assertEquals(5,list.indexOf(5));
}
@Test
public void lastIndexOfTest()
{
int listLength=list.size();
list.addAll(new java.util.LinkedList(list));
CLASS_LOGGER.trace(list);
Assert.assertEquals(listLength+5,list.lastIndexOf(5));
}
@Test
public void sublistTest()
{
int listLength=list.size();
list.addAll(new java.util.LinkedList(list));
CLASS_LOGGER.trace(list);
T l=createInstanceFromList(list);
CLASS_LOGGER.trace(l);
java.util.List<Integer> sl=l.subList(list.size()/3,list.size()*2/3);
Assert.assertEquals(list.size()*2/3-list.size()/3,sl.size());
java.util.Collections.fill(sl,11);
CLASS_LOGGER.trace(sl);
CLASS_LOGGER.trace(l);
for(int i=0;i<l.size();++i)
{
if((i<l.size()*2/3)&(i>=l.size()/3))
{
Assert.assertEquals("at index "+i,11,l.get(i).intValue());
}
else
{
Assert.assertNotEquals("at index "+i,11,l.get(i).intValue());
}
}
sl.removeAll(new java.util.LinkedList(sl));
Assert.assertTrue(sl.isEmpty());
CLASS_LOGGER.trace(l.size());
Assert.assertEquals(list.size()-(list.size()*2/3-list.size()/3),l.size());
for(int i=0;i<l.size();++i)
{
Assert.assertNotEquals("at index "+i,11,l.get(i).intValue());
}
int slLength=l.size();
sl.addAll(list);
Assert.assertEquals(list.size()+slLength,l.size());
for(int i=list.size()/3;i<list.size()/3+list.size();++i)
{
Assert.assertEquals("at index "+i,(i-list.size()/3)%listLength,l.get(i).intValue());
}
l=createInstanceFromList(list);
CLASS_LOGGER.trace(l);
sl=l.subList(list.size()/3,list.size()*2/3);
Assert.assertEquals(list.size()*2/3-list.size()/3,sl.size());
java.util.Collections.fill(sl,11);
CLASS_LOGGER.trace(sl);
CLASS_LOGGER.trace(l);
sl.clear();
Assert.assertTrue(sl.isEmpty());
CLASS_LOGGER.trace("2");
CLASS_LOGGER.trace(l.size());
Assert.assertEquals(list.size()-(list.size()*2/3-list.size()/3),l.size());
for(int i=0;i<l.size();++i)
{
Assert.assertNotEquals("at index "+i,11,l.get(i).intValue());
}
slLength=l.size();
sl.addAll(list);
Assert.assertEquals(list.size()+slLength,l.size());
for(int i=list.size()/3;i<list.size()/3+list.size();++i)
{
Assert.assertEquals("at index "+i,(i-list.size()/3)%listLength,l.get(i).intValue());
}
//indexofSublist
l=createInstanceFromList(list);
CLASS_LOGGER.trace(l);
sl=l.subList(listLength/3,listLength*2/3);
Assert.assertEquals(listLength*2/3-listLength/3,sl.size());
Assert.assertEquals(listLength/3,java.util.Collections.indexOfSubList(l,sl));
//lastIndexOfSublist
Assert.assertEquals(listLength/3+listLength,java.util.Collections.lastIndexOfSubList(l,sl));
}
//endregion (interface) list operations
//region algorithms
@Test
public void shuffleTest()
{
T l=createInstanceFromList(list);
java.util.Collections.shuffle(l);
int count=0;
for(int i=0;i<list.size();++i)
{
if(l.get(i).equals(list.get(i))==false)
++count;
}
Assert.assertTrue("count: "+count,count>0);
}
@Test
public void sortTest()
{
T l=createInstanceFromList(list);
java.util.Collections.shuffle(l);
int count=0;
for(int i=0;i<list.size();++i)
{
if(l.get(i).equals(list.get(i))==false)
++count;
}
Assert.assertTrue("count: "+count,count>0);
java.util.Collections.sort(l);
count=0;
for(int i=0;i<list.size();++i)
{
if(l.get(i).equals(list.get(i))==false)
++count;
}
Assert.assertEquals(0,count);
}
@Test
public void addAllCollectionsTest()
{
int listLength=list.size();
list.addAll(new java.util.LinkedList(list));
CLASS_LOGGER.trace(list);
T l=createInstanceFromList(list);
CLASS_LOGGER.trace(l);
java.util.List<Integer> sl=l.subList(list.size()/3,list.size()*2/3);
Assert.assertEquals(list.size()*2/3-list.size()/3,sl.size());
sl.clear();
java.util.Collections.addAll(sl,11,12,13,14,15,16);
CLASS_LOGGER.trace(sl);
CLASS_LOGGER.trace(l);
for(int i=0;i<l.size();++i)
{
if((i<l.size()/3+6)&(i>=l.size()/3))
{
Assert.assertEquals("at index "+i,11+(i-l.size()/3),l.get(i).intValue());
}
else
{
Assert.assertFalse("at index "+i+" value is "+l.get(i),l.get(i).intValue()>10);
}
}
}
@Test
public void binarySearchTest()
{
for(int i=0;i<list.size();++i)
{
Assert.assertEquals(i, Collections.binarySearch(list, i));
}
T l=createInstanceFromList(list);
java.util.Collections.sort(l);
for(int i=0;i<l.size();++i)
{
Assert.assertEquals("searching for "+i,l.indexOf(i), Collections.binarySearch(l, i));
}
}
@Test
public void copyTest()
{
T l=createInstanceFromList(list);
Collections.shuffle(l);
Assert.assertNotEquals(0,Arrays.compare(list.toArray(new Integer[0]),l.toArray(new Integer[0])));
Collections.copy(l,list);
Assert.assertArrayEquals(list.toArray(new Integer[0]),l.toArray(new Integer[0]));
}
@Test
public void disjointTest()
{
Assert.assertFalse(Collections.disjoint(list,list));
T l=createInstanceFromList(list);
Assert.assertFalse(Collections.disjoint(list,l));
Collections.shuffle(l);
Assert.assertFalse(Collections.disjoint(list,l));
T list1=createInstanceFromList(Arrays.asList(10,11,12,13,14,15,16,17,18,19));
Assert.assertTrue(Collections.disjoint(list,list1));
list1.add(list.get(rand.nextInt(list.size())));
Collections.shuffle(list1);
Assert.assertFalse(Collections.disjoint(list,list1));
}
@Test
public void swapTest()
{
T l=createInstanceFromList(list);
Collections.sort(l);
for(int i=0;i<list.size()/2;++i)
{
Collections.swap(l, i, list.size() - 1 - i);
}
for(int i=0;i<list.size();++i)
{
Assert.assertEquals("looking at "+i,list.get(i), l.get(l.size()-1-i));
}
}
//endregion algorithms
//region setup
/**
* The Before annotation indicates that this method must be executed before
* each test in the class, so as to execute some preconditions necessary for
* the test.
*/
@org.junit.Before
public void methodBefore()
{
list=createInstanceFromList(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
}
/**
* The BeforeClass annotation indicates that the static method to which is
* attached must be executed once and before all tests in the class. That
* happens when the test methods share computationally expensive setup (e.g.
* connect to database).
*/
@org.junit.BeforeClass
public static void methodBeforeClass()
{
//de.elbosso.util.Utilities.configureBasicStdoutLogging(Level.ALL);
rand=new java.util.Random(System.currentTimeMillis());
}
/**
* The After annotation indicates that this method gets executed after
* execution of each test (e.g. reset some variables after execution of
* every test, delete temporary variables etc)
*/
@org.junit.After
public void methodAfter()
{
}
/**
* The AfterClass annotation can be used when a method needs to be executed
* after executing all the tests in a JUnit Test Case class so as to
* clean-up the expensive set-up (e.g disconnect from a database).
* Attention: The method attached with this annotation (similar to
* BeforeClass) must be defined as static.
*/
@org.junit.AfterClass
public static void methodAfterClass()
{
}
//endregion setup
protected abstract T createInstanceFromList(java.util.List<java.lang.Integer> param);
protected abstract T createInstance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment