hotspot/test/compiler/c2/TestCharShortByteSwap.java
changeset 40059 c2304140ed64
parent 27699 9913b19c0948
equal deleted inserted replaced
40058:b4441f6cfe79 40059:c2304140ed64
       
     1 /*
       
     2  * Copyright 2010 Google, Inc.  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  */
       
    24 
       
    25 /*
       
    26  * @test
       
    27  * @bug 6946040
       
    28  * @summary Tests Character/Short.reverseBytes and their intrinsics implementation in the server compiler
       
    29  *
       
    30  * @run main/othervm -Xbatch
       
    31  *      -XX:CompileCommand=compileonly,compiler.c2.TestCharShortByteSwap::testChar
       
    32  *      -XX:CompileCommand=compileonly,compiler.c2.TestCharShortByteSwap::testShort
       
    33  *      compiler.c2.TestCharShortByteSwap
       
    34  */
       
    35 
       
    36 package compiler.c2;
       
    37 
       
    38 // This test must run without any command line arguments.
       
    39 
       
    40 public class TestCharShortByteSwap {
       
    41 
       
    42   private static short initShort(String[] args, short v) {
       
    43     if (args.length > 0) {
       
    44       try {
       
    45         return (short) Integer.valueOf(args[0]).intValue();
       
    46       } catch (NumberFormatException e) { }
       
    47     }
       
    48     return v;
       
    49   }
       
    50 
       
    51   private static char initChar(String[] args, char v) {
       
    52     if (args.length > 0) {
       
    53       try {
       
    54         return (char) Integer.valueOf(args[0]).intValue();
       
    55       } catch (NumberFormatException e) { }
       
    56     }
       
    57     return v;
       
    58   }
       
    59 
       
    60   private static void testChar(char a, char b) {
       
    61     if (a != Character.reverseBytes(b)) {
       
    62       throw new RuntimeException("FAIL: " + (int)a + " != Character.reverseBytes(" + (int)b + ")");
       
    63     }
       
    64     if (b != Character.reverseBytes(a)) {
       
    65       throw new RuntimeException("FAIL: " + (int)b + " != Character.reverseBytes(" + (int)a + ")");
       
    66     }
       
    67   }
       
    68 
       
    69   private static void testShort(short a, short b) {
       
    70     if (a != Short.reverseBytes(b)) {
       
    71       throw new RuntimeException("FAIL: " + (int)a + " != Short.reverseBytes(" + (int)b + ")");
       
    72     }
       
    73     if (b != Short.reverseBytes(a)) {
       
    74       throw new RuntimeException("FAIL: " + (int)b + " != Short.reverseBytes(" + (int)a + ")");
       
    75     }
       
    76   }
       
    77 
       
    78   public static void main(String[] args) {
       
    79     for (int i = 0; i < 100000; ++i) { // Trigger compilation
       
    80       char c1 = initChar(args, (char) 0x0123);
       
    81       char c2 = initChar(args, (char) 0x2301);
       
    82       char c3 = initChar(args, (char) 0xaabb);
       
    83       char c4 = initChar(args, (char) 0xbbaa);
       
    84       short s1 = initShort(args, (short) 0x0123);
       
    85       short s2 = initShort(args, (short) 0x2301);
       
    86       short s3 = initShort(args, (short) 0xaabb);
       
    87       short s4 = initShort(args, (short) 0xbbaa);
       
    88       testChar(c1, c2);
       
    89       testChar(c3, c4);
       
    90       testShort(s1, s2);
       
    91       testShort(s3, s4);
       
    92     }
       
    93   }
       
    94 }