Search in sources :

Example 1 with PropertyMap

use of com.oracle.truffle.object.PropertyMap in project graal by oracle.

the class PropertyMapTest method testPropertyMap.

@Test
public void testPropertyMap() {
    PropertyMap map = PropertyMap.empty();
    Map<Object, Property> referenceMap = new LinkedHashMap<>();
    Random rnd = new Random();
    final int size = 1000;
    int[] randomSequence = rnd.ints().limit(size).toArray();
    int[] shuffledSequence = randomSequence.clone();
    shuffle(shuffledSequence, rnd);
    Layout layout = Layout.newLayout().build();
    // fill the map
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
        assertEqualsOrdered(referenceMap, map);
    }
    // put the same values again, should not modify the map
    PropertyMap initial = map;
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        assertSame(initial, map);
    }
    assertEqualsOrdered(referenceMap, map);
    // update existing values
    for (int i = 0; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (double) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    for (int i = size - 1; i >= 0; i--) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (double) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    // update existing values, in random order
    for (int i = 0; i < size; i++) {
        int id = shuffledSequence[i];
        String key = String.valueOf(id);
        Property value = Property.create(key, newLocation(layout, (long) id), 0);
        map = (PropertyMap) map.copyAndPut(key, value);
        referenceMap.put(key, value);
    }
    assertEqualsOrdered(referenceMap, map);
    // remove keys
    for (int i = size - 10; i < size; i++) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
    for (int i = 10; i >= 0; i--) {
        int id = randomSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
    for (int i = 0; i < size; i++) {
        int id = shuffledSequence[i];
        String key = String.valueOf(id);
        map = (PropertyMap) map.copyAndRemove(key);
        referenceMap.remove(key);
        assertEqualsOrdered(referenceMap, map);
    }
}
Also used : PropertyMap(com.oracle.truffle.object.PropertyMap) Random(java.util.Random) Layout(com.oracle.truffle.api.object.Layout) Property(com.oracle.truffle.api.object.Property) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Layout (com.oracle.truffle.api.object.Layout)1 Property (com.oracle.truffle.api.object.Property)1 PropertyMap (com.oracle.truffle.object.PropertyMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 Test (org.junit.Test)1