test/jdk/java/lang/String/CompactString/VMOptionsTest.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     1
/*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     4
 *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     8
 *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    13
 * accompanied this code).
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    14
 *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    18
 *
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    21
 * questions.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    22
 */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    23
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    24
import java.io.*;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    25
import java.lang.reflect.Field;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    26
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    27
import org.testng.annotations.BeforeClass;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    28
import org.testng.annotations.DataProvider;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    29
import org.testng.annotations.Test;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    30
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    31
import static org.testng.Assert.*;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    32
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    33
/*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    34
 * @test
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    35
 * @bug 8077559
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    36
 * @summary Tests Compact String. This one is testing
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    37
 *          if Compact String enable/disable VM Options is indeed working in String class,
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    38
 *          it's verified by testing if the VM option affect coder and
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    39
 *          COMPACT_STRINGS field in String class.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 33664
diff changeset
    40
 * @modules java.base/java.lang:open
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    41
 * @run testng/othervm -XX:+CompactStrings -DCompactStringEnabled=true VMOptionsTest
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    42
 * @run testng/othervm -XX:-CompactStrings -DCompactStringEnabled=false VMOptionsTest
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    43
 */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    44
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    45
public class VMOptionsTest {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    46
    boolean compactStringEnabled;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    47
    // corresponding "COMPACT_STRINGS" field in String class.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    48
    Field COMPACT_STRINGS;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    49
    // corresponding "coder" field in String class.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    50
    Field coder;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    51
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    52
    // corresponding coder type in String class.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    53
    final byte LATIN1 = 0;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    54
    final byte UTF16  = 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    55
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    56
    @BeforeClass
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    57
    public void setUp() throws Exception {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    58
        compactStringEnabled = Boolean.valueOf(System.getProperty("CompactStringEnabled", null));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    59
        COMPACT_STRINGS = String.class.getDeclaredField("COMPACT_STRINGS");
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    60
        COMPACT_STRINGS.setAccessible(true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    61
        coder = String.class.getDeclaredField("coder");
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    62
        coder.setAccessible(true);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    63
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    64
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    65
    @DataProvider
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    66
    public Object[][] provider() {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    67
        return new Object[][] {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    68
                new Object[] {"", LATIN1},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    69
                new Object[] {"abc", LATIN1},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    70
                new Object[] {"A\uff21", UTF16},
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    71
                new Object[] {"\uff21\uff22", UTF16}
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    72
        };
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    73
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    74
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    75
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    76
     * verify the coder field in String objects.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    77
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    78
    @Test(dataProvider = "provider")
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    79
    public void testCoder(String str, byte expected) throws Exception {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    80
        byte c = (byte) coder.get(str);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    81
        expected = compactStringEnabled ? expected : UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    82
        assertEquals(c, expected);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    83
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    84
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    85
    /*
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    86
     * verify the COMPACT_STRINGS flag in String objects.
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    87
     */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    88
    @Test(dataProvider = "provider")
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    89
    public void testCompactStringFlag(String str, byte ignore) throws Exception {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    90
        assertTrue(COMPACT_STRINGS.get(str).equals(compactStringEnabled));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    91
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents:
diff changeset
    92
}