/** * Constructs a new empty allele-list */ public IndexedAlleleList() { alleles = new IndexedSet<>(); }
/** * Constructs an empty sample-list. */ public IndexedSampleList() { samples = new IndexedSet<>(0); }
/** * Constructs a new allele-list from an array of alleles. * * <p> * Repeats in the input array will be ignored (keeping the first one). The order of alleles in the * resulting list is the same as in the natural traversal of the input collection. * * </p> * @param alleles the original allele array * * @throws java.lang.IllegalArgumentException if {@code alleles} is {@code null} or contains {@code null}s. */ public IndexedAlleleList(final A ... alleles) { this.alleles = new IndexedSet<>(alleles); }
/** * Constructs a new allele-list from a collection of alleles. * * <p> * Repeats in the input collection will be ignored (keeping the first one). The order of alleles in the * resulting list is the same as in the natural traversal of the input collection. * * </p> * @param alleles the original allele collection * * @throws java.lang.IllegalArgumentException if {@code alleles} is {@code null} or contains {@code null}s. */ public IndexedAlleleList(final Collection<A> alleles) { this.alleles = new IndexedSet<>(alleles); }
/** * Constructs a sample-list from an array of samples. * * <p> * Repeats in the input array are ignored (just the first occurrence is kept). * Sample names will be sorted based on the traversal order * of the original array. * </p> * * @param samples input sample array. * * @throws IllegalArgumentException if {@code samples} is {@code null} or it contains {@code nulls}. */ public IndexedSampleList(final String ... samples) { this.samples = new IndexedSet<>(samples); }
/** * Constructs a sample-list from a collection of samples. * * <p> * Repeats in the input collection are ignored (just the first occurrence is kept). * Sample names will be sorted based on the traversal order * of the original collection. * </p> * * @param samples input sample collection. * * @throws IllegalArgumentException if {@code samples} is {@code null} or it contains {@code nulls}. */ public IndexedSampleList(final Collection<String> samples) { this.samples = new IndexedSet<>(samples); }
@Test(dataProvider = "initialCapacityElementCountMaxElementData") public void testCompositionByCollectionAddition(final int initialCapacity, final int elementCount, final int maxElement) { final IndexedSet<Integer> subject = new IndexedSet<>(initialCapacity); final List<Integer> elementList = generateElementCollection(elementCount,maxElement); Assert.assertEquals(subject.addAll(elementList), !elementList.isEmpty()); final Set<Integer> elementSet = new LinkedHashSet<>(elementCount); elementSet.addAll(elementList); assertEquals(subject,elementSet); }
@Test(dataProvider = "elementCountMaxElementData") public void testCompositionByCollectionConstructor(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); assertEquals(subject,elementSet); Assert.assertFalse(subject.addAll(elementList)); }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor"}) public void testClear(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); subject.clear(); elementSet.clear(); assertEquals(subject, elementSet); }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor"}) public void testLookupByIndex(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); final Integer[] elementArray = elementSet.toArray(new Integer[elementSet.size()]); final List<Integer> subjectList = subject.asList(); for (int i = 0; i < subject.size(); i++) { final int element = elementArray[i]; final int subjectElement = subject.get(i); final int subjectListElement = subjectList.get(i); Assert.assertEquals(subjectElement,element); Assert.assertEquals(subjectListElement,element); } }
@Test(dataProvider = "initialCapacityElementCountMaxElementData") public void testCompositionBySingleElementAddition(final int initialCapacity, final int elementCount, final int maxElement) { final Random rnd = Utils.getRandomGenerator(); final IndexedSet<Integer> subject = new IndexedSet<>(initialCapacity); final Set<Integer> elementSet = new LinkedHashSet<>(); for (int i = 0; i < elementCount; i++) { final int nextElement = rnd.nextInt(maxElement + 1); final boolean isNewElement = ! elementSet.contains(nextElement); Assert.assertEquals(subject.add(nextElement), elementSet.add(nextElement)); Assert.assertEquals(subject.size(),elementSet.size()); if (isNewElement) Assert.assertEquals(subject.indexOf(nextElement),elementSet.size() - 1); } assertEquals(subject, elementSet); }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor"}) public void testIndexOf(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); final Integer[] elementArray = elementSet.toArray(new Integer[elementSet.size()]); final List<Integer> subjectList = subject.asList(); for (int i = 0; i < subject.size(); i++) { final int element = elementArray[i]; final int listElement = subjectList.get(i); final int subjectIndex = subject.indexOf(element); Assert.assertEquals(listElement,element); Assert.assertEquals(subjectIndex,i); Assert.assertEquals(subject.indexOf(-element - 1),-1); } }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor","testIndexOf"}) public void testRemoveAndAdd(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); final int removeCount = subject.size(); final Random rnd = Utils.getRandomGenerator(); for (int i = 0; i < removeCount; i++) { final int removeIndex = rnd.nextInt(subject.size()); final int removeElement = subject.get(removeIndex); subject.remove(removeElement); elementSet.remove(removeElement); } subject.addAll(elementList); elementSet.addAll(elementList); assertEquals(subject, elementSet); }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor","testIndexOf"}) public void testRemoveAll(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); final int removeCount = subject.size(); final Random rnd = Utils.getRandomGenerator(); for (int i = 0; i < removeCount; i++) { final int removeIndex = rnd.nextInt(subject.size()); final int removeElement = subject.get(removeIndex); subject.remove(removeElement); elementSet.remove(removeElement); } assertEquals(subject,elementSet); }
@Test(dataProvider = "elementCountMaxElementData", dependsOnMethods = {"testCompositionByCollectionConstructor","testIndexOf"}) public void testRemoveHalf(final int elementCount, final int maxElement) { final List<Integer> elementList = generateElementCollection(elementCount, maxElement); final IndexedSet<Integer> subject = new IndexedSet<>(elementList); final Set<Integer> elementSet = new LinkedHashSet<>(elementList); final int removeCount = (subject.size() + 1) / 2; final Random rnd = Utils.getRandomGenerator(); for (int i = 0; i < removeCount; i++) { final int removeIndex = rnd.nextInt(subject.size()); final int removeElement = subject.get(removeIndex); subject.remove(removeElement); elementSet.remove(removeElement); } assertEquals(subject,elementSet); }