Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,9 +40,13 @@
*/
package org.graalvm.collections.test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.Equivalence;
import org.graalvm.collections.MapCursor;
import org.graalvm.collections.UnmodifiableEconomicMap;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -57,6 +61,90 @@ public void testMapGetDefault() {
Assert.assertEquals(map.get(1, 2), Integer.valueOf(2));
}

@Test
public void testClear() {
EconomicMap<Integer, Integer> map = EconomicMap.create();
map.put(0, 0);
map.put(1, 1);
map.clear();
Assert.assertTrue(map.isEmpty());
}

@Test
public void testRemoveKey() {
EconomicMap<Integer, Integer> map = EconomicMap.create();
map.put(0, 0);
map.put(1, 1);
Assert.assertEquals(Integer.valueOf(0), map.removeKey(0));
Assert.assertNull(map.get(0));
}

@Test
public void testReplaceAll() {
EconomicMap<Integer, Integer> map = EconomicMap.create();
map.put(0, 0);
map.put(1, 1);
map.replaceAll((k, v) -> v + 1);
Assert.assertEquals(Integer.valueOf(1), map.get(0));
Assert.assertEquals(Integer.valueOf(2), map.get(1));
}

@Test
public void testCreateWithInitialCapacity() {
EconomicMap<Integer, Integer> map = EconomicMap.create(10);
for (int i = 0; i < 10; i++) {
map.put(i, i);
}
Assert.assertEquals(10, map.size());
}

@Test
public void testCreateWithEquivalence() {
EconomicMap<Integer, Integer> map = EconomicMap.create(Equivalence.DEFAULT);
map.put(0, 0);
Assert.assertEquals(Integer.valueOf(0), map.get(0));
}

@Test
public void testWrapMap() {
Map<Integer, Integer> javaMap = new HashMap<>();
javaMap.put(0, 0);
EconomicMap<Integer, Integer> map = EconomicMap.wrapMap(javaMap);
Assert.assertEquals(Integer.valueOf(0), map.get(0));
}

@Test
public void testEmptyCursor() {
MapCursor<Integer, Integer> cursor = EconomicMap.emptyCursor();
Assert.assertFalse(cursor.advance());
}

@Test
public void testEmptyMap() {
EconomicMap<Integer, Integer> map = EconomicMap.emptyMap();
Assert.assertTrue(map.isEmpty());
}

@Test
public void testOf() {
EconomicMap<Integer, Integer> map = EconomicMap.of(0, 0);
Assert.assertEquals(Integer.valueOf(0), map.get(0));
}

@Test
public void testOfTwoElements() {
EconomicMap<Integer, Integer> map = EconomicMap.of(0, 0, 1, 1);
Assert.assertEquals(Integer.valueOf(0), map.get(0));
Assert.assertEquals(Integer.valueOf(1), map.get(1));
}

@Test
public void testComputeIfAbsent() {
EconomicMap<Integer, Integer> map = EconomicMap.create();
Assert.assertEquals(Integer.valueOf(0), map.computeIfAbsent(0, k -> 0));
Assert.assertEquals(Integer.valueOf(0), map.get(0));
}

@Test
public void testMapPutAll() {
EconomicMap<Integer, Integer> map = EconomicMap.create();
Expand Down Expand Up @@ -97,4 +185,23 @@ public void testPutIfAbsent() {
Assert.assertEquals(Integer.valueOf(5), map.get(1));
}

@Test(expected = UnsupportedOperationException.class)
public void testPutNullKey() {
EconomicMap.create(0).put(null, 0);
}

@Test(expected = UnsupportedOperationException.class)
public void testContainsNullKey() {
EconomicMap.create(0).containsKey(null);
}

@Test(expected = UnsupportedOperationException.class)
public void testGetNullKey() {
EconomicMap.create(0).get(null);
}

@Test(expected = UnsupportedOperationException.class)
public void testRemoveNullKey() {
EconomicMap.create(0).removeKey(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -68,6 +68,21 @@ public void testUtilities() {
Assert.assertEquals(set.size(), 0);
}

@Test(expected = UnsupportedOperationException.class)
public void testContainsNull() {
Assert.assertFalse(EconomicSet.create(0).contains(null));
}

@Test(expected = UnsupportedOperationException.class)
public void testAddNull() {
Assert.assertFalse(EconomicSet.create(0).add(null));
}

@Test(expected = UnsupportedOperationException.class)
public void testRemoveNull() {
EconomicSet.create(0).remove(null);
}

@Test
public void testAddAll() {
EconomicSet<Integer> set = EconomicSet.create();
Expand Down Expand Up @@ -166,4 +181,99 @@ private static Integer newInteger(int value) {
return new Integer(value);
}

@Test
public void testCreateWithEquivalenceAndCapacity() {
EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY, 1);
Integer a1 = newInteger(1);
Integer a2 = newInteger(1);
Assert.assertTrue(set.add(a1));
Assert.assertFalse(set.add(a1));
Assert.assertFalse("identity equivalence shouldn't match equal value", set.contains(a2));
Assert.assertEquals(1, set.size());
}

@Test
public void testCreateWithEquivalenceAndUnmodifiableSet() {
EconomicSet<Integer> source = EconomicSet.create();
Integer a1 = newInteger(1);
Integer a1dup = newInteger(1);
source.add(a1);
source.add(a1dup); // deduped by default equivalence
EconomicSet<Integer> idSet = EconomicSet.create(Equivalence.IDENTITY, source);
Assert.assertEquals(1, idSet.size());
Assert.assertTrue(idSet.contains(a1));
Assert.assertFalse(idSet.contains(newInteger(1)));
}

@Test
public void testCreateFromIterable() {
EconomicSet<Integer> set = EconomicSet.create(Arrays.asList(0, 1, 0));
Assert.assertEquals(2, set.size());
Assert.assertTrue(set.contains(0));
Assert.assertTrue(set.contains(1));
}

@Test
public void testAddAllIterator() {
EconomicSet<Integer> set = EconomicSet.create();
set.add(0);
set.addAll(Arrays.asList(1, 2, 1).iterator());
Assert.assertEquals(3, set.size());
Assert.assertTrue(set.contains(0));
Assert.assertTrue(set.contains(1));
Assert.assertTrue(set.contains(2));
}

@Test
public void testRemoveAllIterator() {
EconomicSet<Integer> set = EconomicSet.create();
set.addAll(Arrays.asList(0, 1, 2, 3));
set.removeAll(Arrays.asList(1, 3, 4).iterator());
Assert.assertEquals(2, set.size());
Assert.assertTrue(set.contains(0));
Assert.assertTrue(set.contains(2));
Assert.assertFalse(set.contains(1));
Assert.assertFalse(set.contains(3));
}

@Test
public void testIdentityEquivalenceContains() {
EconomicSet<Integer> set = EconomicSet.create(Equivalence.IDENTITY);
Integer a1 = newInteger(42);
Integer a2 = newInteger(42);
set.add(a1);
Assert.assertTrue(set.contains(a1));
Assert.assertFalse(set.contains(a2));
}

@Test
public void testEmptySetBasics() {
EconomicSet<Integer> empty = EconomicSet.emptySet();
Assert.assertTrue(empty.isEmpty());
Assert.assertEquals(0, empty.size());
Assert.assertFalse(empty.contains(123));
Integer[] arr = empty.toArray(new Integer[0]);
Assert.assertEquals(0, arr.length);
}

@Test(expected = UnsupportedOperationException.class)
public void testEmptySetContainsNull() {
EconomicSet.emptySet().contains(null);
}

@Test(expected = IllegalArgumentException.class)
public void testEmptySetAdd() {
EconomicSet.emptySet().add(1);
}

@Test(expected = IllegalArgumentException.class)
public void testEmptySetRemove() {
EconomicSet.emptySet().remove(1);
}

@Test(expected = IllegalArgumentException.class)
public void testEmptySetClear() {
EconomicSet.emptySet().clear();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.collections.test;

import org.graalvm.collections.EconomicMap;
import org.junit.Assert;
import org.junit.Test;

public class EmptyEconomicMapTest {

@Test
public void testIsEmpty() {
Assert.assertTrue(EconomicMap.emptyMap().isEmpty());
}

@Test
public void testSizeZero() {
Assert.assertEquals(0, EconomicMap.emptyMap().size());
}

@Test(expected = IllegalArgumentException.class)
public void testPut() {
EconomicMap.emptyMap().put(1, "1");
}

@Test(expected = IllegalArgumentException.class)
public void testRemoveKey() {
EconomicMap.emptyMap().removeKey(1);
}

@Test(expected = IllegalArgumentException.class)
public void testClear() {
EconomicMap.emptyMap().clear();
}

@Test
public void testContainsKey() {
Assert.assertFalse(EconomicMap.emptyMap().containsKey(1));
}

@Test(expected = UnsupportedOperationException.class)
public void testContainsNullKey() {
Assert.assertFalse(EconomicMap.emptyMap().containsKey(null));
}

@Test
public void testCursorAlwaysEmpty() {
Assert.assertFalse(EconomicMap.emptyMap().getEntries().advance());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,21 @@ public void testAdd() {
EconomicSet.emptySet().add(1);
}

@Test(expected = UnsupportedOperationException.class)
public void testAddNull() {
EconomicSet.emptySet().add(null);
}

@Test(expected = IllegalArgumentException.class)
public void testRemove() {
EconomicSet.emptySet().remove(1);
}

@Test(expected = UnsupportedOperationException.class)
public void testRemoveNull() {
EconomicSet.emptySet().remove(null);
}

@Test(expected = IllegalArgumentException.class)
public void testClear() {
EconomicSet.emptySet().clear();
Expand All @@ -78,6 +88,11 @@ public void testContains() {
Assert.assertFalse(EconomicSet.emptySet().contains(1));
}

@Test(expected = UnsupportedOperationException.class)
public void testContainsNull() {
Assert.assertFalse(EconomicSet.emptySet().contains(null));
}

@Test
public void testIteratorAlwaysEmpty() {
Assert.assertFalse(EconomicSet.emptySet().iterator().hasNext());
Expand Down
Loading