@GwtIncompatible // NavigableMap public void testAsMapNavigableWritesThrough() { NavigableSet<String> strings = Sets.newTreeSet(); Collections.addAll(strings, "one", "two", "three"); NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(3), map.remove("two")); assertThat(strings).containsExactly("one", "three").inOrder(); assertEquals(mapEntry("three", 5), map.subMap("one", false, "zzz", true).pollLastEntry()); assertThat(strings).contains("one"); }
public void testLegacyComparable_of() { ImmutableSortedSet<LegacyComparable> set0 = ImmutableSortedSet.of(); assertThat(set0).isEmpty(); @SuppressWarnings("unchecked") // using a legacy comparable ImmutableSortedSet<LegacyComparable> set1 = ImmutableSortedSet.of(LegacyComparable.Z); assertThat(set1).containsExactly(LegacyComparable.Z); @SuppressWarnings("unchecked") // using a legacy comparable ImmutableSortedSet<LegacyComparable> set2 = ImmutableSortedSet.of(LegacyComparable.Z, LegacyComparable.Y); assertThat(set2).containsExactly(LegacyComparable.Y, LegacyComparable.Z); }
public final SortedSetSubject that(@NullableDecl SortedSet<?> actual) { return new SortedSetSubject(metadata(), actual); }
/** Fails if the subject does not have the given last element. */ public void hasLastElement(@NullableDecl Object element) { if (actualAsNavigableSet().isEmpty()) { fail("has last element", element); return; } if (!Objects.equal(actualAsNavigableSet().last(), element)) { if (actualAsNavigableSet().contains(element)) { failWithoutActual( simpleFact( lenientFormat( "Not true that %s has last element <%s>. " + "It does contain this element, but the last element is <%s>", actualAsString(), element, actualAsNavigableSet().last()))); return; } failWithoutActual( simpleFact( lenientFormat( "Not true that %s has last element <%s>. " + "It does not contain this element, and the last element is <%s>", actualAsString(), element, actualAsNavigableSet().last()))); } }
public void testCopyOfExplicit_ordering_dupes() { SortedSet<String> set = ImmutableSortedSet.copyOf( STRING_LENGTH, asList("in", "the", "quick", "brown", "fox", "jumped", "over", "a", "lazy", "dog")); assertThat(set).containsExactly("a", "in", "the", "over", "quick", "jumped").inOrder(); }
public void testTailSet_tooLarge() { assertThat(ContiguousSet.create(Range.closed(1, 3), integers()).tailSet(4)).isEmpty(); }
public void testSubSetExclusiveExclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { for (int j = i; j < strings.length; j++) { assertThat(set.subSet(strings[i], false, strings[j], false)) .containsExactlyElementsIn(sortedNumberNames(Math.min(i + 1, j), j)) .inOrder(); } } }
@Test public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception { CachedContentIndex index = Mockito.spy(new CachedContentIndex(cacheDir)); SimpleCache simpleCache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(20), index); // Add some content. CacheSpan cacheSpan = simpleCache.startReadWrite(KEY_1, 0); addCache(simpleCache, KEY_1, 0, 15); // Make index.store() throw exception from now on. doAnswer( invocation -> { throw new CacheException("SimpleCacheTest"); }) .when(index) .store(); // Adding more content will make LeastRecentlyUsedCacheEvictor evict previous content. try { addCache(simpleCache, KEY_1, 15, 15); assertWithMessage("Exception was expected").fail(); } catch (CacheException e) { // do nothing. } simpleCache.releaseHoleSpan(cacheSpan); // Although store() has failed, it should remove the first span and add the new one. NavigableSet<CacheSpan> cachedSpans = simpleCache.getCachedSpans(KEY_1); assertThat(cachedSpans).isNotEmpty(); assertThat(cachedSpans).hasSize(1); assertThat(cachedSpans.pollFirst().position).isEqualTo(15); }
public void testCopyOfExplicit_ordering() { SortedSet<String> set = ImmutableSortedSet.copyOf( STRING_LENGTH, asList("in", "the", "quick", "jumped", "over", "a")); assertThat(set).containsExactly("a", "in", "the", "over", "quick", "jumped").inOrder(); }
public void testSubSet_tooSmall() { assertThat(ContiguousSet.create(Range.closed(1, 3), integers()).subSet(-1, 0)).isEmpty(); }
/** Fails if the subject does not have the given first element. */ public void hasFirstElement(@NullableDecl Object element) { if (actualAsNavigableSet().isEmpty()) { fail("has first element", element); return; } if (!Objects.equal(actualAsNavigableSet().first(), element)) { if (actualAsNavigableSet().contains(element)) { failWithoutActual( simpleFact( lenientFormat( "Not true that %s has first element <%s>. " + "It does contain this element, but the first element is <%s>", actualAsString(), element, actualAsNavigableSet().first()))); return; } failWithoutActual( simpleFact( lenientFormat( "Not true that %s has first element <%s>. " + "It does not contain this element, and the first element is <%s>", actualAsString(), element, actualAsNavigableSet().first()))); } }
public void testSingle_headSet() { SortedSet<String> set = of("e"); assertTrue(set.headSet("g") instanceof ImmutableSortedSet); assertThat(set.headSet("g")).contains("e"); assertSame(of(), set.headSet("c")); assertSame(of(), set.headSet("e")); }
public void testTailSetInclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { assertThat(set.tailSet(strings[i], true)) .containsExactlyElementsIn(sortedNumberNames(i, strings.length)) .inOrder(); } }
public void testCopyOf_sortedSetIterable() { SortedSet<String> input = Sets.newTreeSet(STRING_LENGTH); Collections.addAll(input, "in", "the", "quick", "jumped", "over", "a"); SortedSet<String> set = copyOf(input); assertThat(set).containsExactly("a", "in", "jumped", "over", "quick", "the").inOrder(); }
public void testTailSet() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertThat(set.tailSet(Integer.MIN_VALUE)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(2)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(3)).containsExactly(3).inOrder(); assertThat(set.tailSet(Integer.MIN_VALUE, false)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1, false)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(2, false)).containsExactly(3).inOrder(); assertThat(set.tailSet(3, false)).isEmpty(); }
public void testHeadSet_tooSmall() { assertThat(ContiguousSet.create(Range.closed(1, 3), integers()).headSet(0)).isEmpty(); }
@GwtIncompatible // NavigableMap public void testAsMapNavigableWritesThrough() { NavigableSet<String> strings = Sets.newTreeSet(); Collections.addAll(strings, "one", "two", "three"); NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(3), map.remove("two")); assertThat(strings).containsExactly("one", "three").inOrder(); assertEquals(mapEntry("three", 5), map.subMap("one", false, "zzz", true).pollLastEntry()); assertThat(strings).contains("one"); }
public void testSingle_subSet() { SortedSet<String> set = of("e"); assertTrue(set.subSet("c", "g") instanceof ImmutableSortedSet); assertThat(set.subSet("c", "g")).contains("e"); assertThat(set.subSet("e", "g")).contains("e"); assertSame(of(), set.subSet("f", "g")); assertSame(of(), set.subSet("c", "e")); assertSame(of(), set.subSet("c", "d")); }
public void testHeadSetInclusive() { String[] strings = NUMBER_NAMES.toArray(new String[0]); ImmutableSortedSet<String> set = ImmutableSortedSet.copyOf(strings); Arrays.sort(strings); for (int i = 0; i < strings.length; i++) { assertThat(set.headSet(strings[i], true)) .containsExactlyElementsIn(sortedNumberNames(0, i + 1)) .inOrder(); } }