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);
}
}
Aggregations