src/jdk.internal.vm.compiler/share/classes/org.graalvm.util.test/src/org/graalvm/util/test/CollectionSizeTest.java
changeset 48861 47f19ff9903c
child 49873 26ebfe8ce852
equal deleted inserted replaced
48860:5bce1b7e7800 48861:47f19ff9903c
       
     1 /*
       
     2  * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package org.graalvm.util.test;
       
    24 
       
    25 import static org.junit.Assert.assertEquals;
       
    26 
       
    27 import org.graalvm.collections.EconomicMap;
       
    28 import org.graalvm.collections.Equivalence;
       
    29 import org.graalvm.compiler.test.GraalTest;
       
    30 import org.graalvm.util.ObjectSizeEstimate;
       
    31 import org.junit.Assume;
       
    32 import org.junit.Test;
       
    33 
       
    34 public class CollectionSizeTest {
       
    35 
       
    36     /**
       
    37      * Tests the memory size of an empty map and a map with only one or two entries.
       
    38      */
       
    39     @Test
       
    40     public void testSize() {
       
    41         Assume.assumeTrue("Not working in JDK9 due to module visibility.", GraalTest.Java8OrEarlier);
       
    42         EconomicMap<Object, Object> map = EconomicMap.create(Equivalence.IDENTITY);
       
    43         assertEquals(49, ObjectSizeEstimate.forObject(map).getTotalBytes());
       
    44 
       
    45         Integer value = 1;
       
    46         map.put(value, value);
       
    47         assertEquals(153, ObjectSizeEstimate.forObject(map).getTotalBytes());
       
    48 
       
    49         Integer secondValue = 2;
       
    50         map.put(secondValue, secondValue);
       
    51         assertEquals(153 + 20, ObjectSizeEstimate.forObject(map).getTotalBytes());
       
    52     }
       
    53 
       
    54     /**
       
    55      * Tests whether the map actually compresses the entries array when a large number of entries
       
    56      * are deleted.
       
    57      */
       
    58     @Test
       
    59     public void testCompress() {
       
    60         Assume.assumeTrue("Not working in JDK9 due to module visibility.", GraalTest.Java8OrEarlier);
       
    61         EconomicMap<Object, Object> map = EconomicMap.create();
       
    62 
       
    63         // Measuring size of map with one entry.
       
    64         Object firstValue = 0;
       
    65         map.put(firstValue, firstValue);
       
    66         ObjectSizeEstimate afterFirstValue = ObjectSizeEstimate.forObject(map);
       
    67 
       
    68         // Add 999 more entries.
       
    69         for (int i = 1; i < 1000; ++i) {
       
    70             Object value = i;
       
    71             map.put(value, value);
       
    72         }
       
    73         ObjectSizeEstimate beforeRemove = ObjectSizeEstimate.forObject(map);
       
    74 
       
    75         // Remove 999 first entries.
       
    76         for (int i = 0; i < 999; ++i) {
       
    77             map.removeKey(i);
       
    78         }
       
    79         ObjectSizeEstimate afterRemove = ObjectSizeEstimate.forObject(map);
       
    80 
       
    81         // Check that size is same size as with one entry.
       
    82         assertEquals(afterFirstValue, afterRemove);
       
    83 
       
    84         // Add 999 new entries.
       
    85         for (int i = 0; i < 999; ++i) {
       
    86             Object value = i;
       
    87             map.put(value, value);
       
    88         }
       
    89         ObjectSizeEstimate afterAdd = ObjectSizeEstimate.forObject(map);
       
    90 
       
    91         // Check that entries array is same size again.
       
    92         assertEquals(beforeRemove.getPointerCount(), afterAdd.getPointerCount());
       
    93     }
       
    94 
       
    95 }