@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void queryInputResourcePermutation(final SamInputResource resource) throws IOException { final SamReader reader = SamReaderFactory.makeDefault().open(resource); LOG.info(String.format("Query from %s ...", resource)); if (reader.hasIndex()) { final StopWatch stopWatch = new StopWatch(); stopWatch.start(); final SAMRecordIterator q1 = reader.query("chr1", 500000, 100000000, true); observedRecordOrdering1.add(Iterables.slurp(q1)); q1.close(); final SAMRecordIterator q20 = reader.query("chr20", 1, 1000000, true); observedRecordOrdering20.add(Iterables.slurp(q20)); q20.close(); final SAMRecordIterator q3 = reader.query("chr3", 1, 10000000, true); observedRecordOrdering3.add(Iterables.slurp(q3)); q3.close(); stopWatch.stop(); LOG.info(String.format("Finished queries in %sms", stopWatch.getElapsedTime())); Assert.assertEquals( observedRecordOrdering1.size(), 1, "read different records for chromosome 1"); Assert.assertEquals( observedRecordOrdering20.size(), 1, "read different records for chromosome 20"); Assert.assertEquals( observedRecordOrdering3.size(), 1, "read different records for chromosome 3"); } else if (resource.indexMaybe() != null) { LOG.warn("Resource has an index source, but is not indexed: " + resource); } else { LOG.info("Skipping query operation: no index."); } reader.close(); }
@Test public void given_two_collections_should_iterate_over_all_elements_after_joining() { Iterable<String> strings = Iterables.join(Arrays.asList("1"), Arrays.asList("2", "3")); int i = 1; for (String s : strings) { Assert.assertEquals(Integer.toString(i++), s); } strings = Iterables.join(Arrays.asList("1", "2"), Arrays.asList("3")); i = 1; for (String s : strings) { Assert.assertEquals(Integer.toString(i++), s); } }
@Test public void given_two_collections_with_one_empty_should_iterate_over_all_elements_of_other_collection() { Iterable<String> strings = Iterables.join(Collections.<String>emptyList(), Arrays.asList("1", "2")); int i = 1; for (String s : strings) { Assert.assertEquals(Integer.toString(i++), s); } strings = Iterables.join(Arrays.asList("1", "2"), Collections.<String>emptyList()); i = 1; for (String s : strings) { Assert.assertEquals(Integer.toString(i++), s); } }
/** * Creates an {@code ArrayDeque} containing the elements of the specified iterable, in the order * they are returned by the iterable's iterator. * * @since 12.0 */ public static <E> ArrayDeque<E> newArrayDeque(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new ArrayDeque<E>(Collections2.cast(elements)); } ArrayDeque<E> deque = new ArrayDeque<E>(); Iterables.addAll(deque, elements); return deque; }
/** * Creates a {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}, containing * the elements of the specified iterable, in the order they are returned by the iterable's * iterator. * * @param elements the elements that the queue should contain, in order * @return a new {@code LinkedBlockingQueue} containing those elements */ public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? extends E> elements) { if (elements instanceof Collection) { return new LinkedBlockingQueue<E>(Collections2.cast(elements)); } LinkedBlockingQueue<E> queue = new LinkedBlockingQueue<E>(); Iterables.addAll(queue, elements); return queue; }
@Test public void given_two_collections_both_empty_should_not_iterate_over_anything() { Iterable<String> strings = Iterables.join(Collections.<String>emptyList(), Collections.<String>emptyList()); for (String s : strings) { Assert.fail("should be no items: " + s); } }
/** * Creates a {@code PriorityQueue} containing the given elements. * * <p><b>Note:</b> If the specified iterable is a {@code SortedSet} or a {@code PriorityQueue}, * this priority queue will be ordered according to the same ordering. * * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0). */ public static <E extends Comparable> PriorityQueue<E> newPriorityQueue( Iterable<? extends E> elements) { if (elements instanceof Collection) { return new PriorityQueue<E>(Collections2.cast(elements)); } PriorityQueue<E> queue = new PriorityQueue<E>(); Iterables.addAll(queue, elements); return queue; }
@Test public void openPath() throws IOException { final Path path = localBam.toPath(); final List<SAMRecord> records; final SAMFileHeader fileHeader; try (final SamReader reader = SamReaderFactory.makeDefault().open(path)) { LOG.info(String.format("Reading from %s ...", path)); records = Iterables.slurp(reader); fileHeader = reader.getFileHeader(); reader.close(); } try (final SamReader fileReader = SamReaderFactory.makeDefault().open(localBam)) { final List<SAMRecord> expectedRecords = Iterables.slurp(fileReader); final SAMFileHeader expectedFileHeader = fileReader.getFileHeader(); Assert.assertEquals(records, expectedRecords); Assert.assertEquals(fileHeader, expectedFileHeader); } }
@Test public void shouldReturnAnEmptySetForTheSetDifferenceOfNoSets() throws Exception { // Given Iterable<Iterable<String>> emptySet = Iterables.empty(); Iterable<String> expectedDifferenceSet = Collections.emptySet(); // When Set<String> difference = Sets.difference(emptySet); // Then assertThat(difference, is(expectedDifferenceSet)); }
@Test public void given_two_ints_should_map_to_strings() { Collection<String> items = Iterables.select( Arrays.asList(1, 2, 3), new Action<Integer, String>() { @Override public String action(Integer input) { return input.toString(); } }); Assert.assertEquals("1", index(items, 0)); Assert.assertEquals("2", index(items, 1)); Assert.assertEquals("3", index(items, 2)); }
@Test(dataProvider = "composeAllPermutationsOfSamInputResource") public void exhaustInputResourcePermutation(final SamInputResource resource) throws IOException { final SamReader reader = SamReaderFactory.makeDefault().open(resource); LOG.info(String.format("Reading from %s ...", resource)); final List<SAMRecord> slurped = Iterables.slurp(reader); final SAMFileHeader fileHeader = reader.getFileHeader(); reader.hasIndex(); reader.indexing().hasBrowseableIndex(); reader.close(); /* Ensure all tests have read the same records in the same order or, if this is the first test, set it as the template. */ observedHeaders.add(fileHeader); observedRecordOrdering.add(slurped); Assert.assertEquals(observedHeaders.size(), 1, "read different headers than other testcases"); Assert.assertEquals( observedRecordOrdering.size(), 1, "read different records than other testcases"); }
@Test public void given_two_elements_with_different_keys_should_create_a_map_with_two_keys_and_one_item_in_the_list_of_each_value() { List<Integer> ints = Arrays.asList(1, 9); Map<Integer, Collection<Integer>> grouped = Iterables.groupBy( ints, new Action<Integer, Integer>() { @Override public Integer action(Integer input) { return input; } }); Assert.assertEquals(2, grouped.size()); Assert.assertEquals(1, grouped.get(1).size()); Assert.assertEquals(Integer.valueOf(1), index(grouped.get(1), 0)); Assert.assertEquals(Integer.valueOf(9), index(grouped.get(9), 0)); }