jdk/test/sun/invoke/util/WrapperTest.java
author amurillo
Thu, 21 Jul 2016 17:13:23 +0000
changeset 39764 4c2a4f338444
parent 33842 d261fb6ab872
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33842
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     1
/*
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     4
 *
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     8
 *
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    13
 * accompanied this code).
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    14
 *
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    18
 *
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    21
 * questions.
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    22
 */
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    23
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    24
package test.sun.invoke.util;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    25
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    26
import sun.invoke.util.ValueConversions;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    27
import sun.invoke.util.Wrapper;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    28
import java.lang.invoke.MethodHandles;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    29
import java.lang.invoke.MethodType;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    30
import java.lang.invoke.MethodHandle;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    31
import java.io.Serializable;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    32
import java.util.Arrays;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    33
import org.junit.Test;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    34
import static org.junit.Assert.*;
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    35
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    36
/* @test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    37
 * @summary unit tests to assert Wrapper zero identities and conversion behave correctly
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    38
 * @modules java.base/sun.invoke.util
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    39
 * @compile -XDignore.symbol.file WrapperTest.java
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    40
 * @run junit test.sun.invoke.util.WrapperTest
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    41
 */
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    42
public class WrapperTest {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    43
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    44
    @Test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    45
    public void testShortZeroConversion() throws Throwable {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    46
        MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    47
        MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    48
        MethodHandle h3 = h2.asType(MethodType.methodType(short.class));  // add 0
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    49
        MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    50
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    51
        Object x = h4.invokeExact();
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    52
        assertEquals(x, (short)0);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    53
        assertTrue(x == Short.valueOf((short)0));
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    54
        assertTrue(x == Wrapper.SHORT.zero());
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    55
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    56
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    57
    @Test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    58
    public void testIntZeroConversion() throws Throwable {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    59
        MethodHandle h1 = MethodHandles.constant(Integer.class, 42);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    60
        MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    61
        MethodHandle h3 = h2.asType(MethodType.methodType(int.class));  // add 0
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    62
        MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    63
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    64
        Object x = h4.invokeExact();
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    65
        assertEquals(x, 0);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    66
        assertTrue(x == Integer.valueOf(0));
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    67
        assertTrue(x == Wrapper.INT.zero());
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    68
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    69
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    70
    @Test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    71
    public void testLongZeroConversion() throws Throwable {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    72
        MethodHandle h1 = MethodHandles.constant(Long.class, 42L);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    73
        MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    74
        MethodHandle h3 = h2.asType(MethodType.methodType(long.class));  // add 0
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    75
        MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    76
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    77
        Object x = h4.invokeExact();
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    78
        assertEquals(x, 0L);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    79
        assertTrue(x == Long.valueOf(0));
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    80
        assertTrue(x == Wrapper.LONG.zero());
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    81
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    82
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    83
    @Test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    84
    public void testByteZeroConversion() throws Throwable {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    85
        MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    86
        MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    87
        MethodHandle h3 = h2.asType(MethodType.methodType(byte.class));  // add 0
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    88
        MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    89
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    90
        Object x = h4.invokeExact();
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    91
        assertEquals(x, (byte)0);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    92
        assertTrue(x == Byte.valueOf((byte)0));
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    93
        assertTrue(x == Wrapper.BYTE.zero());
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    94
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    95
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    96
    @Test
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    97
    public void testCharacterZeroConversion() throws Throwable {
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    98
        MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
    99
        MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   100
        MethodHandle h3 = h2.asType(MethodType.methodType(char.class));  // add 0
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   101
        MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   102
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   103
        Object x = h4.invokeExact();
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   104
        assertEquals(x, (char)0);
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   105
        assertTrue(x == Character.valueOf((char)0));
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   106
        assertTrue(x == Wrapper.CHAR.zero());
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   107
    }
d261fb6ab872 8142487: Cleanup sun.invoke.util.Wrapper zeroes to be both reliable and lazy
redestad
parents:
diff changeset
   108
}