Search in sources :

Example 91 with SortedSet

use of java.util.SortedSet in project mapdb by jankotek.

the class TreeSubSetTest method testTailSetContents.

/**
     * tailSet returns set with keys in requested range
     */
public void testTailSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.tailSet(two);
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertTrue(sm.contains(four));
    assertTrue(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(two, k);
    k = (Integer) (i.next());
    assertEquals(three, k);
    k = (Integer) (i.next());
    assertEquals(four, k);
    k = (Integer) (i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());
    SortedSet ssm = sm.tailSet(four);
    assertEquals(four, ssm.first());
    assertEquals(five, ssm.last());
    assertTrue(ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, set.size());
}
Also used : NavigableSet(java.util.NavigableSet) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet)

Example 92 with SortedSet

use of java.util.SortedSet in project mapdb by jankotek.

the class TreeSubSetTest method testDescendingSubSetContents2.

public void testDescendingSubSetContents2() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m3);
    assertEquals(1, sm.size());
    assertEquals(m2, sm.first());
    assertEquals(m2, sm.last());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertFalse(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(m2, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(m3));
    assertEquals(4, set.size());
}
Also used : NavigableSet(java.util.NavigableSet) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet)

Example 93 with SortedSet

use of java.util.SortedSet in project mapdb by jankotek.

the class TreeSubSetTest method testDescendingHeadSetContents.

/**
     * headSet returns set with keys in requested range
     */
public void testDescendingHeadSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.headSet(m4);
    assertTrue(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer) (i.next());
    assertEquals(m1, k);
    k = (Integer) (i.next());
    assertEquals(m2, k);
    k = (Integer) (i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, set.size());
    assertEquals(m4, set.first());
}
Also used : NavigableSet(java.util.NavigableSet) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet)

Example 94 with SortedSet

use of java.util.SortedSet in project guava by google.

the class MapsCollectionTest method suite.

public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTest(NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() {

        @Override
        protected SortedMap<String, String> create(Entry<String, String>[] entries) {
            SafeTreeMap<String, String> map = new SafeTreeMap<String, String>();
            putEntries(map, entries);
            return Maps.unmodifiableNavigableMap(map);
        }
    }).named("unmodifiableNavigableMap[SafeTreeMap]").withFeatures(CollectionSize.ANY, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.SERIALIZABLE).createTestSuite());
    suite.addTest(BiMapTestSuiteBuilder.using(new TestStringBiMapGenerator() {

        @Override
        protected BiMap<String, String> create(Entry<String, String>[] entries) {
            BiMap<String, String> bimap = HashBiMap.create(entries.length);
            for (Entry<String, String> entry : entries) {
                checkArgument(!bimap.containsKey(entry.getKey()));
                bimap.put(entry.getKey(), entry.getValue());
            }
            return Maps.unmodifiableBiMap(bimap);
        }
    }).named("unmodifiableBiMap[HashBiMap]").withFeatures(CollectionSize.ANY, MapFeature.ALLOWS_NULL_VALUES, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_ANY_NULL_QUERIES, MapFeature.REJECTS_DUPLICATES_AT_CREATION, CollectionFeature.SERIALIZABLE).createTestSuite());
    suite.addTest(MapTestSuiteBuilder.using(new TestMapGenerator<String, Integer>() {

        @Override
        public SampleElements<Entry<String, Integer>> samples() {
            return new SampleElements<Entry<String, Integer>>(mapEntry("x", 1), mapEntry("xxx", 3), mapEntry("xx", 2), mapEntry("xxxx", 4), mapEntry("aaaaa", 5));
        }

        @Override
        public Map<String, Integer> create(Object... elements) {
            Set<String> set = Sets.newLinkedHashSet();
            for (Object e : elements) {
                Entry<?, ?> entry = (Entry<?, ?>) e;
                checkNotNull(entry.getValue());
                set.add((String) checkNotNull(entry.getKey()));
            }
            return Maps.asMap(set, new Function<String, Integer>() {

                @Override
                public Integer apply(String input) {
                    return input.length();
                }
            });
        }

        @SuppressWarnings("unchecked")
        @Override
        public Entry<String, Integer>[] createArray(int length) {
            return new Entry[length];
        }

        @Override
        public Iterable<Entry<String, Integer>> order(List<Entry<String, Integer>> insertionOrder) {
            return insertionOrder;
        }

        @Override
        public String[] createKeyArray(int length) {
            return new String[length];
        }

        @Override
        public Integer[] createValueArray(int length) {
            return new Integer[length];
        }
    }).named("Maps.asMap[Set, Function]").withFeatures(CollectionSize.ANY, MapFeature.SUPPORTS_REMOVE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).createTestSuite());
    suite.addTest(SortedMapTestSuiteBuilder.using(new TestMapGenerator<String, Integer>() {

        @Override
        public String[] createKeyArray(int length) {
            return new String[length];
        }

        @Override
        public Integer[] createValueArray(int length) {
            return new Integer[length];
        }

        @Override
        public SampleElements<Entry<String, Integer>> samples() {
            return new SampleElements<Entry<String, Integer>>(mapEntry("a", 1), mapEntry("aa", 2), mapEntry("aba", 3), mapEntry("bbbb", 4), mapEntry("ccccc", 5));
        }

        @Override
        public SortedMap<String, Integer> create(Object... elements) {
            SortedSet<String> set = new NonNavigableSortedSet();
            for (Object e : elements) {
                Entry<?, ?> entry = (Entry<?, ?>) e;
                checkNotNull(entry.getValue());
                set.add((String) checkNotNull(entry.getKey()));
            }
            return Maps.asMap(set, new Function<String, Integer>() {

                @Override
                public Integer apply(String input) {
                    return input.length();
                }
            });
        }

        @SuppressWarnings("unchecked")
        @Override
        public Entry<String, Integer>[] createArray(int length) {
            return new Entry[length];
        }

        @Override
        public Iterable<Entry<String, Integer>> order(List<Entry<String, Integer>> insertionOrder) {
            Collections.sort(insertionOrder, new Comparator<Entry<String, Integer>>() {

                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });
            return insertionOrder;
        }
    }).named("Maps.asMap[SortedSet, Function]").withFeatures(CollectionSize.ANY, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, MapFeature.SUPPORTS_REMOVE).createTestSuite());
    suite.addTest(NavigableMapTestSuiteBuilder.using(new TestMapGenerator<String, Integer>() {

        @Override
        public String[] createKeyArray(int length) {
            return new String[length];
        }

        @Override
        public Integer[] createValueArray(int length) {
            return new Integer[length];
        }

        @Override
        public SampleElements<Entry<String, Integer>> samples() {
            return new SampleElements<Entry<String, Integer>>(mapEntry("a", 1), mapEntry("aa", 2), mapEntry("aba", 3), mapEntry("bbbb", 4), mapEntry("ccccc", 5));
        }

        @Override
        public NavigableMap<String, Integer> create(Object... elements) {
            NavigableSet<String> set = Sets.newTreeSet(Ordering.natural());
            for (Object e : elements) {
                Map.Entry<?, ?> entry = (Entry<?, ?>) e;
                checkNotNull(entry.getValue());
                set.add((String) checkNotNull(entry.getKey()));
            }
            return Maps.asMap(set, new Function<String, Integer>() {

                @Override
                public Integer apply(String input) {
                    return input.length();
                }
            });
        }

        @SuppressWarnings("unchecked")
        @Override
        public Entry<String, Integer>[] createArray(int length) {
            return new Entry[length];
        }

        @Override
        public Iterable<Entry<String, Integer>> order(List<Entry<String, Integer>> insertionOrder) {
            Collections.sort(insertionOrder, new Comparator<Entry<String, Integer>>() {

                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });
            return insertionOrder;
        }
    }).named("Maps.asMap[NavigableSet, Function]").withFeatures(CollectionSize.ANY, MapFeature.SUPPORTS_REMOVE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).createTestSuite());
    suite.addTest(filterSuite());
    suite.addTest(transformSuite());
    return suite;
}
Also used : NavigableSet(java.util.NavigableSet) SortedSet(java.util.SortedSet) Set(java.util.Set) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) SortedSet(java.util.SortedSet) TestStringSortedMapGenerator(com.google.common.collect.testing.TestStringSortedMapGenerator) Comparator(java.util.Comparator) Function(com.google.common.base.Function) Helpers.mapEntry(com.google.common.collect.testing.Helpers.mapEntry) Entry(java.util.Map.Entry) TestSuite(junit.framework.TestSuite) SafeTreeMap(com.google.common.collect.testing.SafeTreeMap) TestStringBiMapGenerator(com.google.common.collect.testing.google.TestStringBiMapGenerator) SortedMap(java.util.SortedMap) SampleElements(com.google.common.collect.testing.SampleElements) Map(java.util.Map) NavigableMap(java.util.NavigableMap) SafeTreeMap(com.google.common.collect.testing.SafeTreeMap) SortedMap(java.util.SortedMap)

Example 95 with SortedSet

use of java.util.SortedSet in project j2objc by google.

the class CollectionsTest method test_unmodifiableSortedSetLjava_util_SortedSet.

/**
	 * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet)
	 */
public void test_unmodifiableSortedSetLjava_util_SortedSet() {
    // Test for method java.util.SortedSet
    // java.util.Collections.unmodifiableSortedSet(java.util.SortedSet)
    boolean exception = false;
    SortedSet ss = new TreeSet();
    ss.addAll(s);
    SortedSet c = Collections.unmodifiableSortedSet(ss);
    assertTrue("Returned set is of incorrect size", c.size() == ss.size());
    Iterator i = ll.iterator();
    while (i.hasNext()) assertTrue("Returned set missing elements", c.contains(i.next()));
    try {
        c.add(new Object());
    } catch (UnsupportedOperationException e) {
        exception = true;
    // Correct
    }
    if (!exception) {
        fail("Allowed modification of set");
    }
    try {
        c.remove(new Object());
    } catch (UnsupportedOperationException e) {
        // Correct
        return;
    }
    fail("Allowed modification of set");
}
Also used : TreeSet(java.util.TreeSet) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SortedSet(java.util.SortedSet)

Aggregations

SortedSet (java.util.SortedSet)127 TreeSet (java.util.TreeSet)49 Iterator (java.util.Iterator)43 HashMap (java.util.HashMap)24 NavigableSet (java.util.NavigableSet)22 ArrayList (java.util.ArrayList)20 Map (java.util.Map)20 List (java.util.List)19 Set (java.util.Set)19 TreeMap (java.util.TreeMap)18 HashSet (java.util.HashSet)15 Test (org.junit.Test)13 IOException (java.io.IOException)12 Collection (java.util.Collection)10 Comparator (java.util.Comparator)7 LinkedHashMap (java.util.LinkedHashMap)7 LinkedList (java.util.LinkedList)5 SolverException (cbit.vcell.solver.SolverException)3 TestStringSortedSetGenerator (com.google.common.collect.testing.TestStringSortedSetGenerator)3 File (java.io.File)3